dolibarr  19.0.0-dev
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
27 
28 
32 class MailingTargets // This can't be abstract as it is used for some method
33 {
37  public $db;
38 
42  public $enabled;
43 
47  public $error = '';
48 
49  public $tooltip = '';
50 
54  public $sql;
55 
56  public $desc;
57 
58  public $name;
59 
60  public $evenunsubscribe = 0; // Set this to 1 if you want to flag you also want to include email in target that has opt-out.
61 
62 
68  public function __construct($db)
69  {
70  $this->db = $db;
71  }
72 
78  public function getDesc()
79  {
80  global $langs, $form;
81 
82  $langs->load("mails");
83  $transstring = "MailingModuleDesc".$this->name;
84  $s = '';
85 
86  if ($langs->trans($this->name) != $this->name) {
87  $s = $langs->trans($this->name);
88  } elseif ($langs->trans($transstring) != $transstring) {
89  $s = $langs->trans($transstring);
90  } else {
91  $s = $this->desc;
92  }
93 
94  if ($this->tooltip && is_object($form)) {
95  $s .= ' '.$form->textwithpicto('', $langs->trans($this->tooltip), 1, 1);
96  }
97  return $s;
98  }
99 
105  public function getNbOfRecords()
106  {
107  return 0;
108  }
109 
116  public function getNbOfRecipients($sql)
117  {
118  $result = $this->db->query($sql);
119  if ($result) {
120  $total = 0;
121  while ($obj = $this->db->fetch_object($result)) {
122  $total += $obj->nb;
123  }
124  return $total;
125  } else {
126  $this->error = $this->db->lasterror();
127  return -1;
128  }
129  }
130 
137  public function formFilter()
138  {
139  return '';
140  }
141 
142  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
149  public function update_nb($mailing_id)
150  {
151  // phpcs:enable
152  // Mise a jour nombre de destinataire dans table des mailings
153  $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
154  $sql .= " WHERE fk_mailing = ".((int) $mailing_id);
155  $result = $this->db->query($sql);
156  if ($result) {
157  $obj = $this->db->fetch_object($result);
158  $nb = $obj->nb;
159 
160  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
161  $sql .= " SET nbemail = ".((int) $nb)." WHERE rowid = ".((int) $mailing_id);
162  if (!$this->db->query($sql)) {
163  dol_syslog($this->db->error());
164  $this->error = $this->db->error();
165  return -1;
166  }
167  } else {
168  return -1;
169  }
170  return $nb;
171  }
172 
180  public function addTargetsToDatabase($mailing_id, $cibles)
181  {
182  global $conf;
183 
184  $this->db->begin();
185 
186  // Insert emailing targets from array into database
187  $j = 0;
188  $num = count($cibles);
189  foreach ($cibles as $targetarray) {
190  if (!empty($targetarray['email'])) { // avoid empty email address
191  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
192  $sql .= " (fk_mailing,";
193  $sql .= " fk_contact,";
194  $sql .= " lastname, firstname, email, other, source_url, source_id,";
195  $sql .= " tag,";
196  $sql .= " source_type)";
197  $sql .= " VALUES (".((int) $mailing_id).",";
198  $sql .= (empty($targetarray['fk_contact']) ? '0' : (int) $targetarray['fk_contact']).",";
199  $sql .= "'".$this->db->escape($targetarray['lastname'])."',";
200  $sql .= "'".$this->db->escape($targetarray['firstname'])."',";
201  $sql .= "'".$this->db->escape($targetarray['email'])."',";
202  $sql .= "'".$this->db->escape($targetarray['other'])."',";
203  $sql .= "'".$this->db->escape($targetarray['source_url'])."',";
204  $sql .= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
205  $sql .= "'".$this->db->escape(dol_hash($conf->file->instance_unique_id.";".$targetarray['email'].";".$targetarray['lastname'].";".((int) $mailing_id).";".getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY'), 'md5'))."',";
206  $sql .= "'".$this->db->escape($targetarray['source_type'])."')";
207  dol_syslog(__METHOD__, LOG_DEBUG);
208  $result = $this->db->query($sql);
209  if ($result) {
210  $j++;
211  } else {
212  if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
213  // Si erreur autre que doublon
214  dol_syslog($this->db->error().' : '.$targetarray['email']);
215  $this->error = $this->db->error().' : '.$targetarray['email'];
216  $this->db->rollback();
217  return -1;
218  }
219  }
220  }
221  }
222 
223  dol_syslog(__METHOD__.": mailing ".$j." targets added");
224 
225  /*
226  //Update the status to show thirdparty mail that don't want to be contacted anymore'
227  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
228  $sql .= " SET statut=3";
229  $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
230  $sql .= " AND source_type='thirdparty'";
231  dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
232  $result=$this->db->query($sql);
233 
234  //Update the status to show contact mail that don't want to be contacted anymore'
235  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
236  $sql .= " SET statut=3";
237  $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
238  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
239  dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
240  $result=$this->db->query($sql);
241  */
242 
243  if (empty($this->evenunsubscribe)) {
244  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles as mc";
245  $sql .= " SET mc.statut = 3";
246  $sql .= " WHERE mc.fk_mailing = ".((int) $mailing_id);
247  $sql .= " AND EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = mc.email and mu.entity = ".((int) $conf->entity).")";
248 
249  dol_syslog(__METHOD__.":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG);
250  $result = $this->db->query($sql);
251  if (!$result) {
252  dol_print_error($this->db);
253  }
254  }
255 
256  // Update nb of recipient into emailing record
257  $this->update_nb($mailing_id);
258 
259  $this->db->commit();
260 
261  return $j;
262  }
263 
264  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271  public function clear_target($mailing_id)
272  {
273  // phpcs:enable
274  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
275  $sql .= " WHERE fk_mailing = ".((int) $mailing_id);
276 
277  if (!$this->db->query($sql)) {
278  dol_syslog($this->db->error());
279  }
280 
281  $this->update_nb($mailing_id);
282  }
283 }
Parent class of emailing target selectors modules.
__construct($db)
Constructor.
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.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_print_error($db='', $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:123
dol_hash($chain, $type='0')
Returns a hash (non reversible encryption) of a string.