26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
37 public $element =
'mailing';
42 public $table_element =
'mailing';
47 public $picto =
'email';
97 public $email_replyto;
102 public $email_errorsto;
107 public $joined_file1;
112 public $joined_file2;
117 public $joined_file3;
122 public $joined_file4;
127 public $user_creation;
138 public $user_validation;
155 public $date_creation;
166 public $date_validation;
171 public $extraparams = array();
176 public $statut_dest = array();
181 public $statuts = array();
186 public $substitutionarray;
191 public $substitutionarrayfortest;
194 const STATUS_DRAFT = 0;
195 const STATUS_VALIDATED = 1;
196 const STATUS_SENTPARTIALY = 2;
197 const STATUS_SENTCOMPLETELY = 3;
210 $this->statuts[0] =
'MailingStatusDraft';
211 $this->statuts[1] =
'MailingStatusValidated';
212 $this->statuts[2] =
'MailingStatusSentPartialy';
213 $this->statuts[3] =
'MailingStatusSentCompletely';
215 $this->statut_dest[-1] =
'MailingStatusError';
216 $this->statut_dest[0] =
'MailingStatusNotSent';
217 $this->statut_dest[1] =
'MailingStatusSent';
218 $this->statut_dest[2] =
'MailingStatusRead';
219 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
229 public function create($user, $notrigger = 0)
231 global $conf, $langs;
234 if ($this->body ===
'InvalidHTMLString') {
235 $this->error =
'InvalidHTMLString';
239 $this->title = trim($this->title);
240 $this->email_from = trim($this->email_from);
242 if (!$this->email_from) {
243 $this->error = $langs->trans(
"ErrorMailFromRequired");
251 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
252 $sql .=
" (date_creat, fk_user_creat, entity)";
253 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int) $conf->entity).
")";
256 $this->title = $langs->trans(
"NoTitle");
260 $resql = $this->
db->query(
$sql);
262 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
264 $result = $this->
update($user, 1);
269 if (!$error && !$notrigger) {
282 $this->
db->rollback();
283 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
287 $this->error = $this->
db->lasterror();
288 $this->
db->rollback();
300 public function update($user, $notrigger = 0)
303 if ($this->body ===
'InvalidHTMLString') {
304 $this->error =
'InvalidHTMLString';
311 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
312 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
313 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
314 $sql .=
", body = '".$this->db->escape($this->body).
"'";
315 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
316 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
317 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
318 $sql .=
", bgcolor = '".($this->bgcolor ? $this->
db->escape($this->bgcolor) :
null).
"'";
319 $sql .=
", bgimage = '".($this->bgimage ? $this->
db->escape($this->bgimage) :
null).
"'";
320 $sql .=
" WHERE rowid = ".(int) $this->
id;
323 $resql = $this->
db->query(
$sql);
325 if (!$error && !$notrigger) {
339 $this->
db->rollback();
340 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
344 $this->error = $this->
db->lasterror();
345 $this->
db->rollback();
360 $sql =
"SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage";
361 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
362 $sql .=
", m.statut, m.nbemail";
363 $sql .=
", m.fk_user_creat, m.fk_user_valid";
364 $sql .=
", m.date_creat";
365 $sql .=
", m.date_valid";
366 $sql .=
", m.date_envoi";
367 $sql .=
", m.extraparams";
368 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
369 $sql .=
" WHERE m.rowid = ".(int) $rowid;
371 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
372 $result = $this->
db->query(
$sql);
374 if ($this->
db->num_rows($result)) {
375 $obj = $this->
db->fetch_object($result);
377 $this->
id = $obj->rowid;
378 $this->
ref = $obj->rowid;
379 $this->statut = $obj->statut;
380 $this->nbemail = $obj->nbemail;
381 $this->title = $obj->title;
383 $this->sujet = $obj->sujet;
387 $this->body = $obj->body;
390 $this->bgcolor = $obj->bgcolor;
391 $this->bgimage = $obj->bgimage;
393 $this->email_from = $obj->email_from;
394 $this->email_replyto = $obj->email_replyto;
395 $this->email_errorsto = $obj->email_errorsto;
397 $this->user_creat = $obj->fk_user_creat;
398 $this->user_creation = $obj->fk_user_creat;
399 $this->user_valid = $obj->fk_user_valid;
400 $this->user_validation = $obj->fk_user_valid;
402 $this->date_creat = $this->
db->jdate($obj->date_creat);
403 $this->date_creation = $this->
db->jdate($obj->date_creat);
404 $this->date_valid = $this->
db->jdate($obj->date_valid);
405 $this->date_validation = $this->
db->jdate($obj->date_valid);
406 $this->date_envoi = $this->
db->jdate($obj->date_envoi);
408 $this->extraparams = (array) json_decode($obj->extraparams,
true);
412 dol_syslog(get_class($this).
"::fetch Erreur -1");
416 dol_syslog(get_class($this).
"::fetch Erreur -2");
442 $object->fetch($fromid);
450 if (empty($option1)) {
452 $object->nbemail = 0;
455 $object->bgcolor =
'';
456 $object->bgimage =
'';
459 $object->email_replyto =
'';
460 $object->email_errorsto =
'';
462 $object->user_creat = $user->id;
463 $object->user_valid =
'';
465 $object->date_creat =
'';
466 $object->date_valid =
'';
467 $object->date_envoi =
'';
471 $object->context[
'createfromclone'] =
'createfromclone';
472 $result = $object->create($user);
476 $this->error = $object->error;
477 $this->errors = array_merge($this->errors, $object->errors);
483 if (!empty($option2)) {
484 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
488 $target_array = array();
490 $sql =
"SELECT fk_contact,";
491 $sql .=
" lastname,";
492 $sql .=
" firstname,";
495 $sql .=
" source_url,";
496 $sql .=
" source_id ,";
497 $sql .=
" source_type";
498 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
499 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
501 $result = $this->
db->query(
$sql);
503 if ($this->
db->num_rows($result)) {
504 while ($obj = $this->
db->fetch_object($result)) {
505 $target_array[] = array(
506 'fk_contact'=>$obj->fk_contact,
507 'lastname'=>$obj->lastname,
508 'firstname'=>$obj->firstname,
509 'email'=>$obj->email,
510 'other'=>$obj->other,
511 'source_url'=>$obj->source_url,
512 'source_id'=>$obj->source_id,
513 'source_type'=>$obj->source_type
518 $this->error = $this->
db->lasterror();
522 $mailing_target->addTargetsToDatabase($object->id, $target_array);
526 unset($object->context[
'createfromclone']);
533 $this->
db->rollback();
548 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
549 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
550 $sql .=
" WHERE rowid = ".((int) $this->
id);
553 if ($this->
db->query(
$sql)) {
556 $this->error = $this->
db->lasterror();
569 public function delete($user, $notrigger = 0)
583 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
584 $sql .=
" WHERE rowid = " . ((int) $this->
id);
587 $resql = $this->
db->query(
$sql);
599 $this->
db->rollback();
600 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
604 $this->
db->rollback();
605 $this->error = $this->
db->lasterror();
609 $this->
db->rollback();
623 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
624 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
626 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
627 $resql = $this->
db->query(
$sql);
633 $this->error = $this->
db->lasterror();
649 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
650 $sql .=
" SET statut = 0";
651 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
653 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
654 $resql = $this->
db->query(
$sql);
658 $this->error = $this->
db->lasterror();
672 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
673 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
674 if ($mode ==
'alreadysent') {
675 $sql .=
" AND statut <> 0";
676 } elseif ($mode ==
'alreadysentok') {
677 $sql .=
" AND statut > 0";
678 } elseif ($mode ==
'alreadysentko') {
679 $sql .=
" AND statut = -1";
681 $this->error =
'BadValueForParameterMode';
685 $resql = $this->
db->query(
$sql);
687 $obj = $this->
db->fetch_object($resql);
692 $this->error = $this->
db->lasterror();
706 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
707 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
709 $resql = $this->
db->query(
$sql);
711 $obj = $this->
db->fetch_object($resql);
713 $nbforupdate = $obj->nb;
715 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
716 $sql .=
' WHERE rowid = '.((int) $this->
id);
718 $resqlupdate = $this->
db->query(
$sql);
719 if (! $resqlupdate) {
720 $this->error = $this->
db->lasterror();
723 $this->nbemail = (int) $nbforupdate;
727 $this->error = $this->
db->lasterror();
743 global $conf, $langs;
745 $nofetch = !empty($params[
'nofetch']);
746 $langs->load(
'mails');
749 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
750 if (isset($this->statut)) {
751 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
753 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
754 if (isset($this->title)) {
755 $datas[
'title'] .=
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
757 if (isset($this->sujet)) {
758 $datas[
'subject'] .=
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
774 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
776 global $db, $conf, $langs, $hookmanager;
777 global $dolibarr_main_authentication, $dolibarr_main_demo;
780 if (!empty($conf->dol_no_mouse_hover)) {
787 'objecttype' => $this->element,
791 $classfortooltip =
'classfortooltip';
794 $classfortooltip =
'classforajaxtooltip';
795 $dataparams =
' data-params='.json_encode($params);
800 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
802 if ($option !=
'nolink') {
804 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
805 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
806 $add_save_lastsearch_values = 1;
808 if ($add_save_lastsearch_values) {
809 $url .=
'&save_lastsearch_values=1';
814 if (empty($notooltip)) {
815 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
816 $label = $langs->trans(
"ShowEMailing");
817 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
820 $linkclose .=
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
822 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
825 $linkstart =
'<a href="'.$url.
'"';
826 $linkstart .= $linkclose.
'>';
829 $result .= $linkstart;
831 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
833 if ($withpicto != 2) {
834 $result .= $this->ref;
840 $hookmanager->initHooks(array(
'emailingdao'));
841 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
842 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
844 $result = $hookmanager->resPrint;
846 $result .= $hookmanager->resPrint;
860 return $this->
LibStatut($this->statut, $mode);
875 $langs->load(
"mailing");
877 $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
878 $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts[$status]);
880 $statusType =
'status'.$status;
882 $statusType =
'status3';
885 $statusType =
'status6';
888 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
904 $langs->load(
"mails");
906 $labelStatus = array();
907 $labelStatusShort = array();
909 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
910 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
911 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
912 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
913 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
914 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
915 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
916 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
917 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
918 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
920 $statusType =
'status'.$status;
922 $statusType =
'status8';
925 $statusType =
'status6';
928 $statusType =
'status4';
932 if ($status == - 1) {
933 $param = array(
'badgeParams'=>array(
'attr'=>array(
'title'=>$desc)));
935 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status],
'', $statusType, $mode,
'', $param);