dolibarr 21.0.0-beta
notify.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
6 * Copyright (C) 2021 Thibault FOUCART <support@ptibogxiv.net>
7 * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
8 * Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
9 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
10 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11 * 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 = 'Dolibarr';
666 if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
667 $application = getDolGlobalString('MAIN_APPLICATION_TITLE');
668 }
669 $from = getDolGlobalString('NOTIFICATION_EMAIL_FROM');
670 $object_type = '';
671 $link = '';
672 $num = 0;
673 $error = 0;
674
675 $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
676 $newref = (empty($object->newref) ? $object->ref : $object->newref);
677
678 $sql = '';
679
680 // Check notification per third party
681 if (!empty($object->socid) && $object->socid > 0) {
682 $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
683 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
684 $sql .= " FROM ".$this->db->prefix()."socpeople as c,";
685 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
686 $sql .= " ".$this->db->prefix()."notify_def as n,";
687 $sql .= " ".$this->db->prefix()."societe as s";
688 $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
689 $sql .= " AND n.fk_soc = s.rowid";
690 $sql .= " AND c.statut = 1";
691 if (is_numeric($notifcode)) {
692 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
693 } else {
694 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
695 }
696 $sql .= " AND s.rowid = ".((int) $object->socid);
697
698 $sql .= "\nUNION\n";
699 }
700
701 // Check notification per user
702 $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
703 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
704 $sql .= " FROM ".$this->db->prefix()."user as c,";
705 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
706 $sql .= " ".$this->db->prefix()."notify_def as n";
707 $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
708 $sql .= " AND c.statut = 1";
709 if (is_numeric($notifcode)) {
710 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
711 } else {
712 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
713 }
714
715 // Check notification fixed
716 // TODO Move part found after, into a sql here
717
718
719 // Loop on all notifications enabled
720 $result = $this->db->query($sql);
721 if ($result) {
722 $num = $this->db->num_rows($result);
723 $projtitle = '';
724 if (is_object($object->project) || $object->fetchProject() > 0) {
725 $projtitle = '('.$object->project->title.')';
726 }
727
728 if ($num > 0) {
729 $i = 0;
730 while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
731 $obj = $this->db->fetch_object($result);
732
733 $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
734 $notifcodedefid = $obj->adid;
735 $trackid = '';
736 if ($obj->type_target == 'tocontactid') {
737 $trackid = 'ctc'.$obj->cid;
738 }
739 if ($obj->type_target == 'touserid') {
740 $trackid = 'use'.$obj->cid;
741 }
742
743 if (dol_strlen($obj->email)) {
744 // Set output language
745 $outputlangs = $langs;
746 if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
747 $outputlangs = new Translate('', $conf);
748 $outputlangs->setDefaultLang($obj->default_lang);
749 $outputlangs->loadLangs(array("main", "other"));
750 }
751
752 $appli = $mysoc->name;
753
754 $subject = '['.$appli.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
755
756 switch ($notifcode) {
757 case 'BILL_CANCEL':
758 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
759 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
760 $object_type = 'facture';
761 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceCanceled", $link);
762 break;
763 case 'BILL_VALIDATE':
764 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
765 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
766 $object_type = 'facture';
767 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
768 break;
769 case 'BILL_PAYED':
770 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
771 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
772 $object_type = 'facture';
773 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
774 break;
775 case 'ORDER_CANCEL':
776 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
777 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
778 $object_type = 'order';
779 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderCanceled", $link);
780 break;
781 case 'ORDER_VALIDATE':
782 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
783 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
784 $object_type = 'order';
785 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
786 break;
787 case 'ORDER_CLOSE':
788 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
789 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
790 $object_type = 'order';
791 $labeltouse = getDolGlobalString('ORDER_CLOSE_TEMPLATE');
792 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderClose", $link);
793 break;
794 case 'PROPAL_VALIDATE':
795 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
796 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
797 $object_type = 'propal';
798 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
799 break;
800 case 'PROPAL_CLOSE_REFUSED':
801 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
802 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
803 $object_type = 'propal';
804 $labeltouse = getDolGlobalString('PROPAL_CLOSE_REFUSED_TEMPLATE');
805 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedRefused", $link);
806 if (!empty($object->context['closedfromonlinesignature'])) {
807 $mesg .= ' - From online page';
808 }
809 break;
810 case 'PROPAL_CLOSE_SIGNED':
811 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
812 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
813 $object_type = 'propal';
814 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
815 if (!empty($object->context['closedfromonlinesignature'])) {
816 $mesg .= ' - From online page';
817 }
818 break;
819 case 'FICHINTER_ADD_CONTACT':
820 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
821 $dir_output = $conf->ficheinter->dir_output;
822 $object_type = 'ficheinter';
823 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
824 break;
825 case 'FICHINTER_VALIDATE':
826 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
827 $dir_output = $conf->ficheinter->dir_output;
828 $object_type = 'ficheinter';
829 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
830 break;
831 case 'FICHINTER_MODIFY':
832 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
833 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
834 $dir_output = $conf->ficheinter->dir_output;
835 $object_type = 'ficheinter';
836 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionModified", $link, $context_info);
837 break;
838 case 'FICHINTER_CLOSE':
839 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
840 $dir_output = $conf->ficheinter->dir_output;
841 $object_type = 'ficheinter';
842 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
843 break;
844 case 'ORDER_SUPPLIER_VALIDATE':
845 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
846 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
847 $object_type = 'order_supplier';
848 $labeltouse = isset($conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE : '';
849 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
850 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($outputlangs));
851 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
852 break;
853 case 'ORDER_SUPPLIER_CANCEL':
854 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
855 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
856 $object_type = 'order_supplier';
857 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
858 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($outputlangs));
859 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
860 break;
861 case 'ORDER_SUPPLIER_APPROVE':
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]."/".get_exdir(0, 0, 0, 1, $object);
864 $object_type = 'order_supplier';
865 $labeltouse = isset($conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE : '';
866 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
867 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($outputlangs));
868 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
869 break;
870 case 'ORDER_SUPPLIER_SUBMIT':
871 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
872 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
873 $object_type = 'order_supplier';
874 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
875 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($outputlangs));
876 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
877 break;
878 case 'ORDER_SUPPLIER_REFUSE':
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]."/".get_exdir(0, 0, 0, 1, $object);
881 $object_type = 'order_supplier';
882 $labeltouse = isset($conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE : '';
883 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
884 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($outputlangs));
885 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
886 break;
887 case 'SHIPPING_MODIFY':
888 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
889 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
890 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
891 $object_type = 'shipping';
892 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionModified", $link, $context_info);
893 break;
894 case 'SHIPPING_VALIDATE':
895 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
896 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
897 $object_type = 'shipping';
898 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
899 break;
900 case 'EXPENSE_REPORT_VALIDATE':
901 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
902 $dir_output = $conf->expensereport->dir_output;
903 $object_type = 'expensereport';
904 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
905 break;
906 case 'EXPENSE_REPORT_APPROVE':
907 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
908 $dir_output = $conf->expensereport->dir_output;
909 $object_type = 'expensereport';
910 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
911 break;
912 case 'HOLIDAY_VALIDATE':
913 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
914 $dir_output = $conf->holiday->dir_output;
915 $object_type = 'holiday';
916 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
917 break;
918 case 'HOLIDAY_APPROVE':
919 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
920 $dir_output = $conf->holiday->dir_output;
921 $object_type = 'holiday';
922 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
923 break;
924 case 'ACTION_CREATE':
925 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
926 $dir_output = $conf->agenda->dir_output;
927 $object_type = 'action';
928 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link);
929 break;
930 case 'CONTRACT_MODIFY':
931 $link = '<a href="'.$urlwithroot.'/contrat/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
932 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
933 $dir_output = $conf->contract->multidir_output;
934 $object_type = 'contract';
935 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info);
936 break;
937 default:
938 $object_type = $object->element;
939 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
940 $template = $notifcode.'_TEMPLATE';
941 $mesg = $outputlangs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref.' '.$dir_output;
942 break;
943 }
944
945 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
946 $formmail = new FormMail($this->db);
947 $arraydefaultmessage = null;
948
949 $template = $notifcode.'_TEMPLATE';
950 $labeltouse = getDolGlobalString($template);
951 if (!empty($labeltouse)) {
952 $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $labeltouse);
953 }
954 if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
955 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
956 complete_substitutions_array($substitutionarray, $outputlangs, $object);
957 $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
958 $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
959 } else {
960 $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
961 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
962 $message .= "\n";
963 $message .= $mesg;
964 }
965
966 $ref = dol_sanitizeFileName($newref);
967 $pdf_path = $dir_output."/".$ref.".pdf";
968 if (!dol_is_file($pdf_path) || (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) {
969 // We can't add PDF as it is not generated yet.
970 $filepdf = '';
971 } else {
972 $filepdf = $pdf_path;
973 $filename_list[] = $filepdf;
974 $mimetype_list[] = mime_content_type($filepdf);
975 $mimefilename_list[] = $ref.".pdf";
976 }
977
978 $labeltouse = !empty($labeltouse) ? $labeltouse : '';
979
980 // Replace keyword __SUPERVISOREMAIL__
981 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
982 $newval = '';
983 if ($user->fk_user > 0) {
984 $supervisoruser = new User($this->db);
985 $supervisoruser->fetch($user->fk_user);
986 if ($supervisoruser->email) {
987 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
988 }
989 }
990 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
991 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
992 $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
993 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
994 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
995 }
996
997 $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'outputlangs' => $outputlangs, 'labeltouse' => $labeltouse);
998 if (!isset($action)) {
999 $action = '';
1000 }
1001
1002 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1003 if (empty($reshook)) {
1004 if (!empty($hookmanager->resArray['files'])) {
1005 $filename_list = $hookmanager->resArray['files']['file'];
1006 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
1007 $mimefilename_list = $hookmanager->resArray['files']['filename'];
1008 }
1009 if (!empty($hookmanager->resArray['subject'])) {
1010 $subject .= $hookmanager->resArray['subject'];
1011 }
1012 if (!empty($hookmanager->resArray['message'])) {
1013 $message .= $hookmanager->resArray['message'];
1014 }
1015 }
1016
1017 $mailfile = new CMailFile(
1018 $subject,
1019 $sendto,
1020 $from,
1021 $message,
1022 $filename_list,
1023 $mimetype_list,
1024 $mimefilename_list,
1025 '',
1026 '',
1027 0,
1028 -1,
1029 '',
1030 '',
1031 $trackid,
1032 '',
1033 'notification'
1034 );
1035
1036 if ($mailfile->sendfile()) {
1037 if ($obj->type_target == 'touserid') {
1038 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
1039 $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)."')";
1040 } else {
1041 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
1042 $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)."')";
1043 }
1044 if (!$this->db->query($sql)) {
1045 dol_print_error($this->db);
1046 }
1047 } else {
1048 $error++;
1049 $this->errors[] = $mailfile->error;
1050 }
1051 } else {
1052 dol_syslog("No notification sent for ".$sendto." because email is empty");
1053 }
1054 $i++;
1055 }
1056 } else {
1057 dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
1058 }
1059 } else {
1060 $error++;
1061 $this->errors[] = $this->db->lasterror();
1062 dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
1063 return -1;
1064 }
1065
1066 // Check notification using fixed email
1067 // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and inclulde the case into previous loop of sql result
1068 if (!$error) {
1069 foreach ($conf->global as $key => $val) {
1070 $reg = array();
1071 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
1072 continue;
1073 }
1074
1075 $sendto = $val;
1076
1077 $threshold = (float) $reg[1];
1078 if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
1079 dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
1080 continue;
1081 }
1082
1083 $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
1084 if ($notifcodedefid <= 0) {
1085 dol_print_error($this->db, 'Failed to get id from code');
1086 }
1087 $trackid = '';
1088
1089 $object_type = '';
1090 $link = '';
1091 $num++;
1092
1093 $appli = $mysoc->name;
1094
1095 $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
1096
1097 switch ($notifcode) {
1098 case 'BILL_VALIDATE':
1099 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1100 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1101 $object_type = 'facture';
1102 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
1103 break;
1104 case 'BILL_PAYED':
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("EMailTextInvoicePayed", $link);
1109 break;
1110 case 'ORDER_VALIDATE':
1111 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1112 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1113 $object_type = 'order';
1114 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
1115 break;
1116 case 'ORDER_CLOSE':
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("EMailTextOrderClose", $link);
1121 break;
1122 case 'PROPAL_VALIDATE':
1123 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1124 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1125 $object_type = 'propal';
1126 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
1127 break;
1128 case 'PROPAL_CLOSE_SIGNED':
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("EMailTextProposalClosedSigned", $link);
1133 break;
1134 case 'FICHINTER_ADD_CONTACT':
1135 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1136 $dir_output = $conf->ficheinter->dir_output;
1137 $object_type = 'ficheinter';
1138 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
1139 break;
1140 case 'FICHINTER_VALIDATE':
1141 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1142 $dir_output = $conf->facture->dir_output;
1143 $object_type = 'ficheinter';
1144 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
1145 break;
1146 case 'FICHINTER_MODIFY':
1147 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1148 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1149 $dir_output = $conf->ficheinter->dir_output;
1150 $object_type = 'ficheinter';
1151 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionModified", $link, $context_info);
1152 break;
1153 case 'FICHINTER_CLOSE':
1154 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1155 $dir_output = $conf->facture->dir_output;
1156 $object_type = 'ficheinter';
1157 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
1158 break;
1159 case 'ORDER_SUPPLIER_CANCEL':
1160 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1161 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1162 $object_type = 'order_supplier';
1163 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1164 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($langs));
1165 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1166 break;
1167 case 'ORDER_SUPPLIER_VALIDATE':
1168 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1169 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1170 $object_type = 'order_supplier';
1171 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1172 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs));
1173 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1174 break;
1175 case 'ORDER_SUPPLIER_APPROVE':
1176 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1177 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1178 $object_type = 'order_supplier';
1179 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1180 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs));
1181 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1182 break;
1183 case 'ORDER_SUPPLIER_SUBMIT':
1184 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1185 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1186 $object_type = 'order_supplier';
1187 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1188 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs));
1189 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1190 break;
1191 case 'ORDER_SUPPLIER_REFUSE':
1192 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1193 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1194 $object_type = 'order_supplier';
1195 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1196 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs));
1197 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1198 break;
1199 case 'SHIPPING_MODIFY':
1200 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1201 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1202 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
1203 $object_type = 'order_supplier';
1204 $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionModified", $link, $context_info);
1205 break;
1206 case 'SHIPPING_VALIDATE':
1207 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
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("EMailTextExpeditionValidated", $link);
1211 break;
1212 case 'EXPENSE_REPORT_VALIDATE':
1213 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1214 $dir_output = $conf->expensereport->dir_output;
1215 $object_type = 'expensereport';
1216 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
1217 break;
1218 case 'EXPENSE_REPORT_APPROVE':
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("EMailTextExpenseReportApproved", $link);
1223 break;
1224 case 'HOLIDAY_VALIDATE':
1225 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1226 $dir_output = $conf->holiday->dir_output;
1227 $object_type = 'holiday';
1228 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
1229 break;
1230 case 'HOLIDAY_APPROVE':
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("EMailTextHolidayApproved", $link);
1235 break;
1236 case 'ACTION_CREATE':
1237 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1238 $dir_output = $conf->agenda->dir_output;
1239 $object_type = 'action';
1240 $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link);
1241 break;
1242 case 'CONTRACT_MODIFY':
1243 $link = '<a href="'.$urlwithroot.'/contrat/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1244 $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : '';
1245 $dir_output = $conf->contract->multidir_output;
1246 $object_type = 'contrat';
1247 $mesg = $langs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info);
1248 break;
1249 default:
1250 $object_type = $object->element;
1251 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
1252 $mesg = $langs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref;
1253 break;
1254 }
1255 $ref = dol_sanitizeFileName($newref);
1256 $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
1257 if (!dol_is_file($pdf_path)) {
1258 // We can't add PDF as it is not generated yet.
1259 $filepdf = '';
1260 } else {
1261 $filepdf = $pdf_path;
1262 $filename_list[] = $pdf_path;
1263 $mimetype_list[] = mime_content_type($filepdf);
1264 $mimefilename_list[] = $ref.".pdf";
1265 }
1266
1267 // Set output language
1268 $outputlangs = $langs;
1269
1270 // if an e-mail template is configured for this notification code (for instance 'SHIPPING_VALIDATE_TEMPLATE', ...),
1271 // we fetch this template by its label. Otherwise, a default message content will be sent.
1272 $mailTemplateLabel = getDolGlobalString($notifcode.'_TEMPLATE');
1273 $emailTemplate = null;
1274 if (!empty($mailTemplateLabel)) {
1275 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
1276 $formmail = new FormMail($this->db);
1277 $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $mailTemplateLabel);
1278 }
1279 if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) {
1280 if (property_exists($object, 'thirdparty') && $object->thirdparty instanceof Societe && $object->thirdparty->default_lang && $object->thirdparty->default_lang != $langs->defaultlang) {
1281 $outputlangs = new Translate('', $conf);
1282 $outputlangs->setDefaultLang($object->thirdparty->default_lang);
1283 $outputlangs->loadLangs(array('main', 'other'));
1284 }
1285 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
1286 complete_substitutions_array($substitutionarray, $outputlangs, $object);
1287 $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $outputlangs);
1288 $message = make_substitutions($emailTemplate->content, $substitutionarray, $outputlangs);
1289 } else {
1290 $message = '';
1291 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
1292 $message .= "\n";
1293 $message .= $mesg;
1294
1295 $message = nl2br($message);
1296 }
1297
1298 // Replace keyword __SUPERVISOREMAIL__
1299 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
1300 $newval = '';
1301 if ($user->fk_user > 0) {
1302 $supervisoruser = new User($this->db);
1303 $supervisoruser->fetch($user->fk_user);
1304 if ($supervisoruser->email) {
1305 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
1306 }
1307 }
1308 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
1309 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
1310 $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
1311 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
1312 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
1313 }
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 ($mailfile->sendfile()) {
1351 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
1352 $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)."')";
1353 if (!$this->db->query($sql)) {
1354 dol_print_error($this->db);
1355 }
1356 } else {
1357 $error++;
1358 $this->errors[] = $mailfile->error;
1359 }
1360 }
1361 }
1362 }
1363
1364 if (!$error) {
1365 return $num;
1366 } else {
1367 return -1 * $error;
1368 }
1369 }
1370}
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
getDolGlobalString($key, $default='')
Return 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:149