28require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
39 public $element =
'mailing';
44 public $table_element =
'mailing';
49 public $picto =
'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);
225 if (!$this->email_from) {
226 if ($this->messtype !==
'sms') {
227 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"MailFrom"));
229 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"PhoneFrom"));
239 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
240 $sql .=
" (date_creat, fk_user_creat, entity)";
241 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int)
$conf->entity).
")";
244 $this->title = $langs->trans(
"NoTitle");
249 $resql = $this->db->query($sql);
251 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
253 $result = $this->
update($user, 1);
258 if (!$error && !$notrigger) {
271 $this->db->rollback();
272 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
276 $this->error = $this->db->lasterror();
277 $this->db->rollback();
289 public function update($user, $notrigger = 0)
294 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
295 $this->error =
'InvalidHTMLStringCantBeCleaned';
302 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
303 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
304 $sql .=
", messtype = '".$this->db->escape($this->messtype).
"'";
305 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
306 $sql .=
", body = '".$this->db->escape($this->body).
"'";
307 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
308 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
309 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
310 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
311 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
312 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
313 $sql .=
", note_public = '".$this->db->escape($this->note_public).
"'";
314 $sql .=
", note_private = '".$this->db->escape($this->note_private).
"'";
315 $sql .=
" WHERE rowid = ".(int) $this->
id;
318 $resql = $this->db->query($sql);
320 if (!$error && !$notrigger) {
334 $this->db->rollback();
335 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
339 if ($this->db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
340 $this->error = $langs->trans(
"ErrorTitleAlreadyExists", $this->title);
342 $this->error = $this->db->lasterror();
344 $this->db->rollback();
356 public function fetch($rowid, $ref =
'')
358 $sql =
"SELECT m.rowid, m.messtype, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
359 $sql .=
", m.note_public, m.note_private";
360 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
361 $sql .=
", m.statut as status, m.nbemail";
362 $sql .=
", m.fk_user_creat, m.fk_user_valid";
363 $sql .=
", m.date_creat";
364 $sql .=
", m.date_valid";
365 $sql .=
", m.date_envoi";
366 $sql .=
", m.extraparams";
367 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
368 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
370 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
372 $sql .=
" AND m.rowid = ".(int) $rowid;
375 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
376 $result = $this->db->query($sql);
378 if ($this->db->num_rows($result)) {
379 $obj = $this->db->fetch_object($result);
381 $this->
id = $obj->rowid;
382 $this->
ref = $obj->rowid;
383 $this->title = $obj->title;
384 $this->messtype = $obj->messtype;
386 $this->statut = $obj->status;
387 $this->
status = $obj->status;
389 $this->nbemail = $obj->nbemail;
391 $this->sujet = $obj->sujet;
395 $this->body = $obj->body;
398 $this->bgcolor = $obj->bgcolor;
399 $this->bgimage = $obj->bgimage;
400 $this->evenunsubscribe = $obj->evenunsubscribe;
401 $this->note_public = $obj->note_public;
402 $this->note_private = $obj->note_private;
404 $this->email_from = $obj->email_from;
405 $this->email_replyto = $obj->email_replyto;
406 $this->email_errorsto = $obj->email_errorsto;
408 $this->user_creation_id = $obj->fk_user_creat;
409 $this->user_validation_id = $obj->fk_user_valid;
411 $this->date_creation = $this->db->jdate($obj->date_creat);
412 $this->date_validation = $this->db->jdate($obj->date_valid);
413 $this->date_envoi = $this->db->jdate($obj->date_envoi);
415 $this->extraparams = (array) json_decode($obj->extraparams,
true);
417 if ($this->messtype ==
'sms') {
418 $this->picto =
'phone';
423 dol_syslog(get_class($this).
"::fetch Erreur -1");
427 dol_syslog(get_class($this).
"::fetch Erreur -2");
462 if (empty($option1)) {
477 $object->user_creation_id = $user->id;
478 $object->user_validation_id =
null;
484 $object->context[
'createfromclone'] =
'createfromclone';
485 $result =
$object->create($user);
490 $this->errors = array_merge($this->errors,
$object->errors);
496 if (!empty($option2)) {
497 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
501 $target_array = array();
503 $sql =
"SELECT fk_contact,";
504 $sql .=
" lastname,";
505 $sql .=
" firstname,";
508 $sql .=
" source_url,";
509 $sql .=
" source_id ,";
510 $sql .=
" source_type";
511 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
512 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
514 $result = $this->db->query($sql);
516 if ($this->db->num_rows($result)) {
517 while ($obj = $this->db->fetch_object($result)) {
518 $target_array[] = array(
519 'fk_contact' => $obj->fk_contact,
520 'lastname' => $obj->lastname,
521 'firstname' => $obj->firstname,
522 'email' => $obj->email,
523 'other' => $obj->other,
524 'source_url' => $obj->source_url,
525 'source_id' => $obj->source_id,
526 'source_type' => $obj->source_type
531 $this->error = $this->db->lasterror();
535 $mailing_target->addTargetsToDatabase(
$object->id, $target_array);
539 unset(
$object->context[
'createfromclone']);
546 $this->db->rollback();
561 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
562 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
563 $sql .=
" WHERE rowid = ".((int) $this->
id);
566 if ($this->db->query($sql)) {
569 $this->error = $this->db->lasterror();
582 public function delete($user, $notrigger = 0)
596 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
597 $sql .=
" WHERE rowid = " . ((int) $this->
id);
600 $resql = $this->db->query($sql);
612 $this->db->rollback();
613 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
617 $this->db->rollback();
618 $this->error = $this->db->lasterror();
622 $this->db->rollback();
636 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
637 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
639 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
640 $resql = $this->db->query($sql);
646 $this->error = $this->db->lasterror();
662 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
663 $sql .=
" SET statut = 0";
664 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
666 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
667 $resql = $this->db->query($sql);
671 $this->error = $this->db->lasterror();
688 $sql =
"SELECT email, statut FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
689 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
690 $sql .=
" AND rowid = ".((int) $id);
691 $resql = $this->db->query($sql);
693 $nb = $this->db->num_rows($resql);
694 $obj = $this->db->fetch_object($resql);
695 if ($obj->statut != -1) {
696 $langs->load(
"errors");
697 $this->error = $langs->trans(
'ErrorIsNotInError', $obj->email);
701 $this->error = $this->db->lasterror();
704 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
705 $sql .=
" SET statut = 0";
706 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
707 $sql .=
" AND rowid = ".((int) $id);
708 $sql .=
" AND statut = -1";
710 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
711 $resql = $this->db->query($sql);
715 $this->error = $this->db->lasterror();
728 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
729 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
730 if ($mode ==
'alreadysent') {
731 $sql .=
" AND statut <> 0";
732 } elseif ($mode ==
'alreadysentok') {
733 $sql .=
" AND statut > 0";
734 } elseif ($mode ==
'alreadysentko') {
735 $sql .=
" AND statut = -1";
737 $this->error =
'BadValueForParameterMode';
741 $resql = $this->db->query($sql);
743 $obj = $this->db->fetch_object($resql);
748 $this->error = $this->db->lasterror();
762 $sql =
"SELECT COUNT(rowid) as nb";
763 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
764 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
766 $resql = $this->db->query($sql);
768 $obj = $this->db->fetch_object($resql);
770 $nbforupdate = $obj->nb;
772 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
773 $sql .=
' WHERE rowid = '.((int) $this->
id);
775 $resqlupdate = $this->db->query($sql);
776 if (! $resqlupdate) {
777 $this->error = $this->db->lasterror();
780 $this->nbemail = (int) $nbforupdate;
784 $this->error = $this->db->lasterror();
802 $langs->load(
'mails');
805 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
806 if (isset($this->
status)) {
807 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
809 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
810 if (isset($this->title)) {
811 $datas[
'title'] =
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
813 if (isset($this->sujet)) {
814 $datas[
'subject'] =
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
830 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
832 global
$conf, $langs, $hookmanager;
834 if (!empty(
$conf->dol_no_mouse_hover)) {
841 'objecttype' => $this->element,
845 $classfortooltip =
'classfortooltip';
848 $classfortooltip =
'classforajaxtooltip';
849 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
855 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
857 if ($option !=
'nolink') {
859 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
860 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
861 $add_save_lastsearch_values = 1;
863 if ($add_save_lastsearch_values) {
864 $url .=
'&save_lastsearch_values=1';
869 if (empty($notooltip)) {
871 $label = $langs->trans(
"ShowEMailing");
872 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
874 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
875 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
877 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
880 $linkstart =
'<a href="'.$url.
'"';
881 $linkstart .= $linkclose.
'>';
884 $result .= $linkstart;
886 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
888 if ($withpicto != 2) {
889 $result .= $this->ref;
895 $hookmanager->initHooks(array(
'emailingdao'));
896 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
897 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
899 $result = $hookmanager->resPrint;
901 $result .= $hookmanager->resPrint;
930 $langs->load(
"mailing");
932 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
933 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
935 $statusType =
'status'.$status;
937 $statusType =
'status3';
940 $statusType =
'status6';
943 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
959 $langs->load(
"mails");
961 $labelStatus = array();
962 $labelStatusShort = array();
964 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
965 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
966 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
967 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
968 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
969 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
970 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
971 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
972 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
973 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
975 $statusType =
'status'.$status;
977 $statusType =
'status8';
980 $statusType =
'status6';
983 $statusType =
'status4';
988 $param = array(
'badgeParams' => array(
'attr' => array(
'title' => $desc)));
991 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...