29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/comm/mailing/class/mailing_targets.class.php';
40 public $element =
'mailing';
45 public $table_element =
'mailing';
50 public $picto =
'email';
55 public $messtype =
'email';
75 public $evenunsubscribe;
126 public $email_replyto;
131 public $email_errorsto;
136 public $joined_file1;
141 public $joined_file2;
146 public $joined_file3;
151 public $joined_file4;
161 public $extraparams = array();
166 public $statut_dest = array();
171 public $substitutionarray;
176 public $substitutionarrayfortest;
181 public $targets = array();
189 public $fields = array(
190 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'index' => 1,
'position' => 1,
'comment' =>
'Id'),
191 'fk_project' => array(
'type' =>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label' =>
'Fk project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 10),
194 const STATUS_DRAFT = 0;
195 const STATUS_VALIDATED = 1;
196 const STATUS_SENTPARTIALY = 2;
197 const STATUS_SENTCOMPLETELY = 3;
210 $this->labelStatus[0] =
'MailingStatusDraft';
211 $this->labelStatus[1] =
'MailingStatusValidated';
212 $this->labelStatus[2] =
'MailingStatusSentPartialy';
213 $this->labelStatus[3] =
'MailingStatusSentCompletely';
215 $this->statut_dest[0] =
'MailingStatusNotSent';
216 $this->statut_dest[1] =
'MailingStatusSent';
217 $this->statut_dest[2] =
'MailingStatusRead';
218 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
219 $this->statut_dest[-1] =
'MailingStatusError';
229 public function create($user, $notrigger = 0)
231 global $conf, $langs;
234 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
235 $this->error =
'InvalidHTMLStringCantBeCleaned';
239 $this->title = trim($this->title);
240 $this->email_from = trim($this->email_from);
241 if (empty($this->messtype)) {
242 $this->messtype =
'email';
244 if (!$this->email_from) {
245 if ($this->messtype !==
'sms') {
246 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"MailFrom"));
248 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"PhoneFrom"));
258 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
259 $sql .=
" (messtype, date_creat, fk_user_creat, entity)";
260 $sql .=
" VALUES ('".$this->db->escape($this->messtype).
"', '".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int) $conf->entity).
")";
263 $this->title = $langs->trans(
"NoTitle");
268 $resql = $this->db->query($sql);
270 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
272 $result = $this->
update($user, 1);
277 if (!$error && !$notrigger) {
279 $result = $this->call_trigger(
'MAILING_CREATE', $user);
290 $this->db->rollback();
291 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
295 $this->error = $this->db->lasterror();
296 $this->db->rollback();
308 public function update($user, $notrigger = 0)
313 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
314 $this->error =
'InvalidHTMLStringCantBeCleaned';
318 if (empty($this->messtype)) {
319 $this->messtype =
'email';
325 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing";
326 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
327 $sql .=
", messtype = '".$this->db->escape($this->messtype).
"'";
328 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
329 $sql .=
", body = '".$this->db->escape($this->body).
"'";
330 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
331 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
332 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
333 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
334 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
335 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
336 $sql .=
", note_public = '".$this->db->escape($this->note_public).
"'";
337 $sql .=
", note_private = '".$this->db->escape($this->note_private).
"'";
338 $sql .=
", fk_project = '".((int) $this->fk_project).
"'";
339 $sql .=
" WHERE rowid = ".(int) $this->
id;
342 $resql = $this->db->query($sql);
344 if (!$error && !$notrigger) {
346 $result = $this->call_trigger(
'MAILING_MODIFY', $user);
358 $this->db->rollback();
359 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
363 if ($this->db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
364 $this->error = $langs->trans(
"ErrorTitleAlreadyExists", $this->title);
366 $this->error = $this->db->lasterror();
368 $this->db->rollback();
380 public function fetch($rowid, $ref =
'')
382 $sql =
"SELECT m.rowid, m.messtype, m.titre as title, m.entity, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
383 $sql .=
", m.note_public, m.note_private";
384 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
385 $sql .=
", m.statut as status, m.nbemail";
386 $sql .=
", m.fk_user_creat, m.fk_user_valid";
387 $sql .=
", m.tms as date_modification";
388 $sql .=
", m.date_creat";
389 $sql .=
", m.date_valid";
390 $sql .=
", m.date_envoi";
391 $sql .=
", m.extraparams";
393 $sql .=
", m.name_from";
394 $sql .=
", m.fk_user_modif";
395 $sql .=
", m.fk_user_appro";
396 $sql .=
", m.date_appro";
398 $sql .=
", m.joined_file1";
399 $sql .=
", m.joined_file2";
400 $sql .=
", m.joined_file3";
401 $sql .=
", m.joined_file4";
402 $sql .=
", m.fk_project";
403 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
404 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
406 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
408 $sql .=
" AND m.rowid = ".(int) $rowid;
411 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
412 $result = $this->db->query($sql);
414 if ($this->db->num_rows($result)) {
415 $obj = $this->db->fetch_object($result);
417 $this->
id = $obj->rowid;
418 $this->entity = $obj->entity;
419 $this->
ref = $obj->rowid;
420 $this->title = $obj->title;
421 $this->messtype = $obj->messtype;
423 $this->statut = $obj->status;
424 $this->
status = $obj->status;
426 $this->nbemail = $obj->nbemail;
428 $this->sujet = $obj->sujet;
432 $this->body = $obj->body;
435 $this->bgcolor = $obj->bgcolor;
436 $this->bgimage = $obj->bgimage;
437 $this->evenunsubscribe = $obj->evenunsubscribe;
438 $this->note_public = $obj->note_public;
439 $this->note_private = $obj->note_private;
441 $this->email_from = $obj->email_from;
442 $this->email_replyto = $obj->email_replyto;
443 $this->email_errorsto = $obj->email_errorsto;
445 $this->user_creation_id = $obj->fk_user_creat;
446 $this->user_validation_id = $obj->fk_user_valid;
448 $this->date_creation = $this->db->jdate($obj->date_creat);
449 $this->date_validation = $this->db->jdate($obj->date_valid);
450 $this->date_envoi = $this->db->jdate($obj->date_envoi);
451 $this->date_modification = $this->db->jdate($obj->date_modification);
453 $this->extraparams = (array) json_decode($obj->extraparams,
true);
455 $this->tag = $obj->tag;
456 $this->name_from = $obj->name_from;
457 $this->fk_user_modif = $obj->fk_user_modif;
458 $this->fk_user_appro = $obj->fk_user_appro;
459 $this->date_appro = $obj->date_appro;
460 $this->cible = $obj->cible;
461 $this->joined_file1 = $obj->joined_file1;
462 $this->joined_file2 = $obj->joined_file2;
463 $this->joined_file3 = $obj->joined_file3;
464 $this->joined_file4 = $obj->joined_file4;
465 $this->fk_project = $obj->fk_project;
467 if ($this->messtype ==
'sms') {
468 $this->picto =
'phone';
473 dol_syslog(get_class($this).
"::fetch Erreur -1");
477 dol_syslog(get_class($this).
"::fetch Erreur -2");
513 if (empty($option1)) {
528 $object->user_creation_id = $user->id;
529 $object->user_validation_id =
null;
535 $object->context[
'createfromclone'] =
'createfromclone';
536 $result =
$object->create($user, $notrigger);
546 if (!empty($option2)) {
547 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
551 $target_array = array();
553 $sql =
"SELECT fk_contact,";
554 $sql .=
" lastname,";
555 $sql .=
" firstname,";
558 $sql .=
" source_url,";
559 $sql .=
" source_id ,";
560 $sql .=
" source_type";
561 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
562 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
564 $result = $this->db->query($sql);
566 if ($this->db->num_rows($result)) {
567 while ($obj = $this->db->fetch_object($result)) {
568 $target_array[] = array(
569 'fk_contact' => $obj->fk_contact,
570 'lastname' => $obj->lastname,
571 'firstname' => $obj->firstname,
572 'email' => $obj->email,
573 'other' => $obj->other,
574 'source_url' => $obj->source_url,
575 'source_id' => $obj->source_id,
576 'source_type' => $obj->source_type
581 $this->error = $this->db->lasterror();
585 $mailing_target->addTargetsToDatabase(
$object->id, $target_array);
589 unset(
$object->context[
'createfromclone']);
596 $this->db->rollback();
611 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
612 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
613 $sql .=
" WHERE rowid = ".((int) $this->
id);
616 if ($this->db->query($sql)) {
619 $this->error = $this->db->lasterror();
634 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
635 $sql .=
" SET statut = 0, tms = '".$this->db->idate($now).
"', fk_user_modif=".$user->id;
636 $sql .=
" WHERE rowid = ".((int) $this->
id);
639 if ($this->db->query($sql)) {
642 $this->error = $this->db->lasterror();
654 public function delete($user, $notrigger = 0)
661 $result = $this->call_trigger(
'MAILING_DELETE', $user);
668 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
669 $sql .=
" WHERE rowid = " . ((int) $this->
id);
672 $resql = $this->db->query($sql);
684 $this->db->rollback();
685 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
689 $this->db->rollback();
690 $this->error = $this->db->lasterror();
694 $this->db->rollback();
708 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
709 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
711 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
712 $resql = $this->db->query($sql);
718 $this->error = $this->db->lasterror();
734 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
735 $sql .=
" SET statut = 0";
736 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
738 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
739 $resql = $this->db->query($sql);
743 $this->error = $this->db->lasterror();
760 $sql =
"SELECT email, statut FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
761 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
762 $sql .=
" AND rowid = ".((int) $id);
763 $resql = $this->db->query($sql);
765 $nb = $this->db->num_rows($resql);
766 $obj = $this->db->fetch_object($resql);
767 if ($obj->statut != -1) {
768 $langs->load(
"errors");
769 $this->error = $langs->trans(
'ErrorIsNotInError', $obj->email);
773 $this->error = $this->db->lasterror();
776 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
777 $sql .=
" SET statut = 0";
778 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
779 $sql .=
" AND rowid = ".((int) $id);
780 $sql .=
" AND statut = -1";
782 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
783 $resql = $this->db->query($sql);
787 $this->error = $this->db->lasterror();
800 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
801 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
802 if ($mode ==
'alreadysent') {
803 $sql .=
" AND statut <> 0";
804 } elseif ($mode ==
'alreadysentok') {
805 $sql .=
" AND statut > 0";
806 } elseif ($mode ==
'alreadysentko') {
807 $sql .=
" AND statut = -1";
808 } elseif ($mode ==
'all') {
811 $this->error =
'BadValueForParameterMode';
815 $resql = $this->db->query($sql);
817 $obj = $this->db->fetch_object($resql);
822 $this->error = $this->db->lasterror();
836 $sql =
"SELECT COUNT(rowid) as nb";
837 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
838 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
840 $resql = $this->db->query($sql);
842 $obj = $this->db->fetch_object($resql);
844 $nbforupdate = $obj->nb;
846 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
847 $sql .=
' WHERE rowid = '.((int) $this->
id);
849 $resqlupdate = $this->db->query($sql);
850 if (! $resqlupdate) {
851 $this->error = $this->db->lasterror();
854 $this->nbemail = (int) $nbforupdate;
858 $this->error = $this->db->lasterror();
876 $langs->load(
'mails');
879 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
880 if (isset($this->
status)) {
881 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
883 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
884 if (isset($this->title)) {
885 $datas[
'title'] =
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
887 if (isset($this->sujet)) {
888 $datas[
'subject'] =
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
904 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
906 global $conf, $langs, $hookmanager;
908 if (!empty($conf->dol_no_mouse_hover)) {
915 'objecttype' => $this->element,
919 $classfortooltip =
'classfortooltip';
922 $classfortooltip =
'classforajaxtooltip';
923 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
929 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
931 if ($option !=
'nolink') {
933 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
934 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
935 $add_save_lastsearch_values = 1;
937 if ($add_save_lastsearch_values) {
938 $url .=
'&save_lastsearch_values=1';
943 if (empty($notooltip)) {
945 $label = $langs->trans(
"ShowEMailing");
946 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
948 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
949 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
951 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
954 $linkstart =
'<a href="'.$url.
'"';
955 $linkstart .= $linkclose.
'>';
958 $result .= $linkstart;
960 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
962 if ($withpicto != 2) {
963 $result .= $this->ref;
969 $hookmanager->initHooks(array(
'emailingdao'));
970 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
971 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
973 $result = $hookmanager->resPrint;
975 $result .= $hookmanager->resPrint;
1004 $langs->load(
"mailing");
1006 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
1007 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
1009 $statusType =
'status'.$status;
1011 $statusType =
'status3';
1014 $statusType =
'status6';
1017 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
1033 $langs->load(
"mails");
1035 $labelStatus = array();
1036 $labelStatusShort = array();
1038 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
1039 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
1040 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
1041 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
1042 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
1043 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
1044 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
1045 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
1046 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
1047 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
1049 $statusType =
'status'.$status;
1050 if ($status == -1) {
1051 $statusType =
'status8';
1054 $statusType =
'status6';
1057 $statusType =
'status4';
1061 if ($status == -1) {
1062 $param = array(
'badgeParams' => array(
'attr' => array(
'title' => $desc)));
1065 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status],
'', $statusType, $mode,
'', $param);
if(! $sortfield) if(! $sortorder) $object
Parent class of all other business classes (invoices, contracts, proposals, orders,...
setErrorsFromObject($object)
setErrorsFromObject
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.
setDraft($user)
SetDraft 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)
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.
resetTargetErrorStatus($user, $id)
Reset status of a specific recipient in error.
createFromClone(User $user, $fromid, $option1, $option2, $notrigger=0)
Load an object from its id and create a new one in database.
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 titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.