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