notify.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. function __construct($db)
  47. {
  48. $this->db = $db;
  49. }
  50. /**
  51. * Return message that say how many notification (and to which email) will occurs on requested event.
  52. * This is to show confirmation messages before event is recorded.
  53. *
  54. * @param string $action Id of action in llx_c_action_trigger
  55. * @param int $socid Id of third party
  56. * @param Object $object Object the notification is about
  57. * @return string Message
  58. */
  59. function confirmMessage($action,$socid,$object)
  60. {
  61. global $langs;
  62. $langs->load("mails");
  63. $listofnotiftodo=$this->getNotificationsArray($action,$socid,$object);
  64. $nb=count($listofnotiftodo);
  65. if ($nb <= 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("NoNotificationsWillBeSent");
  66. if ($nb == 1) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ANotificationsWillBeSent");
  67. if ($nb >= 2) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("SomeNotificationsWillBeSent",$nb);
  68. $i=0;
  69. foreach ($listofnotiftodo as $key => $val)
  70. {
  71. if ($i) $texte.=', ';
  72. else $texte.=' (';
  73. if ($val['isemailvalid']) $texte.=$val['email'];
  74. else $texte.=$val['emaildesc'];
  75. $i++;
  76. }
  77. if ($i) $texte.=')';
  78. return $texte;
  79. }
  80. /**
  81. * Return number of notifications activated for action code (and third party)
  82. *
  83. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  84. * @param int $socid Id of third party or 0 for all thirdparties
  85. * @param Object $object Object the notification is about (need it to check threshold value of some notifications)
  86. * @return array|int <0 if KO, array of notifications to send if OK
  87. */
  88. function getNotificationsArray($notifcode,$socid,$object=null)
  89. {
  90. global $conf, $user;
  91. $error=0;
  92. $resarray=array();
  93. $valueforthreshold = 0;
  94. if (is_object($object)) $valueforthreshold = $object->total_ht;
  95. if (! $error)
  96. {
  97. $sql = "SELECT a.code, c.email, c.rowid";
  98. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
  99. $sql.= " ".MAIN_DB_PREFIX."socpeople as c,";
  100. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  101. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  102. $sql.= " WHERE n.fk_contact = c.rowid";
  103. $sql.= " AND a.rowid = n.fk_action";
  104. $sql.= " AND n.fk_soc = s.rowid";
  105. if ($notifcode)
  106. {
  107. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  108. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  109. }
  110. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  111. if ($socid > 0) $sql.= " AND s.rowid = ".$socid;
  112. dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
  113. $resql = $this->db->query($sql);
  114. if ($resql)
  115. {
  116. $num = $this->db->num_rows($resql);
  117. $i=0;
  118. while ($i < $num)
  119. {
  120. $obj = $this->db->fetch_object($resql);
  121. if ($obj)
  122. {
  123. $isvalid=isValidEmail($newval2);
  124. $resarray[] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>trim($obj->email), 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
  125. }
  126. $i++;
  127. }
  128. }
  129. else
  130. {
  131. $error++;
  132. $this->error=$this->db->lasterror();
  133. }
  134. }
  135. if (! $error)
  136. {
  137. // List of notifications enabled for fixed email
  138. foreach($conf->global as $key => $val)
  139. {
  140. if ($notifcode)
  141. {
  142. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  143. }
  144. else
  145. {
  146. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  147. }
  148. $threshold = (float) $reg[1];
  149. if ($valueforthreshold < $threshold) continue;
  150. $tmpemail=explode(',',$val);
  151. foreach($tmpemail as $key2 => $val2)
  152. {
  153. $newval2=trim($val2);
  154. if ($newval2 == '__SUPERVISOREMAIL__')
  155. {
  156. if ($user->fk_user > 0)
  157. {
  158. $tmpuser=new User($this->db);
  159. $tmpuser->fetch($user->fk_user);
  160. if ($tmpuser->email) $newval2=$tmpuser->email;
  161. else $newval2='';
  162. }
  163. else $newval2='';
  164. }
  165. if ($newval2)
  166. {
  167. $isvalid=isValidEmail($newval2, 0);
  168. $resarray[]=array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>trim($newval2), 'isemailvalid'=>$isvalid);
  169. }
  170. }
  171. }
  172. }
  173. if ($error) return -1;
  174. //var_dump($resarray);
  175. return $resarray;
  176. }
  177. /**
  178. * Check if notification are active for couple action/company.
  179. * If yes, send mail and save trace into llx_notify.
  180. *
  181. * @param string $notifcode Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
  182. * @param Object $object Object the notification deals on
  183. * @return int <0 if KO, or number of changes if OK
  184. */
  185. function send($notifcode, $object)
  186. {
  187. global $user,$conf,$langs,$mysoc,$dolibarr_main_url_root;
  188. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  189. dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
  190. $langs->load("other");
  191. // Define $urlwithroot
  192. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  193. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  194. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  195. // Define some vars
  196. $application = $mysoc->name;
  197. //if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE;
  198. $replyto = $conf->notification->email_from;
  199. $filename = basename($file);
  200. $mimefile = dol_mimetype($file);
  201. $object_type = '';
  202. $link = '';
  203. $num = 0;
  204. if (! in_array(
  205. $notifcode,
  206. array(
  207. 'BILL_VALIDATE',
  208. 'ORDER_VALIDATE',
  209. 'PROPAL_VALIDATE',
  210. 'FICHINTER_VALIDATE',
  211. 'ORDER_SUPPLIER_VALIDATE',
  212. 'ORDER_SUPPLIER_APPROVE',
  213. 'ORDER_SUPPLIER_REFUSE',
  214. 'SHIPPING_VALIDATE'
  215. )
  216. )
  217. )
  218. {
  219. return 0;
  220. }
  221. $oldref=(empty($object->oldref)?$object->ref:$object->oldref);
  222. $newref=(empty($object->newref)?$object->ref:$object->newref);
  223. // Check notification per third party
  224. $sql = "SELECT s.nom, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
  225. $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
  226. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
  227. $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
  228. $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
  229. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  230. $sql.= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
  231. $sql.= " AND n.fk_soc = s.rowid";
  232. if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
  233. else $sql.= " AND a.code = '".$notifcode."'"; // New usage
  234. $sql .= " AND s.rowid = ".$object->socid;
  235. $result = $this->db->query($sql);
  236. if ($result)
  237. {
  238. $num = $this->db->num_rows($result);
  239. if ($num > 0)
  240. {
  241. $i = 0;
  242. while ($i < $num && ! $error) // For each notification couple defined (third party/actioncode)
  243. {
  244. $obj = $this->db->fetch_object($result);
  245. $sendto = dolGetFirstLastname($obj->firstname,$obj->lastname) . " <".$obj->email.">";
  246. $notifcodedefid = $obj->adid;
  247. if (dol_strlen($obj->email))
  248. {
  249. // Set output language
  250. $outputlangs = $langs;
  251. if ($obj->default_lang && $obj->default_lang != $langs->defaultlang)
  252. {
  253. $outputlangs = new Translate('', $conf);
  254. $outputlangs->setDefaultLang($obj->default_lang);
  255. }
  256. switch ($notifcode) {
  257. case 'BILL_VALIDATE':
  258. $link='/compta/facture.php?facid='.$object->id;
  259. $dir_output = $conf->facture->dir_output;
  260. $object_type = 'facture';
  261. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
  262. break;
  263. case 'ORDER_VALIDATE':
  264. $link='/commande/card.php?id='.$object->id;
  265. $dir_output = $conf->commande->dir_output;
  266. $object_type = 'order';
  267. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
  268. break;
  269. case 'PROPAL_VALIDATE':
  270. $link='/comm/propal.php?id='.$object->id;
  271. $dir_output = $conf->propal->dir_output;
  272. $object_type = 'propal';
  273. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
  274. break;
  275. case 'FICHINTER_VALIDATE':
  276. $link='/fichinter/card.php?id='.$object->id;
  277. $dir_output = $conf->facture->dir_output;
  278. $object_type = 'ficheinter';
  279. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$object->ref);
  280. break;
  281. case 'ORDER_SUPPLIER_VALIDATE':
  282. $link='/fourn/commande/card.php?id='.$object->id;
  283. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  284. $object_type = 'order_supplier';
  285. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  286. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$object->ref,$user->getFullName($langs));
  287. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  288. break;
  289. case 'ORDER_SUPPLIER_APPROVE':
  290. $link='/fourn/commande/card.php?id='.$object->id;
  291. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  292. $object_type = 'order_supplier';
  293. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  294. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  295. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  296. break;
  297. case 'ORDER_SUPPLIER_REFUSE':
  298. $link='/fourn/commande/card.php?id='.$object->id;
  299. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  300. $object_type = 'order_supplier';
  301. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  302. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$newref,$user->getFullName($langs));
  303. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  304. break;
  305. case 'SHIPPING_VALIDATE':
  306. $dir_output = $conf->expedition->dir_output.'/sending/';
  307. $object_type = 'order_supplier';
  308. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  309. break;
  310. }
  311. $ref = dol_sanitizeFileName($newref);
  312. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  313. if (! dol_is_file($pdf_path))
  314. {
  315. // We can't add PDF as it is not generated yet.
  316. $filepdf = '';
  317. }
  318. else
  319. {
  320. $filepdf = $pdf_path;
  321. }
  322. $subject = '['.$application.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification");
  323. $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  324. $message.= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  325. $message.= "\n";
  326. $message.= $mesg;
  327. if ($link) $message=dol_concatdesc($message,$urlwithroot.$link);
  328. $mailfile = new CMailFile(
  329. $subject,
  330. $sendto,
  331. $replyto,
  332. $message,
  333. array($file),
  334. array($mimefile),
  335. array($filename[count($filename)-1]),
  336. '',
  337. '',
  338. 0,
  339. -1
  340. );
  341. if ($mailfile->sendfile())
  342. {
  343. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, objet_id, email)";
  344. $sql.= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".$object->socid.", ".$obj->cid.", '".$obj->type."', '".$object_type."', ".$object->id.", '".$this->db->escape($obj->email)."')";
  345. if (! $this->db->query($sql))
  346. {
  347. dol_print_error($this->db);
  348. }
  349. }
  350. else
  351. {
  352. $error++;
  353. $this->errors[]=$mailfile->error;
  354. }
  355. }
  356. else
  357. {
  358. dol_syslog("No notification sent for ".$sendto." because email is empty");
  359. }
  360. $i++;
  361. }
  362. }
  363. else
  364. {
  365. dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".$object->socid);
  366. }
  367. }
  368. else
  369. {
  370. $error++;
  371. $this->errors[]=$this->db->lasterror();
  372. return -1;
  373. }
  374. // Check notification using fixed email
  375. if (! $error)
  376. {
  377. foreach($conf->global as $key => $val)
  378. {
  379. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
  380. $threshold = (float) $reg[1];
  381. if ($object->total_ht <= $threshold)
  382. {
  383. dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
  384. continue;
  385. }
  386. $param='NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_'.$reg[1];
  387. $sendto = $conf->global->$param;
  388. $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
  389. if ($notifcodedefid <= 0) dol_print_error($this->db, 'Failed to get id from code');
  390. $object_type = '';
  391. $link = '';
  392. $num++;
  393. switch ($notifcode) {
  394. case 'BILL_VALIDATE':
  395. $link='/compta/facture.php?facid='.$object->id;
  396. $dir_output = $conf->facture->dir_output;
  397. $object_type = 'facture';
  398. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
  399. break;
  400. case 'ORDER_VALIDATE':
  401. $link='/commande/card.php?id='.$object->id;
  402. $dir_output = $conf->commande->dir_output;
  403. $object_type = 'order';
  404. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
  405. break;
  406. case 'PROPAL_VALIDATE':
  407. $link='/comm/propal.php?id='.$object->id;
  408. $dir_output = $conf->propal->dir_output;
  409. $object_type = 'propal';
  410. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
  411. break;
  412. case 'FICHINTER_VALIDATE':
  413. $link='/fichinter/card.php?id='.$object->id;
  414. $dir_output = $conf->facture->dir_output;
  415. $object_type = 'ficheinter';
  416. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$newref);
  417. break;
  418. case 'ORDER_SUPPLIER_VALIDATE':
  419. $link='/fourn/commande/card.php?id='.$object->id;
  420. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  421. $object_type = 'order_supplier';
  422. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  423. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$newref,$user->getFullName($langs));
  424. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  425. break;
  426. case 'ORDER_SUPPLIER_APPROVE':
  427. $link='/fourn/commande/card.php?id='.$object->id;
  428. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  429. $object_type = 'order_supplier';
  430. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  431. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  432. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  433. break;
  434. case 'ORDER_SUPPLIER_APPROVE2':
  435. $link='/fourn/commande/card.php?id='.$object->id;
  436. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  437. $object_type = 'order_supplier';
  438. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  439. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
  440. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  441. break;
  442. case 'ORDER_SUPPLIER_REFUSE':
  443. $link='/fourn/commande/card.php?id='.$object->id;
  444. $dir_output = $conf->fournisseur->dir_output.'/commande/';
  445. $object_type = 'order_supplier';
  446. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  447. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$newref,$user->getFullName($langs));
  448. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  449. break;
  450. case 'SHIPPING_VALIDATE':
  451. $dir_output = $conf->expedition->dir_output.'/sending/';
  452. $object_type = 'order_supplier';
  453. $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
  454. break;
  455. }
  456. $ref = dol_sanitizeFileName($newref);
  457. $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
  458. if (! dol_is_file($pdf_path))
  459. {
  460. // We can't add PDF as it is not generated yet.
  461. $filepdf = '';
  462. }
  463. else
  464. {
  465. $filepdf = $pdf_path;
  466. }
  467. $subject = '['.$application.'] '.$langs->transnoentitiesnoconv("DolibarrNotification");
  468. $message = $langs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
  469. $message.= $langs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
  470. $message.= "\n";
  471. $message.= $mesg;
  472. if ($link) $message=dol_concatdesc($message,$urlwithroot.$link);
  473. // Replace keyword __SUPERVISOREMAIL__
  474. if (preg_match('/__SUPERVISOREMAIL__/', $sendto))
  475. {
  476. $newval='';
  477. if ($user->fk_user > 0)
  478. {
  479. $supervisoruser=new User($this->db);
  480. $supervisoruser->fetch($user->fk_user);
  481. if ($supervisoruser->email) $newval=trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
  482. }
  483. dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
  484. $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
  485. $sendto = preg_replace('/^[\s,]+/','',$sendto); // Clean start of string
  486. $sendto = preg_replace('/[\s,]+$/','',$sendto); // Clean end of string
  487. }
  488. if ($sendto)
  489. {
  490. $mailfile = new CMailFile(
  491. $subject,
  492. $sendto,
  493. $replyto,
  494. $message,
  495. array($file),
  496. array($mimefile),
  497. array($filename[count($filename)-1]),
  498. '',
  499. '',
  500. 0,
  501. -1
  502. );
  503. if ($mailfile->sendfile())
  504. {
  505. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, objet_id, email)";
  506. $sql.= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".$object->socid.", null, 'email', '".$object_type."', ".$object->id.", '".$this->db->escape($conf->global->$param)."')";
  507. if (! $this->db->query($sql))
  508. {
  509. dol_print_error($this->db);
  510. }
  511. }
  512. else
  513. {
  514. $error++;
  515. $this->errors[]=$mailfile->error;
  516. }
  517. }
  518. }
  519. }
  520. if (! $error) return $num;
  521. else return -1 * $error;
  522. }
  523. }