dolibarr 24.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-2026 MDW <mdeweerd@users.noreply.github.com>
11 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
12 * Copyright (C) 2026 Pierre Ardoin <developpeur@lesmetiersdubatiment.fr>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
33require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
34
35
39class Notify
40{
44 public $id;
45
49 public $db;
50
54 public $type;
55
59 public $threshold;
60
64 public $context;
65
69 public $event;
70
74 public $socid;
75
79 public $contact_id;
80
84 public $fk_user;
85
89 public $email;
90
91
97 public $datec;
98
104 public $datem;
105
109 public $error = '';
110
114 public $errors = array();
115
119 public $author;
123 public $ref;
127 public $date;
131 public $duree;
135 public $note;
136
140 public $fk_project;
141
145 public static $arrayofnotifsupported = array(
146 'BILL_CANCEL',
147 'BILL_VALIDATE',
148 'BILL_PAYED',
149 'ORDER_CANCEL',
150 'ORDER_CREATE',
151 'ORDER_VALIDATE',
152 'ORDER_CLOSE',
153 'PROPAL_VALIDATE',
154 'PROPAL_CLOSE_SIGNED',
155 'PROPAL_CLOSE_REFUSED',
156 'FICHINTER_VALIDATE',
157 'FICHINTER_MODIFY',
158 'FICHINTER_CLOSE',
159 'FICHINTER_ADD_CONTACT',
160 'ORDER_SUPPLIER_CANCEL',
161 'ORDER_SUPPLIER_VALIDATE',
162 'ORDER_SUPPLIER_APPROVE',
163 'ORDER_SUPPLIER_SUBMIT',
164 'ORDER_SUPPLIER_REFUSE',
165 'SHIPPING_MODIFY',
166 'SHIPPING_VALIDATE',
167 'EXPENSE_REPORT_VALIDATE',
168 'EXPENSE_REPORT_APPROVE',
169 'HOLIDAY_VALIDATE',
170 'HOLIDAY_APPROVE',
171 'ACTION_CREATE',
172 'CONTRACT_MODIFY',
173 'STOCKTRANSFER_CREATE',
174 'STOCKTRANSFER_MODIFY',
175 'STOCKTRANSFER_VALIDATE',
176 'STOCKTRANSFER_UNVALIDATE',
177 'STOCKTRANSFER_CLOSE'
178 );
179
185 public function __construct($db)
186 {
187 $this->db = $db;
188 }
189
190
200 public function confirmMessage($action, $socid, $object)
201 {
202 global $langs;
203 $langs->load("mails");
204
205 // Get full list of all notifications subscribed for $action, $socid and $object
206 $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
207
208 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_USER')) {
209 foreach ($listofnotiftodo as $val) {
210 if ($val['type'] == 'touser') {
211 unset($listofnotiftodo[$val['email']]);
212 //$listofnotiftodo = array_merge($listofnotiftodo);
213 }
214 }
215 }
216 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_CONTACT')) {
217 foreach ($listofnotiftodo as $val) {
218 if ($val['type'] == 'tocontact') {
219 unset($listofnotiftodo[$val['email']]);
220 //$listofnotiftodo = array_merge($listofnotiftodo);
221 }
222 }
223 }
224 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_FIX')) {
225 foreach ($listofnotiftodo as $val) {
226 if ($val['type'] == 'tofixedemail') {
227 unset($listofnotiftodo[$val['email']]);
228 //$listofnotiftodo = array_merge($listofnotiftodo);
229 }
230 }
231 }
232
233 $texte = '';
234 $nb = -1;
235 if (is_array($listofnotiftodo)) {
236 $nb = count($listofnotiftodo);
237 }
238 if ($nb < 0) {
239 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
240 } elseif ($nb == 0) {
241 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("NoNotificationsWillBeSent");
242 } elseif ($nb == 1) {
243 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ANotificationsWillBeSent");
244 } else { // Always >= 2 if ($nb >= 2) {
245 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("SomeNotificationsWillBeSent", $nb);
246 }
247
248 if (is_array($listofnotiftodo)) {
249 $i = 0;
250 foreach ($listofnotiftodo as $val) {
251 if ($i) {
252 $texte .= ', ';
253 } else {
254 $texte .= ' (';
255 }
256 if ($val['isemailvalid']) {
257 $texte .= $val['email'];
258 } else {
259 $texte .= $val['emaildesc'];
260 }
261 $i++;
262 }
263 if ($i) {
264 $texte .= ')';
265 }
266 }
267
268 return $texte;
269 }
270
277 public function delete($user = null)
278 {
279 $error = 0;
280
281 dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
282
283 $this->db->begin();
284
285 if (!$error) {
286 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
287 $sql .= " WHERE rowid = ".((int) $this->id);
288
289 if (!$this->db->query($sql)) {
290 $error++;
291 $this->errors[] = $this->db->lasterror();
292 }
293 }
294
295 if (!$error) {
296 $this->db->commit();
297 return 1;
298 } else {
299 $this->db->rollback();
300 return -1 * $error;
301 }
302 }
303
311 public function create($user = null, $notrigger = 0)
312 {
313 $now = dol_now();
314
315 $error = 0;
316
317 // Check parameters
318 if (empty($this->socid)) {
319 $this->error = 'BadValueForParameter';
320 $this->errors[] = $this->error;
321 return -1;
322 }
323
324 if (empty($this->datec)) {
325 $this->datec = $now;
326 }
327
328 $this->db->begin();
329
330 $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (fk_soc, fk_action, fk_contact, type, datec)";
331 $sql .= " VALUES (".((int) $this->socid).", ".((int) $this->event).", ".((int) $this->contact_id).",";
332 $sql .= "'".$this->db->escape($this->type)."', '".$this->db->idate($this->datec)."')";
333
334 $resql = $this->db->query($sql);
335 if ($resql) {
336 if ($this->db->affected_rows($resql)) {
337 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."notify_def");
338 }
339 } else {
340 $error++;
341 $this->error = $this->db->lasterror();
342 $this->errors[] = $this->error;
343 }
344
345 if (!$error) {
346 $this->db->commit();
347 return $this->id;
348 } else {
349 $this->db->rollback();
350 return -1;
351 }
352 }
353
362 public function fetch($id, $socid = 0, $type = 'email')
363 {
364 if (empty($id) && empty($socid)) {
365 return -1;
366 }
367
368 $sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
369 $sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
370
371 if ($id) {
372 $sql .= " WHERE rowid = ".((int) $id);
373 } elseif ($socid > 0) {
374 $sql .= " WHERE fk_soc = ".((int) $socid);
375 if ($type) {
376 $sql .= " AND type = '".$this->db->escape($type)."'";
377 }
378 }
379
380 $resql = $this->db->query($sql);
381 if ($resql) {
382 if ($this->db->num_rows($resql)) {
383 $obj = $this->db->fetch_object($resql);
384
385 $this->id = $obj->rowid;
386 $this->type = $obj->type;
387 $this->event = $obj->event;
388 $this->socid = $obj->socid;
389 $this->contact_id = $obj->contact_id;
390 $this->fk_user = $obj->fk_user;
391 $this->email = $obj->email;
392 $this->threshold = $obj->threshold;
393 $this->context = $obj->context;
394 $this->datec = $this->db->jdate($obj->datec);
395 $this->datem = $this->db->jdate($obj->datem);
396 }
397 $this->db->free($resql);
398
399 return 1;
400 } else {
401 dol_print_error($this->db);
402 return -1;
403 }
404 }
405
413 public function update($user = null, $notrigger = 0)
414 {
415 global $langs;
416
417 $error = 0;
418
419 if (!$this->id) {
420 return -1;
421 }
422
423 $this->db->begin();
424
425 $sql = "UPDATE ".MAIN_DB_PREFIX."notify_def SET";
426 $sql .= " type = '".$this->db->escape($this->type)."'";
427 // $sql .= ",fk_user = ".((int) $this->fk_user);
428 // $sql .= ",email = '".$this->db->escape($this->email)."'";
429 // $sql .= ",threshold = '".$this->db->escape($this->threshold)."'";
430 // $sql .= ",context = '".$this->db->escape($this->context)."'";
431 $sql .= ",fk_soc = ".((int) $this->socid);
432 $sql .= ",fk_action = ".((int) $this->event);
433 $sql .= ",fk_contact = ".((int) $this->contact_id);
434 $sql .= " WHERE rowid = ".((int) $this->id);
435
436 $result = $this->db->query($sql);
437 if (!$result) {
438 $error++;
439 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
440 $this->error = $langs->trans('ErrorDuplicateField');
441 } else {
442 $this->error = $this->db->lasterror();
443 }
444 $this->errors[] = $this->error;
445 }
446
447 if (!$error) {
448 $this->db->commit();
449 return 1;
450 } else {
451 $this->db->rollback();
452 return -1;
453 }
454 }
455
466 public function getNotificationsArray($notifcode, $socid = 0, $object = null, $userid = 0, $scope = array('thirdparty', 'user', 'global'))
467 {
468 global $conf, $user;
469
470 $error = 0;
471 $resarray = array();
472
473 $valueforthreshold = 0;
474 if (is_object($object)) {
475 $valueforthreshold = $object->total_ht;
476 }
477
478 $sqlnotifcode = '';
479 if ($notifcode) {
480 if (is_numeric($notifcode)) {
481 $sqlnotifcode = " AND n.fk_action = ".((int) $notifcode); // Old usage
482 } else {
483 $sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
484 }
485 }
486
487 // Subscription per contact
488 if (!$error) {
489 if ($socid >= 0 && in_array('thirdparty', $scope)) {
490 $sql = "SELECT a.code, c.email, c.rowid, c.statut as status";
491 $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
492 $sql .= " ".$this->db->prefix()."socpeople as c,";
493
494 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
495 $sql .= " ".$this->db->prefix()."societe as s";
496 $sql .= " WHERE n.fk_contact = c.rowid";
497 $sql .= " AND a.rowid = n.fk_action";
498 $sql .= " AND n.fk_soc = s.rowid";
499 $sql .= $sqlnotifcode;
500 $sql .= " AND s.entity IN (".getEntity('societe').")";
501 if ($socid > 0) {
502 $sql .= " AND s.rowid = ".((int) $socid);
503 }
504
505 dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
506
507 $resql = $this->db->query($sql);
508 if ($resql) {
509 $num = $this->db->num_rows($resql);
510 $i = 0;
511 while ($i < $num) {
512 $obj = $this->db->fetch_object($resql);
513 // we want to notify only if contact is enable
514 if ($obj && $obj->status == 1) {
515 $newval2 = trim($obj->email);
516 $isvalid = isValidEmail($newval2);
517 if (empty($resarray[$newval2])) {
518 $resarray[$newval2] = array('type' => 'tocontact', 'code' => trim($obj->code), 'emaildesc' => 'Contact id '.$obj->rowid, 'email' => $newval2, 'contactid' => $obj->rowid, 'isemailvalid' => $isvalid);
519 }
520 }
521 $i++;
522 }
523 } else {
524 $error++;
525 $this->error = $this->db->lasterror();
526 }
527 }
528 }
529
530 // Subscription per user
531 if (!$error) {
532 if ($userid >= 0 && in_array('user', $scope)) {
533 $sql = "SELECT a.code, c.email, c.rowid";
534 $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
535 $sql .= " ".$this->db->prefix()."user as c,";
536 $sql .= " ".$this->db->prefix()."c_action_trigger as a";
537 $sql .= " WHERE n.fk_user = c.rowid";
538 $sql .= " AND a.rowid = n.fk_action";
539 $sql .= $sqlnotifcode;
540 $sql .= " AND c.entity IN (".getEntity('user').")";
541 if ($userid > 0) {
542 $sql .= " AND c.rowid = ".((int) $userid);
543 }
544
545 dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
546
547 $resql = $this->db->query($sql);
548 if ($resql) {
549 $num = $this->db->num_rows($resql);
550 $i = 0;
551 while ($i < $num) {
552 $obj = $this->db->fetch_object($resql);
553 if ($obj) {
554 $newval2 = trim($obj->email);
555 $isvalid = isValidEmail($newval2);
556 if (empty($resarray[$newval2])) {
557 $resarray[$newval2] = array('type' => 'touser', 'code' => trim($obj->code), 'emaildesc' => 'User id '.$obj->rowid, 'email' => $newval2, 'userid' => (int) $obj->rowid, 'isemailvalid' => $isvalid);
558 }
559 }
560 $i++;
561 }
562 } else {
563 $error++;
564 $this->error = $this->db->lasterror();
565 }
566 }
567 }
568
569 // Subscription global
570 if (!$error) {
571 if (in_array('global', $scope)) {
572 // List of notifications enabled for fixed email
573 foreach ($conf->global as $key => $val) {
574 if ($notifcode) {
575 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
576 continue;
577 }
578 } else {
579 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
580 continue;
581 }
582 }
583
584 $threshold = (float) $reg[1];
585 if ($valueforthreshold < $threshold) {
586 continue;
587 }
588
589 $tmpemail = explode(',', $val);
590 foreach ($tmpemail as $key2 => $val2) {
591 $newval2 = trim($val2);
592 if ($newval2 == '__SUPERVISOREMAIL__') {
593 $newval2 = $this->getSupervisorEmail();
594 }
595 if ($newval2 == '__AUTHOREMAIL__') {
596 $newval2 = $this->getAuthorEmail($object);
597 }
598 if ($newval2) {
599 $isvalid = isValidEmail($newval2, 0);
600 if (empty($resarray[$newval2])) {
601 $resarray[$newval2] = array('type' => 'tofixedemail', 'code' => trim($key), 'emaildesc' => trim($val2), 'email' => $newval2, 'isemailvalid' => $isvalid);
602 }
603 }
604 }
605 }
606 }
607 }
608
609 if ($error) {
610 return -1;
611 }
612
613 return $resarray;
614 }
615
627 public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
628 {
629 global $user, $conf, $langs, $mysoc;
630 global $hookmanager;
632 global $action;
633
634 // Complete the array Notify::$arrayofnotifsupported
635 if (!is_object($hookmanager)) {
636 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
637 $hookmanager = new HookManager($this->db);
638 }
639 $hookmanager->initHooks(array('notification'));
640
641 $parameters = array('notifcode' => $notifcode);
642 $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
643 if (empty($reshook)) {
644 if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
645 Notify::$arrayofnotifsupported = array_merge(Notify::$arrayofnotifsupported, $hookmanager->resArray['arrayofnotifsupported']);
646 }
647 }
648
649 // If the trigger code is not managed by the Notification module
650 if (!in_array($notifcode, Notify::$arrayofnotifsupported)) {
651 return 0;
652 }
653
654 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
655
656 dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object id=".$object->id);
657
658 $langs->load("other");
659
660 // Define $urlwithroot
661 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
662 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
663 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
664
665 // Define some vars
666 $application = constant('DOL_APPLICATION_TITLE');
667 $applicationcustom = getDolGlobalString('MAIN_APPLICATION_TITLE');
668 if ($applicationcustom) {
669 $application = (preg_match('/^\+/', $applicationcustom) ? $application : '').$applicationcustom;
670 }
671
672 $from = getDolGlobalString('NOTIFICATION_EMAIL_FROM');
673 if (empty($from)) {
674 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
675 }
676
677 $object_type = '';
678 $link = '';
679 $num = 0;
680 $error = 0;
681
682 $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
683 $newref = (empty($object->newref) ? $object->ref : $object->newref);
684
685 $sql = '';
686
687 // Check notification per third party @phan-suppress-next-line PhanUndeclaredProperty
688 if (!empty($object->socid) && $object->socid > 0) {
689 $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
690 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
691 $sql .= " FROM ".$this->db->prefix()."socpeople as c,";
692 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
693 $sql .= " ".$this->db->prefix()."notify_def as n,";
694 $sql .= " ".$this->db->prefix()."societe as s";
695 $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
696 $sql .= " AND n.fk_soc = s.rowid";
697 $sql .= " AND c.statut = 1";
698 if (is_numeric($notifcode)) {
699 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
700 } else {
701 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
702 }
703 // @phan-suppress-next-line PhanUndeclaredProperty
704 $sql .= " AND s.rowid = ".((int) $object->socid);
705
706 $sql .= "\nUNION\n";
707 }
708
709 // Check notification per user
710 $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
711 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
712 $sql .= " FROM ".$this->db->prefix()."user as c,";
713 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
714 $sql .= " ".$this->db->prefix()."notify_def as n";
715 $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
716 $sql .= " AND c.statut = 1";
717 if (is_numeric($notifcode)) {
718 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
719 } else {
720 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
721 }
722
723 // Check notification fixed
724 // TODO Move part found after, into a sql here
725
726
727 // Loop on all notifications enabled
728 $result = $this->db->query($sql);
729 if ($result) {
730 $num = $this->db->num_rows($result);
731 $projtitle = '';
732 if (is_object($object->project) || $object->fetchProject() > 0) {
733 $projtitle = '('.$object->project->title.')';
734 }
735
736 if ($num > 0) {
737 $i = 0;
738 while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
739 $obj = $this->db->fetch_object($result);
740
741 $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
742 $notifcodedefid = $obj->adid;
743 $trackid = '';
744 if ($obj->type_target == 'tocontactid') {
745 $trackid = 'ctc'.$obj->cid;
746 }
747 if ($obj->type_target == 'touserid') {
748 $trackid = 'use'.$obj->cid;
749 }
750
751 if (dol_strlen($obj->email)) {
752 // Set output language
753 $outputlangs = $langs;
754 if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
755 $outputlangs = new Translate('', $conf);
756 $outputlangs->setDefaultLang($obj->default_lang);
757 $outputlangs->loadLangs(array("main", "other"));
758 }
759
760 $appli = $mysoc->name;
761
762 $subject = '['.$appli.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
763
764 switch ($notifcode) {
765 case 'BILL_CANCEL':
766 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
767 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
768 $object_type = 'facture';
769 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceCanceled", $link);
770 break;
771 case 'BILL_VALIDATE':
772 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
773 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
774 $object_type = 'facture';
775 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
776 break;
777 case 'BILL_PAYED':
778 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
779 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
780 $object_type = 'facture';
781 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
782 break;
783 case 'ORDER_CANCEL':
784 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
785 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
786 $object_type = 'order';
787 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderCanceled", $link);
788 break;
789 case 'ORDER_VALIDATE':
790 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
791 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
792 $object_type = 'order';
793 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
794 break;
795 case 'ORDER_CLOSE':
796 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
797 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
798 $object_type = 'order';
799 $labeltouse = getDolGlobalString('ORDER_CLOSE_TEMPLATE');
800 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderClose", $link);
801 break;
802 case 'PROPAL_VALIDATE':
803 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
804 $dir_output = $conf->propal->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
805 $object_type = 'propal';
806 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
807 break;
808 case 'PROPAL_CLOSE_REFUSED':
809 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
810 $dir_output = $conf->propal->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
811 $object_type = 'propal';
812 $labeltouse = getDolGlobalString('PROPAL_CLOSE_REFUSED_TEMPLATE');
813 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedRefused", $link);
814 if (!empty($object->context['closedfromonlinesignature'])) {
815 $mesg .= ' - From online page';
816 }
817 break;
818 case 'PROPAL_CLOSE_SIGNED':
819 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
820 $dir_output = $conf->propal->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
821 $object_type = 'propal';
822 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
823 if (!empty($object->context['closedfromonlinesignature'])) {
824 $mesg .= ' - From online page';
825 }
826 break;
827 case 'FICHINTER_ADD_CONTACT':
828 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
829 $dir_output = $conf->ficheinter->dir_output;
830 $object_type = 'fichinter';
831 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
832 break;
833 case 'FICHINTER_VALIDATE':
834 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
835 $dir_output = $conf->ficheinter->dir_output;
836 $object_type = 'fichinter';
837 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
838 break;
839 case 'FICHINTER_MODIFY':
840 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
841 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
842 $dir_output = $conf->ficheinter->dir_output;
843 $object_type = 'fichinter';
844 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionModified", $link, $context_info);
845 break;
846 case 'FICHINTER_CLOSE':
847 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
848 $dir_output = $conf->ficheinter->dir_output;
849 $object_type = 'fichinter';
850 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
851 break;
852 case 'ORDER_SUPPLIER_VALIDATE':
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 ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
855 $object_type = 'order_supplier';
856 $labeltouse = getDolGlobalString('ORDER_SUPPLIER_VALIDATE_TEMPLATE');
857 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
858 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($outputlangs));
859 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
860 break;
861 case 'ORDER_SUPPLIER_CANCEL':
862 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
863 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
864 $object_type = 'order_supplier';
865 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
866 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($outputlangs));
867 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
868 break;
869 case 'ORDER_SUPPLIER_APPROVE':
870 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
871 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
872 $object_type = 'order_supplier';
873 $labeltouse = getDolGlobalString('ORDER_SUPPLIER_APPROVE_TEMPLATE');
874 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
875 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($outputlangs));
876 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
877 break;
878 case 'ORDER_SUPPLIER_SUBMIT':
879 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
880 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
881 $object_type = 'order_supplier';
882 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
883 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($outputlangs));
884 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
885 break;
886 case 'ORDER_SUPPLIER_REFUSE':
887 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
888 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
889 $object_type = 'order_supplier';
890 $labeltouse = getDolGlobalString('ORDER_SUPPLIER_REFUSE_TEMPLATE');
891 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
892 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($outputlangs));
893 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
894 break;
895 case 'SHIPPING_MODIFY':
896 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
897 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
898 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
899 $object_type = 'shipping';
900 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionModified", $link, $context_info);
901 break;
902 case 'SHIPPING_VALIDATE':
903 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
904 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
905 $object_type = 'shipping';
906 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
907 break;
908 case 'EXPENSE_REPORT_VALIDATE':
909 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
910 $dir_output = $conf->expensereport->dir_output;
911 $object_type = 'expensereport';
912 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
913 break;
914 case 'EXPENSE_REPORT_APPROVE':
915 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
916 $dir_output = $conf->expensereport->dir_output;
917 $object_type = 'expensereport';
918 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
919 break;
920 case 'HOLIDAY_VALIDATE':
921 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
922 $dir_output = $conf->holiday->dir_output;
923 $object_type = 'holiday';
924 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
925 break;
926 case 'HOLIDAY_APPROVE':
927 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
928 $dir_output = $conf->holiday->dir_output;
929 $object_type = 'holiday';
930 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
931 break;
932 case 'ACTION_CREATE':
933 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
934 $dir_output = $conf->agenda->dir_output;
935 $object_type = 'action';
936 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link);
937 break;
938 case 'CONTRACT_MODIFY':
939 $link = '<a href="'.$urlwithroot.'/contrat/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
940 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
941 $dir_output = $conf->contract->multidir_output;
942 $object_type = 'contract';
943 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info);
944 break;
945 default:
946 $object_type = $object->element;
947 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
948 $template = $notifcode.'_TEMPLATE';
949 $mesg = $outputlangs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref.' '.$dir_output;
950 break;
951 }
952
953 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
954 $formmail = new FormMail($this->db);
955 $arraydefaultmessage = null;
956
957 $template = $notifcode.'_TEMPLATE';
958 $labeltouse = getDolGlobalString($template);
959 if (!empty($labeltouse)) {
960 $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $object_type, $user, $outputlangs, 0, 1, $labeltouse);
961 }
962 if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
963 if (method_exists($object, 'fetch_thirdparty') && empty($object->thirdparty)) {
964 $object->fetch_thirdparty();
965 }
966 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
967 complete_substitutions_array($substitutionarray, $outputlangs, $object);
968 // Note the substitution array should contains __REF__, __NEWREF__ ....
969 $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
970 $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
971 } else {
972 $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
973 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
974 $message .= "\n";
975 $message .= $mesg;
976 }
977
978 $ref = dol_sanitizeFileName($newref);
979 $pdf_path = $dir_output."/".$ref.".pdf";
980 if (!dol_is_file($pdf_path) || (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) {
981 // We can't add PDF as it is not generated yet.
982 $filepdf = '';
983 } else {
984 $filepdf = $pdf_path;
985 $filename_list[] = $filepdf;
986 $mimetype_list[] = mime_content_type($filepdf);
987 $mimefilename_list[] = $ref.".pdf";
988 }
989
990 $labeltouse = !empty($labeltouse) ? $labeltouse : '';
991
992 $sendto = $this->replaceSpecialRecipientToken($sendto, '__SUPERVISOREMAIL__', $this->getSupervisorEmail(1));
993 $sendto = $this->replaceSpecialRecipientToken($sendto, '__AUTHOREMAIL__', $this->getAuthorEmail($object, 1));
994
995 $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'outputlangs' => $outputlangs, 'labeltouse' => $labeltouse);
996 if (!isset($action)) {
997 $action = '';
998 }
999
1000 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1001 if (empty($reshook)) {
1002 if (!empty($hookmanager->resArray['files'])) {
1003 $filename_list = $hookmanager->resArray['files']['file'];
1004 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
1005 $mimefilename_list = $hookmanager->resArray['files']['filename'];
1006 }
1007 if (!empty($hookmanager->resArray['subject'])) {
1008 $subject .= $hookmanager->resArray['subject'];
1009 }
1010 if (!empty($hookmanager->resArray['message'])) {
1011 $message .= $hookmanager->resArray['message'];
1012 }
1013 }
1014
1015 $mailfile = new CMailFile(
1016 $subject,
1017 $sendto,
1018 $from,
1019 $message,
1020 $filename_list,
1021 $mimetype_list,
1022 $mimefilename_list,
1023 '',
1024 '',
1025 0,
1026 -1,
1027 '',
1028 '',
1029 $trackid,
1030 '',
1031 'notification'
1032 );
1033
1034 if (! empty($mailfile->error) || ! empty($mailfile->errors)) {
1035 $this->error = $mailfile->error;
1036 $this->errors = $mailfile->errors;
1037 return -1;
1038 }
1039
1040 if ($mailfile->sendfile()) {
1041 if ($obj->type_target == 'touserid') {
1042 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
1043 // @phan-suppress-next-line PhanUndeclaredProperty
1044 $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)."')";
1045 } else {
1046 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
1047 // @phan-suppress-next-line PhanUndeclaredProperty
1048 $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)."')";
1049 }
1050 if (!$this->db->query($sql)) {
1051 dol_print_error($this->db);
1052 }
1053 } else {
1054 $error++;
1055 $this->errors[] = $mailfile->error;
1056 }
1057 } else {
1058 dol_syslog("No notification sent for ".$sendto." because email is empty");
1059 }
1060 $i++;
1061 }
1062 } else {
1063 // @phan-suppress-next-line PhanUndeclaredProperty
1064 dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
1065 }
1066 } else {
1067 $error++;
1068 $this->errors[] = $this->db->lasterror();
1069 dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
1070 return -1;
1071 }
1072
1073 // Check notification using fixed email
1074 // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and include the case into previous loop of sql result
1075 if (!$error) {
1076 foreach ($conf->global as $key => $val) {
1077 $reg = array();
1078 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
1079 continue;
1080 }
1081
1082 $sendto = $val;
1083
1084 $threshold = (float) $reg[1];
1085 if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
1086 dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
1087 continue;
1088 }
1089
1090 $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
1091 if ($notifcodedefid <= 0) {
1092 dol_print_error($this->db, 'Failed to get id from code');
1093 }
1094 $trackid = '';
1095
1096 $object_type = '';
1097 $link = '';
1098 $num++;
1099
1100 $appli = $mysoc->name;
1101
1102 $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
1103
1104 switch ($notifcode) {
1105 case 'BILL_VALIDATE':
1106 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1107 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1108 $object_type = 'facture';
1109 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
1110 break;
1111 case 'BILL_PAYED':
1112 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1113 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1114 $object_type = 'facture';
1115 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
1116 break;
1117 case 'ORDER_VALIDATE':
1118 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1119 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1120 $object_type = 'order';
1121 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
1122 break;
1123 case 'ORDER_CLOSE':
1124 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1125 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1126 $object_type = 'order';
1127 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderClose", $link);
1128 break;
1129 case 'PROPAL_VALIDATE':
1130 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1131 $dir_output = $conf->propal->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1132 $object_type = 'propal';
1133 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
1134 break;
1135 case 'PROPAL_CLOSE_SIGNED':
1136 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1137 $dir_output = $conf->propal->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1138 $object_type = 'propal';
1139 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
1140 break;
1141 case 'FICHINTER_ADD_CONTACT':
1142 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1143 $dir_output = $conf->ficheinter->dir_output;
1144 $object_type = 'fichinter';
1145 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
1146 break;
1147 case 'FICHINTER_VALIDATE':
1148 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1149 $dir_output = $conf->facture->dir_output;
1150 $object_type = 'fichinter';
1151 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
1152 break;
1153 case 'FICHINTER_MODIFY':
1154 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1155 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1156 $dir_output = $conf->ficheinter->dir_output;
1157 $object_type = 'fichinter';
1158 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionModified", $link, $context_info);
1159 break;
1160 case 'FICHINTER_CLOSE':
1161 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1162 $dir_output = $conf->facture->dir_output;
1163 $object_type = 'fichinter';
1164 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
1165 break;
1166 case 'ORDER_SUPPLIER_CANCEL':
1167 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1168 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
1169 $object_type = 'order_supplier';
1170 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1171 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($langs));
1172 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1173 break;
1174 case 'ORDER_SUPPLIER_VALIDATE':
1175 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1176 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
1177 $object_type = 'order_supplier';
1178 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1179 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs));
1180 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1181 break;
1182 case 'ORDER_SUPPLIER_APPROVE':
1183 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1184 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
1185 $object_type = 'order_supplier';
1186 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1187 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs));
1188 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1189 break;
1190 case 'ORDER_SUPPLIER_SUBMIT':
1191 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1192 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
1193 $object_type = 'order_supplier';
1194 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1195 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs));
1196 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1197 break;
1198 case 'ORDER_SUPPLIER_REFUSE':
1199 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1200 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity ?? $conf->entity]."/".get_exdir(0, 0, 0, 1, $object);
1201 $object_type = 'order_supplier';
1202 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1203 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs));
1204 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1205 break;
1206 case 'SHIPPING_MODIFY':
1207 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1208 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1209 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
1210 $object_type = 'order_supplier';
1211 $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionModified", $link, $context_info);
1212 break;
1213 case 'SHIPPING_VALIDATE':
1214 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1215 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
1216 $object_type = 'order_supplier';
1217 $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
1218 break;
1219 case 'EXPENSE_REPORT_VALIDATE':
1220 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1221 $dir_output = $conf->expensereport->dir_output;
1222 $object_type = 'expensereport';
1223 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
1224 break;
1225 case 'EXPENSE_REPORT_APPROVE':
1226 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1227 $dir_output = $conf->expensereport->dir_output;
1228 $object_type = 'expensereport';
1229 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
1230 break;
1231 case 'HOLIDAY_VALIDATE':
1232 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1233 $dir_output = $conf->holiday->dir_output;
1234 $object_type = 'holiday';
1235 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
1236 break;
1237 case 'HOLIDAY_APPROVE':
1238 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1239 $dir_output = $conf->holiday->dir_output;
1240 $object_type = 'holiday';
1241 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
1242 break;
1243 case 'ACTION_CREATE':
1244 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1245 $dir_output = $conf->agenda->dir_output;
1246 $object_type = 'action';
1247 $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link);
1248 break;
1249 case 'CONTRACT_MODIFY':
1250 $link = '<a href="'.$urlwithroot.'/contrat/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1251 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1252 $dir_output = $conf->contract->multidir_output;
1253 $object_type = 'contrat';
1254 $mesg = $langs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info);
1255 break;
1256 default:
1257 $object_type = $object->element;
1258 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
1259 $mesg = $langs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref;
1260 break;
1261 }
1262 $ref = dol_sanitizeFileName($newref);
1263 $pdf_path = $dir_output."/".$ref.".pdf";
1264 if (!dol_is_file($pdf_path)) {
1265 // We can't add PDF as it is not generated yet.
1266 $filepdf = '';
1267 } else {
1268 $filepdf = $pdf_path;
1269 $filename_list[] = $pdf_path;
1270 $mimetype_list[] = mime_content_type($filepdf);
1271 $mimefilename_list[] = $ref.".pdf";
1272 }
1273
1274 // Set output language
1275 $outputlangs = $langs;
1276
1277 // if an e-mail template is configured for this notification code (for instance 'SHIPPING_VALIDATE_TEMPLATE', ...),
1278 // we fetch this template by its label. Otherwise, a default message content will be sent.
1279 $mailTemplateLabel = getDolGlobalString($notifcode.'_TEMPLATE');
1280 $emailTemplate = null;
1281 if (!empty($mailTemplateLabel)) {
1282 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
1283 $formmail = new FormMail($this->db);
1284 $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type, $user, $outputlangs, 0, 1, $mailTemplateLabel);
1285 }
1286 if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) {
1287 if (property_exists($object, 'thirdparty')) {
1288 if (!($object->thirdparty instanceof Societe)) {
1289 $object->fetch_thirdparty();
1290 }
1291
1292 if ($object->thirdparty instanceof Societe && $object->thirdparty->default_lang && $object->thirdparty->default_lang != $langs->defaultlang) {
1293 $outputlangs = new Translate('', $conf);
1294 $outputlangs->setDefaultLang($object->thirdparty->default_lang);
1295 $outputlangs->loadLangs(array('main', 'other'));
1296 }
1297 }
1298
1299 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
1300 complete_substitutions_array($substitutionarray, $outputlangs, $object);
1301 $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $outputlangs);
1302 $message = make_substitutions($emailTemplate->content, $substitutionarray, $outputlangs);
1303 } else {
1304 $message = '';
1305 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
1306 $message .= "\n";
1307 $message .= $mesg;
1308
1309 $message = nl2br($message);
1310 }
1311
1312 $sendto = $this->replaceSpecialRecipientToken($sendto, '__SUPERVISOREMAIL__', $this->getSupervisorEmail(1));
1313 $sendto = $this->replaceSpecialRecipientToken($sendto, '__AUTHOREMAIL__', $this->getAuthorEmail($object, 1));
1314
1315 if ($sendto) {
1316 $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'subject' => &$subject, 'message' => &$message);
1317 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1318 if (empty($reshook)) {
1319 if (!empty($hookmanager->resArray['files'])) {
1320 $filename_list = $hookmanager->resArray['files']['file'];
1321 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
1322 $mimefilename_list = $hookmanager->resArray['files']['filename'];
1323 }
1324 if (!empty($hookmanager->resArray['subject'])) {
1325 $subject .= $hookmanager->resArray['subject'];
1326 }
1327 if (!empty($hookmanager->resArray['message'])) {
1328 $message .= $hookmanager->resArray['message'];
1329 }
1330 }
1331 $mailfile = new CMailFile(
1332 $subject,
1333 $sendto,
1334 $from,
1335 $message,
1336 $filename_list,
1337 $mimetype_list,
1338 $mimefilename_list,
1339 '',
1340 '',
1341 0,
1342 1,
1343 '',
1344 $trackid,
1345 '',
1346 '',
1347 'notification'
1348 );
1349
1350 if (! empty($mailfile->error) || ! empty($mailfile->errors)) {
1351 $this->error = $mailfile->error;
1352 $this->errors = $mailfile->errors;
1353 return -1;
1354 }
1355
1356 if ($mailfile->sendfile()) {
1357 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
1358 // @phan-suppress-next-line PhanUndeclaredProperty
1359 $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".((int) $notifcodedefid).", ".((!empty($object->socid) && $object->socid > 0) ? ((int) $object->socid) : 'null').", null, 'email', 'tofixedemail', '".$this->db->escape($object_type)."', ".((int) $object->id).", '".$this->db->escape($sendto)."')";
1360 if (!$this->db->query($sql)) {
1361 dol_print_error($this->db);
1362 }
1363 } else {
1364 $error++;
1365 $this->errors[] = $mailfile->error;
1366 }
1367 }
1368 }
1369 }
1370
1371 if (!$error) {
1372 return $num;
1373 } else {
1374 return -1 * $error;
1375 }
1376 }
1377
1384 private function getSupervisorEmail($withLabel = 0)
1385 {
1386 global $user;
1387
1388 if (empty($user->fk_user) || $user->fk_user <= 0) {
1389 return '';
1390 }
1391
1392 $supervisoruser = new User($this->db);
1393 $supervisoruser->fetch($user->fk_user);
1394 if (empty($supervisoruser->email)) {
1395 return '';
1396 }
1397
1398 if ($withLabel) {
1399 return trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
1400 }
1401
1402 return trim((string) $supervisoruser->email);
1403 }
1404
1412 private function getAuthorEmail($object, $withLabel = 0)
1413 {
1414 if (!is_object($object)) {
1415 return '';
1416 }
1417
1418 $author = null;
1419
1420 // @phan-suppress-next-line PhanUndeclaredProperty
1421 if (isset($object->user_author) && is_object($object->user_author) && !empty($object->user_author->email)) {
1422 // @phan-suppress-next-line PhanUndeclaredProperty
1423 $author = $object->user_author;
1424 } else {
1425 $authorid = 0;
1426 foreach (array('fk_user_author', 'user_author_id', 'fk_user_creat') as $fieldname) {
1427 if (!empty($object->$fieldname)) {
1428 // @phan-suppress-next-line PhanUndeclaredProperty
1429 $authorid = (int) $object->$fieldname;
1430 break;
1431 }
1432 }
1433 if ($authorid > 0) {
1434 $author = new User($this->db);
1435 $author->fetch($authorid);
1436 }
1437 }
1438
1439 if (empty($author) || empty($author->email)) {
1440 return '';
1441 }
1442
1443 if ($withLabel) {
1444 return trim(dolGetFirstLastname($author->firstname, $author->lastname).' <'.$author->email.'>');
1445 }
1446
1447 return trim((string) $author->email);
1448 }
1449
1458 private function replaceSpecialRecipientToken($sendto, $token, $replacement)
1459 {
1460 if (!preg_match('/'.preg_quote($token, '/').'/', $sendto)) {
1461 return $sendto;
1462 }
1463
1464 dol_syslog("Replace the ".$token." key into recipient email string with ".$replacement);
1465 $sendto = preg_replace('/'.preg_quote($token, '/').'/', $replacement, $sendto);
1466 $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
1467 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
1468 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
1469
1470 return $sendto;
1471 }
1472}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
global $dolibarr_main_url_root
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
Class to manage hooks.
Class to manage the table of subscription to notifications.
replaceSpecialRecipientToken($sendto, $token, $replacement)
Replace a special token inside recipient list.
fetch($id, $socid=0, $type='email')
Load record from 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)
getSupervisorEmail($withLabel=0)
Return supervisor email.
getAuthorEmail($object, $withLabel=0)
Return author email of object.
send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
Check if notification are active for couple action/company.
update($user=null, $notrigger=0)
Update record in database.
create($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.
global $mysoc
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
getDolGlobalString($key, $default='')
Return a 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.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130