dolibarr  20.0.0-beta
notify.class.php
Go to the documentation of this file.
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  * Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
9  * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
10  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>.
24  */
25 
31 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
32 
33 
37 class Notify
38 {
42  public $id;
43 
47  public $db;
48 
52  public $type;
53 
57  public $threshold;
58 
62  public $context;
63 
64 
68  public $event;
69 
73  public $socid;
74 
78  public $contact_id;
79 
83  public $fk_user;
84 
88  public $email;
89 
90 
96  public $datec;
97 
103  public $datem;
104 
108  public $error = '';
109 
113  public $errors = array();
114 
115  public $author;
116  public $ref;
117  public $date;
118  public $duree;
119  public $note;
120 
124  public $fk_project;
125 
126  // This codes actions are defined into table llx_notify_def
127  public static $arrayofnotifsupported = array(
128  'BILL_CANCEL',
129  'BILL_VALIDATE',
130  'BILL_PAYED',
131  'ORDER_CANCEL',
132  'ORDER_CREATE',
133  'ORDER_VALIDATE',
134  'ORDER_CLOSE',
135  'PROPAL_VALIDATE',
136  'PROPAL_CLOSE_SIGNED',
137  'PROPAL_CLOSE_REFUSED',
138  'FICHINTER_VALIDATE',
139  'FICHINTER_CLOSE',
140  'FICHINTER_ADD_CONTACT',
141  'ORDER_SUPPLIER_CANCEL',
142  'ORDER_SUPPLIER_VALIDATE',
143  'ORDER_SUPPLIER_APPROVE',
144  'ORDER_SUPPLIER_SUBMIT',
145  'ORDER_SUPPLIER_REFUSE',
146  'SHIPPING_VALIDATE',
147  'EXPENSE_REPORT_VALIDATE',
148  'EXPENSE_REPORT_APPROVE',
149  'HOLIDAY_VALIDATE',
150  'HOLIDAY_APPROVE',
151  'ACTION_CREATE'
152  );
153 
159  public function __construct($db)
160  {
161  $this->db = $db;
162  }
163 
164 
174  public function confirmMessage($action, $socid, $object)
175  {
176  global $langs;
177  $langs->load("mails");
178 
179  // Get full list of all notifications subscribed for $action, $socid and $object
180  $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
181 
182  if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_USER')) {
183  foreach ($listofnotiftodo as $val) {
184  if ($val['type'] == 'touser') {
185  unset($listofnotiftodo[$val['email']]);
186  //$listofnotiftodo = array_merge($listofnotiftodo);
187  }
188  }
189  }
190  if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_CONTACT')) {
191  foreach ($listofnotiftodo as $val) {
192  if ($val['type'] == 'tocontact') {
193  unset($listofnotiftodo[$val['email']]);
194  //$listofnotiftodo = array_merge($listofnotiftodo);
195  }
196  }
197  }
198  if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_FIX')) {
199  foreach ($listofnotiftodo as $val) {
200  if ($val['type'] == 'tofixedemail') {
201  unset($listofnotiftodo[$val['email']]);
202  //$listofnotiftodo = array_merge($listofnotiftodo);
203  }
204  }
205  }
206 
207  $texte = '';
208  $nb = -1;
209  if (is_array($listofnotiftodo)) {
210  $nb = count($listofnotiftodo);
211  }
212  if ($nb < 0) {
213  $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
214  } elseif ($nb == 0) {
215  $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("NoNotificationsWillBeSent");
216  } elseif ($nb == 1) {
217  $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ANotificationsWillBeSent");
218  } else { // Always >= 2 if ($nb >= 2) {
219  $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("SomeNotificationsWillBeSent", $nb);
220  }
221 
222  if (is_array($listofnotiftodo)) {
223  $i = 0;
224  foreach ($listofnotiftodo as $val) {
225  if ($i) {
226  $texte .= ', ';
227  } else {
228  $texte .= ' (';
229  }
230  if ($val['isemailvalid']) {
231  $texte .= $val['email'];
232  } else {
233  $texte .= $val['emaildesc'];
234  }
235  $i++;
236  }
237  if ($i) {
238  $texte .= ')';
239  }
240  }
241 
242  return $texte;
243  }
244 
251  public function delete(User $user = null)
252  {
253  $error = 0;
254 
255  dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
256 
257  $this->db->begin();
258 
259  if (!$error) {
260  $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
261  $sql .= " WHERE rowid = ".((int) $this->id);
262 
263  if (!$this->db->query($sql)) {
264  $error++;
265  $this->errors[] = $this->db->lasterror();
266  }
267  }
268 
269  if (!$error) {
270  $this->db->commit();
271  return 1;
272  } else {
273  $this->db->rollback();
274  return -1 * $error;
275  }
276  }
277 
285  public function create(User $user = null, $notrigger = 0)
286  {
287  $now = dol_now();
288 
289  $error = 0;
290 
291  // Check parameters
292  if (empty($this->socid)) {
293  $this->error = 'BadValueForParameter';
294  $this->errors[] = $this->error;
295  return -1;
296  }
297 
298  if (empty($this->datec)) {
299  $this->datec = $now;
300  }
301 
302  $this->db->begin();
303 
304  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (fk_soc, fk_action, fk_contact, type, datec)";
305  $sql .= " VALUES (".((int) $this->socid).", ".((int) $this->event).", ".((int) $this->contact_id).",";
306  $sql .= "'".$this->db->escape($this->type)."', '".$this->db->idate($this->datec)."')";
307 
308  $resql = $this->db->query($sql);
309  if ($resql) {
310  if ($this->db->affected_rows($resql)) {
311  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."notify_def");
312  }
313  } else {
314  $error++;
315  $this->error = $this->db->lasterror();
316  $this->errors[] = $this->error;
317  }
318 
319  if (!$error) {
320  $this->db->commit();
321  return $this->id;
322  } else {
323  $this->db->rollback();
324  return -1;
325  }
326  }
327 
336  public function fetch($id, $socid = 0, $type = 'email')
337  {
338  if (empty($id) && empty($socid)) {
339  return -1;
340  }
341 
342  $sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
343  $sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
344 
345  if ($id) {
346  $sql .= " WHERE rowid = ".((int) $id);
347  } elseif ($socid > 0) {
348  $sql .= " WHERE fk_soc = ".((int) $socid);
349  if ($type) {
350  $sql .= " AND type = '".$this->db->escape($type)."'";
351  }
352  }
353 
354  $resql = $this->db->query($sql);
355  if ($resql) {
356  if ($this->db->num_rows($resql)) {
357  $obj = $this->db->fetch_object($resql);
358 
359  $this->id = $obj->rowid;
360  $this->type = $obj->type;
361  $this->event = $obj->event;
362  $this->socid = $obj->socid;
363  $this->contact_id = $obj->contact_id;
364  $this->fk_user = $obj->fk_user;
365  $this->email = $obj->email;
366  $this->threshold = $obj->threshold;
367  $this->context = $obj->context;
368  $this->datec = $this->db->jdate($obj->datec);
369  $this->datem = $this->db->jdate($obj->datem);
370  }
371  $this->db->free($resql);
372 
373  return 1;
374  } else {
375  dol_print_error($this->db);
376  return -1;
377  }
378  }
379 
387  public function update(User $user = null, $notrigger = -1)
388  {
389  global $langs;
390 
391  $error = 0;
392 
393  if (!$this->id) {
394  return -1;
395  }
396 
397  $this->db->begin();
398 
399  $sql = "UPDATE ".MAIN_DB_PREFIX."notify_def SET";
400  $sql .= " type = '".$this->db->escape($this->type)."'";
401  // $sql .= ",fk_user = ".((int) $this->fk_user);
402  // $sql .= ",email = '".$this->db->escape($this->email)."'";
403  // $sql .= ",threshold = '".$this->db->escape($this->threshold)."'";
404  // $sql .= ",context = '".$this->db->escape($this->context)."'";
405  $sql .= ",fk_soc = ".((int) $this->socid);
406  $sql .= ",fk_action = ".((int) $this->event);
407  $sql .= ",fk_contact = ".((int) $this->contact_id);
408  $sql .= " WHERE rowid = ".((int) $this->id);
409 
410  $result = $this->db->query($sql);
411  if (!$result) {
412  $error++;
413  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
414  $this->error = $langs->trans('ErrorDuplicateField');
415  } else {
416  $this->error = $this->db->lasterror();
417  }
418  $this->errors[] = $this->error;
419  }
420 
421  if (!$error) {
422  $this->db->commit();
423  return 1;
424  } else {
425  $this->db->rollback();
426  return -1;
427  }
428  }
429 
440  public function getNotificationsArray($notifcode, $socid = 0, $object = null, $userid = 0, $scope = array('thirdparty', 'user', 'global'))
441  {
442  global $conf, $user;
443 
444  $error = 0;
445  $resarray = array();
446 
447  $valueforthreshold = 0;
448  if (is_object($object)) {
449  $valueforthreshold = $object->total_ht;
450  }
451 
452  $sqlnotifcode = '';
453  if ($notifcode) {
454  if (is_numeric($notifcode)) {
455  $sqlnotifcode = " AND n.fk_action = ".((int) $notifcode); // Old usage
456  } else {
457  $sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
458  }
459  }
460 
461  // Subscription per contact
462  if (!$error) {
463  if ($socid >= 0 && in_array('thirdparty', $scope)) {
464  $sql = "SELECT a.code, c.email, c.rowid";
465  $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
466  $sql .= " ".$this->db->prefix()."socpeople as c,";
467  $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
468  $sql .= " ".$this->db->prefix()."societe as s";
469  $sql .= " WHERE n.fk_contact = c.rowid";
470  $sql .= " AND a.rowid = n.fk_action";
471  $sql .= " AND n.fk_soc = s.rowid";
472  $sql .= $sqlnotifcode;
473  $sql .= " AND s.entity IN (".getEntity('societe').")";
474  if ($socid > 0) {
475  $sql .= " AND s.rowid = ".((int) $socid);
476  }
477 
478  dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
479 
480  $resql = $this->db->query($sql);
481  if ($resql) {
482  $num = $this->db->num_rows($resql);
483  $i = 0;
484  while ($i < $num) {
485  $obj = $this->db->fetch_object($resql);
486  if ($obj) {
487  $newval2 = trim($obj->email);
488  $isvalid = isValidEmail($newval2);
489  if (empty($resarray[$newval2])) {
490  $resarray[$newval2] = array('type' => 'tocontact', 'code' => trim($obj->code), 'emaildesc' => 'Contact id '.$obj->rowid, 'email' => $newval2, 'contactid' => $obj->rowid, 'isemailvalid' => $isvalid);
491  }
492  }
493  $i++;
494  }
495  } else {
496  $error++;
497  $this->error = $this->db->lasterror();
498  }
499  }
500  }
501 
502  // Subscription per user
503  if (!$error) {
504  if ($userid >= 0 && in_array('user', $scope)) {
505  $sql = "SELECT a.code, c.email, c.rowid";
506  $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
507  $sql .= " ".$this->db->prefix()."user as c,";
508  $sql .= " ".$this->db->prefix()."c_action_trigger as a";
509  $sql .= " WHERE n.fk_user = c.rowid";
510  $sql .= " AND a.rowid = n.fk_action";
511  $sql .= $sqlnotifcode;
512  $sql .= " AND c.entity IN (".getEntity('user').")";
513  if ($userid > 0) {
514  $sql .= " AND c.rowid = ".((int) $userid);
515  }
516 
517  dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
518 
519  $resql = $this->db->query($sql);
520  if ($resql) {
521  $num = $this->db->num_rows($resql);
522  $i = 0;
523  while ($i < $num) {
524  $obj = $this->db->fetch_object($resql);
525  if ($obj) {
526  $newval2 = trim($obj->email);
527  $isvalid = isValidEmail($newval2);
528  if (empty($resarray[$newval2])) {
529  $resarray[$newval2] = array('type' => 'touser', 'code' => trim($obj->code), 'emaildesc' => 'User id '.$obj->rowid, 'email' => $newval2, 'userid' => $obj->rowid, 'isemailvalid' => $isvalid);
530  }
531  }
532  $i++;
533  }
534  } else {
535  $error++;
536  $this->error = $this->db->lasterror();
537  }
538  }
539  }
540 
541  // Subscription global
542  if (!$error) {
543  if (in_array('global', $scope)) {
544  // List of notifications enabled for fixed email
545  foreach ($conf->global as $key => $val) {
546  if ($notifcode) {
547  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
548  continue;
549  }
550  } else {
551  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
552  continue;
553  }
554  }
555 
556  $threshold = (float) $reg[1];
557  if ($valueforthreshold < $threshold) {
558  continue;
559  }
560 
561  $tmpemail = explode(',', $val);
562  foreach ($tmpemail as $key2 => $val2) {
563  $newval2 = trim($val2);
564  if ($newval2 == '__SUPERVISOREMAIL__') {
565  if ($user->fk_user > 0) {
566  $tmpuser = new User($this->db);
567  $tmpuser->fetch($user->fk_user);
568  if ($tmpuser->email) {
569  $newval2 = trim($tmpuser->email);
570  } else {
571  $newval2 = '';
572  }
573  } else {
574  $newval2 = '';
575  }
576  }
577  if ($newval2) {
578  $isvalid = isValidEmail($newval2, 0);
579  if (empty($resarray[$newval2])) {
580  $resarray[$newval2] = array('type' => 'tofixedemail', 'code' => trim($key), 'emaildesc' => trim($val2), 'email' => $newval2, 'isemailvalid' => $isvalid);
581  }
582  }
583  }
584  }
585  }
586  }
587 
588  if ($error) {
589  return -1;
590  }
591 
592  //var_dump($resarray);
593  return $resarray;
594  }
595 
607  public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
608  {
609  global $user, $conf, $langs, $mysoc;
610  global $hookmanager;
611  global $dolibarr_main_url_root;
612  global $action;
613 
614  // Complete the array Notify::$arrayofnotifsupported
615  if (!is_object($hookmanager)) {
616  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
617  $hookmanager = new HookManager($this->db);
618  }
619  $hookmanager->initHooks(array('notification'));
620 
621  $parameters = array('notifcode' => $notifcode);
622  $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
623  if (empty($reshook)) {
624  if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
625  Notify::$arrayofnotifsupported = array_merge(Notify::$arrayofnotifsupported, $hookmanager->resArray['arrayofnotifsupported']);
626  }
627  }
628 
629  // If the trigger code is not managed by the Notification module
630  if (!in_array($notifcode, Notify::$arrayofnotifsupported)) {
631  return 0;
632  }
633 
634  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
635 
636  dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object id=".$object->id);
637 
638  $langs->load("other");
639 
640  // Define $urlwithroot
641  $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
642  $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
643  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
644 
645  // Define some vars
646  $application = 'Dolibarr';
647  if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
648  $application = getDolGlobalString('MAIN_APPLICATION_TITLE');
649  }
650  $from = getDolGlobalString('NOTIFICATION_EMAIL_FROM');
651  $object_type = '';
652  $link = '';
653  $num = 0;
654  $error = 0;
655 
656  $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
657  $newref = (empty($object->newref) ? $object->ref : $object->newref);
658 
659  $sql = '';
660 
661  // Check notification per third party
662  if (!empty($object->socid) && $object->socid > 0) {
663  $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
664  $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
665  $sql .= " FROM ".$this->db->prefix()."socpeople as c,";
666  $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
667  $sql .= " ".$this->db->prefix()."notify_def as n,";
668  $sql .= " ".$this->db->prefix()."societe as s";
669  $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
670  $sql .= " AND n.fk_soc = s.rowid";
671  $sql .= " AND c.statut = 1";
672  if (is_numeric($notifcode)) {
673  $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
674  } else {
675  $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
676  }
677  $sql .= " AND s.rowid = ".((int) $object->socid);
678 
679  $sql .= "\nUNION\n";
680  }
681 
682  // Check notification per user
683  $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
684  $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
685  $sql .= " FROM ".$this->db->prefix()."user as c,";
686  $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
687  $sql .= " ".$this->db->prefix()."notify_def as n";
688  $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
689  $sql .= " AND c.statut = 1";
690  if (is_numeric($notifcode)) {
691  $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
692  } else {
693  $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
694  }
695 
696  // Check notification fixed
697  // TODO Move part found after, into a sql here
698 
699 
700  // Loop on all notifications enabled
701  $result = $this->db->query($sql);
702  if ($result) {
703  $num = $this->db->num_rows($result);
704  $projtitle = '';
705  if (is_object($object->project) || $object->fetch_project() > 0) {
706  $projtitle = '('.$object->project->title.')';
707  }
708 
709  if ($num > 0) {
710  $i = 0;
711  while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
712  $obj = $this->db->fetch_object($result);
713 
714  $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
715  $notifcodedefid = $obj->adid;
716  $trackid = '';
717  if ($obj->type_target == 'tocontactid') {
718  $trackid = 'ctc'.$obj->cid;
719  }
720  if ($obj->type_target == 'touserid') {
721  $trackid = 'use'.$obj->cid;
722  }
723 
724  if (dol_strlen($obj->email)) {
725  // Set output language
726  $outputlangs = $langs;
727  if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
728  $outputlangs = new Translate('', $conf);
729  $outputlangs->setDefaultLang($obj->default_lang);
730  $outputlangs->loadLangs(array("main", "other"));
731  }
732 
733  $appli = $mysoc->name;
734 
735  $subject = '['.$appli.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
736 
737  switch ($notifcode) {
738  case 'BILL_CANCEL':
739  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
740  $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
741  $object_type = 'facture';
742  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceCanceled", $link);
743  break;
744  case 'BILL_VALIDATE':
745  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
746  $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
747  $object_type = 'facture';
748  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
749  break;
750  case 'BILL_PAYED':
751  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
752  $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
753  $object_type = 'facture';
754  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
755  break;
756  case 'ORDER_CANCEL':
757  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
758  $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
759  $object_type = 'order';
760  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderCanceled", $link);
761  break;
762  case 'ORDER_VALIDATE':
763  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
764  $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
765  $object_type = 'order';
766  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
767  break;
768  case 'ORDER_CLOSE':
769  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
770  $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
771  $object_type = 'order';
772  $labeltouse = getDolGlobalString('ORDER_CLOSE_TEMPLATE');
773  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderClose", $link);
774  break;
775  case 'PROPAL_VALIDATE':
776  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
777  $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
778  $object_type = 'propal';
779  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
780  break;
781  case 'PROPAL_CLOSE_REFUSED':
782  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
783  $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
784  $object_type = 'propal';
785  $labeltouse = getDolGlobalString('PROPAL_CLOSE_REFUSED_TEMPLATE');
786  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedRefused", $link);
787  if (!empty($object->context['closedfromonlinesignature'])) {
788  $mesg .= ' - From online page';
789  }
790  break;
791  case 'PROPAL_CLOSE_SIGNED':
792  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
793  $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
794  $object_type = 'propal';
795  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
796  if (!empty($object->context['closedfromonlinesignature'])) {
797  $mesg .= ' - From online page';
798  }
799  break;
800  case 'FICHINTER_ADD_CONTACT':
801  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
802  $dir_output = $conf->ficheinter->dir_output;
803  $object_type = 'ficheinter';
804  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
805  break;
806  case 'FICHINTER_VALIDATE':
807  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
808  $dir_output = $conf->ficheinter->dir_output;
809  $object_type = 'ficheinter';
810  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
811  break;
812  case 'FICHINTER_CLOSE':
813  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
814  $dir_output = $conf->ficheinter->dir_output;
815  $object_type = 'ficheinter';
816  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
817  break;
818  case 'ORDER_SUPPLIER_VALIDATE':
819  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
820  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
821  $object_type = 'order_supplier';
822  $labeltouse = isset($conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE : '';
823  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
824  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($outputlangs));
825  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
826  break;
827  case 'ORDER_SUPPLIER_CANCEL':
828  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
829  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
830  $object_type = 'order_supplier';
831  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
832  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($outputlangs));
833  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
834  break;
835  case 'ORDER_SUPPLIER_APPROVE':
836  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
837  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
838  $object_type = 'order_supplier';
839  $labeltouse = isset($conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE : '';
840  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
841  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($outputlangs));
842  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
843  break;
844  case 'ORDER_SUPPLIER_SUBMIT':
845  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
846  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
847  $object_type = 'order_supplier';
848  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
849  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($outputlangs));
850  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
851  break;
852  case 'ORDER_SUPPLIER_REFUSE':
853  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
854  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
855  $object_type = 'order_supplier';
856  $labeltouse = isset($conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE : '';
857  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
858  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($outputlangs));
859  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
860  break;
861  case 'SHIPPING_VALIDATE':
862  $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
863  $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
864  $object_type = 'shipping';
865  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
866  break;
867  case 'EXPENSE_REPORT_VALIDATE':
868  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
869  $dir_output = $conf->expensereport->dir_output;
870  $object_type = 'expensereport';
871  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
872  break;
873  case 'EXPENSE_REPORT_APPROVE':
874  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
875  $dir_output = $conf->expensereport->dir_output;
876  $object_type = 'expensereport';
877  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
878  break;
879  case 'HOLIDAY_VALIDATE':
880  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
881  $dir_output = $conf->holiday->dir_output;
882  $object_type = 'holiday';
883  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
884  break;
885  case 'HOLIDAY_APPROVE':
886  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
887  $dir_output = $conf->holiday->dir_output;
888  $object_type = 'holiday';
889  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
890  break;
891  case 'ACTION_CREATE':
892  $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
893  $dir_output = $conf->agenda->dir_output;
894  $object_type = 'action';
895  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link);
896  break;
897  default:
898  $object_type = $object->element;
899  $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
900  $template = $notifcode.'_TEMPLATE';
901  $mesg = $outputlangs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref.' '.$dir_output;
902  break;
903  }
904 
905  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
906  $formmail = new FormMail($this->db);
907  $arraydefaultmessage = null;
908 
909  $template = $notifcode.'_TEMPLATE';
910  $labeltouse = getDolGlobalString($template);
911  if (!empty($labeltouse)) {
912  $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $labeltouse);
913  }
914  if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
915  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
916  complete_substitutions_array($substitutionarray, $outputlangs, $object);
917  $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
918  $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
919  } else {
920  $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
921  $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
922  $message .= "\n";
923  $message .= $mesg;
924  }
925 
926  $ref = dol_sanitizeFileName($newref);
927  $pdf_path = $dir_output."/".$ref.".pdf";
928  if (!dol_is_file($pdf_path) || (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) {
929  // We can't add PDF as it is not generated yet.
930  $filepdf = '';
931  } else {
932  $filepdf = $pdf_path;
933  $filename_list[] = $filepdf;
934  $mimetype_list[] = mime_content_type($filepdf);
935  $mimefilename_list[] = $ref.".pdf";
936  }
937 
938  $labeltouse = !empty($labeltouse) ? $labeltouse : '';
939 
940  // Replace keyword __SUPERVISOREMAIL__
941  if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
942  $newval = '';
943  if ($user->fk_user > 0) {
944  $supervisoruser = new User($this->db);
945  $supervisoruser->fetch($user->fk_user);
946  if ($supervisoruser->email) {
947  $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
948  }
949  }
950  dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
951  $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
952  $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
953  $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
954  $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
955  }
956 
957  $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'outputlangs' => $outputlangs, 'labeltouse' => $labeltouse);
958  if (!isset($action)) {
959  $action = '';
960  }
961 
962  $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
963  if (empty($reshook)) {
964  if (!empty($hookmanager->resArray['files'])) {
965  $filename_list = $hookmanager->resArray['files']['file'];
966  $mimetype_list = $hookmanager->resArray['files']['mimefile'];
967  $mimefilename_list = $hookmanager->resArray['files']['filename'];
968  }
969  if (!empty($hookmanager->resArray['subject'])) {
970  $subject .= $hookmanager->resArray['subject'];
971  }
972  if (!empty($hookmanager->resArray['message'])) {
973  $message .= $hookmanager->resArray['message'];
974  }
975  }
976 
977  $mailfile = new CMailFile(
978  $subject,
979  $sendto,
980  $from,
981  $message,
982  $filename_list,
983  $mimetype_list,
984  $mimefilename_list,
985  '',
986  '',
987  0,
988  -1,
989  '',
990  '',
991  $trackid,
992  '',
993  'notification'
994  );
995 
996  if ($mailfile->sendfile()) {
997  if ($obj->type_target == 'touserid') {
998  $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
999  $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)."')";
1000  } else {
1001  $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
1002  $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)."')";
1003  }
1004  if (!$this->db->query($sql)) {
1005  dol_print_error($this->db);
1006  }
1007  } else {
1008  $error++;
1009  $this->errors[] = $mailfile->error;
1010  }
1011  } else {
1012  dol_syslog("No notification sent for ".$sendto." because email is empty");
1013  }
1014  $i++;
1015  }
1016  } else {
1017  dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
1018  }
1019  } else {
1020  $error++;
1021  $this->errors[] = $this->db->lasterror();
1022  dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
1023  return -1;
1024  }
1025 
1026  // Check notification using fixed email
1027  // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and inclulde the case into previous loop of sql result
1028  if (!$error) {
1029  foreach ($conf->global as $key => $val) {
1030  $reg = array();
1031  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
1032  continue;
1033  }
1034 
1035  $sendto = $val;
1036 
1037  $threshold = (float) $reg[1];
1038  if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
1039  dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
1040  continue;
1041  }
1042 
1043  $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
1044  if ($notifcodedefid <= 0) {
1045  dol_print_error($this->db, 'Failed to get id from code');
1046  }
1047  $trackid = '';
1048 
1049  $object_type = '';
1050  $link = '';
1051  $num++;
1052 
1053  $appli = $mysoc->name;
1054 
1055  $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
1056 
1057  switch ($notifcode) {
1058  case 'BILL_VALIDATE':
1059  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1060  $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1061  $object_type = 'facture';
1062  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
1063  break;
1064  case 'BILL_PAYED':
1065  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1066  $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1067  $object_type = 'facture';
1068  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
1069  break;
1070  case 'ORDER_VALIDATE':
1071  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1072  $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1073  $object_type = 'order';
1074  $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
1075  break;
1076  case 'ORDER_CLOSE':
1077  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1078  $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1079  $object_type = 'order';
1080  $mesg = $langs->transnoentitiesnoconv("EMailTextOrderClose", $link);
1081  break;
1082  case 'PROPAL_VALIDATE':
1083  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1084  $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1085  $object_type = 'propal';
1086  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
1087  break;
1088  case 'PROPAL_CLOSE_SIGNED':
1089  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1090  $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1091  $object_type = 'propal';
1092  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
1093  break;
1094  case 'FICHINTER_ADD_CONTACT':
1095  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1096  $dir_output = $conf->ficheinter->dir_output;
1097  $object_type = 'ficheinter';
1098  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
1099  break;
1100  case 'FICHINTER_VALIDATE':
1101  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1102  $dir_output = $conf->facture->dir_output;
1103  $object_type = 'ficheinter';
1104  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
1105  break;
1106  case 'FICHINTER_CLOSE':
1107  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1108  $dir_output = $conf->facture->dir_output;
1109  $object_type = 'ficheinter';
1110  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
1111  break;
1112  case 'ORDER_SUPPLIER_CANCEL':
1113  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1114  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1115  $object_type = 'order_supplier';
1116  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1117  $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($langs));
1118  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1119  break;
1120  case 'ORDER_SUPPLIER_VALIDATE':
1121  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1122  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1123  $object_type = 'order_supplier';
1124  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1125  $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs));
1126  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1127  break;
1128  case 'ORDER_SUPPLIER_APPROVE':
1129  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1130  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1131  $object_type = 'order_supplier';
1132  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1133  $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs));
1134  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1135  break;
1136  case 'ORDER_SUPPLIER_SUBMIT':
1137  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1138  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1139  $object_type = 'order_supplier';
1140  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1141  $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs));
1142  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1143  break;
1144  case 'ORDER_SUPPLIER_REFUSE':
1145  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1146  $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1147  $object_type = 'order_supplier';
1148  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1149  $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs));
1150  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1151  break;
1152  case 'SHIPPING_VALIDATE':
1153  $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1154  $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
1155  $object_type = 'order_supplier';
1156  $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
1157  break;
1158  case 'EXPENSE_REPORT_VALIDATE':
1159  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1160  $dir_output = $conf->expensereport->dir_output;
1161  $object_type = 'expensereport';
1162  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
1163  break;
1164  case 'EXPENSE_REPORT_APPROVE':
1165  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1166  $dir_output = $conf->expensereport->dir_output;
1167  $object_type = 'expensereport';
1168  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
1169  break;
1170  case 'HOLIDAY_VALIDATE':
1171  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1172  $dir_output = $conf->holiday->dir_output;
1173  $object_type = 'holiday';
1174  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
1175  break;
1176  case 'HOLIDAY_APPROVE':
1177  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1178  $dir_output = $conf->holiday->dir_output;
1179  $object_type = 'holiday';
1180  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
1181  break;
1182  case 'ACTION_CREATE':
1183  $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1184  $dir_output = $conf->agenda->dir_output;
1185  $object_type = 'action';
1186  $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link);
1187  break;
1188  default:
1189  $object_type = $object->element;
1190  $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
1191  $mesg = $langs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref;
1192  break;
1193  }
1194  $ref = dol_sanitizeFileName($newref);
1195  $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
1196  if (!dol_is_file($pdf_path)) {
1197  // We can't add PDF as it is not generated yet.
1198  $filepdf = '';
1199  } else {
1200  $filepdf = $pdf_path;
1201  $filename_list[] = $pdf_path;
1202  $mimetype_list[] = mime_content_type($filepdf);
1203  $mimefilename_list[] = $ref.".pdf";
1204  }
1205 
1206  // Set output language
1207  $outputlangs = $langs;
1208 
1209  // if an e-mail template is configured for this notification code (for instance 'SHIPPING_VALIDATE_TEMPLATE', ...),
1210  // we fetch this template by its label. Otherwise, a default message content will be sent.
1211  $mailTemplateLabel = getDolGlobalString($notifcode.'_TEMPLATE');
1212  $emailTemplate = null;
1213  if (!empty($mailTemplateLabel)) {
1214  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
1215  $formmail = new FormMail($this->db);
1216  $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $mailTemplateLabel);
1217  }
1218  if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) {
1219  if (property_exists($object, 'thirdparty') && $object->thirdparty instanceof Societe && $object->thirdparty->default_lang && $object->thirdparty->default_lang != $langs->defaultlang) {
1220  $outputlangs = new Translate('', $conf);
1221  $outputlangs->setDefaultLang($object->thirdparty->default_lang);
1222  $outputlangs->loadLangs(array('main', 'other'));
1223  }
1224  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
1225  complete_substitutions_array($substitutionarray, $outputlangs, $object);
1226  $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $outputlangs);
1227  $message = make_substitutions($emailTemplate->content, $substitutionarray, $outputlangs);
1228  } else {
1229  $message = '';
1230  $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
1231  $message .= "\n";
1232  $message .= $mesg;
1233 
1234  $message = nl2br($message);
1235  }
1236 
1237  // Replace keyword __SUPERVISOREMAIL__
1238  if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
1239  $newval = '';
1240  if ($user->fk_user > 0) {
1241  $supervisoruser = new User($this->db);
1242  $supervisoruser->fetch($user->fk_user);
1243  if ($supervisoruser->email) {
1244  $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
1245  }
1246  }
1247  dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
1248  $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
1249  $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
1250  $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
1251  $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
1252  }
1253 
1254  if ($sendto) {
1255  $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'subject' => &$subject, 'message' => &$message);
1256  $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1257  if (empty($reshook)) {
1258  if (!empty($hookmanager->resArray['files'])) {
1259  $filename_list = $hookmanager->resArray['files']['file'];
1260  $mimetype_list = $hookmanager->resArray['files']['mimefile'];
1261  $mimefilename_list = $hookmanager->resArray['files']['filename'];
1262  }
1263  if (!empty($hookmanager->resArray['subject'])) {
1264  $subject .= $hookmanager->resArray['subject'];
1265  }
1266  if (!empty($hookmanager->resArray['message'])) {
1267  $message .= $hookmanager->resArray['message'];
1268  }
1269  }
1270  $mailfile = new CMailFile(
1271  $subject,
1272  $sendto,
1273  $from,
1274  $message,
1275  $filename_list,
1276  $mimetype_list,
1277  $mimefilename_list,
1278  '',
1279  '',
1280  0,
1281  1,
1282  '',
1283  $trackid,
1284  '',
1285  '',
1286  'notification'
1287  );
1288 
1289  if ($mailfile->sendfile()) {
1290  $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
1291  $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($sendto)."')";
1292  if (!$this->db->query($sql)) {
1293  dol_print_error($this->db);
1294  }
1295  } else {
1296  $error++;
1297  $this->errors[] = $mailfile->error;
1298  }
1299  }
1300  }
1301  }
1302 
1303  if (!$error) {
1304  return $num;
1305  } else {
1306  return -1 * $error;
1307  }
1308  }
1309 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
Class to manage hooks.
Class to manage the table of subscription to notifications.
fetch($id, $socid=0, $type='email')
Load record from database.
update(User $user=null, $notrigger=-1)
Update record in database.
confirmMessage($action, $socid, $object)
Return message that say how many notification (and to which email) will occurs on requested event.
getNotificationsArray($notifcode, $socid=0, $object=null, $userid=0, $scope=array('thirdparty', 'user', 'global'))
Return number of notifications activated, for all or a given action code (and third party)
send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
Check if notification are active for couple action/company.
create(User $user=null, $notrigger=0)
Create notification information record.
__construct($db)
Constructor.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:519
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div float
Buy price without taxes.
Definition: style.css.php:960
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123