dolibarr  13.0.2
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@inodbox.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  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 
32 class Mailing extends CommonObject
33 {
37  public $element = 'mailing';
38 
42  public $table_element = 'mailing';
43 
47  public $picto = 'email';
48 
52  public $title;
53 
57  public $sujet;
58 
62  public $body;
63 
67  public $nbemail;
68 
72  public $bgcolor;
73 
77  public $bgimage;
78 
82  public $statut; // Status 0=Draft, 1=Validated, 2=Sent partially, 3=Sent completely
83 
87  public $email_from;
88 
92  public $email_replyto;
93 
97  public $email_errorsto;
98 
102  public $joined_file1;
103 
107  public $joined_file2;
108 
112  public $joined_file3;
113 
117  public $joined_file4;
118 
123  public $user_creation;
124 
128  public $user_creat;
129 
134  public $user_validation;
135 
139  public $user_valid;
140 
144  public $date_creat;
145 
149  public $date_valid;
150 
154  public $extraparams = array();
155 
159  public $statut_dest = array();
160 
164  public $statuts = array();
165 
166 
172  public function __construct($db)
173  {
174  $this->db = $db;
175 
176  // List of language codes for status
177  $this->statuts[0] = 'MailingStatusDraft';
178  $this->statuts[1] = 'MailingStatusValidated';
179  $this->statuts[2] = 'MailingStatusSentPartialy';
180  $this->statuts[3] = 'MailingStatusSentCompletely';
181 
182  $this->statut_dest[-1] = 'MailingStatusError';
183  $this->statut_dest[0] = 'MailingStatusNotSent';
184  $this->statut_dest[1] = 'MailingStatusSent';
185  $this->statut_dest[2] = 'MailingStatusRead';
186  $this->statut_dest[3] = 'MailingStatusReadAndUnsubscribe'; // Read but ask to not be contacted anymore
187  }
188 
195  public function create($user)
196  {
197  global $conf, $langs;
198 
199  $this->db->begin();
200 
201  $this->title = trim($this->title);
202  $this->email_from = trim($this->email_from);
203 
204  if (!$this->email_from) {
205  $this->error = $langs->trans("ErrorMailFromRequired");
206  return -1;
207  }
208 
209  $now = dol_now();
210 
211  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing";
212  $sql .= " (date_creat, fk_user_creat, entity)";
213  $sql .= " VALUES ('".$this->db->idate($now)."', ".$user->id.", ".$conf->entity.")";
214 
215  if (!$this->title) {
216  $this->title = $langs->trans("NoTitle");
217  }
218 
219  dol_syslog("Mailing::Create", LOG_DEBUG);
220  $result = $this->db->query($sql);
221  if ($result) {
222  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing");
223 
224  if ($this->update($user) > 0) {
225  $this->db->commit();
226  } else {
227  $this->error = $this->db->lasterror();
228  $this->db->rollback();
229  return -1;
230  }
231 
232  return $this->id;
233  } else {
234  $this->error = $this->db->lasterror();
235  $this->db->rollback();
236  return -1;
237  }
238  }
239 
246  public function update($user)
247  {
248  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
249  $sql .= " SET titre = '".$this->db->escape($this->title)."'";
250  $sql .= ", sujet = '".$this->db->escape($this->sujet)."'";
251  $sql .= ", body = '".$this->db->escape($this->body)."'";
252  $sql .= ", email_from = '".$this->db->escape($this->email_from)."'";
253  $sql .= ", email_replyto = '".$this->db->escape($this->email_replyto)."'";
254  $sql .= ", email_errorsto = '".$this->db->escape($this->email_errorsto)."'";
255  $sql .= ", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) : null)."'";
256  $sql .= ", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) : null)."'";
257  $sql .= " WHERE rowid = ".(int) $this->id;
258 
259  dol_syslog("Mailing::Update", LOG_DEBUG);
260  $result = $this->db->query($sql);
261  if ($result) {
262  return 1;
263  } else {
264  $this->error = $this->db->lasterror();
265  return -1;
266  }
267  }
268 
275  public function fetch($rowid)
276  {
277  global $conf;
278 
279  $sql = "SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage";
280  $sql .= ", m.email_from, m.email_replyto, m.email_errorsto";
281  $sql .= ", m.statut, m.nbemail";
282  $sql .= ", m.fk_user_creat, m.fk_user_valid";
283  $sql .= ", m.date_creat";
284  $sql .= ", m.date_valid";
285  $sql .= ", m.date_envoi";
286  $sql .= ", m.extraparams";
287  $sql .= " FROM ".MAIN_DB_PREFIX."mailing as m";
288  $sql .= " WHERE m.rowid = ".(int) $rowid;
289 
290  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
291  $result = $this->db->query($sql);
292  if ($result) {
293  if ($this->db->num_rows($result)) {
294  $obj = $this->db->fetch_object($result);
295 
296  $this->id = $obj->rowid;
297  $this->ref = $obj->rowid;
298  $this->statut = $obj->statut;
299  $this->nbemail = $obj->nbemail;
300  $this->title = $obj->title;
301 
302  $this->sujet = $obj->sujet;
303  if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5))) {
304  $this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5);
305  } else {
306  $this->body = $obj->body;
307  }
308 
309  $this->bgcolor = $obj->bgcolor;
310  $this->bgimage = $obj->bgimage;
311 
312  $this->email_from = $obj->email_from;
313  $this->email_replyto = $obj->email_replyto;
314  $this->email_errorsto = $obj->email_errorsto;
315 
316  $this->user_creat = $obj->fk_user_creat;
317  $this->user_valid = $obj->fk_user_valid;
318 
319  $this->date_creat = $this->db->jdate($obj->date_creat);
320  $this->date_valid = $this->db->jdate($obj->date_valid);
321  $this->date_envoi = $this->db->jdate($obj->date_envoi);
322 
323  $this->extraparams = (array) json_decode($obj->extraparams, true);
324 
325  return 1;
326  } else {
327  dol_syslog(get_class($this)."::fetch Erreur -1");
328  return -1;
329  }
330  } else {
331  dol_syslog(get_class($this)."::fetch Erreur -2");
332  return -2;
333  }
334  }
335 
336 
346  public function createFromClone(User $user, $fromid, $option1, $option2)
347  {
348  global $langs;
349 
350  $error = 0;
351 
352  $object = new Mailing($this->db);
353 
354  $this->db->begin();
355 
356  // Load source object
357  $object->fetch($fromid);
358  $object->id = 0;
359  $object->statut = 0;
360 
361  // Clear fields
362  $object->title = $langs->trans("CopyOf").' '.$object->title.' '.dol_print_date(dol_now());
363 
364  // If no option copy content
365  if (empty($option1)) {
366  // Clear values
367  $object->nbemail = 0;
368  $object->sujet = '';
369  $object->body = '';
370  $object->bgcolor = '';
371  $object->bgimage = '';
372 
373  //$object->email_from = ''; // We do not reset from email because it is a mandatory value
374  $object->email_replyto = '';
375  $object->email_errorsto = '';
376 
377  $object->user_creat = $user->id;
378  $object->user_valid = '';
379 
380  $object->date_creat = '';
381  $object->date_valid = '';
382  $object->date_envoi = '';
383  }
384 
385  // Create clone
386  $object->context['createfromclone'] = 'createfromclone';
387  $result = $object->create($user);
388 
389  // Other options
390  if ($result < 0) {
391  $this->error = $object->error;
392  $this->errors = array_merge($this->errors, $object->errors);
393  $error++;
394  }
395 
396  if (!$error) {
397  // Clone recipient targets
398  if (!empty($option2)) {
399  require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
400 
401  $mailing_target = new MailingTargets($this->db);
402 
403  $target_array = array();
404 
405  $sql = "SELECT fk_contact,";
406  $sql .= " lastname,";
407  $sql .= " firstname,";
408  $sql .= " email,";
409  $sql .= " other,";
410  $sql .= " source_url,";
411  $sql .= " source_id ,";
412  $sql .= " source_type";
413  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles";
414  $sql .= " WHERE fk_mailing = ".$fromid;
415 
416  $result = $this->db->query($sql);
417  if ($result) {
418  if ($this->db->num_rows($result)) {
419  while ($obj = $this->db->fetch_object($result)) {
420  $target_array[] = array(
421  'fk_contact'=>$obj->fk_contact,
422  'lastname'=>$obj->lastname,
423  'firstname'=>$obj->firstname,
424  'email'=>$obj->email,
425  'other'=>$obj->other,
426  'source_url'=>$obj->source_url,
427  'source_id'=>$obj->source_id,
428  'source_type'=>$obj->source_type
429  );
430  }
431  }
432  } else {
433  $this->error = $this->db->lasterror();
434  return -1;
435  }
436 
437  $mailing_target->addTargetsToDatabase($object->id, $target_array);
438  }
439  }
440 
441  unset($object->context['createfromclone']);
442 
443  // End
444  if (!$error) {
445  $this->db->commit();
446  return $object->id;
447  } else {
448  $this->db->rollback();
449  return -1;
450  }
451  }
452 
459  public function valid($user)
460  {
461  $now = dol_now();
462 
463  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
464  $sql .= " SET statut = 1, date_valid = '".$this->db->idate($now)."', fk_user_valid=".$user->id;
465  $sql .= " WHERE rowid = ".$this->id;
466 
467  dol_syslog("Mailing::valid", LOG_DEBUG);
468  if ($this->db->query($sql)) {
469  return 1;
470  } else {
471  $this->error = $this->db->lasterror();
472  return -1;
473  }
474  }
475 
476 
483  public function delete($rowid)
484  {
485  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing";
486  $sql .= " WHERE rowid = ".$rowid;
487 
488  dol_syslog("Mailing::delete", LOG_DEBUG);
489  $resql = $this->db->query($sql);
490  if ($resql) {
491  return $this->delete_targets();
492  } else {
493  $this->error = $this->db->lasterror();
494  return -1;
495  }
496  }
497 
498  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
504  public function delete_targets()
505  {
506  // phpcs:enable
507  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
508  $sql .= " WHERE fk_mailing = ".$this->id;
509 
510  dol_syslog("Mailing::delete_targets", LOG_DEBUG);
511  $resql = $this->db->query($sql);
512  if ($resql) {
513  $this->refreshNbOfTargets();
514 
515  return 1;
516  } else {
517  $this->error = $this->db->lasterror();
518  return 0;
519  }
520  }
521 
522 
523  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
530  public function reset_targets_status($user)
531  {
532  // phpcs:enable
533  $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
534  $sql .= " SET statut = 0";
535  $sql .= " WHERE fk_mailing = ".$this->id;
536 
537  dol_syslog("Mailing::reset_targets_status", LOG_DEBUG);
538  $resql = $this->db->query($sql);
539  if ($resql) {
540  return 1;
541  } else {
542  $this->error = $this->db->lasterror();
543  return -1;
544  }
545  }
546 
547 
554  public function countNbOfTargets($mode)
555  {
556  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
557  $sql .= " WHERE fk_mailing = ".$this->id;
558  if ($mode == 'alreadysent') {
559  $sql .= " AND statut <> 0";
560  } elseif ($mode == 'alreadysentok') {
561  $sql .= " AND statut > 0";
562  } elseif ($mode == 'alreadysentko') {
563  $sql .= " AND statut = -1";
564  } else {
565  $this->error = 'BadValueForParameterMode';
566  return -2;
567  }
568 
569  $resql = $this->db->query($sql);
570  if ($resql) {
571  $obj = $this->db->fetch_object($resql);
572  if ($obj) {
573  return $obj->nb;
574  }
575  } else {
576  $this->error = $this->db->lasterror();
577  return -1;
578  }
579  return 0;
580  }
581 
588  public function refreshNbOfTargets()
589  {
590  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
591  $sql .= " WHERE fk_mailing = ".$this->id;
592 
593  $resql = $this->db->query($sql);
594  if ($resql) {
595  $obj = $this->db->fetch_object($resql);
596  if ($obj) {
597  $nbforupdate = $obj->nb;
598 
599  $sql = 'UPDATE '.MAIN_DB_PREFIX.'mailing SET nbemail = '.((int) $nbforupdate);
600  $sql .= ' WHERE rowid = '.$this->id;
601 
602  $resqlupdate = $this->db->query($sql);
603  if (! $resqlupdate) {
604  $this->error = $this->db->lasterror();
605  return -1;
606  } else {
607  $this->nbemail = (int) $nbforupdate;
608  }
609  }
610  } else {
611  $this->error = $this->db->lasterror();
612  return -1;
613  }
614 
615  return 1;
616  }
617 
628  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
629  {
630  global $db, $conf, $langs, $hookmanager;
631  global $dolibarr_main_authentication, $dolibarr_main_demo;
632  global $menumanager;
633 
634  if (!empty($conf->dol_no_mouse_hover)) {
635  $notooltip = 1; // Force disable tooltips
636  }
637 
638  $result = '';
639  $companylink = '';
640 
641  $label = '<u>'.$langs->trans("ShowEMailing").'</u>';
642  $label .= '<br>';
643  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
644 
645  $url = DOL_URL_ROOT.'/comm/mailing/card.php?id='.$this->id;
646 
647  if ($option != 'nolink') {
648  // Add param to save lastsearch_values or not
649  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
650  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
651  $add_save_lastsearch_values = 1;
652  }
653  if ($add_save_lastsearch_values) {
654  $url .= '&save_lastsearch_values=1';
655  }
656  }
657 
658  $linkclose = '';
659  if (empty($notooltip)) {
660  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
661  $label = $langs->trans("ShowEMailing");
662  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
663  }
664  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
665  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
666 
667  /*
668  $hookmanager->initHooks(array('myobjectdao'));
669  $parameters=array('id'=>$this->id);
670  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
671  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
672  */
673  } else {
674  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
675  }
676 
677  $linkstart = '<a href="'.$url.'"';
678  $linkstart .= $linkclose.'>';
679  $linkend = '</a>';
680 
681  $result .= $linkstart;
682  if ($withpicto) {
683  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
684  }
685  if ($withpicto != 2) {
686  $result .= $this->ref;
687  }
688  $result .= $linkend;
689  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
690 
691  global $action;
692  $hookmanager->initHooks(array('emailingdao'));
693  $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
694  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
695  if ($reshook > 0) {
696  $result = $hookmanager->resPrint;
697  } else {
698  $result .= $hookmanager->resPrint;
699  }
700 
701  return $result;
702  }
703 
710  public function getLibStatut($mode = 0)
711  {
712  return $this->LibStatut($this->statut, $mode);
713  }
714 
715  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
723  public function LibStatut($status, $mode = 0)
724  {
725  // phpcs:enable
726  global $langs;
727  $langs->load("mailing");
728 
729  $labelStatus = $langs->trans($this->statuts[$status]);
730  $labelStatusShort = $langs->trans($this->statuts[$status]);
731 
732  $statusType = 'status'.$status;
733  if ($status == 2) {
734  $statusType = 'status3';
735  }
736  if ($status == 3) {
737  $statusType = 'status6';
738  }
739 
740  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
741  }
742 
743 
753  public static function libStatutDest($status, $mode = 0, $desc = '')
754  {
755  global $langs;
756  $langs->load("mails");
757 
758  $labelStatus = array();
759  $labelStatusShort = array();
760 
761  $labelStatus[-1] = $langs->trans('MailingStatusError');
762  $labelStatus[0] = $langs->trans('MailingStatusNotSent');
763  $labelStatus[1] = $langs->trans('MailingStatusSent');
764  $labelStatus[2] = $langs->trans('MailingStatusRead');
765  $labelStatus[3] = $langs->trans('MailingStatusNotContact');
766  $labelStatusShort[-1] = $langs->trans('MailingStatusError');
767  $labelStatusShort[0] = $langs->trans('MailingStatusNotSent');
768  $labelStatusShort[1] = $langs->trans('MailingStatusSent');
769  $labelStatusShort[2] = $langs->trans('MailingStatusRead');
770  $labelStatusShort[3] = $langs->trans('MailingStatusNotContact');
771 
772  $statusType = 'status'.$status;
773  if ($status == -1) {
774  $statusType = 'status8';
775  }
776  if ($status == 1) {
777  $statusType = 'status6';
778  }
779  if ($status == 2) {
780  $statusType = 'status4';
781  }
782 
783  $param = array();
784  if ($status == - 1) {
785  $param = array('badgeParams'=>array('attr'=>array('title'=>$desc)));
786  }
787  return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode, '', $param);
788  }
789 }
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
getLibStatut($mode=0)
Return label of status of emailing (draft, validated, ...)
createFromClone(User $user, $fromid, $option1, $option2)
Load an object from its id and create a new one in database.
update($user)
Update emailing record.
delete_targets()
Delete targets emailing.
dol_html_entity_decode($a, $b, $c= 'UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
$conf db
API class for accounts.
Definition: inc.php:54
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
countNbOfTargets($mode)
Count number of target with status.
valid($user)
Validate emailing.
refreshNbOfTargets()
Refresh denormalized value -&gt;nbemail into emailing record Note: There is also the method update_nb in...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class to manage emailings module.
reset_targets_status($user)
Change status of each recipient.
fetch($rowid)
Get object from database.
__construct($db)
Constructor.
print $_SERVER["PHP_SELF"]
Edit parameters.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
dol_print_date($time, $format= '', $tzoutput= 'auto', $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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
create($user)
Create an EMailing.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
static libStatutDest($status, $mode=0, $desc= '')
Renvoi le libelle d&#39;un statut donne TODO Add class mailin_target.class.php.
Parent class of emailing target selectors modules.
dol_textishtml($msg, $option=0)
Return if a text is a html content.