dolibarr  7.0.0-beta
mailing.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.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  */
19 
26 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
27 
28 
32 class Mailing extends CommonObject
33 {
34  public $element='mailing';
35  public $table_element='mailing';
36  public $picto='email';
37 
38  var $titre;
39  var $sujet;
40  var $body;
41  var $nbemail;
42  var $bgcolor;
43  var $bgimage;
44 
45  var $statut; // Status 0=Draft, 1=Validated, 2=Sent partially, 3=Sent completely
46 
47  var $email_from;
48  var $email_replyto;
49  var $email_errorsto;
50 
51  var $joined_file1;
52  var $joined_file2;
53  var $joined_file3;
54  var $joined_file4;
55 
56  var $user_creat;
57  var $user_valid;
58 
59  var $date_creat;
60  var $date_valid;
61 
62  var $extraparams=array();
63 
64  public $statut_dest=array();
65  public $statuts=array();
66 
67 
73  function __construct($db)
74  {
75  $this->db = $db;
76 
77  // List of language codes for status
78  $this->statuts[0] = 'MailingStatusDraft';
79  $this->statuts[1] = 'MailingStatusValidated';
80  $this->statuts[2] = 'MailingStatusSentPartialy';
81  $this->statuts[3] = 'MailingStatusSentCompletely';
82 
83  $this->statut_dest[-1] = 'MailingStatusError';
84  $this->statut_dest[0] = 'MailingStatusNotSent';
85  $this->statut_dest[1] = 'MailingStatusSent';
86  $this->statut_dest[2] = 'MailingStatusRead';
87  $this->statut_dest[3] = 'MailingStatusReadAndUnsubscribe'; // Read but ask to not be contacted anymore
88 
89  }
90 
97  function create($user)
98  {
99  global $conf, $langs;
100 
101  $this->db->begin();
102 
103  $this->titre=trim($this->titre);
104  $this->email_from=trim($this->email_from);
105 
106  if (! $this->email_from)
107  {
108  $this->error = $langs->trans("ErrorMailFromRequired");
109  return -1;
110  }
111 
112  $now=dol_now();
113 
114  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing";
115  $sql .= " (date_creat, fk_user_creat, entity)";
116  $sql .= " VALUES ('".$this->db->idate($now)."', ".$user->id.", ".$conf->entity.")";
117 
118  if (! $this->titre)
119  {
120  $this->titre = $langs->trans("NoTitle");
121  }
122 
123  dol_syslog("Mailing::Create", LOG_DEBUG);
124  $result=$this->db->query($sql);
125  if ($result)
126  {
127  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing");
128 
129  if ($this->update($user) > 0)
130  {
131  $this->db->commit();
132  }
133  else
134  {
135  $this->error=$this->db->lasterror();
136  $this->db->rollback();
137  return -1;
138  }
139 
140  return $this->id;
141  }
142  else
143  {
144  $this->error=$this->db->lasterror();
145  $this->db->rollback();
146  return -1;
147  }
148  }
149 
156  function update($user)
157  {
158  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
159  $sql .= " SET titre = '".$this->db->escape($this->titre)."'";
160  $sql .= ", sujet = '".$this->db->escape($this->sujet)."'";
161  $sql .= ", body = '".$this->db->escape($this->body)."'";
162  $sql .= ", email_from = '".$this->db->escape($this->email_from)."'";
163  $sql .= ", email_replyto = '".$this->db->escape($this->email_replyto)."'";
164  $sql .= ", email_errorsto = '".$this->db->escape($this->email_errorsto)."'";
165  $sql .= ", bgcolor = '".($this->bgcolor?$this->db->escape($this->bgcolor):null)."'";
166  $sql .= ", bgimage = '".($this->bgimage?$this->db->escape($this->bgimage):null)."'";
167  $sql .= " WHERE rowid = ".$this->id;
168 
169  dol_syslog("Mailing::Update", LOG_DEBUG);
170  $result=$this->db->query($sql);
171  if ($result)
172  {
173  return 1;
174  }
175  else
176  {
177  $this->error=$this->db->lasterror();
178  return -1;
179  }
180  }
181 
188  function fetch($rowid)
189  {
190  global $conf;
191 
192  $sql = "SELECT m.rowid, m.titre, m.sujet, m.body, m.bgcolor, m.bgimage";
193  $sql.= ", m.email_from, m.email_replyto, m.email_errorsto";
194  $sql.= ", m.statut, m.nbemail";
195  $sql.= ", m.fk_user_creat, m.fk_user_valid";
196  $sql.= ", m.date_creat";
197  $sql.= ", m.date_valid";
198  $sql.= ", m.date_envoi";
199  $sql.= ", m.extraparams";
200  $sql.= " FROM ".MAIN_DB_PREFIX."mailing as m";
201  $sql.= " WHERE m.rowid = ".(int) $rowid;
202 
203  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
204  $result=$this->db->query($sql);
205  if ($result)
206  {
207  if ($this->db->num_rows($result))
208  {
209  $obj = $this->db->fetch_object($result);
210 
211  $this->id = $obj->rowid;
212  $this->ref = $obj->rowid;
213  $this->statut = $obj->statut;
214  $this->nbemail = $obj->nbemail;
215  $this->titre = $obj->titre;
216 
217  $this->sujet = $obj->sujet;
218  if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401))) {
219  $this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML401);
220  }else {
221  $this->body = $obj->body;
222  }
223 
224  $this->bgcolor = $obj->bgcolor;
225  $this->bgimage = $obj->bgimage;
226 
227  $this->email_from = $obj->email_from;
228  $this->email_replyto = $obj->email_replyto;
229  $this->email_errorsto = $obj->email_errorsto;
230 
231  $this->user_creat = $obj->fk_user_creat;
232  $this->user_valid = $obj->fk_user_valid;
233 
234  $this->date_creat = $this->db->jdate($obj->date_creat);
235  $this->date_valid = $this->db->jdate($obj->date_valid);
236  $this->date_envoi = $this->db->jdate($obj->date_envoi);
237 
238  $this->extraparams = (array) json_decode($obj->extraparams, true);
239 
240  return 1;
241  }
242  else
243  {
244  dol_syslog(get_class($this)."::fetch Erreur -1");
245  return -1;
246  }
247  }
248  else
249  {
250  dol_syslog(get_class($this)."::fetch Erreur -2");
251  return -2;
252  }
253  }
254 
255 
264  function createFromClone($fromid,$option1,$option2)
265  {
266  global $user,$langs;
267 
268  $error=0;
269 
270  $object=new Mailing($this->db);
271 
272  $object->context['createfromclone']='createfromclone';
273 
274  $this->db->begin();
275 
276  // Load source object
277  $object->fetch($fromid);
278  $object->id=0;
279  $object->statut=0;
280 
281  // Clear fields
282  $object->titre=$langs->trans("CopyOf").' '.$object->titre.' '.dol_print_date(dol_now());
283 
284  // If no option copy content
285  if (empty($option1))
286  {
287  // Clear values
288  $object->nbemail = 0;
289  $object->sujet = '';
290  $object->body = '';
291  $object->bgcolor = '';
292  $object->bgimage = '';
293 
294  $object->email_from = '';
295  $object->email_replyto = '';
296  $object->email_errorsto = '';
297 
298  $object->user_creat = $user->id;
299  $object->user_valid = '';
300 
301  $object->date_creat = '';
302  $object->date_valid = '';
303  $object->date_envoi = '';
304  }
305 
306  // Create clone
307  $result=$object->create($user);
308 
309  // Other options
310  if ($result < 0)
311  {
312  $this->error=$object->error;
313  $error++;
314  }
315 
316  if (! $error)
317  {
318  //Clone target
319  if (!empty($option2)) {
320 
321  require_once DOL_DOCUMENT_ROOT .'/core/modules/mailings/modules_mailings.php';
322 
323  $mailing_target = new MailingTargets($this->db);
324 
325  $target_array=array();
326 
327  $sql = "SELECT fk_contact, ";
328  $sql.=" lastname, ";
329  $sql.=" firstname,";
330  $sql.=" email,";
331  $sql.=" other,";
332  $sql.=" source_url,";
333  $sql.=" source_id ,";
334  $sql.=" source_type ";
335  $sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles ";
336  $sql.= " WHERE fk_mailing = ".$fromid;
337 
338  dol_syslog(get_class($this)."::createFromClone", LOG_DEBUG);
339  $result=$this->db->query($sql);
340  if ($result)
341  {
342  if ($this->db->num_rows($result))
343  {
344  while ($obj = $this->db->fetch_object($result)) {
345 
346  $target_array[]=array('fk_contact'=>$obj->fk_contact,
347  'lastname'=>$obj->lastname,
348  'firstname'=>$obj->firstname,
349  'email'=>$obj->email,
350  'other'=>$obj->other,
351  'source_url'=>$obj->source_url,
352  'source_id'=>$obj->source_id,
353  'source_type'=>$obj->source_type);
354  }
355 
356  }
357  }
358  else
359  {
360  $this->error=$this->db->lasterror();
361  return -1;
362  }
363 
364  $mailing_target->add_to_target($object->id, $target_array);
365  }
366 
367  }
368 
369  unset($object->context['createfromclone']);
370 
371  // End
372  if (! $error)
373  {
374  $this->db->commit();
375  return $object->id;
376  }
377  else
378  {
379  $this->db->rollback();
380  return -1;
381  }
382  }
383 
390  function valid($user)
391  {
392  $now=dol_now();
393 
394  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
395  $sql .= " SET statut = 1, date_valid = '".$this->db->idate($now)."', fk_user_valid=".$user->id;
396  $sql .= " WHERE rowid = ".$this->id;
397 
398  dol_syslog("Mailing::valid", LOG_DEBUG);
399  if ($this->db->query($sql))
400  {
401  return 1;
402  }
403  else
404  {
405  $this->error=$this->db->lasterror();
406  return -1;
407  }
408  }
409 
410 
417  function delete($rowid)
418  {
419  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing";
420  $sql.= " WHERE rowid = ".$rowid;
421 
422  dol_syslog("Mailing::delete", LOG_DEBUG);
423  $resql=$this->db->query($sql);
424  if ($resql)
425  {
426  return $this->delete_targets();
427  }
428  else
429  {
430  $this->error=$this->db->lasterror();
431  return -1;
432  }
433  }
434 
440  function delete_targets()
441  {
442  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
443  $sql.= " WHERE fk_mailing = ".$this->id;
444 
445  dol_syslog("Mailing::delete_targets", LOG_DEBUG);
446  $resql=$this->db->query($sql);
447  if ($resql)
448  {
449  return 1;
450  }
451  else
452  {
453  $this->error=$this->db->lasterror();
454  return 0;
455  }
456  }
457 
458 
465  function reset_targets_status($user)
466  {
467  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
468  $sql.= " SET statut = 0";
469  $sql.= " WHERE fk_mailing = ".$this->id;
470 
471  dol_syslog("Mailing::reset_targets_status", LOG_DEBUG);
472  $resql=$this->db->query($sql);
473  if ($resql)
474  {
475  return 1;
476  }
477  else
478  {
479  $this->error=$this->db->lasterror();
480  return -1;
481  }
482  }
483 
484 
491  function countNbOfTargets($mode)
492  {
493  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
494  $sql.= " WHERE fk_mailing = ".$this->id;
495  if ($mode == 'alreadysent') $sql.= " AND statut <> 0";
496  else
497  {
498  $this->error='BadValueForParameterMode';
499  return -2;
500  }
501 
502  $resql=$this->db->query($sql);
503  if ($resql)
504  {
505  $obj = $this->db->fetch_object($resql);
506  if ($obj) return $obj->nb;
507  }
508  else
509  {
510  $this->error=$this->db->lasterror();
511  return -1;
512  }
513  return 0;
514  }
515 
516 
523  function getLibStatut($mode=0)
524  {
525  return $this->LibStatut($this->statut,$mode);
526  }
527 
535  function LibStatut($statut,$mode=0)
536  {
537  global $langs;
538  $langs->load('mails');
539 
540  if ($mode == 0)
541  {
542  return $langs->trans($this->statuts[$statut]);
543  }
544  if ($mode == 1)
545  {
546  return $langs->trans($this->statuts[$statut]);
547  }
548  if ($mode == 2)
549  {
550  if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
551  if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]);
552  if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
553  if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
554  }
555  if ($mode == 3)
556  {
557  if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
558  if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1');
559  if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
560  if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6');
561  }
562  if ($mode == 4)
563  {
564  if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
565  if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]);
566  if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
567  if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
568  }
569  if ($mode == 5)
570  {
571  if ($statut == 0) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut0');
572  if ($statut == 1) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut1');
573  if ($statut == 2) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut3');
574  if ($statut == 3) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut6');
575  }
576  }
577 
578 
588  public static function libStatutDest($statut,$mode=0,$desc='')
589  {
590  global $langs;
591  $langs->load('mails');
592 
593  if ($mode == 0)
594  {
595  return $langs->trans('MailingStatusError');
596  }
597  if ($mode == 1)
598  {
599  return $langs->trans('MailingStatusSent');
600  }
601  if ($mode == 2)
602  {
603  if ($statut==-1) return $langs->trans("MailingStatusError").' '.img_error($desc);
604  if ($statut==1) return $langs->trans("MailingStatusSent").' '.img_picto($langs->trans("MailingStatusSent"),'statut6');
605  if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut4');
606  if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut3');
607  }
608  if ($mode == 3)
609  {
610  if ($statut==-1) return $langs->trans("MailingStatusError").' '.img_error($desc);
611  if ($statut==1) return $langs->trans("MailingStatusSent").' '.img_picto($langs->trans("MailingStatusSent"),'statut6');
612  if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut4');
613  if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut3');
614  }
615  if ($mode == 4)
616  {
617  if ($statut==-1) return $langs->trans("MailingStatusError").' '.img_error($desc);
618  if ($statut==1) return $langs->trans("MailingStatusSent").' '.img_picto($langs->trans("MailingStatusSent"),'statut6');
619  if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut4');
620  if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut3');
621  }
622  if ($mode == 5)
623  {
624  if ($statut==-1) return $langs->trans("MailingStatusError").' '.img_error($desc);
625  if ($statut==1) return $langs->trans("MailingStatusSent").' '.img_picto($langs->trans("MailingStatusSent"),'statut6');
626  if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut4');
627  if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut3');
628  }
629  if ($mode == 6)
630  {
631  if ($statut==-1) return $langs->trans("MailingStatusError").' '.img_error($desc);
632  if ($statut==1) return $langs->trans("MailingStatusSent").' '.img_picto($langs->trans("MailingStatusSent"),'statut6');
633  if ($statut==2) return $langs->trans("MailingStatusRead").' '.img_picto($langs->trans("MailingStatusRead"),'statut4');
634  if ($statut==3) return $langs->trans("MailingStatusNotContact").' '.img_picto($langs->trans("MailingStatusNotContact"),'statut3');
635  }
636  }
637 
638 }
639 
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
getLibStatut($mode=0)
Retourne le libelle du statut d'un mailing (brouillon, validee, ...
update($user)
Update emailing record.
delete_targets()
Delete targets emailing.
createFromClone($fromid, $option1, $option2)
Load an object from its id and create a new one in database.
create($user)
Create an EMailing.
static libStatutDest($statut, $mode=0, $desc='')
Renvoi le libelle d'un statut donne TODO Add class mailin_target.class.php.
countNbOfTargets($mode)
Count number of target with status.
valid($user)
Validate emailing.
img_error($titlealt= 'default')
Show error logo.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage emailings module.
dol_now($mode='gmt')
Return date for now.
reset_targets_status($user)
Change status of each recipient.
fetch($rowid)
Get object from database.
__construct($db)
Constructor.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
dol_html_entity_decode($a, $b, $c='UTF-8')
Replace html_entity_decode functions to manage errors.
LibStatut($statut, $mode=0)
Renvoi le libelle d'un statut donne.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
Parent class of emailing target selectors modules.
dol_textishtml($msg, $option=0)
Return if a text is a html content.