modules_mailings.php 8.2 KB

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