dolibarr  20.0.0-alpha
pomme.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
25 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
26 
27 
32 {
33  public $name = 'DolibarrUsers'; // Identifiant du module mailing
34  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
35  public $desc = 'Dolibarr users with emails'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
36  public $require_module = array(); // Module mailing actif si modules require_module actifs
37  public $require_admin = 1; // Module mailing actif pour user admin ou non
38 
42  public $picto = 'user';
43 
44 
50  public function __construct($db)
51  {
52  $this->db = $db;
53  }
54 
55 
64  public function getSqlArrayForStats()
65  {
66  global $conf, $langs;
67 
68  $langs->load("users");
69 
70  $statssql = array();
71  $sql = "SELECT '".$this->db->escape($langs->trans("DolibarrUsers"))."' as label,";
72  $sql .= " count(distinct(u.email)) as nb";
73  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
74  $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicit dans ce test
75  $sql .= " AND u.entity IN (0,".$conf->entity.")";
76 
77  $statssql[0] = $sql;
78 
79  return $statssql;
80  }
81 
82 
91  public function getNbOfRecipients($sql = '')
92  {
93  global $conf;
94 
95  $sql = "SELECT count(distinct(u.email)) as nb";
96  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
97  $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicit dans ce test
98  $sql .= " AND u.entity IN (0,".$conf->entity.")";
99  if (empty($this->evenunsubscribe)) {
100  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = u.email and mu.entity = ".((int) $conf->entity).")";
101  }
102 
103  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
104  return parent::getNbOfRecipients($sql);
105  }
106 
107 
113  public function formFilter()
114  {
115  global $langs;
116 
117  $langs->load("users");
118 
119  $s = '';
120  $s .= '<select id="filter_pomme"" name="filter" class="flat minwidth100">';
121  $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
122  $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
123  $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
124  $s .= '</select>';
125  $s .= ajax_combobox("filter_pomme");
126 
127  $s .= ' ';
128  $s .= '<select id="filteremployee_pomme" name="filteremployee" class="flat minwidth100">';
129  $s .= '<option value="-1">'.$langs->trans("Employee").'</option>';
130  $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
131  $s .= '<option value="0">'.$langs->trans("No").'</option>';
132  $s .= '</select>';
133  $s .= ajax_combobox("filteremployee_pomme");
134 
135  return $s;
136  }
137 
138 
145  public function url($id)
146  {
147  return '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$id.'">'.img_object('', "user").'</a>';
148  }
149 
150 
151  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
158  public function add_to_target($mailing_id)
159  {
160  // phpcs:enable
161  global $conf, $langs;
162  $langs->load("companies");
163 
164  $cibles = array();
165 
166  // La requete doit retourner: id, email, fk_contact, lastname, firstname
167  $sql = "SELECT u.rowid as id, u.email as email, null as fk_contact,";
168  $sql .= " u.lastname, u.firstname as firstname, u.civility as civility_id, u.login, u.office_phone";
169  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
170  $sql .= " WHERE u.email <> ''"; // u.email IS NOT NULL est implicit dans ce test
171  $sql .= " AND u.entity IN (0,".$conf->entity.")";
172  $sql .= " AND u.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
173  if (GETPOSTISSET("filter") && GETPOST("filter") == '1') {
174  $sql .= " AND u.statut=1";
175  }
176  if (GETPOSTISSET("filter") && GETPOST("filter") == '0') {
177  $sql .= " AND u.statut=0";
178  }
179  if (GETPOSTISSET("filteremployee") && GETPOST("filteremployee") == '1') {
180  $sql .= " AND u.employee=1";
181  }
182  if (GETPOSTISSET("filteremployee") && GETPOST("filteremployee") == '0') {
183  $sql .= " AND u.employee=0";
184  }
185  if (empty($this->evenunsubscribe)) {
186  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = u.email and mu.entity = ".((int) $conf->entity).")";
187  }
188  $sql .= " ORDER BY u.email";
189 
190  // Stocke destinataires dans cibles
191  $result = $this->db->query($sql);
192  if ($result) {
193  $num = $this->db->num_rows($result);
194  $i = 0;
195  $j = 0;
196 
197  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
198 
199  $old = '';
200  while ($i < $num) {
201  $obj = $this->db->fetch_object($result);
202  if ($old != $obj->email) {
203  $cibles[$j] = array(
204  'email' => $obj->email,
205  'fk_contact' => $obj->fk_contact,
206  'lastname' => $obj->lastname,
207  'firstname' => $obj->firstname,
208  'other' =>
209  ($langs->transnoentities("Login").'='.$obj->login).';'.
210  ($langs->transnoentities("UserTitle").'='.$obj->civility_id).';'.
211  ($langs->transnoentities("PhonePro").'='.$obj->office_phone),
212  'source_url' => $this->url($obj->id),
213  'source_id' => $obj->id,
214  'source_type' => 'user'
215  );
216  $old = $obj->email;
217  $j++;
218  }
219 
220  $i++;
221  }
222  } else {
223  dol_syslog($this->db->error());
224  $this->error = $this->db->error();
225  return -1;
226  }
227 
228  return parent::addTargetsToDatabase($mailing_id, $cibles);
229  }
230 }
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:463
Parent class of emailing target selectors modules.
Class to offer a selector of emailing targets with Rule 'Pomme'.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
__construct($db)
Constructor.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.