28require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
39 public $element =
'mailing';
44 public $table_element =
'mailing';
49 public $picto =
'email';
54 public $messtype =
'email';
74 public $evenunsubscribe;
125 public $email_replyto;
130 public $email_errorsto;
135 public $joined_file1;
140 public $joined_file2;
145 public $joined_file3;
150 public $joined_file4;
160 public $extraparams = array();
165 public $statut_dest = array();
170 public $substitutionarray;
175 public $substitutionarrayfortest;
177 const STATUS_DRAFT = 0;
178 const STATUS_VALIDATED = 1;
179 const STATUS_SENTPARTIALY = 2;
180 const STATUS_SENTCOMPLETELY = 3;
193 $this->labelStatus[0] =
'MailingStatusDraft';
194 $this->labelStatus[1] =
'MailingStatusValidated';
195 $this->labelStatus[2] =
'MailingStatusSentPartialy';
196 $this->labelStatus[3] =
'MailingStatusSentCompletely';
198 $this->statut_dest[0] =
'MailingStatusNotSent';
199 $this->statut_dest[1] =
'MailingStatusSent';
200 $this->statut_dest[2] =
'MailingStatusRead';
201 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
202 $this->statut_dest[-1] =
'MailingStatusError';
212 public function create($user, $notrigger = 0)
214 global
$conf, $langs;
217 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
218 $this->error =
'InvalidHTMLStringCantBeCleaned';
222 $this->title = trim($this->title);
223 $this->email_from = trim($this->email_from);
224 if (empty($this->messtype)) {
225 $this->messtype =
'email';
227 if (!$this->email_from) {
228 if ($this->messtype !==
'sms') {
229 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"MailFrom"));
231 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"PhoneFrom"));
241 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
242 $sql .=
" (messtype, date_creat, fk_user_creat, entity)";
243 $sql .=
" VALUES ('".$this->db->escape($this->messtype).
"', '".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int)
$conf->entity).
")";
246 $this->title = $langs->trans(
"NoTitle");
251 $resql = $this->db->query($sql);
253 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
255 $result = $this->
update($user, 1);
260 if (!$error && !$notrigger) {
273 $this->db->rollback();
274 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
278 $this->error = $this->db->lasterror();
279 $this->db->rollback();
291 public function update($user, $notrigger = 0)
296 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
297 $this->error =
'InvalidHTMLStringCantBeCleaned';
301 if (empty($this->messtype)) {
302 $this->messtype =
'email';
308 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing";
309 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
310 $sql .=
", messtype = '".$this->db->escape($this->messtype).
"'";
311 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
312 $sql .=
", body = '".$this->db->escape($this->body).
"'";
313 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
314 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
315 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
316 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
317 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
318 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
319 $sql .=
", note_public = '".$this->db->escape($this->note_public).
"'";
320 $sql .=
", note_private = '".$this->db->escape($this->note_private).
"'";
321 $sql .=
" WHERE rowid = ".(int) $this->
id;
324 $resql = $this->db->query($sql);
326 if (!$error && !$notrigger) {
340 $this->db->rollback();
341 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
345 if ($this->db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
346 $this->error = $langs->trans(
"ErrorTitleAlreadyExists", $this->title);
348 $this->error = $this->db->lasterror();
350 $this->db->rollback();
362 public function fetch($rowid, $ref =
'')
364 $sql =
"SELECT m.rowid, m.messtype, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
365 $sql .=
", m.note_public, m.note_private";
366 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
367 $sql .=
", m.statut as status, m.nbemail";
368 $sql .=
", m.fk_user_creat, m.fk_user_valid";
369 $sql .=
", m.date_creat";
370 $sql .=
", m.date_valid";
371 $sql .=
", m.date_envoi";
372 $sql .=
", m.extraparams";
373 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
374 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
376 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
378 $sql .=
" AND m.rowid = ".(int) $rowid;
381 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
382 $result = $this->db->query($sql);
384 if ($this->db->num_rows($result)) {
385 $obj = $this->db->fetch_object($result);
387 $this->
id = $obj->rowid;
388 $this->
ref = $obj->rowid;
389 $this->title = $obj->title;
390 $this->messtype = $obj->messtype;
392 $this->statut = $obj->status;
393 $this->
status = $obj->status;
395 $this->nbemail = $obj->nbemail;
397 $this->sujet = $obj->sujet;
401 $this->body = $obj->body;
404 $this->bgcolor = $obj->bgcolor;
405 $this->bgimage = $obj->bgimage;
406 $this->evenunsubscribe = $obj->evenunsubscribe;
407 $this->note_public = $obj->note_public;
408 $this->note_private = $obj->note_private;
410 $this->email_from = $obj->email_from;
411 $this->email_replyto = $obj->email_replyto;
412 $this->email_errorsto = $obj->email_errorsto;
414 $this->user_creation_id = $obj->fk_user_creat;
415 $this->user_validation_id = $obj->fk_user_valid;
417 $this->date_creation = $this->db->jdate($obj->date_creat);
418 $this->date_validation = $this->db->jdate($obj->date_valid);
419 $this->date_envoi = $this->db->jdate($obj->date_envoi);
421 $this->extraparams = (array) json_decode($obj->extraparams,
true);
423 if ($this->messtype ==
'sms') {
424 $this->picto =
'phone';
429 dol_syslog(get_class($this).
"::fetch Erreur -1");
433 dol_syslog(get_class($this).
"::fetch Erreur -2");
468 if (empty($option1)) {
483 $object->user_creation_id = $user->id;
484 $object->user_validation_id =
null;
490 $object->context[
'createfromclone'] =
'createfromclone';
491 $result =
$object->create($user);
496 $this->errors = array_merge($this->errors,
$object->errors);
502 if (!empty($option2)) {
503 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
507 $target_array = array();
509 $sql =
"SELECT fk_contact,";
510 $sql .=
" lastname,";
511 $sql .=
" firstname,";
514 $sql .=
" source_url,";
515 $sql .=
" source_id ,";
516 $sql .=
" source_type";
517 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
518 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
520 $result = $this->db->query($sql);
522 if ($this->db->num_rows($result)) {
523 while ($obj = $this->db->fetch_object($result)) {
524 $target_array[] = array(
525 'fk_contact' => $obj->fk_contact,
526 'lastname' => $obj->lastname,
527 'firstname' => $obj->firstname,
528 'email' => $obj->email,
529 'other' => $obj->other,
530 'source_url' => $obj->source_url,
531 'source_id' => $obj->source_id,
532 'source_type' => $obj->source_type
537 $this->error = $this->db->lasterror();
541 $mailing_target->addTargetsToDatabase(
$object->id, $target_array);
545 unset(
$object->context[
'createfromclone']);
552 $this->db->rollback();
567 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
568 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
569 $sql .=
" WHERE rowid = ".((int) $this->
id);
572 if ($this->db->query($sql)) {
575 $this->error = $this->db->lasterror();
588 public function delete($user, $notrigger = 0)
602 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
603 $sql .=
" WHERE rowid = " . ((int) $this->
id);
606 $resql = $this->db->query($sql);
618 $this->db->rollback();
619 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
623 $this->db->rollback();
624 $this->error = $this->db->lasterror();
628 $this->db->rollback();
642 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
643 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
645 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
646 $resql = $this->db->query($sql);
652 $this->error = $this->db->lasterror();
668 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
669 $sql .=
" SET statut = 0";
670 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
672 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
673 $resql = $this->db->query($sql);
677 $this->error = $this->db->lasterror();
694 $sql =
"SELECT email, statut FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
695 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
696 $sql .=
" AND rowid = ".((int) $id);
697 $resql = $this->db->query($sql);
699 $nb = $this->db->num_rows($resql);
700 $obj = $this->db->fetch_object($resql);
701 if ($obj->statut != -1) {
702 $langs->load(
"errors");
703 $this->error = $langs->trans(
'ErrorIsNotInError', $obj->email);
707 $this->error = $this->db->lasterror();
710 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
711 $sql .=
" SET statut = 0";
712 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
713 $sql .=
" AND rowid = ".((int) $id);
714 $sql .=
" AND statut = -1";
716 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
717 $resql = $this->db->query($sql);
721 $this->error = $this->db->lasterror();
734 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
735 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
736 if ($mode ==
'alreadysent') {
737 $sql .=
" AND statut <> 0";
738 } elseif ($mode ==
'alreadysentok') {
739 $sql .=
" AND statut > 0";
740 } elseif ($mode ==
'alreadysentko') {
741 $sql .=
" AND statut = -1";
743 $this->error =
'BadValueForParameterMode';
747 $resql = $this->db->query($sql);
749 $obj = $this->db->fetch_object($resql);
754 $this->error = $this->db->lasterror();
768 $sql =
"SELECT COUNT(rowid) as nb";
769 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
770 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
772 $resql = $this->db->query($sql);
774 $obj = $this->db->fetch_object($resql);
776 $nbforupdate = $obj->nb;
778 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
779 $sql .=
' WHERE rowid = '.((int) $this->
id);
781 $resqlupdate = $this->db->query($sql);
782 if (! $resqlupdate) {
783 $this->error = $this->db->lasterror();
786 $this->nbemail = (int) $nbforupdate;
790 $this->error = $this->db->lasterror();
808 $langs->load(
'mails');
811 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
812 if (isset($this->
status)) {
813 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
815 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
816 if (isset($this->title)) {
817 $datas[
'title'] =
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
819 if (isset($this->sujet)) {
820 $datas[
'subject'] =
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
836 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
838 global
$conf, $langs, $hookmanager;
840 if (!empty(
$conf->dol_no_mouse_hover)) {
847 'objecttype' => $this->element,
851 $classfortooltip =
'classfortooltip';
854 $classfortooltip =
'classforajaxtooltip';
855 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
861 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
863 if ($option !=
'nolink') {
865 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
866 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
867 $add_save_lastsearch_values = 1;
869 if ($add_save_lastsearch_values) {
870 $url .=
'&save_lastsearch_values=1';
875 if (empty($notooltip)) {
877 $label = $langs->trans(
"ShowEMailing");
878 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
880 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
881 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
883 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
886 $linkstart =
'<a href="'.$url.
'"';
887 $linkstart .= $linkclose.
'>';
890 $result .= $linkstart;
892 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
894 if ($withpicto != 2) {
895 $result .= $this->ref;
901 $hookmanager->initHooks(array(
'emailingdao'));
902 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
903 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
905 $result = $hookmanager->resPrint;
907 $result .= $hookmanager->resPrint;
936 $langs->load(
"mailing");
938 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
939 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
941 $statusType =
'status'.$status;
943 $statusType =
'status3';
946 $statusType =
'status6';
949 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
965 $langs->load(
"mails");
967 $labelStatus = array();
968 $labelStatusShort = array();
970 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
971 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
972 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
973 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
974 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
975 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
976 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
977 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
978 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
979 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
981 $statusType =
'status'.$status;
983 $statusType =
'status8';
986 $statusType =
'status6';
989 $statusType =
'status4';
994 $param = array(
'badgeParams' => array(
'attr' => array(
'title' => $desc)));
997 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status],
'', $statusType, $mode,
'', $param);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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.
resetTargetErrorStatus($user, $id)
Reset status of a specific recipient in error.
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.
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)
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...