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