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