Merge pull request #15792 from frederic34/notifications

simplify notications class
This commit is contained in:
Laurent Destailleur 2020-12-21 18:14:04 +01:00 committed by GitHub
commit 04d249cef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -113,25 +113,38 @@ class Notify
$listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0); $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
$nb = -1; $nb = -1;
if (is_array($listofnotiftodo)) $nb = count($listofnotiftodo); if (is_array($listofnotiftodo)) {
if ($nb < 0) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend"); $nb = count($listofnotiftodo);
if ($nb == 0) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("NoNotificationsWillBeSent"); }
if ($nb == 1) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ANotificationsWillBeSent"); if ($nb < 0) {
if ($nb >= 2) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("SomeNotificationsWillBeSent", $nb); $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
} elseif ($nb == 0) {
$texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("NoNotificationsWillBeSent");
} elseif ($nb == 1) {
$texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ANotificationsWillBeSent");
} elseif ($nb >= 2) {
$texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("SomeNotificationsWillBeSent", $nb);
}
if (is_array($listofnotiftodo)) if (is_array($listofnotiftodo)) {
{
$i = 0; $i = 0;
foreach ($listofnotiftodo as $key => $val) foreach ($listofnotiftodo as $key => $val) {
{ if ($i) {
if ($i) $texte .= ', '; $texte .= ', ';
else $texte .= ' ('; } else {
if ($val['isemailvalid']) $texte .= $val['email']; $texte .= ' (';
else $texte .= $val['emaildesc']; }
if ($val['isemailvalid']) {
$texte .= $val['email'];
} else {
$texte .= $val['emaildesc'];
}
$i++; $i++;
} }
if ($i) $texte .= ')'; if ($i) {
} $texte .= ')';
}
}
return $texte; return $texte;
} }
@ -154,12 +167,19 @@ class Notify
$resarray = array(); $resarray = array();
$valueforthreshold = 0; $valueforthreshold = 0;
if (is_object($object)) $valueforthreshold = $object->total_ht; if (is_object($object)) {
$valueforthreshold = $object->total_ht;
}
if ($notifcode) {
if (is_numeric($notifcode)) {
$sqlnotifcode = " AND n.fk_action = ".$notifcode; // Old usage
} else {
$sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
}
}
if (!$error) if (!$error) {
{ if ($socid >= 0 && in_array('thirdparty', $scope)) {
if ($socid >= 0 && in_array('thirdparty', $scope))
{
$sql = "SELECT a.code, c.email, c.rowid"; $sql = "SELECT a.code, c.email, c.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,"; $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
$sql .= " ".MAIN_DB_PREFIX."socpeople as c,"; $sql .= " ".MAIN_DB_PREFIX."socpeople as c,";
@ -168,29 +188,26 @@ class Notify
$sql .= " WHERE n.fk_contact = c.rowid"; $sql .= " WHERE n.fk_contact = c.rowid";
$sql .= " AND a.rowid = n.fk_action"; $sql .= " AND a.rowid = n.fk_action";
$sql .= " AND n.fk_soc = s.rowid"; $sql .= " AND n.fk_soc = s.rowid";
if ($notifcode) $sql .= $sqlnotifcode;
{
if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
}
$sql .= " AND s.entity IN (".getEntity('societe').")"; $sql .= " AND s.entity IN (".getEntity('societe').")";
if ($socid > 0) $sql .= " AND s.rowid = ".$socid; if ($socid > 0) {
$sql .= " AND s.rowid = ".$socid;
}
dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG); dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql) {
{
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
$i = 0; $i = 0;
while ($i < $num) while ($i < $num) {
{
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($obj) if ($obj) {
{
$newval2 = trim($obj->email); $newval2 = trim($obj->email);
$isvalid = isValidEmail($newval2); $isvalid = isValidEmail($newval2);
if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid); if (empty($resarray[$newval2])) {
$resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
}
} }
$i++; $i++;
} }
@ -201,39 +218,34 @@ class Notify
} }
} }
if (!$error) if (!$error) {
{ if ($userid >= 0 && in_array('user', $scope)) {
if ($userid >= 0 && in_array('user', $scope))
{
$sql = "SELECT a.code, c.email, c.rowid"; $sql = "SELECT a.code, c.email, c.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,"; $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
$sql .= " ".MAIN_DB_PREFIX."user as c,"; $sql .= " ".MAIN_DB_PREFIX."user as c,";
$sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a"; $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a";
$sql .= " WHERE n.fk_user = c.rowid"; $sql .= " WHERE n.fk_user = c.rowid";
$sql .= " AND a.rowid = n.fk_action"; $sql .= " AND a.rowid = n.fk_action";
if ($notifcode) $sql .= $sqlnotifcode;
{
if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
}
$sql .= " AND c.entity IN (".getEntity('user').")"; $sql .= " AND c.entity IN (".getEntity('user').")";
if ($userid > 0) $sql .= " AND c.rowid = ".$userid; if ($userid > 0) {
$sql .= " AND c.rowid = ".$userid;
}
dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG); dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql) {
{
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
$i = 0; $i = 0;
while ($i < $num) while ($i < $num) {
{
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($obj) if ($obj) {
{
$newval2 = trim($obj->email); $newval2 = trim($obj->email);
$isvalid = isValidEmail($newval2); $isvalid = isValidEmail($newval2);
if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid); if (empty($resarray[$newval2])) {
$resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
}
} }
$i++; $i++;
} }
@ -244,48 +256,55 @@ class Notify
} }
} }
if (!$error) if (!$error) {
{ if (in_array('global', $scope)) {
if (in_array('global', $scope))
{
// List of notifications enabled for fixed email // List of notifications enabled for fixed email
foreach ($conf->global as $key => $val) foreach ($conf->global as $key => $val) {
{ if ($notifcode) {
if ($notifcode) if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
{ continue;
if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue; }
} else { } else {
if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue; if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
continue;
}
} }
$threshold = (float) $reg[1]; $threshold = (float) $reg[1];
if ($valueforthreshold < $threshold) continue; if ($valueforthreshold < $threshold) {
continue;
}
$tmpemail = explode(',', $val); $tmpemail = explode(',', $val);
foreach ($tmpemail as $key2 => $val2) foreach ($tmpemail as $key2 => $val2) {
{
$newval2 = trim($val2); $newval2 = trim($val2);
if ($newval2 == '__SUPERVISOREMAIL__') if ($newval2 == '__SUPERVISOREMAIL__') {
{ if ($user->fk_user > 0) {
if ($user->fk_user > 0)
{
$tmpuser = new User($this->db); $tmpuser = new User($this->db);
$tmpuser->fetch($user->fk_user); $tmpuser->fetch($user->fk_user);
if ($tmpuser->email) $newval2 = trim($tmpuser->email); if ($tmpuser->email) {
else $newval2 = ''; $newval2 = trim($tmpuser->email);
} else $newval2 = ''; } else {
$newval2 = '';
}
} else {
$newval2 = '';
}
} }
if ($newval2) if ($newval2) {
{
$isvalid = isValidEmail($newval2, 0); $isvalid = isValidEmail($newval2, 0);
if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid); if (empty($resarray[$newval2])) {
$resarray[$newval2] = array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
}
} }
} }
} }
} }
} }
if ($error) return -1; if ($error) {
return -1;
}
//var_dump($resarray); //var_dump($resarray);
return $resarray; return $resarray;
@ -309,11 +328,12 @@ class Notify
global $dolibarr_main_url_root; global $dolibarr_main_url_root;
global $action; global $action;
if (!in_array($notifcode, $this->arrayofnotifsupported)) return 0; if (!in_array($notifcode, $this->arrayofnotifsupported)) {
return 0;
}
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
if (!is_object($hookmanager)) if (!is_object($hookmanager)) {
{
include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
$hookmanager = new HookManager($this->db); $hookmanager = new HookManager($this->db);
} }
@ -330,7 +350,9 @@ class Notify
// Define some vars // Define some vars
$application = 'Dolibarr'; $application = 'Dolibarr';
if (!empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE; if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
$application = $conf->global->MAIN_APPLICATION_TITLE;
}
$replyto = $conf->notification->email_from; $replyto = $conf->notification->email_from;
$object_type = ''; $object_type = '';
$link = ''; $link = '';
@ -343,8 +365,7 @@ class Notify
$sql = ''; $sql = '';
// Check notification per third party // Check notification per third party
if (!empty($object->socid) && $object->socid > 0) if (!empty($object->socid) && $object->socid > 0) {
{
$sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,"; $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
$sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type"; $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c,"; $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
@ -354,8 +375,11 @@ class Notify
$sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action"; $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
$sql .= " AND n.fk_soc = s.rowid"; $sql .= " AND n.fk_soc = s.rowid";
$sql .= " AND c.statut = 1"; $sql .= " AND c.statut = 1";
if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage if (is_numeric($notifcode)) {
else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage $sql .= " AND n.fk_action = ".$notifcode; // Old usage
} else {
$sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
}
$sql .= " AND s.rowid = ".$object->socid; $sql .= " AND s.rowid = ".$object->socid;
$sql .= "\nUNION\n"; $sql .= "\nUNION\n";
@ -369,41 +393,42 @@ class Notify
$sql .= " ".MAIN_DB_PREFIX."notify_def as n"; $sql .= " ".MAIN_DB_PREFIX."notify_def as n";
$sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action"; $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
$sql .= " AND c.statut = 1"; $sql .= " AND c.statut = 1";
if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage if (is_numeric($notifcode)) {
else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage $sql .= " AND n.fk_action = ".$notifcode; // Old usage
} else {
$sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
}
$result = $this->db->query($sql); $result = $this->db->query($sql);
if ($result) if ($result) {
{
$num = $this->db->num_rows($result); $num = $this->db->num_rows($result);
$projtitle = ''; $projtitle = '';
if (!empty($object->fk_project)) if (!empty($object->fk_project)) {
{
require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
$proj = new Project($this->db); $proj = new Project($this->db);
$proj->fetch($object->fk_project); $proj->fetch($object->fk_project);
$projtitle = '('.$proj->title.')'; $projtitle = '('.$proj->title.')';
} }
if ($num > 0) if ($num > 0) {
{
$i = 0; $i = 0;
while ($i < $num && !$error) // For each notification couple defined (third party/actioncode) while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
{
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">"; $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
$notifcodedefid = $obj->adid; $notifcodedefid = $obj->adid;
$trackid = ''; $trackid = '';
if ($obj->type_target == 'tocontactid') $trackid = 'con'.$obj->id; if ($obj->type_target == 'tocontactid') {
if ($obj->type_target == 'touserid') $trackid = 'use'.$obj->id; $trackid = 'con'.$obj->id;
}
if ($obj->type_target == 'touserid') {
$trackid = 'use'.$obj->id;
}
if (dol_strlen($obj->email)) if (dol_strlen($obj->email)) {
{
// Set output language // Set output language
$outputlangs = $langs; $outputlangs = $langs;
if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
{
$outputlangs = new Translate('', $conf); $outputlangs = new Translate('', $conf);
$outputlangs->setDefaultLang($obj->default_lang); $outputlangs->setDefaultLang($obj->default_lang);
$outputlangs->loadLangs(array("main", "other")); $outputlangs->loadLangs(array("main", "other"));
@ -511,8 +536,7 @@ class Notify
} }
$ref = dol_sanitizeFileName($newref); $ref = dol_sanitizeFileName($newref);
$pdf_path = $dir_output."/".$ref."/".$ref.".pdf"; $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
if (!dol_is_file($pdf_path)) if (!dol_is_file($pdf_path)) {
{
// We can't add PDF as it is not generated yet. // We can't add PDF as it is not generated yet.
$filepdf = ''; $filepdf = '';
} else { } else {
@ -525,13 +549,18 @@ class Notify
$message .= $mesg; $message .= $mesg;
$parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list); $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
if (!isset($action)) $action = ''; if (!isset($action)) {
$action = '';
}
$reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook)) if (empty($reshook)) {
{ if (!empty($hookmanager->resArray['subject'])) {
if (!empty($hookmanager->resArray['subject'])) $subject .= $hookmanager->resArray['subject']; $subject .= $hookmanager->resArray['subject'];
if (!empty($hookmanager->resArray['message'])) $message .= $hookmanager->resArray['message']; }
if (!empty($hookmanager->resArray['message'])) {
$message .= $hookmanager->resArray['message'];
}
} }
$mailfile = new CMailFile( $mailfile = new CMailFile(
@ -553,17 +582,15 @@ class Notify
'notification' 'notification'
); );
if ($mailfile->sendfile()) if ($mailfile->sendfile()) {
{
if ($obj->type_target == 'touserid') { if ($obj->type_target == 'touserid') {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
$sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')"; $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
} else { } else {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
$sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')"; $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
} }
if (!$this->db->query($sql)) if (!$this->db->query($sql)) {
{
dol_print_error($this->db); dol_print_error($this->db);
} }
} else { } else {
@ -579,23 +606,22 @@ class Notify
dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid)); dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
} }
} else { } else {
$error++; $error++;
$this->errors[] = $this->db->lasterror(); $this->errors[] = $this->db->lasterror();
dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR); dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
return -1; return -1;
} }
// Check notification using fixed email // Check notification using fixed email
if (!$error) if (!$error) {
{ foreach ($conf->global as $key => $val) {
foreach ($conf->global as $key => $val)
{
$reg = array(); $reg = array();
if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue; if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
continue;
}
$threshold = (float) $reg[1]; $threshold = (float) $reg[1];
if (!empty($object->total_ht) && $object->total_ht <= $threshold) if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
{
dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification"); dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
continue; continue;
} }
@ -604,7 +630,9 @@ class Notify
$sendto = $conf->global->$param; $sendto = $conf->global->$param;
$notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid'); $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
if ($notifcodedefid <= 0) dol_print_error($this->db, 'Failed to get id from code'); if ($notifcodedefid <= 0) {
dol_print_error($this->db, 'Failed to get id from code');
}
$trackid = ''; $trackid = '';
$object_type = ''; $object_type = '';
@ -721,8 +749,7 @@ class Notify
} }
$ref = dol_sanitizeFileName($newref); $ref = dol_sanitizeFileName($newref);
$pdf_path = $dir_output."/".$ref."/".$ref.".pdf"; $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
if (!dol_is_file($pdf_path)) if (!dol_is_file($pdf_path)) {
{
// We can't add PDF as it is not generated yet. // We can't add PDF as it is not generated yet.
$filepdf = ''; $filepdf = '';
} else { } else {
@ -736,14 +763,14 @@ class Notify
$message = nl2br($message); $message = nl2br($message);
// Replace keyword __SUPERVISOREMAIL__ // Replace keyword __SUPERVISOREMAIL__
if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
{
$newval = ''; $newval = '';
if ($user->fk_user > 0) if ($user->fk_user > 0) {
{
$supervisoruser = new User($this->db); $supervisoruser = new User($this->db);
$supervisoruser->fetch($user->fk_user); $supervisoruser->fetch($user->fk_user);
if ($supervisoruser->email) $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>'); if ($supervisoruser->email) {
$newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
}
} }
dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval); dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
$sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto); $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
@ -752,14 +779,16 @@ class Notify
$sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
} }
if ($sendto) if ($sendto) {
{
$parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list); $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
$reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook)) if (empty($reshook)) {
{ if (!empty($hookmanager->resArray['subject'])) {
if (!empty($hookmanager->resArray['subject'])) $subject .= $hookmanager->resArray['subject']; $subject .= $hookmanager->resArray['subject'];
if (!empty($hookmanager->resArray['message'])) $message .= $hookmanager->resArray['message']; }
if (!empty($hookmanager->resArray['message'])) {
$message .= $hookmanager->resArray['message'];
}
} }
$mailfile = new CMailFile( $mailfile = new CMailFile(
$subject, $subject,
@ -780,12 +809,10 @@ class Notify
'notification' 'notification'
); );
if ($mailfile->sendfile()) if ($mailfile->sendfile()) {
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
$sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", null, 'email', 'tofixedemail', '".$object_type."', ".$object->id.", '".$this->db->escape($conf->global->$param)."')"; $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", null, 'email', 'tofixedemail', '".$object_type."', ".$object->id.", '".$this->db->escape($conf->global->$param)."')";
if (!$this->db->query($sql)) if (!$this->db->query($sql)) {
{
dol_print_error($this->db); dol_print_error($this->db);
} }
} else { } else {
@ -796,7 +823,10 @@ class Notify
} }
} }
if (!$error) return $num; if (!$error) {
else return -1 * $error; return $num;
} else {
return -1 * $error;
}
} }
} }