dolibarr  9.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 <http://www.gnu.org/licenses/>.
18  * or see http://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  function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
62  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  function getNbOfRecords()
84  {
85  return 0;
86  }
87 
94  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  }
102  else
103  {
104  $this->error=$this->db->lasterror();
105  return -1;
106  }
107  }
108 
115  function formFilter()
116  {
117  return '';
118  }
119 
120  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
127  function update_nb($mailing_id)
128  {
129  // phpcs:enable
130  // Mise a jour nombre de destinataire dans table des mailings
131  $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
132  $sql .= " WHERE fk_mailing = ".$mailing_id;
133  $result=$this->db->query($sql);
134  if ($result)
135  {
136  $obj=$this->db->fetch_object($result);
137  $nb=$obj->nb;
138 
139  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
140  $sql .= " SET nbemail = ".$nb." WHERE rowid = ".$mailing_id;
141  if (!$this->db->query($sql))
142  {
143  dol_syslog($this->db->error());
144  $this->error=$this->db->error();
145  return -1;
146  }
147  }
148  else {
149  return -1;
150  }
151  return $nb;
152  }
153 
154  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
162  function add_to_target($mailing_id, $cibles)
163  {
164  // phpcs:enable
165  global $conf;
166 
167  $this->db->begin();
168 
169  // Insert emailing targest from array into database
170  $j = 0;
171  $num = count($cibles);
172  foreach ($cibles as $targetarray)
173  {
174  if (! empty($targetarray['email'])) // avoid empty email address
175  {
176  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
177  $sql.= " (fk_mailing,";
178  $sql.= " fk_contact,";
179  $sql.= " lastname, firstname, email, other, source_url, source_id,";
180  $sql.= " tag,";
181  $sql.= " source_type)";
182  $sql.= " VALUES (".$mailing_id.",";
183  $sql.= (empty($targetarray['fk_contact']) ? '0' : "'".$targetarray['fk_contact']."'") .",";
184  $sql.= "'".$this->db->escape($targetarray['lastname'])."',";
185  $sql.= "'".$this->db->escape($targetarray['firstname'])."',";
186  $sql.= "'".$this->db->escape($targetarray['email'])."',";
187  $sql.= "'".$this->db->escape($targetarray['other'])."',";
188  $sql.= "'".$this->db->escape($targetarray['source_url'])."',";
189  $sql.= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
190  $sql .= "'".$this->db->escape(dol_hash($targetarray['email'].';'.$targetarray['lastname'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY))."',";
191  $sql .= "'".$this->db->escape($targetarray['source_type'])."')";
192  dol_syslog(get_class($this)."::".__METHOD__,LOG_DEBUG);
193  $result=$this->db->query($sql);
194  if ($result)
195  {
196  $j++;
197  }
198  else
199  {
200  if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS')
201  {
202  // Si erreur autre que doublon
203  dol_syslog($this->db->error());
204  $this->error=$this->db->error();
205  $this->db->rollback();
206  return -1;
207  }
208  }
209  }
210  }
211 
212  dol_syslog(get_class($this)."::".__METHOD__.": mailing ".$j." targets added");
213 
214  //Update the status to show thirdparty mail that don't want to be contacted anymore'
215  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
216  $sql .= " SET statut=3";
217  $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
218  $sql .= " AND source_type='thirdparty'";
219  dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
220  $result=$this->db->query($sql);
221 
222 
223 
224  //Update the status to show contact mail that don't want to be contacted anymore'
225  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
226  $sql .= " SET statut=3";
227  $sql .= " WHERE fk_mailing=".$mailing_id." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
228  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
229  dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
230  $result=$this->db->query($sql);
231 
232 
233  $this->update_nb($mailing_id);
234 
235  $this->db->commit();
236  return $j;
237  }
238 
239  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
246  function clear_target($mailing_id)
247  {
248  // phpcs:enable
249  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
250  $sql .= " WHERE fk_mailing = ".$mailing_id;
251 
252  if (! $this->db->query($sql))
253  {
254  dol_syslog($this->db->error());
255  }
256 
257  $this->update_nb($mailing_id);
258  }
259 }
__construct($db)
Constructor.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
add_to_target($mailing_id, $cibles)
Ajoute destinataires dans table des cibles.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
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_hash($chain, $type='0')
Returns a hash of a string.
getNbOfRecipients($sql)
Retourne nombre de destinataires.
getDesc()
Return description of email selector.
Parent class of emailing target selectors modules.
getNbOfRecords()
Return number of records for email selector.