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;
133 public $user_creation;
145 public $user_validation;
162 public $date_creation;
173 public $date_validation;
183 public $extraparams = array();
188 public $statut_dest = array();
193 public $labelStatus = array();
198 public $substitutionarray;
203 public $substitutionarrayfortest;
205 const STATUS_DRAFT = 0;
206 const STATUS_VALIDATED = 1;
207 const STATUS_SENTPARTIALY = 2;
208 const STATUS_SENTCOMPLETELY = 3;
223 $this->labelStatus[0] =
'MailingStatusDraft';
224 $this->labelStatus[1] =
'MailingStatusValidated';
225 $this->labelStatus[2] =
'MailingStatusSentPartialy';
226 $this->labelStatus[3] =
'MailingStatusSentCompletely';
228 $this->statut_dest[0] =
'MailingStatusNotSent';
229 $this->statut_dest[1] =
'MailingStatusSent';
230 $this->statut_dest[2] =
'MailingStatusRead';
231 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
232 $this->statut_dest[-1] =
'MailingStatusError';
242 public function create($user, $notrigger = 0)
244 global $conf, $langs;
247 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
248 $this->error =
'InvalidHTMLStringCantBeCleaned';
252 $this->title = trim($this->title);
253 $this->email_from = trim($this->email_from);
255 if (!$this->email_from) {
256 $this->error = $langs->trans(
"ErrorMailFromRequired");
264 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
265 $sql .=
" (date_creat, fk_user_creat, entity)";
266 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int) $conf->entity).
")";
269 $this->title = $langs->trans(
"NoTitle");
273 $resql = $this->db->query($sql);
275 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
277 $result = $this->
update($user, 1);
282 if (!$error && !$notrigger) {
295 $this->db->rollback();
296 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
300 $this->error = $this->db->lasterror();
301 $this->db->rollback();
313 public function update($user, $notrigger = 0)
316 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
317 $this->error =
'InvalidHTMLStringCantBeCleaned';
324 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
325 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
326 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
327 $sql .=
", body = '".$this->db->escape($this->body).
"'";
328 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
329 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
330 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
331 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
332 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
333 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
334 $sql .=
" WHERE rowid = ".(int) $this->
id;
337 $resql = $this->db->query($sql);
339 if (!$error && !$notrigger) {
353 $this->db->rollback();
354 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
358 $this->error = $this->db->lasterror();
359 $this->db->rollback();
371 public function fetch($rowid, $ref =
'')
375 $sql =
"SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
376 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
377 $sql .=
", m.statut, m.nbemail";
378 $sql .=
", m.fk_user_creat, m.fk_user_valid";
379 $sql .=
", m.date_creat";
380 $sql .=
", m.date_valid";
381 $sql .=
", m.date_envoi";
382 $sql .=
", m.extraparams";
383 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
384 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
386 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
388 $sql .=
" AND m.rowid = ".(int) $rowid;
391 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
392 $result = $this->db->query($sql);
394 if ($this->db->num_rows($result)) {
395 $obj = $this->db->fetch_object($result);
397 $this->
id = $obj->rowid;
398 $this->
ref = $obj->rowid;
399 $this->
statut = $obj->statut;
400 $this->nbemail = $obj->nbemail;
401 $this->title = $obj->title;
403 $this->sujet = $obj->sujet;
407 $this->body = $obj->body;
410 $this->bgcolor = $obj->bgcolor;
411 $this->bgimage = $obj->bgimage;
412 $this->evenunsubscribe = $obj->evenunsubscribe;
414 $this->email_from = $obj->email_from;
415 $this->email_replyto = $obj->email_replyto;
416 $this->email_errorsto = $obj->email_errorsto;
418 $this->user_creation_id = $obj->fk_user_creat;
419 $this->user_validation_id = $obj->fk_user_valid;
421 $this->date_creat = $this->db->jdate($obj->date_creat);
422 $this->date_creation = $this->db->jdate($obj->date_creat);
423 $this->date_valid = $this->db->jdate($obj->date_valid);
424 $this->date_validation = $this->db->jdate($obj->date_valid);
425 $this->date_envoi = $this->db->jdate($obj->date_envoi);
427 $this->extraparams = (array) json_decode($obj->extraparams,
true);
431 dol_syslog(get_class($this).
"::fetch Erreur -1");
435 dol_syslog(get_class($this).
"::fetch Erreur -2");
456 $object =
new Mailing($this->db);
461 $object->fetch($fromid);
469 if (empty($option1)) {
471 $object->nbemail = 0;
474 $object->bgcolor =
'';
475 $object->bgimage =
'';
476 $object->evenunsubscribe = 0;
479 $object->email_replyto =
'';
480 $object->email_errorsto =
'';
482 $object->user_creation_id = $user->id;
483 $object->user_validation_id =
'';
485 $object->date_creat =
'';
486 $object->date_valid =
'';
487 $object->date_envoi =
'';
491 $object->context[
'createfromclone'] =
'createfromclone';
492 $result = $object->create($user);
496 $this->error = $object->error;
497 $this->errors = array_merge($this->errors, $object->errors);
503 if (!empty($option2)) {
504 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
508 $target_array = array();
510 $sql =
"SELECT fk_contact,";
511 $sql .=
" lastname,";
512 $sql .=
" firstname,";
515 $sql .=
" source_url,";
516 $sql .=
" source_id ,";
517 $sql .=
" source_type";
518 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
519 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
521 $result = $this->db->query($sql);
523 if ($this->db->num_rows($result)) {
524 while ($obj = $this->db->fetch_object($result)) {
525 $target_array[] = array(
526 'fk_contact'=>$obj->fk_contact,
527 'lastname'=>$obj->lastname,
528 'firstname'=>$obj->firstname,
529 'email'=>$obj->email,
530 'other'=>$obj->other,
531 'source_url'=>$obj->source_url,
532 'source_id'=>$obj->source_id,
533 'source_type'=>$obj->source_type
538 $this->error = $this->db->lasterror();
542 $mailing_target->addTargetsToDatabase($object->id, $target_array);
546 unset($object->context[
'createfromclone']);
553 $this->db->rollback();
568 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
569 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
570 $sql .=
" WHERE rowid = ".((int) $this->
id);
573 if ($this->db->query($sql)) {
576 $this->error = $this->db->lasterror();
589 public function delete($user, $notrigger = 0)
603 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
604 $sql .=
" WHERE rowid = " . ((int) $this->
id);
607 $resql = $this->db->query($sql);
619 $this->db->rollback();
620 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
624 $this->db->rollback();
625 $this->error = $this->db->lasterror();
629 $this->db->rollback();
643 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
644 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
646 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
647 $resql = $this->db->query($sql);
653 $this->error = $this->db->lasterror();
669 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
670 $sql .=
" SET statut = 0";
671 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
673 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
674 $resql = $this->db->query($sql);
678 $this->error = $this->db->lasterror();
692 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
693 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
694 if ($mode ==
'alreadysent') {
695 $sql .=
" AND statut <> 0";
696 } elseif ($mode ==
'alreadysentok') {
697 $sql .=
" AND statut > 0";
698 } elseif ($mode ==
'alreadysentko') {
699 $sql .=
" AND statut = -1";
701 $this->error =
'BadValueForParameterMode';
705 $resql = $this->db->query($sql);
707 $obj = $this->db->fetch_object($resql);
712 $this->error = $this->db->lasterror();
726 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
727 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
729 $resql = $this->db->query($sql);
731 $obj = $this->db->fetch_object($resql);
733 $nbforupdate = $obj->nb;
735 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
736 $sql .=
' WHERE rowid = '.((int) $this->
id);
738 $resqlupdate = $this->db->query($sql);
739 if (! $resqlupdate) {
740 $this->error = $this->db->lasterror();
743 $this->nbemail = (int) $nbforupdate;
747 $this->error = $this->db->lasterror();
763 global $conf, $langs;
765 $nofetch = !empty($params[
'nofetch']);
766 $langs->load(
'mails');
769 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
770 if (isset($this->
statut)) {
771 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
773 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
774 if (isset($this->title)) {
775 $datas[
'title'] .=
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
777 if (isset($this->sujet)) {
778 $datas[
'subject'] .=
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
794 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
796 global $conf, $langs, $hookmanager;
798 if (!empty($conf->dol_no_mouse_hover)) {
805 'objecttype' => $this->element,
809 $classfortooltip =
'classfortooltip';
812 $classfortooltip =
'classforajaxtooltip';
813 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
819 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
821 if ($option !=
'nolink') {
823 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
824 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
825 $add_save_lastsearch_values = 1;
827 if ($add_save_lastsearch_values) {
828 $url .=
'&save_lastsearch_values=1';
833 if (empty($notooltip)) {
835 $label = $langs->trans(
"ShowEMailing");
836 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
838 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
839 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
841 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
844 $linkstart =
'<a href="'.$url.
'"';
845 $linkstart .= $linkclose.
'>';
848 $result .= $linkstart;
850 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
852 if ($withpicto != 2) {
853 $result .= $this->ref;
859 $hookmanager->initHooks(array(
'emailingdao'));
860 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
861 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
863 $result = $hookmanager->resPrint;
865 $result .= $hookmanager->resPrint;
894 $langs->load(
"mailing");
896 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
897 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
899 $statusType =
'status'.$status;
901 $statusType =
'status3';
904 $statusType =
'status6';
907 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
923 $langs->load(
"mails");
925 $labelStatus = array();
926 $labelStatusShort = array();
928 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
929 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
930 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
931 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
932 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
933 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
934 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
935 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
936 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
937 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
939 $statusType =
'status'.$status;
941 $statusType =
'status8';
944 $statusType =
'status6';
947 $statusType =
'status4';
951 if ($status == - 1) {
952 $param = array(
'badgeParams'=>array(
'attr'=>array(
'title'=>$desc)));
954 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.
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 a 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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut