PageRenderTime 64ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/mailings/modules_mailings.php

https://bitbucket.org/speedealing/speedealing
PHP | 246 lines | 141 code | 27 blank | 78 comment | 15 complexity | 69f341bd3d41d3ed373a3f4803fe374e MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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 <http://www.gnu.org/licenses/>.
  18. * or see http://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. * \class MailingTargets
  28. * \brief Parent class of emailing target selectors modules
  29. */
  30. class MailingTargets // This can't be abstract as it is used for some method
  31. {
  32. var $db;
  33. var $error;
  34. /**
  35. * Constructor
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. function __construct($db = '')
  40. {
  41. $this->db = $db;
  42. }
  43. /**
  44. * Return description of email selector
  45. *
  46. * @return string Retourne la traduction de la cle MailingModuleDescXXX ou XXX nom du module, ou $this->desc si non trouve
  47. */
  48. function getDesc()
  49. {
  50. global $langs;
  51. $langs->load("mails");
  52. $transstring="MailingModuleDesc".$this->name;
  53. if ($langs->trans($transstring) != $transstring) return $langs->trans($transstring);
  54. else return $this->desc;
  55. }
  56. /**
  57. * Return number of records for email selector
  58. *
  59. * @return string Example
  60. */
  61. function getNbOfRecords()
  62. {
  63. return 0;
  64. }
  65. /**
  66. * Retourne nombre de destinataires
  67. *
  68. * @param string $sql Requete sql de comptage
  69. * @return int Nb de destinataires si ok, < 0 si erreur
  70. */
  71. function getNbOfRecipients($sql)
  72. {
  73. $result=$this->db->query($sql);
  74. if ($result)
  75. {
  76. $obj = $this->db->fetch_object($result);
  77. return $obj->nb;
  78. }
  79. else
  80. {
  81. $this->error=$this->db->error();
  82. return -1;
  83. }
  84. }
  85. /**
  86. * Affiche formulaire de filtre qui apparait dans page de selection
  87. * des destinataires de mailings
  88. *
  89. * @return string Retourne zone select
  90. */
  91. function formFilter()
  92. {
  93. return '';
  94. }
  95. /**
  96. * Met a jour nombre de destinataires
  97. *
  98. * @param int $mailing_id Id of emailing
  99. * @return int < 0 si erreur, nb destinataires si ok
  100. */
  101. function update_nb($mailing_id)
  102. {
  103. // Mise a jour nombre de destinataire dans table des mailings
  104. $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
  105. $sql .= " WHERE fk_mailing = ".$mailing_id;
  106. $result=$this->db->query($sql);
  107. if ($result)
  108. {
  109. $obj=$this->db->fetch_object($result);
  110. $nb=$obj->nb;
  111. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
  112. $sql .= " SET nbemail = ".$nb." WHERE rowid = ".$mailing_id;
  113. if (!$this->db->query($sql))
  114. {
  115. dol_syslog($this->db->error());
  116. $this->error=$this->db->error();
  117. return -1;
  118. }
  119. }
  120. else {
  121. return -1;
  122. }
  123. return $nb;
  124. }
  125. /**
  126. * Ajoute destinataires dans table des cibles
  127. *
  128. * @param int $mailing_id Id of emailing
  129. * @param array $cibles Array with targets
  130. * @return int < 0 si erreur, nb ajout si ok
  131. */
  132. function add_to_target($mailing_id, $cibles)
  133. {
  134. global $conf;
  135. $this->db->begin();
  136. // Insert emailing targest from array into database
  137. $j = 0;
  138. $num = count($cibles);
  139. for ($i = 0 ; $i < $num ; $i++)
  140. {
  141. if (! empty($cibles[$i]['email'])) // avoid empty email address
  142. {
  143. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
  144. $sql .= " (fk_mailing,";
  145. $sql .= " fk_contact,";
  146. $sql .= " nom, prenom, email, other, source_url, source_id,";
  147. if (! empty($conf->global->MAILING_EMAIL_UNSUBSCRIBE)) {
  148. $sql .= " tag,";
  149. }
  150. $sql.= " source_type)";
  151. $sql .= " VALUES (".$mailing_id.",";
  152. $sql .= (empty($cibles[$i]['fk_contact']) ? '0' : "'".$cibles[$i]['fk_contact']."'") .",";
  153. $sql .= "'".$this->db->escape($cibles[$i]['name'])."',";
  154. $sql .= "'".$this->db->escape($cibles[$i]['firstname'])."',";
  155. $sql .= "'".$this->db->escape($cibles[$i]['email'])."',";
  156. $sql .= "'".$this->db->escape($cibles[$i]['other'])."',";
  157. $sql .= "'".$this->db->escape($cibles[$i]['source_url'])."',";
  158. $sql .= "'".$this->db->escape($cibles[$i]['source_id'])."',";
  159. if (! empty($conf->global->MAILING_EMAIL_UNSUBSCRIBE)) {
  160. $sql .= "'".$this->db->escape(md5($cibles[$i]['email'].';'.$cibles[$i]['name'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY))."',";
  161. }
  162. $sql .= "'".$this->db->escape($cibles[$i]['source_type'])."')";
  163. $result=$this->db->query($sql);
  164. if ($result)
  165. {
  166. $j++;
  167. }
  168. else
  169. {
  170. if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS')
  171. {
  172. // Si erreur autre que doublon
  173. dol_syslog($this->db->error());
  174. $this->error=$this->db->error();
  175. $this->db->rollback();
  176. return -1;
  177. }
  178. }
  179. }
  180. }
  181. dol_syslog(get_class($this)."::add_to_target: sql ".$sql,LOG_DEBUG);
  182. dol_syslog(get_class($this)."::add_to_target: mailing ".$j." targets added");
  183. //Update the status to show thirdparty mail that don't want to be contacted anymore'
  184. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  185. $sql .= " SET statut=3";
  186. $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
  187. $sql .= " AND source_type='thirdparty'";
  188. $result=$this->db->query($sql);
  189. dol_syslog(get_class($this)."::add_to_target: mailing update status to display thirdparty mail that do not want to be contacted sql:".$sql);
  190. //Update the status to show contact mail that don't want to be contacted anymore'
  191. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  192. $sql .= " SET statut=3";
  193. $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
  194. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.fk_stcomm=-1 AND s.rowid=sc.fk_soc)";
  195. $sql .= " AND source_type='contact'";
  196. $result=$this->db->query($sql);
  197. dol_syslog(get_class($this)."::add_to_target: mailing update status to display contact mail that do not want to be contacted sql:".$sql);
  198. $this->update_nb($mailing_id);
  199. $this->db->commit();
  200. return $j;
  201. }
  202. /**
  203. * Supprime tous les destinataires de la table des cibles
  204. *
  205. * @param int $mailing_id Id of emailing
  206. * @return void
  207. */
  208. function clear_target($mailing_id)
  209. {
  210. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
  211. $sql .= " WHERE fk_mailing = ".$mailing_id;
  212. if (! $this->db->query($sql))
  213. {
  214. dol_syslog($this->db->error());
  215. }
  216. $this->update_nb($mailing_id);
  217. }
  218. }
  219. ?>