notify.class.php 41 KB

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