notify.class.php 29 KB

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