notify.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/class/notify.class.php
  22. * \ingroup notification
  23. * \brief File of class to manage notifications
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  26. /**
  27. * Class to manage notifications
  28. */
  29. class Notify
  30. {
  31. /**
  32. * @var int ID
  33. */
  34. public $id;
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. /**
  40. * @var string Error code (or message)
  41. */
  42. public $error = '';
  43. /**
  44. * @var string[] Error codes (or messages)
  45. */
  46. public $errors = array();
  47. public $author;
  48. public $ref;
  49. public $date;
  50. public $duree;
  51. public $note;
  52. /**
  53. * @var int Project ID
  54. */
  55. public $fk_project;
  56. // Les codes actions sont definis dans la table llx_notify_def
  57. // codes actions supported are
  58. // @todo defined also into interface_50_modNotificiation_Notificiation.class.php
  59. public $arrayofnotifsupported = array(
  60. 'BILL_VALIDATE',
  61. 'BILL_PAYED',
  62. 'ORDER_VALIDATE',
  63. 'PROPAL_VALIDATE',
  64. 'PROPAL_CLOSE_SIGNED',
  65. 'FICHINTER_VALIDATE',
  66. 'FICHINTER_ADD_CONTACT',
  67. 'ORDER_SUPPLIER_VALIDATE',
  68. 'ORDER_SUPPLIER_APPROVE',
  69. 'ORDER_SUPPLIER_REFUSE',
  70. 'SHIPPING_VALIDATE',
  71. 'EXPENSE_REPORT_VALIDATE',
  72. 'EXPENSE_REPORT_APPROVE',
  73. 'HOLIDAY_VALIDATE',
  74. 'HOLIDAY_APPROVE'
  75. );
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. public function __construct($db)
  82. {
  83. $this->db = $db;
  84. }
  85. /**
  86. * Return message that say how many notification (and to which email) will occurs on requested event.
  87. * This is to show confirmation messages before event is recorded.
  88. *
  89. * @param string $action Id of action in llx_c_action_trigger
  90. * @param int $socid Id of third party
  91. * @param Object $object Object the notification is about
  92. * @return string Message
  93. */
  94. public function confirmMessage($action, $socid, $object)
  95. {
  96. global $langs;
  97. $langs->load("mails");
  98. $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
  99. $nb = -1;
  100. if (is_array($listofnotiftodo)) $nb = count($listofnotiftodo);
  101. if ($nb < 0) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
  102. if ($nb == 0) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("NoNotificationsWillBeSent");
  103. if ($nb == 1) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ANotificationsWillBeSent");
  104. if ($nb >= 2) $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("SomeNotificationsWillBeSent", $nb);
  105. if (is_array($listofnotiftodo))
  106. {
  107. $i = 0;
  108. foreach ($listofnotiftodo as $key => $val)
  109. {
  110. if ($i) $texte .= ', ';
  111. else $texte .= ' (';
  112. if ($val['isemailvalid']) $texte .= $val['email'];
  113. else $texte .= $val['emaildesc'];
  114. $i++;
  115. }
  116. if ($i) $texte .= ')';
  117. }
  118. return $texte;
  119. }
  120. /**
  121. * Return number of notifications activated for action code (and third party)
  122. *
  123. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  124. * @param int $socid Id of third party or 0 for all thirdparties or -1 for no thirdparties
  125. * @param Object $object Object the notification is about (need it to check threshold value of some notifications)
  126. * @param int $userid Id of user or 0 for all users or -1 for no users
  127. * @param array $scope Scope where to search
  128. * @return array|int <0 if KO, array of notifications to send if OK
  129. */
  130. public function getNotificationsArray($notifcode, $socid = 0, $object = null, $userid = 0, $scope = array('thirdparty', 'user', 'global'))
  131. {
  132. global $conf, $user;
  133. $error = 0;
  134. $resarray = array();
  135. $valueforthreshold = 0;
  136. if (is_object($object)) $valueforthreshold = $object->total_ht;
  137. if (!$error)
  138. {
  139. if ($socid >= 0 && in_array('thirdparty', $scope))
  140. {
  141. $sql = "SELECT a.code, c.email, c.rowid";
  142. $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  143. $sql .= " ".MAIN_DB_PREFIX."socpeople as c,";
  144. $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  145. $sql .= " ".MAIN_DB_PREFIX."societe as s";
  146. $sql .= " WHERE n.fk_contact = c.rowid";
  147. $sql .= " AND a.rowid = n.fk_action";
  148. $sql .= " AND n.fk_soc = s.rowid";
  149. if ($notifcode)
  150. {
  151. if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
  152. else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
  153. }
  154. $sql .= " AND s.entity IN (".getEntity('societe').")";
  155. if ($socid > 0) $sql .= " AND s.rowid = ".$socid;
  156. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  157. $resql = $this->db->query($sql);
  158. if ($resql)
  159. {
  160. $num = $this->db->num_rows($resql);
  161. $i = 0;
  162. while ($i < $num)
  163. {
  164. $obj = $this->db->fetch_object($resql);
  165. if ($obj)
  166. {
  167. $newval2 = trim($obj->email);
  168. $isvalid = isValidEmail($newval2);
  169. 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);
  170. }
  171. $i++;
  172. }
  173. } else {
  174. $error++;
  175. $this->error = $this->db->lasterror();
  176. }
  177. }
  178. }
  179. if (!$error)
  180. {
  181. if ($userid >= 0 && in_array('user', $scope))
  182. {
  183. $sql = "SELECT a.code, c.email, c.rowid";
  184. $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  185. $sql .= " ".MAIN_DB_PREFIX."user as c,";
  186. $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a";
  187. $sql .= " WHERE n.fk_user = c.rowid";
  188. $sql .= " AND a.rowid = n.fk_action";
  189. if ($notifcode)
  190. {
  191. if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
  192. else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
  193. }
  194. $sql .= " AND c.entity IN (".getEntity('user').")";
  195. if ($userid > 0) $sql .= " AND c.rowid = ".$userid;
  196. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  197. $resql = $this->db->query($sql);
  198. if ($resql)
  199. {
  200. $num = $this->db->num_rows($resql);
  201. $i = 0;
  202. while ($i < $num)
  203. {
  204. $obj = $this->db->fetch_object($resql);
  205. if ($obj)
  206. {
  207. $newval2 = trim($obj->email);
  208. $isvalid = isValidEmail($newval2);
  209. 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);
  210. }
  211. $i++;
  212. }
  213. } else {
  214. $error++;
  215. $this->error = $this->db->lasterror();
  216. }
  217. }
  218. }
  219. if (!$error)
  220. {
  221. if (in_array('global', $scope))
  222. {
  223. // List of notifications enabled for fixed email
  224. foreach ($conf->global as $key => $val)
  225. {
  226. if ($notifcode)
  227. {
  228. if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  229. } else {
  230. if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  231. }
  232. $threshold = (float) $reg[1];
  233. if ($valueforthreshold < $threshold) continue;
  234. $tmpemail = explode(',', $val);
  235. foreach ($tmpemail as $key2 => $val2)
  236. {
  237. $newval2 = trim($val2);
  238. if ($newval2 == '__SUPERVISOREMAIL__')
  239. {
  240. if ($user->fk_user > 0)
  241. {
  242. $tmpuser = new User($this->db);
  243. $tmpuser->fetch($user->fk_user);
  244. if ($tmpuser->email) $newval2 = trim($tmpuser->email);
  245. else $newval2 = '';
  246. } else $newval2 = '';
  247. }
  248. if ($newval2)
  249. {
  250. $isvalid = isValidEmail($newval2, 0);
  251. if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. if ($error) return -1;
  258. //var_dump($resarray);
  259. return $resarray;
  260. }
  261. /**
  262. * Check if notification are active for couple action/company.
  263. * If yes, send mail and save trace into llx_notify.
  264. *
  265. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  266. * @param Object $object Object the notification deals on
  267. * @param array $filename_list List of files to attach (full path of filename on file system)
  268. * @param array $mimetype_list List of MIME type of attached files
  269. * @param array $mimefilename_list List of attached file name in message
  270. * @return int <0 if KO, or number of changes if OK
  271. */
  272. public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
  273. {
  274. global $user, $conf, $langs, $mysoc;
  275. global $hookmanager;
  276. global $dolibarr_main_url_root;
  277. global $action;
  278. if (!in_array($notifcode, $this->arrayofnotifsupported)) return 0;
  279. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  280. if (!is_object($hookmanager))
  281. {
  282. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  283. $hookmanager = new HookManager($this->db);
  284. }
  285. $hookmanager->initHooks(array('notification'));
  286. dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
  287. $langs->load("other");
  288. // Define $urlwithroot
  289. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  290. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  291. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  292. // Define some vars
  293. $application = 'Dolibarr';
  294. if (!empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE;
  295. $replyto = $conf->notification->email_from;
  296. $object_type = '';
  297. $link = '';
  298. $num = 0;
  299. $error = 0;
  300. $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
  301. $newref = (empty($object->newref) ? $object->ref : $object->newref);
  302. $sql = '';
  303. // Check notification per third party
  304. if ($object->socid > 0)
  305. {
  306. $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
  307. $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  308. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
  309. $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  310. $sql .= " ".MAIN_DB_PREFIX."notify_def as n,";
  311. $sql .= " ".MAIN_DB_PREFIX."societe as s";
  312. $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
  313. $sql .= " AND n.fk_soc = s.rowid";
  314. $sql .= " AND c.statut = 1";
  315. if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
  316. else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
  317. $sql .= " AND s.rowid = ".$object->socid;
  318. $sql .= "\nUNION\n";
  319. }
  320. // Check notification per user
  321. $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
  322. $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  323. $sql .= " FROM ".MAIN_DB_PREFIX."user as c,";
  324. $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  325. $sql .= " ".MAIN_DB_PREFIX."notify_def as n";
  326. $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
  327. $sql .= " AND c.statut = 1";
  328. if (is_numeric($notifcode)) $sql .= " AND n.fk_action = ".$notifcode; // Old usage
  329. else $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
  330. $result = $this->db->query($sql);
  331. if ($result)
  332. {
  333. $num = $this->db->num_rows($result);
  334. $projtitle = '';
  335. if (!empty($object->fk_project))
  336. {
  337. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  338. $proj = new Project($this->db);
  339. $proj->fetch($object->fk_project);
  340. $projtitle = '('.$proj->title.')';
  341. }
  342. if ($num > 0)
  343. {
  344. $i = 0;
  345. while ($i < $num && !$error) // For each notification couple defined (third party/actioncode)
  346. {
  347. $obj = $this->db->fetch_object($result);
  348. $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
  349. $notifcodedefid = $obj->adid;
  350. $trackid = '';
  351. if ($obj->type_target == 'tocontactid') $trackid = 'con'.$obj->id;
  352. if ($obj->type_target == 'touserid') $trackid = 'use'.$obj->id;
  353. if (dol_strlen($obj->email))
  354. {
  355. // Set output language
  356. $outputlangs = $langs;
  357. if ($obj->default_lang && $obj->default_lang != $langs->defaultlang)
  358. {
  359. $outputlangs = new Translate('', $conf);
  360. $outputlangs->setDefaultLang($obj->default_lang);
  361. $outputlangs->loadLangs(array("main", "other"));
  362. }
  363. $subject = '['.$mysoc->name.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
  364. switch ($notifcode) {
  365. case 'BILL_VALIDATE':
  366. $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '">' . $newref . '</a>';
  367. $dir_output = $conf->facture->dir_output;
  368. $object_type = 'facture';
  369. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
  370. break;
  371. case 'BILL_PAYED':
  372. $link ='<a href="' . $urlwithroot . '/compta/facture/card.php?facid='.$object->id . '">' . $newref . '</a>';
  373. $dir_output = $conf->facture->dir_output;
  374. $object_type = 'facture';
  375. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
  376. break;
  377. case 'ORDER_VALIDATE':
  378. $link = '<a href="' . $urlwithroot . '/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  379. $dir_output = $conf->commande->dir_output;
  380. $object_type = 'order';
  381. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
  382. break;
  383. case 'PROPAL_VALIDATE':
  384. $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
  385. $dir_output = $conf->propal->multidir_output[$object->entity];
  386. $object_type = 'propal';
  387. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
  388. break;
  389. case 'PROPAL_CLOSE_SIGNED':
  390. $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
  391. $dir_output = $conf->propal->multidir_output[$object->entity];
  392. $object_type = 'propal';
  393. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
  394. break;
  395. case 'FICHINTER_ADD_CONTACT':
  396. $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
  397. $dir_output = $conf->ficheinter->dir_output;
  398. $object_type = 'ficheinter';
  399. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
  400. break;
  401. case 'FICHINTER_VALIDATE':
  402. $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
  403. $dir_output = $conf->ficheinter->dir_output;
  404. $object_type = 'ficheinter';
  405. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
  406. break;
  407. case 'ORDER_SUPPLIER_VALIDATE':
  408. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  409. $dir_output = $conf->fournisseur->commande->dir_output;
  410. $object_type = 'order_supplier';
  411. $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
  412. $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderValidatedBy", $link, $user->getFullName($outputlangs));
  413. $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  414. break;
  415. case 'ORDER_SUPPLIER_APPROVE':
  416. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  417. $dir_output = $conf->fournisseur->commande->dir_output;
  418. $object_type = 'order_supplier';
  419. $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
  420. $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($outputlangs));
  421. $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  422. break;
  423. case 'ORDER_SUPPLIER_REFUSE':
  424. $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  425. $dir_output = $conf->fournisseur->commande->dir_output;
  426. $object_type = 'order_supplier';
  427. $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
  428. $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderRefusedBy", $link, $user->getFullName($outputlangs));
  429. $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  430. break;
  431. case 'SHIPPING_VALIDATE':
  432. $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'">'.$newref.'</a>';
  433. $dir_output = $conf->expedition->dir_output.'/sending/';
  434. $object_type = 'expedition';
  435. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
  436. break;
  437. case 'EXPENSE_REPORT_VALIDATE':
  438. $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
  439. $dir_output = $conf->expensereport->dir_output;
  440. $object_type = 'expensereport';
  441. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
  442. break;
  443. case 'EXPENSE_REPORT_APPROVE':
  444. $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
  445. $dir_output = $conf->expensereport->dir_output;
  446. $object_type = 'expensereport';
  447. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
  448. break;
  449. case 'HOLIDAY_VALIDATE':
  450. $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
  451. $dir_output = $conf->holiday->dir_output;
  452. $object_type = 'holiday';
  453. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
  454. break;
  455. case 'HOLIDAY_APPROVE':
  456. $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
  457. $dir_output = $conf->holiday->dir_output;
  458. $object_type = 'holiday';
  459. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
  460. break;
  461. }
  462. $ref = dol_sanitizeFileName($newref);
  463. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  464. if (!dol_is_file($pdf_path))
  465. {
  466. // We can't add PDF as it is not generated yet.
  467. $filepdf = '';
  468. } else {
  469. $filepdf = $pdf_path;
  470. }
  471. $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
  472. $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
  473. $message .= "\n";
  474. $message .= $mesg;
  475. $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
  476. if (!isset($action)) $action = '';
  477. $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  478. if (empty($reshook))
  479. {
  480. if (!empty($hookmanager->resArray['subject'])) $subject .= $hookmanager->resArray['subject'];
  481. if (!empty($hookmanager->resArray['message'])) $message .= $hookmanager->resArray['message'];
  482. }
  483. $mailfile = new CMailFile(
  484. $subject,
  485. $sendto,
  486. $replyto,
  487. $message,
  488. $filename_list,
  489. $mimetype_list,
  490. $mimefilename_list,
  491. '',
  492. '',
  493. 0,
  494. -1,
  495. '',
  496. '',
  497. $trackid,
  498. '',
  499. 'notification'
  500. );
  501. if ($mailfile->sendfile())
  502. {
  503. if ($obj->type_target == 'touserid') {
  504. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
  505. $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)."')";
  506. } else {
  507. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
  508. $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)."')";
  509. }
  510. if (!$this->db->query($sql))
  511. {
  512. dol_print_error($this->db);
  513. }
  514. } else {
  515. $error++;
  516. $this->errors[] = $mailfile->error;
  517. }
  518. } else {
  519. dol_syslog("No notification sent for ".$sendto." because email is empty");
  520. }
  521. $i++;
  522. }
  523. } else {
  524. dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".$object->socid);
  525. }
  526. } else {
  527. $error++;
  528. $this->errors[] = $this->db->lasterror();
  529. dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
  530. return -1;
  531. }
  532. // Check notification using fixed email
  533. if (!$error)
  534. {
  535. foreach ($conf->global as $key => $val)
  536. {
  537. $reg = array();
  538. if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  539. $threshold = (float) $reg[1];
  540. if (!empty($object->total_ht) && $object->total_ht <= $threshold)
  541. {
  542. dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
  543. continue;
  544. }
  545. $param = 'NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_'.$reg[1];
  546. $sendto = $conf->global->$param;
  547. $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
  548. if ($notifcodedefid <= 0) dol_print_error($this->db, 'Failed to get id from code');
  549. $trackid = '';
  550. $object_type = '';
  551. $link = '';
  552. $num++;
  553. $subject = '['.$mysoc->name.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
  554. switch ($notifcode) {
  555. case 'BILL_VALIDATE':
  556. $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
  557. $dir_output = $conf->facture->dir_output;
  558. $object_type = 'facture';
  559. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
  560. break;
  561. case 'BILL_PAYED':
  562. $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
  563. $dir_output = $conf->facture->dir_output;
  564. $object_type = 'facture';
  565. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
  566. break;
  567. case 'ORDER_VALIDATE':
  568. $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  569. $dir_output = $conf->commande->dir_output;
  570. $object_type = 'order';
  571. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
  572. break;
  573. case 'PROPAL_VALIDATE':
  574. $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
  575. $dir_output = $conf->propal->multidir_output[$object->entity];
  576. $object_type = 'propal';
  577. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
  578. break;
  579. case 'PROPAL_CLOSE_SIGNED':
  580. $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
  581. $dir_output = $conf->propal->multidir_output[$object->entity];
  582. $object_type = 'propal';
  583. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
  584. break;
  585. case 'FICHINTER_ADD_CONTACT':
  586. $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
  587. $dir_output = $conf->ficheinter->dir_output;
  588. $object_type = 'ficheinter';
  589. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
  590. break;
  591. case 'FICHINTER_VALIDATE':
  592. $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
  593. $dir_output = $conf->facture->dir_output;
  594. $object_type = 'ficheinter';
  595. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
  596. break;
  597. case 'ORDER_SUPPLIER_VALIDATE':
  598. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  599. $dir_output = $conf->fournisseur->commande->dir_output;
  600. $object_type = 'order_supplier';
  601. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  602. $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy", $link, $user->getFullName($langs));
  603. $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  604. break;
  605. case 'ORDER_SUPPLIER_APPROVE':
  606. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  607. $dir_output = $conf->fournisseur->commande->dir_output;
  608. $object_type = 'order_supplier';
  609. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  610. $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($langs));
  611. $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  612. break;
  613. case 'ORDER_SUPPLIER_APPROVE2':
  614. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  615. $dir_output = $conf->fournisseur->commande->dir_output;
  616. $object_type = 'order_supplier';
  617. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  618. $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($langs));
  619. $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  620. break;
  621. case 'ORDER_SUPPLIER_REFUSE':
  622. $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
  623. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  624. $object_type = 'order_supplier';
  625. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  626. $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy", $link, $user->getFullName($langs));
  627. $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  628. break;
  629. case 'SHIPPING_VALIDATE':
  630. $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'">'.$newref.'</a>';
  631. $dir_output = $conf->expedition->dir_output.'/sending/';
  632. $object_type = 'order_supplier';
  633. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
  634. break;
  635. case 'EXPENSE_REPORT_VALIDATE':
  636. $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
  637. $dir_output = $conf->expensereport->dir_output;
  638. $object_type = 'expensereport';
  639. $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
  640. break;
  641. case 'EXPENSE_REPORT_APPROVE':
  642. $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
  643. $dir_output = $conf->expensereport->dir_output;
  644. $object_type = 'expensereport';
  645. $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
  646. break;
  647. case 'HOLIDAY_VALIDATE':
  648. $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
  649. $dir_output = $conf->holiday->dir_output;
  650. $object_type = 'holiday';
  651. $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
  652. break;
  653. case 'HOLIDAY_APPROVE':
  654. $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
  655. $dir_output = $conf->holiday->dir_output;
  656. $object_type = 'holiday';
  657. $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
  658. break;
  659. }
  660. $ref = dol_sanitizeFileName($newref);
  661. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  662. if (!dol_is_file($pdf_path))
  663. {
  664. // We can't add PDF as it is not generated yet.
  665. $filepdf = '';
  666. } else {
  667. $filepdf = $pdf_path;
  668. }
  669. $message .= $langs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
  670. $message .= "\n";
  671. $message .= $mesg;
  672. $message = nl2br($message);
  673. // Replace keyword __SUPERVISOREMAIL__
  674. if (preg_match('/__SUPERVISOREMAIL__/', $sendto))
  675. {
  676. $newval = '';
  677. if ($user->fk_user > 0)
  678. {
  679. $supervisoruser = new User($this->db);
  680. $supervisoruser->fetch($user->fk_user);
  681. if ($supervisoruser->email) $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
  682. }
  683. dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
  684. $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
  685. $sendto = preg_replace('/,\s*,/', ',', $sendto); // in some case you can have $sendto like "email, __SUPERVISOREMAIL__ , otheremail" then you have "email, , othermail" and it's not valid
  686. $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
  687. $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
  688. }
  689. if ($sendto)
  690. {
  691. $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
  692. $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  693. if (empty($reshook))
  694. {
  695. if (!empty($hookmanager->resArray['subject'])) $subject .= $hookmanager->resArray['subject'];
  696. if (!empty($hookmanager->resArray['message'])) $message .= $hookmanager->resArray['message'];
  697. }
  698. $mailfile = new CMailFile(
  699. $subject,
  700. $sendto,
  701. $replyto,
  702. $message,
  703. $filename_list,
  704. $mimetype_list,
  705. $mimefilename_list,
  706. '',
  707. '',
  708. 0,
  709. 1,
  710. '',
  711. $trackid,
  712. '',
  713. '',
  714. 'notification'
  715. );
  716. if ($mailfile->sendfile())
  717. {
  718. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
  719. $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)."')";
  720. if (!$this->db->query($sql))
  721. {
  722. dol_print_error($this->db);
  723. }
  724. } else {
  725. $error++;
  726. $this->errors[] = $mailfile->error;
  727. }
  728. }
  729. }
  730. }
  731. if (!$error) return $num;
  732. else return -1 * $error;
  733. }
  734. }