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 21/** 22 * \file htdocs/core/modules/mailings/modules_mailings.php 23 * \ingroup mailing 24 * \brief File with parent class of emailing target selectors modules 25 */ 26require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php'; 27 28 29/** 30 * Parent class of emailing target selectors modules 31 */ 32class MailingTargets // This can't be abstract as it is used for some method 33{ 34 /** 35 * @var DoliDB Database handler. 36 */ 37 public $db; 38 39 /** 40 * @var string Error code (or message) 41 */ 42 public $error = ''; 43 44 public $tooltip = ''; 45 46 47 /** 48 * Constructor 49 * 50 * @param DoliDB $db Database handler 51 */ 52 public function __construct($db) 53 { 54 $this->db = $db; 55 } 56 57 /** 58 * Return description of email selector 59 * 60 * @return string Return translation of module label. Try translation of $this->name then translation of 'MailingModuleDesc'.$this->name, or $this->desc if not found 61 */ 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) { 71 $s = $langs->trans($this->name); 72 } elseif ($langs->trans($transstring) != $transstring) { 73 $s = $langs->trans($transstring); 74 } else { 75 $s = $this->desc; 76 } 77 78 if ($this->tooltip && is_object($form)) { 79 $s .= ' '.$form->textwithpicto('', $langs->trans($this->tooltip), 1, 1); 80 } 81 return $s; 82 } 83 84 /** 85 * Return number of records for email selector 86 * 87 * @return integer Example 88 */ 89 public function getNbOfRecords() 90 { 91 return 0; 92 } 93 94 /** 95 * Retourne nombre de destinataires 96 * 97 * @param string $sql Sql request to count 98 * @return int Nb of recipient, or <0 if error 99 */ 100 public function getNbOfRecipients($sql) 101 { 102 $result = $this->db->query($sql); 103 if ($result) { 104 $obj = $this->db->fetch_object($result); 105 return $obj->nb; 106 } else { 107 $this->error = $this->db->lasterror(); 108 return -1; 109 } 110 } 111 112 /** 113 * Affiche formulaire de filtre qui apparait dans page de selection 114 * des destinataires de mailings 115 * 116 * @return string Retourne zone select 117 */ 118 public function formFilter() 119 { 120 return ''; 121 } 122 123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 124 /** 125 * Met a jour nombre de destinataires 126 * 127 * @param int $mailing_id Id of emailing 128 * @return int < 0 si erreur, nb destinataires si ok 129 */ 130 public function update_nb($mailing_id) 131 { 132 // phpcs:enable 133 // Mise a jour nombre de destinataire dans table des mailings 134 $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles"; 135 $sql .= " WHERE fk_mailing = ".((int) $mailing_id); 136 $result = $this->db->query($sql); 137 if ($result) { 138 $obj = $this->db->fetch_object($result); 139 $nb = $obj->nb; 140 141 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing"; 142 $sql .= " SET nbemail = ".$nb." WHERE rowid = ".((int) $mailing_id); 143 if (!$this->db->query($sql)) { 144 dol_syslog($this->db->error()); 145 $this->error = $this->db->error(); 146 return -1; 147 } 148 } else { 149 return -1; 150 } 151 return $nb; 152 } 153 154 /** 155 * Add a list of targets int the database 156 * 157 * @param int $mailing_id Id of emailing 158 * @param array $cibles Array with targets 159 * @return int < 0 si erreur, nb ajout si ok 160 */ 161 public function addTargetsToDatabase($mailing_id, $cibles) 162 { 163 global $conf; 164 global $dolibarr_main_instance_unique_id; 165 166 $this->db->begin(); 167 168 // Insert emailing targets from array into database 169 $j = 0; 170 $num = count($cibles); 171 foreach ($cibles as $targetarray) { 172 if (!empty($targetarray['email'])) { // avoid empty email address 173 $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles"; 174 $sql .= " (fk_mailing,"; 175 $sql .= " fk_contact,"; 176 $sql .= " lastname, firstname, email, other, source_url, source_id,"; 177 $sql .= " tag,"; 178 $sql .= " source_type)"; 179 $sql .= " VALUES (".$mailing_id.","; 180 $sql .= (empty($targetarray['fk_contact']) ? '0' : "'".$this->db->escape($targetarray['fk_contact'])."'").","; 181 $sql .= "'".$this->db->escape($targetarray['lastname'])."',"; 182 $sql .= "'".$this->db->escape($targetarray['firstname'])."',"; 183 $sql .= "'".$this->db->escape($targetarray['email'])."',"; 184 $sql .= "'".$this->db->escape($targetarray['other'])."',"; 185 $sql .= "'".$this->db->escape($targetarray['source_url'])."',"; 186 $sql .= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").","; 187 $sql .= "'".$this->db->escape(dol_hash($dolibarr_main_instance_unique_id.';'.$targetarray['email'].';'.$targetarray['lastname'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY, 'md5'))."',"; 188 $sql .= "'".$this->db->escape($targetarray['source_type'])."')"; 189 dol_syslog(__METHOD__, LOG_DEBUG); 190 $result = $this->db->query($sql); 191 if ($result) { 192 $j++; 193 } else { 194 if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') { 195 // Si erreur autre que doublon 196 dol_syslog($this->db->error().' : '.$targetarray['email']); 197 $this->error = $this->db->error().' : '.$targetarray['email']; 198 $this->db->rollback(); 199 return -1; 200 } 201 } 202 } 203 } 204 205 dol_syslog(__METHOD__.": mailing ".$j." targets added"); 206 207 /* 208 //Update the status to show thirdparty mail that don't want to be contacted anymore' 209 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; 210 $sql .= " SET statut=3"; 211 $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)"; 212 $sql .= " AND source_type='thirdparty'"; 213 dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted"); 214 $result=$this->db->query($sql); 215 216 //Update the status to show contact mail that don't want to be contacted anymore' 217 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; 218 $sql .= " SET statut=3"; 219 $sql .= " WHERE fk_mailing=".$mailing_id." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc "; 220 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))"; 221 dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG); 222 $result=$this->db->query($sql); 223 */ 224 225 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; 226 $sql .= " SET statut=3"; 227 $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')."'))"; 228 229 dol_syslog(__METHOD__.":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG); 230 $result = $this->db->query($sql); 231 if (!$result) { 232 dol_print_error($this->db); 233 } 234 235 $this->update_nb($mailing_id); 236 237 $this->db->commit(); 238 239 return $j; 240 } 241 242 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 243 /** 244 * Supprime tous les destinataires de la table des cibles 245 * 246 * @param int $mailing_id Id of emailing 247 * @return void 248 */ 249 public function clear_target($mailing_id) 250 { 251 // phpcs:enable 252 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles"; 253 $sql .= " WHERE fk_mailing = ".((int) $mailing_id); 254 255 if (!$this->db->query($sql)) { 256 dol_syslog($this->db->error()); 257 } 258 259 $this->update_nb($mailing_id); 260 } 261} 262