PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/asterix14/dolibarr
PHP | 213 lines | 116 code | 21 blank | 76 comment | 12 complexity | 4645384b9ef18b8b8891b42e27bc3a68 MD5 | raw file
Possible License(s): LGPL-2.0
  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 2 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 MailingTargets($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. $this->db->begin();
  135. // Insert emailing targest from array into database
  136. $j = 0;
  137. $num = count($cibles);
  138. for ($i = 0 ; $i < $num ; $i++)
  139. {
  140. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
  141. $sql .= " (fk_mailing,";
  142. $sql .= " fk_contact,";
  143. $sql .= " nom, prenom, email, other, source_url, source_id, source_type)";
  144. $sql .= " VALUES (".$mailing_id.",";
  145. $sql .= (empty($cibles[$i]['fk_contact']) ? '0' : "'".$cibles[$i]['fk_contact']."'") .",";
  146. $sql .= "'".$this->db->escape($cibles[$i]['name'])."',";
  147. $sql .= "'".$this->db->escape($cibles[$i]['firstname'])."',";
  148. $sql .= "'".$this->db->escape($cibles[$i]['email'])."',";
  149. $sql .= "'".$this->db->escape($cibles[$i]['other'])."',";
  150. $sql .= "'".$this->db->escape($cibles[$i]['source_url'])."',";
  151. $sql .= "'".$this->db->escape($cibles[$i]['source_id'])."',";
  152. $sql .= "'".$this->db->escape($cibles[$i]['source_type'])."')";
  153. $result=$this->db->query($sql);
  154. if ($result)
  155. {
  156. $j++;
  157. }
  158. else
  159. {
  160. if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS')
  161. {
  162. // Si erreur autre que doublon
  163. dol_syslog($this->db->error());
  164. $this->error=$this->db->error();
  165. $this->db->rollback();
  166. return -1;
  167. }
  168. }
  169. }
  170. dol_syslog(get_class($this)."::add_to_target: mailing ".$j." targets added");
  171. $this->update_nb($mailing_id);
  172. $this->db->commit();
  173. return $j;
  174. }
  175. /**
  176. * Supprime tous les destinataires de la table des cibles
  177. *
  178. * @param int $mailing_id Id of emailing
  179. * @return void
  180. */
  181. function clear_target($mailing_id)
  182. {
  183. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
  184. $sql .= " WHERE fk_mailing = ".$mailing_id;
  185. if (! $this->db->query($sql))
  186. {
  187. dol_syslog($this->db->error());
  188. }
  189. $this->update_nb($mailing_id);
  190. }
  191. }
  192. ?>