dolibarr 20.0.0
modules_mailings.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
27
28
32class MailingTargets // This can't be abstract as it is used for some method
33{
37 public $db;
38
42 public $error = '';
43
47 public $errors;
48
52 public $enabled;
53
57 public $name;
58
62 public $desc;
63
67 public $tooltip = '';
68
72 public $sql;
73
74
75 public $evenunsubscribe = 0; // Set this to 1 if you want to flag you also want to include email in target that has opt-out.
76
77
83 public function __construct($db)
84 {
85 $this->db = $db;
86 }
87
93 public function getDesc()
94 {
95 global $langs, $form;
96
97 $langs->load("mails");
98 $transstring = "MailingModuleDesc".$this->name;
99 $s = '';
100
101 if ($langs->trans($this->name) != $this->name) {
102 $s = $langs->trans($this->name);
103 } elseif ($langs->trans($transstring) != $transstring) {
104 $s = $langs->trans($transstring);
105 } else {
106 $s = $this->desc;
107 }
108
109 if ($this->tooltip && is_object($form)) {
110 $s .= ' '.$form->textwithpicto('', $langs->trans($this->tooltip), 1, 1);
111 }
112 return $s;
113 }
114
120 public function getNbOfRecords()
121 {
122 return 0;
123 }
124
131 public function getNbOfRecipients($sql)
132 {
133 $result = $this->db->query($sql);
134 if ($result) {
135 $total = 0;
136 while ($obj = $this->db->fetch_object($result)) {
137 $total += $obj->nb;
138 }
139 return $total;
140 } else {
141 $this->error = $this->db->lasterror();
142 return -1;
143 }
144 }
145
152 public function formFilter()
153 {
154 return '';
155 }
156
157 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
164 public function update_nb($mailing_id)
165 {
166 // phpcs:enable
167 // Mise a jour nombre de destinataire dans table des mailings
168 $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
169 $sql .= " WHERE fk_mailing = ".((int) $mailing_id);
170 $result = $this->db->query($sql);
171 if ($result) {
172 $obj = $this->db->fetch_object($result);
173 $nb = $obj->nb;
174
175 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
176 $sql .= " SET nbemail = ".((int) $nb)." WHERE rowid = ".((int) $mailing_id);
177 if (!$this->db->query($sql)) {
178 dol_syslog($this->db->error());
179 $this->error = $this->db->error();
180 return -1;
181 }
182 } else {
183 return -1;
184 }
185 return $nb;
186 }
187
195 public function addTargetsToDatabase($mailing_id, $cibles)
196 {
197 global $conf;
198
199 $this->db->begin();
200
201 // Insert emailing targets from array into database
202 $j = 0;
203 $num = count($cibles);
204 foreach ($cibles as $targetarray) {
205 if (!empty($targetarray['email'])) { // avoid empty email address
206 $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
207 $sql .= " (fk_mailing,";
208 $sql .= " fk_contact,";
209 $sql .= " lastname, firstname, email, other, source_url, source_id,";
210 $sql .= " tag,";
211 $sql .= " source_type)";
212 $sql .= " VALUES (".((int) $mailing_id).",";
213 $sql .= (empty($targetarray['fk_contact']) ? '0' : (int) $targetarray['fk_contact']).",";
214 $sql .= "'".$this->db->escape($targetarray['lastname'])."',";
215 $sql .= "'".$this->db->escape($targetarray['firstname'])."',";
216 $sql .= "'".$this->db->escape($targetarray['email'])."',";
217 $sql .= "'".$this->db->escape($targetarray['other'])."',";
218 $sql .= "'".$this->db->escape($targetarray['source_url'])."',";
219 $sql .= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
220 $sql .= "'".$this->db->escape(dol_hash($conf->file->instance_unique_id.";".$targetarray['email'].";".$targetarray['lastname'].";".((int) $mailing_id).";".getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY'), 'md5'))."',";
221 $sql .= "'".$this->db->escape($targetarray['source_type'])."')";
222 dol_syslog(__METHOD__, LOG_DEBUG);
223 $result = $this->db->query($sql);
224 if ($result) {
225 $j++;
226 } else {
227 if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
228 // Si erreur autre que doublon
229 dol_syslog($this->db->error().' : '.$targetarray['email']);
230 $this->error = $this->db->error().' : '.$targetarray['email'];
231 $this->db->rollback();
232 return -1;
233 }
234 }
235 }
236 }
237
238 dol_syslog(__METHOD__.": mailing ".$j." targets added");
239
240 /*
241 //Update the status to show thirdparty mail that don't want to be contacted anymore'
242 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
243 $sql .= " SET statut=3";
244 $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
245 $sql .= " AND source_type='thirdparty'";
246 dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
247 $result=$this->db->query($sql);
248
249 //Update the status to show contact mail that don't want to be contacted anymore'
250 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
251 $sql .= " SET statut=3";
252 $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
253 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
254 dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
255 $result=$this->db->query($sql);
256 */
257
258 if (empty($this->evenunsubscribe)) {
259 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles as mc";
260 $sql .= " SET mc.statut = 3";
261 $sql .= " WHERE mc.fk_mailing = ".((int) $mailing_id);
262 $sql .= " AND EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = mc.email and mu.entity = ".((int) $conf->entity).")";
263
264 dol_syslog(__METHOD__.":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG);
265 $result = $this->db->query($sql);
266 if (!$result) {
267 dol_print_error($this->db);
268 }
269 }
270
271 // Update nb of recipient into emailing record
272 $this->update_nb($mailing_id);
273
274 $this->db->commit();
275
276 return $j;
277 }
278
279 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
286 public function clear_target($mailing_id)
287 {
288 // phpcs:enable
289 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
290 $sql .= " WHERE fk_mailing = ".((int) $mailing_id);
291
292 if (!$this->db->query($sql)) {
293 dol_syslog($this->db->error());
294 }
295
296 $this->update_nb($mailing_id);
297 }
298
299
307 public static function getEmailingSelectorsList($forcedir = null)
308 {
309 global $langs, $db;
310
311 $files = array();
312 $fullpath = array();
313 $relpath = array();
314 $iscoreorexternal = array();
315 $modules = array();
316 $orders = array();
317 $i = 0;
318
319 $diremailselector = array('/core/modules/mailings/'); // $conf->modules_parts['emailings'] is not required
320 if (is_array($forcedir)) {
321 $diremailselector = $forcedir;
322 }
323
324 foreach ($diremailselector as $reldir) {
325 $dir = dol_buildpath($reldir, 0);
326 $newdir = dol_osencode($dir);
327
328 // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
329 if (!is_dir($newdir)) {
330 continue;
331 }
332
333 $handle = opendir($newdir);
334 if (is_resource($handle)) {
335 while (($file = readdir($handle)) !== false) {
336 $reg = array();
337 if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.modules.php/', $file, $reg)) {
338 if (preg_match('/\.back$/', $file) || preg_match('/^(.+)\.disabled\.php/', $file)) {
339 continue;
340 }
341
342 $part1 = $reg[1];
343
344 //$modName = ucfirst($reg[1]);
345 $modName = 'mailing_'.$reg[1]; // name of selector submodule
346 //print "file=$file modName=$modName"; exit;
347 if (in_array($modName, $modules)) {
348 $langs->load("errors");
349 print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateEmalingSelector", $modName, "").'</div>';
350 } else {
351 try {
352 //print $newdir.'/'.$file;
353 include_once $newdir.'/'.$file;
354 } catch (Exception $e) {
355 print $e->getMessage();
356 }
357 }
358
359 $files[$i] = $file;
360 $fullpath[$i] = $dir.'/'.$file;
361 $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
362 $iscoreorexternal[$i] = ($reldir == '/core/modules/mailings/' ? 'internal' : 'external');
363 $modules[$i] = $modName;
364 $orders[$i] = $part1; // Set sort criteria value
365
366 $i++;
367 }
368 }
369 closedir($handle);
370 }
371 }
372 //echo "<pre>";print_r($modules);echo "</pre>";
373
374 asort($orders);
375
376 $widget = array();
377 $j = 0;
378
379 // Loop on each emailing selector
380 foreach ($orders as $key => $value) {
381 $modName = $modules[$key];
382 if (empty($modName)) {
383 continue;
384 }
385
386 if (!class_exists($modName)) {
387 print 'Error: An emailing selector file was found but its class "'.$modName.'" was not found.'."<br>\n";
388 continue;
389 }
390
391 $objMod = new $modName($db);
392 if (is_object($objMod)) {
393 // Define disabledbyname and disabledbymodule
394 $disabledbyname = 0;
395 $disabledbymodule = 0; // TODO Set to 2 if module is not enabled
396 $module = '';
397
398 // Check if widget file is disabled by name
399 if (preg_match('/NORUN$/i', $files[$key])) {
400 $disabledbyname = 1;
401 }
402
403 // We set info of modules
404 $widget[$j]['picto'] = (empty($objMod->picto) ? (empty($objMod->boximg) ? img_object('', 'generic') : $objMod->boximg) : img_object('', $objMod->picto));
405 $widget[$j]['file'] = $files[$key];
406 $widget[$j]['fullpath'] = $fullpath[$key];
407 $widget[$j]['relpath'] = $relpath[$key];
408 $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
409 $widget[$j]['version'] = empty($objMod->version) ? '' : $objMod->version;
410 $widget[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
411 if ($disabledbyname > 0 || $disabledbymodule > 1) {
412 $widget[$j]['status'] = '';
413 }
414
415 $text = '<b>'.$langs->trans("Description").':</b><br>';
416 $text .= $objMod->boxlabel.'<br>';
417 $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
418 if ($disabledbymodule == 2) {
419 $text .= $langs->trans("WidgetDisabledAsModuleDisabled", $module).'<br>';
420 }
421
422 $widget[$j]['info'] = $text;
423 }
424 $j++;
425 }
426
427 return $widget;
428 }
429}
Parent class of emailing target selectors modules.
__construct($db)
Constructor.
static getEmailingSelectorsList($forcedir=null)
Return list of widget.
addTargetsToDatabase($mailing_id, $cibles)
Add a list of targets into the database.
getDesc()
Return description of email selector.
getNbOfRecipients($sql)
Retourne nombre de destinataires.
clear_target($mailing_id)
Supprime tous les destinataires de la table des cibles.
update_nb($mailing_id)
Met a jour nombre de destinataires.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
getNbOfRecords()
Return number of records for email selector.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.