dolibarr 19.0.3
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}
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.
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:124
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.