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