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;
115 public $email_replyto;
120 public $email_errorsto;
125 public $joined_file1;
130 public $joined_file2;
135 public $joined_file3;
140 public $joined_file4;
150 public $extraparams = array();
155 public $statut_dest = array();
160 public $substitutionarray;
165 public $substitutionarrayfortest;
167 const STATUS_DRAFT = 0;
168 const STATUS_VALIDATED = 1;
169 const STATUS_SENTPARTIALY = 2;
170 const STATUS_SENTCOMPLETELY = 3;
183 $this->labelStatus[0] =
'MailingStatusDraft';
184 $this->labelStatus[1] =
'MailingStatusValidated';
185 $this->labelStatus[2] =
'MailingStatusSentPartialy';
186 $this->labelStatus[3] =
'MailingStatusSentCompletely';
188 $this->statut_dest[0] =
'MailingStatusNotSent';
189 $this->statut_dest[1] =
'MailingStatusSent';
190 $this->statut_dest[2] =
'MailingStatusRead';
191 $this->statut_dest[3] =
'MailingStatusReadAndUnsubscribe';
192 $this->statut_dest[-1] =
'MailingStatusError';
202 public function create($user, $notrigger = 0)
204 global
$conf, $langs;
207 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
208 $this->error =
'InvalidHTMLStringCantBeCleaned';
212 $this->title = trim($this->title);
213 $this->email_from = trim($this->email_from);
215 if (!$this->email_from) {
216 if ($this->messtype !==
'sms') {
217 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"MailFrom"));
219 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"PhoneFrom"));
229 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing";
230 $sql .=
" (date_creat, fk_user_creat, entity)";
231 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $user->id).
", ".((int)
$conf->entity).
")";
234 $this->title = $langs->trans(
"NoTitle");
239 $resql = $this->db->query($sql);
241 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing");
243 $result = $this->
update($user, 1);
248 if (!$error && !$notrigger) {
261 $this->db->rollback();
262 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
266 $this->error = $this->db->lasterror();
267 $this->db->rollback();
279 public function update($user, $notrigger = 0)
284 if (preg_match(
'/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
285 $this->error =
'InvalidHTMLStringCantBeCleaned';
292 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
293 $sql .=
" SET titre = '".$this->db->escape($this->title).
"'";
294 $sql .=
", messtype = '".$this->db->escape($this->messtype).
"'";
295 $sql .=
", sujet = '".$this->db->escape($this->sujet).
"'";
296 $sql .=
", body = '".$this->db->escape($this->body).
"'";
297 $sql .=
", email_from = '".$this->db->escape($this->email_from).
"'";
298 $sql .=
", email_replyto = '".$this->db->escape($this->email_replyto).
"'";
299 $sql .=
", email_errorsto = '".$this->db->escape($this->email_errorsto).
"'";
300 $sql .=
", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) :
null).
"'";
301 $sql .=
", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) :
null).
"'";
302 $sql .=
", evenunsubscribe = ".((int) $this->evenunsubscribe);
303 $sql .=
" WHERE rowid = ".(int) $this->
id;
306 $resql = $this->db->query($sql);
308 if (!$error && !$notrigger) {
322 $this->db->rollback();
323 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
327 if ($this->db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
328 $this->error = $langs->trans(
"ErrorTitleAlreadyExists", $this->title);
330 $this->error = $this->db->lasterror();
332 $this->db->rollback();
344 public function fetch($rowid, $ref =
'')
346 $sql =
"SELECT m.rowid, m.messtype, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
347 $sql .=
", m.email_from, m.email_replyto, m.email_errorsto";
348 $sql .=
", m.statut as status, m.nbemail";
349 $sql .=
", m.fk_user_creat, m.fk_user_valid";
350 $sql .=
", m.date_creat";
351 $sql .=
", m.date_valid";
352 $sql .=
", m.date_envoi";
353 $sql .=
", m.extraparams";
354 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing as m";
355 $sql .=
" WHERE entity IN (".getEntity(
'mailing').
")";
357 $sql .=
" AND m.titre = '".$this->db->escape($ref).
"'";
359 $sql .=
" AND m.rowid = ".(int) $rowid;
362 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
363 $result = $this->db->query($sql);
365 if ($this->db->num_rows($result)) {
366 $obj = $this->db->fetch_object($result);
368 $this->
id = $obj->rowid;
369 $this->
ref = $obj->rowid;
370 $this->title = $obj->title;
371 $this->messtype = $obj->messtype;
373 $this->statut = $obj->status;
374 $this->
status = $obj->status;
376 $this->nbemail = $obj->nbemail;
378 $this->sujet = $obj->sujet;
382 $this->body = $obj->body;
385 $this->bgcolor = $obj->bgcolor;
386 $this->bgimage = $obj->bgimage;
387 $this->evenunsubscribe = $obj->evenunsubscribe;
389 $this->email_from = $obj->email_from;
390 $this->email_replyto = $obj->email_replyto;
391 $this->email_errorsto = $obj->email_errorsto;
393 $this->user_creation_id = $obj->fk_user_creat;
394 $this->user_validation_id = $obj->fk_user_valid;
396 $this->date_creation = $this->db->jdate($obj->date_creat);
397 $this->date_validation = $this->db->jdate($obj->date_valid);
398 $this->date_envoi = $this->db->jdate($obj->date_envoi);
400 $this->extraparams = (array) json_decode($obj->extraparams,
true);
402 if ($this->messtype ==
'sms') {
403 $this->picto =
'phone';
408 dol_syslog(get_class($this).
"::fetch Erreur -1");
412 dol_syslog(get_class($this).
"::fetch Erreur -2");
447 if (empty($option1)) {
460 $object->user_creation_id = $user->id;
461 $object->user_validation_id =
null;
467 $object->context[
'createfromclone'] =
'createfromclone';
468 $result =
$object->create($user);
473 $this->errors = array_merge($this->errors,
$object->errors);
479 if (!empty($option2)) {
480 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
484 $target_array = array();
486 $sql =
"SELECT fk_contact,";
487 $sql .=
" lastname,";
488 $sql .=
" firstname,";
491 $sql .=
" source_url,";
492 $sql .=
" source_id ,";
493 $sql .=
" source_type";
494 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
495 $sql .=
" WHERE fk_mailing = ".((int) $fromid);
497 $result = $this->db->query($sql);
499 if ($this->db->num_rows($result)) {
500 while ($obj = $this->db->fetch_object($result)) {
501 $target_array[] = array(
502 'fk_contact' => $obj->fk_contact,
503 'lastname' => $obj->lastname,
504 'firstname' => $obj->firstname,
505 'email' => $obj->email,
506 'other' => $obj->other,
507 'source_url' => $obj->source_url,
508 'source_id' => $obj->source_id,
509 'source_type' => $obj->source_type
514 $this->error = $this->db->lasterror();
518 $mailing_target->addTargetsToDatabase(
$object->id, $target_array);
522 unset(
$object->context[
'createfromclone']);
529 $this->db->rollback();
544 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing ";
545 $sql .=
" SET statut = 1, date_valid = '".$this->db->idate($now).
"', fk_user_valid=".$user->id;
546 $sql .=
" WHERE rowid = ".((int) $this->
id);
549 if ($this->db->query($sql)) {
552 $this->error = $this->db->lasterror();
565 public function delete($user, $notrigger = 0)
579 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"mailing";
580 $sql .=
" WHERE rowid = " . ((int) $this->
id);
583 $resql = $this->db->query($sql);
595 $this->db->rollback();
596 dol_syslog(__METHOD__ .
' ' . $this->error, LOG_ERR);
600 $this->db->rollback();
601 $this->error = $this->db->lasterror();
605 $this->db->rollback();
619 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
620 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
622 dol_syslog(
"Mailing::delete_targets", LOG_DEBUG);
623 $resql = $this->db->query($sql);
629 $this->error = $this->db->lasterror();
645 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
646 $sql .=
" SET statut = 0";
647 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
649 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
650 $resql = $this->db->query($sql);
654 $this->error = $this->db->lasterror();
671 $sql =
"SELECT email, statut FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
672 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
673 $sql .=
" AND rowid = ".((int) $id);
674 $resql = $this->db->query($sql);
676 $nb = $this->db->num_rows($resql);
677 $obj = $this->db->fetch_object($resql);
678 if ($obj->statut != -1) {
679 $langs->load(
"errors");
680 $this->error = $langs->trans(
'ErrorIsNotInError', $obj->email);
684 $this->error = $this->db->lasterror();
687 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_cibles";
688 $sql .=
" SET statut = 0";
689 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
690 $sql .=
" AND rowid = ".((int) $id);
691 $sql .=
" AND statut = -1";
693 dol_syslog(
"Mailing::reset_targets_status", LOG_DEBUG);
694 $resql = $this->db->query($sql);
698 $this->error = $this->db->lasterror();
711 $sql =
"SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
712 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
713 if ($mode ==
'alreadysent') {
714 $sql .=
" AND statut <> 0";
715 } elseif ($mode ==
'alreadysentok') {
716 $sql .=
" AND statut > 0";
717 } elseif ($mode ==
'alreadysentko') {
718 $sql .=
" AND statut = -1";
720 $this->error =
'BadValueForParameterMode';
724 $resql = $this->db->query($sql);
726 $obj = $this->db->fetch_object($resql);
731 $this->error = $this->db->lasterror();
745 $sql =
"SELECT COUNT(rowid) as nb";
746 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles";
747 $sql .=
" WHERE fk_mailing = ".((int) $this->
id);
749 $resql = $this->db->query($sql);
751 $obj = $this->db->fetch_object($resql);
753 $nbforupdate = $obj->nb;
755 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'mailing SET nbemail = '.((int) $nbforupdate);
756 $sql .=
' WHERE rowid = '.((int) $this->
id);
758 $resqlupdate = $this->db->query($sql);
759 if (! $resqlupdate) {
760 $this->error = $this->db->lasterror();
763 $this->nbemail = (int) $nbforupdate;
767 $this->error = $this->db->lasterror();
785 $langs->load(
'mails');
788 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ShowEMailing").
'</u>';
789 if (isset($this->
status)) {
790 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
792 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
793 if (isset($this->title)) {
794 $datas[
'title'] =
'<br><b>'.$langs->trans(
'MailTitle').
':</b> '.$this->title;
796 if (isset($this->sujet)) {
797 $datas[
'subject'] =
'<br><b>'.$langs->trans(
'MailTopic').
':</b> '.$this->sujet;
813 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
815 global
$conf, $langs, $hookmanager;
817 if (!empty(
$conf->dol_no_mouse_hover)) {
824 'objecttype' => $this->element,
828 $classfortooltip =
'classfortooltip';
831 $classfortooltip =
'classforajaxtooltip';
832 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
838 $url = DOL_URL_ROOT.
'/comm/mailing/card.php?id='.$this->id;
840 if ($option !=
'nolink') {
842 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
843 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
844 $add_save_lastsearch_values = 1;
846 if ($add_save_lastsearch_values) {
847 $url .=
'&save_lastsearch_values=1';
852 if (empty($notooltip)) {
854 $label = $langs->trans(
"ShowEMailing");
855 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
857 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
858 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
860 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
863 $linkstart =
'<a href="'.$url.
'"';
864 $linkstart .= $linkclose.
'>';
867 $result .= $linkstart;
869 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
871 if ($withpicto != 2) {
872 $result .= $this->ref;
878 $hookmanager->initHooks(array(
'emailingdao'));
879 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
880 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
882 $result = $hookmanager->resPrint;
884 $result .= $hookmanager->resPrint;
913 $langs->load(
"mailing");
915 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
916 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
918 $statusType =
'status'.$status;
920 $statusType =
'status3';
923 $statusType =
'status6';
926 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
942 $langs->load(
"mails");
944 $labelStatus = array();
945 $labelStatusShort = array();
947 $labelStatus[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
948 $labelStatus[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
949 $labelStatus[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
950 $labelStatus[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
951 $labelStatus[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
952 $labelStatusShort[-1] = $langs->transnoentitiesnoconv(
'MailingStatusError');
953 $labelStatusShort[0] = $langs->transnoentitiesnoconv(
'MailingStatusNotSent');
954 $labelStatusShort[1] = $langs->transnoentitiesnoconv(
'MailingStatusSent');
955 $labelStatusShort[2] = $langs->transnoentitiesnoconv(
'MailingStatusRead');
956 $labelStatusShort[3] = $langs->transnoentitiesnoconv(
'MailingStatusNotContact');
958 $statusType =
'status'.$status;
960 $statusType =
'status8';
963 $statusType =
'status6';
966 $statusType =
'status4';
971 $param = array(
'badgeParams' => array(
'attr' => array(
'title' => $desc)));
974 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_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (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...