dolibarr  7.0.0-beta
modules_mailings.php
Go to the documentation of this file.
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
27 
28 
32 class MailingTargets // This can't be abstract as it is used for some method
33 {
34  var $db;
35  var $error;
36  var $tooltip='';
37 
38 
44  function __construct($db)
45  {
46  $this->db = $db;
47  }
48 
54  function getDesc()
55  {
56  global $langs, $form;
57 
58  $langs->load("mails");
59  $transstring="MailingModuleDesc".$this->name;
60  $s='';
61 
62  if ($langs->trans($this->name) != $this->name) $s=$langs->trans($this->name);
63  elseif ($langs->trans($transstring) != $transstring) $s=$langs->trans($transstring);
64  else $s=$this->desc;
65 
66  if ($this->tooltip && is_object($form)) $s .= ' '.$form->textwithpicto('', $langs->trans($this->tooltip), 1, 1);
67  return $s;
68  }
69 
75  function getNbOfRecords()
76  {
77  return 0;
78  }
79 
86  function getNbOfRecipients($sql)
87  {
88  $result=$this->db->query($sql);
89  if ($result)
90  {
91  $obj = $this->db->fetch_object($result);
92  return $obj->nb;
93  }
94  else
95  {
96  $this->error=$this->db->lasterror();
97  return -1;
98  }
99  }
100 
107  function formFilter()
108  {
109  return '';
110  }
111 
118  function update_nb($mailing_id)
119  {
120  // Mise a jour nombre de destinataire dans table des mailings
121  $sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
122  $sql .= " WHERE fk_mailing = ".$mailing_id;
123  $result=$this->db->query($sql);
124  if ($result)
125  {
126  $obj=$this->db->fetch_object($result);
127  $nb=$obj->nb;
128 
129  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
130  $sql .= " SET nbemail = ".$nb." WHERE rowid = ".$mailing_id;
131  if (!$this->db->query($sql))
132  {
133  dol_syslog($this->db->error());
134  $this->error=$this->db->error();
135  return -1;
136  }
137  }
138  else {
139  return -1;
140  }
141  return $nb;
142  }
143 
151  function add_to_target($mailing_id, $cibles)
152  {
153  global $conf;
154 
155  $this->db->begin();
156 
157  // Insert emailing targest from array into database
158  $j = 0;
159  $num = count($cibles);
160  foreach ($cibles as $targetarray)
161  {
162  if (! empty($targetarray['email'])) // avoid empty email address
163  {
164  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
165  $sql.= " (fk_mailing,";
166  $sql.= " fk_contact,";
167  $sql.= " lastname, firstname, email, other, source_url, source_id,";
168  $sql.= " tag,";
169  $sql.= " source_type)";
170  $sql.= " VALUES (".$mailing_id.",";
171  $sql.= (empty($targetarray['fk_contact']) ? '0' : "'".$targetarray['fk_contact']."'") .",";
172  $sql.= "'".$this->db->escape($targetarray['lastname'])."',";
173  $sql.= "'".$this->db->escape($targetarray['firstname'])."',";
174  $sql.= "'".$this->db->escape($targetarray['email'])."',";
175  $sql.= "'".$this->db->escape($targetarray['other'])."',";
176  $sql.= "'".$this->db->escape($targetarray['source_url'])."',";
177  $sql.= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
178  $sql .= "'".$this->db->escape(dol_hash($targetarray['email'].';'.$targetarray['lastname'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY))."',";
179  $sql .= "'".$this->db->escape($targetarray['source_type'])."')";
180  dol_syslog(get_class($this)."::".__METHOD__,LOG_DEBUG);
181  $result=$this->db->query($sql);
182  if ($result)
183  {
184  $j++;
185  }
186  else
187  {
188  if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS')
189  {
190  // Si erreur autre que doublon
191  dol_syslog($this->db->error());
192  $this->error=$this->db->error();
193  $this->db->rollback();
194  return -1;
195  }
196  }
197  }
198  }
199 
200  dol_syslog(get_class($this)."::".__METHOD__.": mailing ".$j." targets added");
201 
202  //Update the status to show thirdparty mail that don't want to be contacted anymore'
203  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
204  $sql .= " SET statut=3";
205  $sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
206  $sql .= " AND source_type='thirdparty'";
207  dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
208  $result=$this->db->query($sql);
209 
210 
211 
212  //Update the status to show contact mail that don't want to be contacted anymore'
213  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
214  $sql .= " SET statut=3";
215  $sql .= " WHERE fk_mailing=".$mailing_id." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
216  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
217  dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
218  $result=$this->db->query($sql);
219 
220 
221  $this->update_nb($mailing_id);
222 
223  $this->db->commit();
224  return $j;
225  }
226 
233  function clear_target($mailing_id)
234  {
235  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
236  $sql .= " WHERE fk_mailing = ".$mailing_id;
237 
238  if (! $this->db->query($sql))
239  {
240  dol_syslog($this->db->error());
241  }
242 
243  $this->update_nb($mailing_id);
244  }
245 
246 }
247 
__construct($db)
Constructor.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
add_to_target($mailing_id, $cibles)
Ajoute destinataires dans table des cibles.
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
update_nb($mailing_id)
Met a jour nombre de destinataires.
clear_target($mailing_id)
Supprime tous les destinataires de la table des cibles.
dol_hash($chain, $type='0')
Returns a hash of a string.
getNbOfRecipients($sql)
Retourne nombre de destinataires.
getDesc()
Return description of email selector.
Parent class of emailing target selectors modules.
getNbOfRecords()
Return number of records for email selector.