modules_mailings.php 8.1 KB

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