26require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
37 public $element =
'mailing';
42 public $table_element =
'mailing';
47 public $picto =
'email';
67 public $evenunsubscribe;
102 public $email_replyto;
107 public $email_errorsto;
112 public $joined_file1;
117 public $joined_file2;
122 public $joined_file3;
127 public $joined_file4;
132 public $user_creation;
143 public $user_validation;
160 public $date_creation;
171 public $date_validation;
176 public $extraparams = array();
181 public $statut_dest = array();
186 public $statuts = array();
191 public $substitutionarray;
196 public $substitutionarrayfortest;
198 const STATUS_DRAFT = 0;
199 const STATUS_VALIDATED = 1;
200 const STATUS_SENTPARTIALY = 2;
201 const STATUS_SENTCOMPLETELY = 3;
216 $this->statuts[0] =
'MailingStatusDraft';
217 $this->statuts[1] =
'MailingStatusValidated';
218 $this->statuts[2] =
'MailingStatusSentPartialy';
219 $this->statuts[3] =
'MailingStatusSentCompletely';
221 $this->statut_dest[0] =
'MailingStatusNotSent';
222 $this->statut_dest[1] =
'MailingStatusSent';
223 $this->statut_dest[2] =
'MailingStatusRead';
224 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
225 $this->statut_dest[-1] =
'MailingStatusError';
235 public function create($user, $notrigger = 0)
237 global $conf, $langs;
240 if ($this->body ===
'InvalidHTMLStringCantBeCleaned') {
241 $this->error =
'InvalidHTMLStringCantBeCleaned';
245 $this->title = trim($this->title);
246 $this->email_from = trim($this->email_from);
248 if (!$this->email_from) {
249 $this->error = $langs->trans(
"ErrorMailFromRequired");
257 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
258 $sql .=
" (date_creat, fk_user_creat, entity)";
259 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int) $conf->entity).
")";
262 $this->title = $langs->trans(
"NoTitle");
266 $resql = $this->db->query($sql);
268 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
270 $result = $this->
update($user, 1);
275 if (!$error && !$notrigger) {
288 $this->db->rollback();
289 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
293 $this->error = $this->db->lasterror();
294 $this->db->rollback();
306 public function update($user, $notrigger = 0)
309 if ($this->body ===
'InvalidHTMLStringCantBeCleaned') {
310 $this->error =
'InvalidHTMLStringCantBeCleaned';
317 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
318 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
319 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
320 $sql .=
", body = '".$this->db->escape($this->body).
"'";
321 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
322 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
323 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
324 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
325 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
326 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
327 $sql .=
" WHERE rowid = ".(int) $this->
id;
330 $resql = $this->db->query($sql);
332 if (!$error && !$notrigger) {
346 $this->db->rollback();
347 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
351 $this->error = $this->db->lasterror();
352 $this->db->rollback();
364 public function fetch($rowid, $ref =
'')
368 $sql =
"SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
369 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
370 $sql .=
", m.statut, m.nbemail";
371 $sql .=
", m.fk_user_creat, m.fk_user_valid";
372 $sql .=
", m.date_creat";
373 $sql .=
", m.date_valid";
374 $sql .=
", m.date_envoi";
375 $sql .=
", m.extraparams";
376 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
377 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
379 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
381 $sql .=
" AND m.rowid = ".(int) $rowid;
384 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
385 $result = $this->db->query($sql);
387 if ($this->db->num_rows($result)) {
388 $obj = $this->db->fetch_object($result);
390 $this->
id = $obj->rowid;
391 $this->
ref = $obj->rowid;
392 $this->
statut = $obj->statut;
393 $this->nbemail = $obj->nbemail;
394 $this->title = $obj->title;
396 $this->sujet = $obj->sujet;
400 $this->body = $obj->body;
403 $this->bgcolor = $obj->bgcolor;
404 $this->bgimage = $obj->bgimage;
405 $this->evenunsubscribe = $obj->evenunsubscribe;
407 $this->email_from = $obj->email_from;
408 $this->email_replyto = $obj->email_replyto;
409 $this->email_errorsto = $obj->email_errorsto;
411 $this->user_creat = $obj->fk_user_creat;
412 $this->user_creation = $obj->fk_user_creat;
413 $this->user_valid = $obj->fk_user_valid;
414 $this->user_validation = $obj->fk_user_valid;
416 $this->date_creat = $this->db->jdate($obj->date_creat);
417 $this->date_creation = $this->db->jdate($obj->date_creat);
418 $this->date_valid = $this->db->jdate($obj->date_valid);
419 $this->date_validation = $this->db->jdate($obj->date_valid);
420 $this->date_envoi = $this->db->jdate($obj->date_envoi);
422 $this->extraparams = (array) json_decode($obj->extraparams,
true);
426 dol_syslog(get_class($this).
"::fetch Erreur -1");
430 dol_syslog(get_class($this).
"::fetch Erreur -2");
451 $object =
new Mailing($this->db);
456 $object->fetch($fromid);
464 if (empty($option1)) {
466 $object->nbemail = 0;
469 $object->bgcolor =
'';
470 $object->bgimage =
'';
471 $object->evenunsubscribe = 0;
474 $object->email_replyto =
'';
475 $object->email_errorsto =
'';
477 $object->user_creat = $user->id;
478 $object->user_valid =
'';
480 $object->date_creat =
'';
481 $object->date_valid =
'';
482 $object->date_envoi =
'';
486 $object->context[
'createfromclone'] =
'createfromclone';
487 $result = $object->create($user);
491 $this->error = $object->error;
492 $this->errors = array_merge($this->errors, $object->errors);
498 if (!empty($option2)) {
499 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
503 $target_array = array();
505 $sql =
"SELECT fk_contact,";
506 $sql .=
" lastname,";
507 $sql .=
" firstname,";
510 $sql .=
" source_url,";
511 $sql .=
" source_id ,";
512 $sql .=
" source_type";
513 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
514 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
516 $result = $this->db->query($sql);
518 if ($this->db->num_rows($result)) {
519 while ($obj = $this->db->fetch_object($result)) {
520 $target_array[] = array(
521 'fk_contact'=>$obj->fk_contact,
522 'lastname'=>$obj->lastname,
523 'firstname'=>$obj->firstname,
524 'email'=>$obj->email,
525 'other'=>$obj->other,
526 'source_url'=>$obj->source_url,
527 'source_id'=>$obj->source_id,
528 'source_type'=>$obj->source_type
533 $this->error = $this->db->lasterror();
537 $mailing_target->addTargetsToDatabase($object->id, $target_array);
541 unset($object->context[
'createfromclone']);
548 $this->db->rollback();
563 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
564 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
565 $sql .=
" WHERE rowid = ".((int) $this->
id);
568 if ($this->db->query($sql)) {
571 $this->error = $this->db->lasterror();
584 public function delete($user, $notrigger = 0)
598 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
599 $sql .=
" WHERE rowid = " . ((int) $this->
id);
602 $resql = $this->db->query($sql);
614 $this->db->rollback();
615 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
619 $this->db->rollback();
620 $this->error = $this->db->lasterror();
624 $this->db->rollback();
638 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
639 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
641 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
642 $resql = $this->db->query($sql);
648 $this->error = $this->db->lasterror();
664 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
665 $sql .=
" SET statut = 0";
666 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
668 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
669 $resql = $this->db->query($sql);
673 $this->error = $this->db->lasterror();
687 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
688 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
689 if ($mode ==
'alreadysent') {
690 $sql .=
" AND statut <> 0";
691 } elseif ($mode ==
'alreadysentok') {
692 $sql .=
" AND statut > 0";
693 } elseif ($mode ==
'alreadysentko') {
694 $sql .=
" AND statut = -1";
696 $this->error =
'BadValueForParameterMode';
700 $resql = $this->db->query($sql);
702 $obj = $this->db->fetch_object($resql);
707 $this->error = $this->db->lasterror();
721 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
722 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
724 $resql = $this->db->query($sql);
726 $obj = $this->db->fetch_object($resql);
728 $nbforupdate = $obj->nb;
730 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
731 $sql .=
' WHERE rowid = '.((int) $this->
id);
733 $resqlupdate = $this->db->query($sql);
734 if (! $resqlupdate) {
735 $this->error = $this->db->lasterror();
738 $this->nbemail = (int) $nbforupdate;
742 $this->error = $this->db->lasterror();
758 global $conf, $langs;
760 $nofetch = !empty($params[
'nofetch']);
761 $langs->load(
'mails');
764 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
765 if (isset($this->
statut)) {
766 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
768 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
769 if (isset($this->title)) {
770 $datas[
'title'] .=
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
772 if (isset($this->sujet)) {
773 $datas[
'subject'] .=
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
789 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
791 global $conf, $langs, $hookmanager;
793 if (!empty($conf->dol_no_mouse_hover)) {
800 'objecttype' => $this->element,
804 $classfortooltip =
'classfortooltip';
807 $classfortooltip =
'classforajaxtooltip';
808 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
814 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
816 if ($option !=
'nolink') {
818 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
819 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
820 $add_save_lastsearch_values = 1;
822 if ($add_save_lastsearch_values) {
823 $url .=
'&save_lastsearch_values=1';
828 if (empty($notooltip)) {
829 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
830 $label = $langs->trans(
"ShowEMailing");
831 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
833 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
834 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
836 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
839 $linkstart =
'<a href="'.$url.
'"';
840 $linkstart .= $linkclose.
'>';
843 $result .= $linkstart;
845 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
847 if ($withpicto != 2) {
848 $result .= $this->ref;
854 $hookmanager->initHooks(array(
'emailingdao'));
855 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
856 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
858 $result = $hookmanager->resPrint;
860 $result .= $hookmanager->resPrint;
889 $langs->load(
"mailing");
891 $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
892 $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts[$status]);
894 $statusType =
'status'.$status;
896 $statusType =
'status3';
899 $statusType =
'status6';
902 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
918 $langs->load(
"mails");
920 $labelStatus = array();
921 $labelStatusShort = array();
923 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
924 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
925 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
926 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
927 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
928 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
929 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
930 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
931 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
932 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
934 $statusType =
'status'.$status;
936 $statusType =
'status8';
939 $statusType =
'status6';
942 $statusType =
'status4';
946 if ($status == - 1) {
947 $param = array(
'badgeParams'=>array(
'attr'=>array(
'title'=>$desc)));
949 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status],
'', $statusType, $mode,
'', $param);
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage emailings module.
static libStatutDest($status, $mode=0, $desc='')
Return the label of a given status of a recipient TODO Add class mailin_target.class....
getLibStatut($mode=0)
Return label of status of emailing (draft, validated, ...)
delete_targets()
Delete targets emailing.
valid($user)
Validate emailing.
countNbOfTargets($mode)
Count number of target with status.
create($user, $notrigger=0)
Create an EMailing.
fetch($rowid, $ref='')
Get object from database.
getTooltipContentArray($params)
getTooltipContentArray
__construct($db)
Constructor.
update($user, $notrigger=0)
Update emailing record.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
createFromClone(User $user, $fromid, $option1, $option2)
Load an object from its id and create a new one in database.
refreshNbOfTargets()
Refresh denormalized value ->nbemail into emailing record Note: There is also the method update_nb in...
reset_targets_status($user)
Change status of each recipient.
LibStatut($status, $mode=0)
Return the label of a given status.
Parent class of emailing target selectors modules.
Class to manage Dolibarr users.
print $langs trans("Ref").' m m m statut
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_textishtml($msg, $option=0)
Return if a text is a html content.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.