dolibarr  13.0.2
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 $error = '';
43 
44  public $tooltip = '';
45 
46 
52  public function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
62  public function getDesc()
63  {
64  global $langs, $form;
65 
66  $langs->load("mails");
67  $transstring = "MailingModuleDesc".$this->name;
68  $s = '';
69 
70  if ($langs->trans($this->name) != $this->name) $s = $langs->trans($this->name);
71  elseif ($langs->trans($transstring) != $transstring) $s = $langs->trans($transstring);
72  else $s = $this->desc;
73 
74  if ($this->tooltip && is_object($form)) $s .= ' '.$form->textwithpicto('', $langs->trans($this->tooltip), 1, 1);
75  return $s;
76  }
77 
83  public function getNbOfRecords()
84  {
85  return 0;
86  }
87 
94  public function getNbOfRecipients($sql)
95  {
96  $result = $this->db->query($sql);
97  if ($result)
98  {
99  $obj = $this->db->fetch_object($result);
100  return $obj->nb;
101  } else {
102  $this->error = $this->db->lasterror();
103  return -1;
104  }
105  }
106 
113  public function formFilter()
114  {
115  return '';
116  }
117 
118  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
125  public function update_nb($mailing_id)
126  {
127  // phpcs:enable
128  // Mise a jour nombre de destinataire dans table des mailings
129  $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
130  $sql .= " WHERE fk_mailing = ".$mailing_id;
131  $result = $this->db->query($sql);
132  if ($result)
133  {
134  $obj = $this->db->fetch_object($result);
135  $nb = $obj->nb;
136 
137  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
138  $sql .= " SET nbemail = ".$nb." WHERE rowid = ".$mailing_id;
139  if (!$this->db->query($sql))
140  {
141  dol_syslog($this->db->error());
142  $this->error = $this->db->error();
143  return -1;
144  }
145  } else {
146  return -1;
147  }
148  return $nb;
149  }
150 
158  public function addTargetsToDatabase($mailing_id, $cibles)
159  {
160  global $conf;
161  global $dolibarr_main_instance_unique_id;
162 
163  $this->db->begin();
164 
165  // Insert emailing targets from array into database
166  $j = 0;
167  $num = count($cibles);
168  foreach ($cibles as $targetarray)
169  {
170  if (!empty($targetarray['email'])) // avoid empty email address
171  {
172  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
173  $sql .= " (fk_mailing,";
174  $sql .= " fk_contact,";
175  $sql .= " lastname, firstname, email, other, source_url, source_id,";
176  $sql .= " tag,";
177  $sql .= " source_type)";
178  $sql .= " VALUES (".$mailing_id.",";
179  $sql .= (empty($targetarray['fk_contact']) ? '0' : "'".$this->db->escape($targetarray['fk_contact'])."'").",";
180  $sql .= "'".$this->db->escape($targetarray['lastname'])."',";
181  $sql .= "'".$this->db->escape($targetarray['firstname'])."',";
182  $sql .= "'".$this->db->escape($targetarray['email'])."',";
183  $sql .= "'".$this->db->escape($targetarray['other'])."',";
184  $sql .= "'".$this->db->escape($targetarray['source_url'])."',";
185  $sql .= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
186  $sql .= "'".$this->db->escape(dol_hash($dolibarr_main_instance_unique_id.';'.$targetarray['email'].';'.$targetarray['lastname'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY), 'md5')."',";
187  $sql .= "'".$this->db->escape($targetarray['source_type'])."')";
188  dol_syslog(__METHOD__, LOG_DEBUG);
189  $result = $this->db->query($sql);
190  if ($result)
191  {
192  $j++;
193  } else {
194  if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS')
195  {
196  // Si erreur autre que doublon
197  dol_syslog($this->db->error().' : '.$targetarray['email']);
198  $this->error = $this->db->error().' : '.$targetarray['email'];
199  $this->db->rollback();
200  return -1;
201  }
202  }
203  }
204  }
205 
206  dol_syslog(__METHOD__.": mailing ".$j." targets added");
207 
208  /*
209  //Update the status to show thirdparty mail that don't want to be contacted anymore'
210  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
211  $sql .= " SET statut=3";
212  $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
213  $sql .= " AND source_type='thirdparty'";
214  dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
215  $result=$this->db->query($sql);
216 
217  //Update the status to show contact mail that don't want to be contacted anymore'
218  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
219  $sql .= " SET statut=3";
220  $sql .= " WHERE fk_mailing=".$mailing_id." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
221  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
222  dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
223  $result=$this->db->query($sql);
224  */
225 
226  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
227  $sql .= " SET statut=3";
228  $sql .= " WHERE fk_mailing=".$mailing_id." AND email IN (SELECT mu.email FROM ".MAIN_DB_PREFIX."mailing_unsubscribe AS mu WHERE mu.entity IN ('".getEntity('mailing')."'))";
229 
230  dol_syslog(__METHOD__.":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG);
231  $result = $this->db->query($sql);
232  if (!$result)
233  {
234  dol_print_error($this->db);
235  }
236 
237  $this->update_nb($mailing_id);
238 
239  $this->db->commit();
240 
241  return $j;
242  }
243 
244  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
251  public function clear_target($mailing_id)
252  {
253  // phpcs:enable
254  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
255  $sql .= " WHERE fk_mailing = ".$mailing_id;
256 
257  if (!$this->db->query($sql))
258  {
259  dol_syslog($this->db->error());
260  }
261 
262  $this->update_nb($mailing_id);
263  }
264 }
__construct($db)
Constructor.
dol_hash($chain, $type= '0')
Returns a hash of a string.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
$conf db
API class for accounts.
Definition: inc.php:54
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update_nb($mailing_id)
Met a jour nombre de destinataires.
clear_target($mailing_id)
Supprime tous les destinataires de la table des cibles.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getNbOfRecipients($sql)
Retourne nombre de destinataires.
getDesc()
Return description of email selector.
addTargetsToDatabase($mailing_id, $cibles)
Add a list of targets int the database.
Parent class of emailing target selectors modules.
getNbOfRecords()
Return number of records for email selector.