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