notify.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 <http://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. 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. 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. 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 = '".$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. }
  174. else
  175. {
  176. $error++;
  177. $this->error=$this->db->lasterror();
  178. }
  179. }
  180. }
  181. if (! $error)
  182. {
  183. if ($userid >= 0 && in_array('user', $scope))
  184. {
  185. $sql = "SELECT a.code, c.email, c.rowid";
  186. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  187. $sql.= " ".MAIN_DB_PREFIX."user as c,";
  188. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a";
  189. $sql.= " WHERE n.fk_user = c.rowid";
  190. $sql.= " AND a.rowid = n.fk_action";
  191. if ($notifcode)
  192. {
  193. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  194. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  195. }
  196. $sql.= " AND c.entity IN (".getEntity('user').")";
  197. if ($userid > 0) $sql.= " AND c.rowid = ".$userid;
  198. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  199. $resql = $this->db->query($sql);
  200. if ($resql)
  201. {
  202. $num = $this->db->num_rows($resql);
  203. $i=0;
  204. while ($i < $num)
  205. {
  206. $obj = $this->db->fetch_object($resql);
  207. if ($obj)
  208. {
  209. $newval2=trim($obj->email);
  210. $isvalid=isValidEmail($newval2);
  211. 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);
  212. }
  213. $i++;
  214. }
  215. }
  216. else
  217. {
  218. $error++;
  219. $this->error=$this->db->lasterror();
  220. }
  221. }
  222. }
  223. if (! $error)
  224. {
  225. if (in_array('global', $scope))
  226. {
  227. // List of notifications enabled for fixed email
  228. foreach($conf->global as $key => $val)
  229. {
  230. if ($notifcode)
  231. {
  232. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  233. }
  234. else
  235. {
  236. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  237. }
  238. $threshold = (float) $reg[1];
  239. if ($valueforthreshold < $threshold) continue;
  240. $tmpemail=explode(',',$val);
  241. foreach($tmpemail as $key2 => $val2)
  242. {
  243. $newval2=trim($val2);
  244. if ($newval2 == '__SUPERVISOREMAIL__')
  245. {
  246. if ($user->fk_user > 0)
  247. {
  248. $tmpuser=new User($this->db);
  249. $tmpuser->fetch($user->fk_user);
  250. if ($tmpuser->email) $newval2=trim($tmpuser->email);
  251. else $newval2='';
  252. }
  253. else $newval2='';
  254. }
  255. if ($newval2)
  256. {
  257. $isvalid=isValidEmail($newval2, 0);
  258. if (empty($resarray[$newval2])) $resarray[$newval2]=array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
  259. }
  260. }
  261. }
  262. }
  263. }
  264. if ($error) return -1;
  265. //var_dump($resarray);
  266. return $resarray;
  267. }
  268. /**
  269. * Check if notification are active for couple action/company.
  270. * If yes, send mail and save trace into llx_notify.
  271. *
  272. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  273. * @param Object $object Object the notification deals on
  274. * @param array $filename_list List of files to attach (full path of filename on file system)
  275. * @param array $mimetype_list List of MIME type of attached files
  276. * @param array $mimefilename_list List of attached file name in message
  277. * @return int <0 if KO, or number of changes if OK
  278. */
  279. function send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
  280. {
  281. global $user,$conf,$langs,$mysoc;
  282. global $hookmanager;
  283. global $dolibarr_main_url_root;
  284. if (! in_array($notifcode, $this->arrayofnotifsupported)) return 0;
  285. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  286. if (! is_object($hookmanager))
  287. {
  288. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  289. $hookmanager=new HookManager($this->db);
  290. }
  291. $hookmanager->initHooks(array('notification'));
  292. dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
  293. $langs->load("other");
  294. // Define $urlwithroot
  295. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  296. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  297. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  298. // Define some vars
  299. $application = 'Dolibarr';
  300. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE;
  301. $replyto = $conf->notification->email_from;
  302. $object_type = '';
  303. $link = '';
  304. $num = 0;
  305. $oldref=(empty($object->oldref)?$object->ref:$object->oldref);
  306. $newref=(empty($object->newref)?$object->ref:$object->newref);
  307. $sql = '';
  308. // Check notification per third party
  309. if ($object->socid > 0)
  310. {
  311. $sql.= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
  312. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  313. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
  314. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  315. $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
  316. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  317. $sql.= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
  318. $sql.= " AND n.fk_soc = s.rowid";
  319. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  320. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  321. $sql .= " AND s.rowid = ".$object->socid;
  322. $sql.= "\nUNION\n";
  323. }
  324. // Check notification per user
  325. $sql.= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
  326. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  327. $sql.= " FROM ".MAIN_DB_PREFIX."user as c,";
  328. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  329. $sql.= " ".MAIN_DB_PREFIX."notify_def as n";
  330. $sql.= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
  331. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  332. else $sql.= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
  333. $result = $this->db->query($sql);
  334. if ($result)
  335. {
  336. $num = $this->db->num_rows($result);
  337. $projtitle='';
  338. if (! empty($object->fk_project))
  339. {
  340. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  341. $proj = new Project($this->db);
  342. $proj->fetch($object->fk_project);
  343. $projtitle='('.$proj->title.')';
  344. }
  345. if ($num > 0)
  346. {
  347. $i = 0;
  348. while ($i < $num && ! $error) // For each notification couple defined (third party/actioncode)
  349. {
  350. $obj = $this->db->fetch_object($result);
  351. $sendto = dolGetFirstLastname($obj->firstname,$obj->lastname) . " <".$obj->email.">";
  352. $notifcodedefid = $obj->adid;
  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='/compta/facture/card.php?facid='.$object->id;
  367. $dir_output = $conf->facture->dir_output;
  368. $object_type = 'facture';
  369. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
  370. break;
  371. case 'BILL_PAYED':
  372. $link='/compta/facture/card.php?facid='.$object->id;
  373. $dir_output = $conf->facture->dir_output;
  374. $object_type = 'facture';
  375. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed",$newref);
  376. break;
  377. case 'ORDER_VALIDATE':
  378. $link='/commande/card.php?id='.$object->id;
  379. $dir_output = $conf->commande->dir_output;
  380. $object_type = 'order';
  381. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
  382. break;
  383. case 'PROPAL_VALIDATE':
  384. $link='/comm/propal/card.php?id='.$object->id;
  385. $dir_output = $conf->propal->multidir_output[$object->entity];
  386. $object_type = 'propal';
  387. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
  388. break;
  389. case 'PROPAL_CLOSE_SIGNED':
  390. $link='/comm/propal/card.php?id='.$object->id;
  391. $dir_output = $conf->propal->multidir_output[$object->entity];
  392. $object_type = 'propal';
  393. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned",$newref);
  394. break;
  395. case 'FICHINTER_ADD_CONTACT':
  396. $link='/fichinter/card.php?id='.$object->id;
  397. $dir_output = $conf->ficheinter->dir_output;
  398. $object_type = 'ficheinter';
  399. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$newref);
  400. break;
  401. case 'FICHINTER_VALIDATE':
  402. $link='/fichinter/card.php?id='.$object->id;
  403. $dir_output = $conf->ficheinter->dir_output;
  404. $object_type = 'ficheinter';
  405. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated",$newref);
  406. break;
  407. case 'ORDER_SUPPLIER_VALIDATE':
  408. $link='/fourn/commande/card.php?id='.$object->id;
  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",$newref,$user->getFullName($langs));
  413. $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  414. break;
  415. case 'ORDER_SUPPLIER_APPROVE':
  416. $link='/fourn/commande/card.php?id='.$object->id;
  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",$newref,$user->getFullName($langs));
  421. $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  422. break;
  423. case 'ORDER_SUPPLIER_REFUSE':
  424. $link='/fourn/commande/card.php?id='.$object->id;
  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",$newref,$user->getFullName($langs));
  429. $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
  430. break;
  431. case 'SHIPPING_VALIDATE':
  432. $dir_output = $conf->expedition->dir_output.'/sending/';
  433. $object_type = 'order_supplier';
  434. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  435. break;
  436. case 'EXPENSE_REPORT_VALIDATE':
  437. $dir_output = $conf->expensereport->dir_output;
  438. $object_type = 'expensereport';
  439. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated",$newref);
  440. break;
  441. case 'EXPENSE_REPORT_APPROVE':
  442. $dir_output = $conf->expensereport->dir_output;
  443. $object_type = 'expensereport';
  444. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved",$newref);
  445. break;
  446. case 'HOLIDAY_VALIDATE':
  447. $dir_output = $conf->holiday->dir_output;
  448. $object_type = 'holiday';
  449. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated",$newref);
  450. break;
  451. case 'HOLIDAY_APPROVE':
  452. $dir_output = $conf->holiday->dir_output;
  453. $object_type = 'holiday';
  454. $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved",$newref);
  455. break;
  456. }
  457. $ref = dol_sanitizeFileName($newref);
  458. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  459. if (! dol_is_file($pdf_path))
  460. {
  461. // We can't add PDF as it is not generated yet.
  462. $filepdf = '';
  463. }
  464. else
  465. {
  466. $filepdf = $pdf_path;
  467. }
  468. $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  469. $message.= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  470. $message.= "\n";
  471. $message.= $mesg;
  472. if ($link) $message.= "\n" . $urlwithroot . $link;
  473. $parameters=array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
  474. $reshook=$hookmanager->executeHooks('formatNotificationMessage',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  475. if (empty($reshook))
  476. {
  477. if (! empty($hookmanager->resArray['subject'])) $subject.=$hookmanager->resArray['subject'];
  478. if (! empty($hookmanager->resArray['message'])) $message.=$hookmanager->resArray['message'];
  479. }
  480. $mailfile = new CMailFile(
  481. $subject,
  482. $sendto,
  483. $replyto,
  484. $message,
  485. $filename_list,
  486. $mimetype_list,
  487. $mimefilename_list,
  488. '',
  489. '',
  490. 0,
  491. -1
  492. );
  493. if ($mailfile->sendfile())
  494. {
  495. if ($obj->type_target == 'touserid') {
  496. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
  497. $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)."')";
  498. }
  499. else {
  500. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
  501. $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)."')";
  502. }
  503. if (! $this->db->query($sql))
  504. {
  505. dol_print_error($this->db);
  506. }
  507. }
  508. else
  509. {
  510. $error++;
  511. $this->errors[]=$mailfile->error;
  512. }
  513. }
  514. else
  515. {
  516. dol_syslog("No notification sent for ".$sendto." because email is empty");
  517. }
  518. $i++;
  519. }
  520. }
  521. else
  522. {
  523. dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".$object->socid);
  524. }
  525. }
  526. else
  527. {
  528. $error++;
  529. $this->errors[]=$this->db->lasterror();
  530. dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
  531. return -1;
  532. }
  533. // Check notification using fixed email
  534. if (! $error)
  535. {
  536. foreach($conf->global as $key => $val)
  537. {
  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. $object_type = '';
  550. $link = '';
  551. $num++;
  552. $subject = '['.$mysoc->name.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle?' '.$projtitle:'');
  553. switch ($notifcode) {
  554. case 'BILL_VALIDATE':
  555. $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '">' . $newref . '</a>';
  556. $dir_output = $conf->facture->dir_output;
  557. $object_type = 'facture';
  558. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$link);
  559. break;
  560. case 'BILL_PAYED':
  561. $link ='<a href="' . $urlwithroot . '/compta/facture/card.php?facid='.$object->id . '">' . $newref . '</a>';
  562. $dir_output = $conf->facture->dir_output;
  563. $object_type = 'facture';
  564. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed",$link);
  565. break;
  566. case 'ORDER_VALIDATE':
  567. $link = '<a href="' . $urlwithroot . '/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  568. $dir_output = $conf->commande->dir_output;
  569. $object_type = 'order';
  570. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$link);
  571. break;
  572. case 'PROPAL_VALIDATE':
  573. $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
  574. $dir_output = $conf->propal->multidir_output[$object->entity];
  575. $object_type = 'propal';
  576. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$link);
  577. break;
  578. case 'PROPAL_CLOSE_SIGNED':
  579. $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
  580. $dir_output = $conf->propal->multidir_output[$object->entity];
  581. $object_type = 'propal';
  582. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned",$link);
  583. break;
  584. case 'FICHINTER_ADD_CONTACT':
  585. $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id='.$object->id . '">' . $newref . '</a>';
  586. $dir_output = $conf->facture->dir_output;
  587. $object_type = 'ficheinter';
  588. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$link);
  589. break;
  590. case 'FICHINTER_VALIDATE':
  591. $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id='.$object->id . '">' . $newref . '</a>';
  592. $dir_output = $conf->facture->dir_output;
  593. $object_type = 'ficheinter';
  594. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$link);
  595. break;
  596. case 'ORDER_SUPPLIER_VALIDATE':
  597. $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  598. $dir_output = $conf->fournisseur->commande->dir_output;
  599. $object_type = 'order_supplier';
  600. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  601. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$link,$user->getFullName($langs));
  602. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  603. break;
  604. case 'ORDER_SUPPLIER_APPROVE':
  605. $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  606. $dir_output = $conf->fournisseur->commande->dir_output;
  607. $object_type = 'order_supplier';
  608. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  609. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$link,$user->getFullName($langs));
  610. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  611. break;
  612. case 'ORDER_SUPPLIER_APPROVE2':
  613. $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  614. $dir_output = $conf->fournisseur->commande->dir_output;
  615. $object_type = 'order_supplier';
  616. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  617. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$link,$user->getFullName($langs));
  618. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  619. break;
  620. case 'ORDER_SUPPLIER_REFUSE':
  621. $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
  622. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  623. $object_type = 'order_supplier';
  624. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  625. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$link,$user->getFullName($langs));
  626. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  627. break;
  628. case 'SHIPPING_VALIDATE':
  629. $dir_output = $conf->expedition->dir_output.'/sending/';
  630. $object_type = 'order_supplier';
  631. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  632. break;
  633. case 'EXPENSE_REPORT_VALIDATE':
  634. $dir_output = $conf->expensereport->dir_output;
  635. $object_type = 'expensereport';
  636. $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated",$newref);
  637. break;
  638. case 'EXPENSE_REPORT_APPROVE':
  639. $dir_output = $conf->expensereport->dir_output;
  640. $object_type = 'expensereport';
  641. $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved",$newref);
  642. break;
  643. case 'HOLIDAY_VALIDATE':
  644. $dir_output = $conf->holiday->dir_output;
  645. $object_type = 'holiday';
  646. $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated",$newref);
  647. break;
  648. case 'HOLIDAY_APPROVE':
  649. $dir_output = $conf->holiday->dir_output;
  650. $object_type = 'holiday';
  651. $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved",$newref);
  652. break;
  653. }
  654. $ref = dol_sanitizeFileName($newref);
  655. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  656. if (! dol_is_file($pdf_path))
  657. {
  658. // We can't add PDF as it is not generated yet.
  659. $filepdf = '';
  660. }
  661. else
  662. {
  663. $filepdf = $pdf_path;
  664. }
  665. $message = $langs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  666. $message.= $langs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  667. $message.= "\n";
  668. $message.= $mesg;
  669. //if ($link) $message.= "\n" . $urlwithroot . $link; // link already added around the ref into the text
  670. $message = nl2br($message);
  671. // Replace keyword __SUPERVISOREMAIL__
  672. if (preg_match('/__SUPERVISOREMAIL__/', $sendto))
  673. {
  674. $newval='';
  675. if ($user->fk_user > 0)
  676. {
  677. $supervisoruser=new User($this->db);
  678. $supervisoruser->fetch($user->fk_user);
  679. if ($supervisoruser->email) $newval=trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
  680. }
  681. dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
  682. $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
  683. $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
  684. $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
  685. $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
  686. }
  687. if ($sendto)
  688. {
  689. $parameters=array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
  690. $reshook=$hookmanager->executeHooks('formatNotificationMessage',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  691. if (empty($reshook))
  692. {
  693. if (! empty($hookmanager->resArray['subject'])) $subject.=$hookmanager->resArray['subject'];
  694. if (! empty($hookmanager->resArray['message'])) $message.=$hookmanager->resArray['message'];
  695. }
  696. $mailfile = new CMailFile(
  697. $subject,
  698. $sendto,
  699. $replyto,
  700. $message,
  701. $filename_list,
  702. $mimetype_list,
  703. $mimefilename_list,
  704. '',
  705. '',
  706. 0,
  707. 1
  708. );
  709. if ($mailfile->sendfile())
  710. {
  711. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
  712. $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)."')";
  713. if (! $this->db->query($sql))
  714. {
  715. dol_print_error($this->db);
  716. }
  717. }
  718. else
  719. {
  720. $error++;
  721. $this->errors[]=$mailfile->error;
  722. }
  723. }
  724. }
  725. }
  726. if (! $error) return $num;
  727. else return -1 * $error;
  728. }
  729. }