dolibarr 20.0.0
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 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
918 complete_substitutions_array($substitutionarray, $outputlangs, $object);
919 $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
920 $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
921 } else {
922 $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
923 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
924 $message .= "\n";
925 $message .= $mesg;
926 }
927
928 $ref = dol_sanitizeFileName($newref);
929 $pdf_path = $dir_output."/".$ref.".pdf";
930 if (!dol_is_file($pdf_path) || (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) {
931 // We can't add PDF as it is not generated yet.
932 $filepdf = '';
933 } else {
934 $filepdf = $pdf_path;
935 $filename_list[] = $filepdf;
936 $mimetype_list[] = mime_content_type($filepdf);
937 $mimefilename_list[] = $ref.".pdf";
938 }
939
940 $labeltouse = !empty($labeltouse) ? $labeltouse : '';
941
942 // Replace keyword __SUPERVISOREMAIL__
943 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
944 $newval = '';
945 if ($user->fk_user > 0) {
946 $supervisoruser = new User($this->db);
947 $supervisoruser->fetch($user->fk_user);
948 if ($supervisoruser->email) {
949 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
950 }
951 }
952 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
953 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
954 $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
955 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
956 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
957 }
958
959 $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'outputlangs' => $outputlangs, 'labeltouse' => $labeltouse);
960 if (!isset($action)) {
961 $action = '';
962 }
963
964 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
965 if (empty($reshook)) {
966 if (!empty($hookmanager->resArray['files'])) {
967 $filename_list = $hookmanager->resArray['files']['file'];
968 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
969 $mimefilename_list = $hookmanager->resArray['files']['filename'];
970 }
971 if (!empty($hookmanager->resArray['subject'])) {
972 $subject .= $hookmanager->resArray['subject'];
973 }
974 if (!empty($hookmanager->resArray['message'])) {
975 $message .= $hookmanager->resArray['message'];
976 }
977 }
978
979 $mailfile = new CMailFile(
980 $subject,
981 $sendto,
982 $from,
983 $message,
984 $filename_list,
985 $mimetype_list,
986 $mimefilename_list,
987 '',
988 '',
989 0,
990 -1,
991 '',
992 '',
993 $trackid,
994 '',
995 'notification'
996 );
997
998 if ($mailfile->sendfile()) {
999 if ($obj->type_target == 'touserid') {
1000 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
1001 $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)."')";
1002 } else {
1003 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, 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 }
1006 if (!$this->db->query($sql)) {
1007 dol_print_error($this->db);
1008 }
1009 } else {
1010 $error++;
1011 $this->errors[] = $mailfile->error;
1012 }
1013 } else {
1014 dol_syslog("No notification sent for ".$sendto." because email is empty");
1015 }
1016 $i++;
1017 }
1018 } else {
1019 dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
1020 }
1021 } else {
1022 $error++;
1023 $this->errors[] = $this->db->lasterror();
1024 dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
1025 return -1;
1026 }
1027
1028 // Check notification using fixed email
1029 // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and inclulde the case into previous loop of sql result
1030 if (!$error) {
1031 foreach ($conf->global as $key => $val) {
1032 $reg = array();
1033 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
1034 continue;
1035 }
1036
1037 $sendto = $val;
1038
1039 $threshold = (float) $reg[1];
1040 if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
1041 dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
1042 continue;
1043 }
1044
1045 $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
1046 if ($notifcodedefid <= 0) {
1047 dol_print_error($this->db, 'Failed to get id from code');
1048 }
1049 $trackid = '';
1050
1051 $object_type = '';
1052 $link = '';
1053 $num++;
1054
1055 $appli = $mysoc->name;
1056
1057 $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
1058
1059 switch ($notifcode) {
1060 case 'BILL_VALIDATE':
1061 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1062 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1063 $object_type = 'facture';
1064 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
1065 break;
1066 case 'BILL_PAYED':
1067 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1068 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
1069 $object_type = 'facture';
1070 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
1071 break;
1072 case 'ORDER_VALIDATE':
1073 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1074 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1075 $object_type = 'order';
1076 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
1077 break;
1078 case 'ORDER_CLOSE':
1079 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1080 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
1081 $object_type = 'order';
1082 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderClose", $link);
1083 break;
1084 case 'PROPAL_VALIDATE':
1085 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1086 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1087 $object_type = 'propal';
1088 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
1089 break;
1090 case 'PROPAL_CLOSE_SIGNED':
1091 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1092 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
1093 $object_type = 'propal';
1094 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
1095 break;
1096 case 'FICHINTER_ADD_CONTACT':
1097 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1098 $dir_output = $conf->ficheinter->dir_output;
1099 $object_type = 'ficheinter';
1100 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
1101 break;
1102 case 'FICHINTER_VALIDATE':
1103 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1104 $dir_output = $conf->facture->dir_output;
1105 $object_type = 'ficheinter';
1106 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
1107 break;
1108 case 'FICHINTER_CLOSE':
1109 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1110 $dir_output = $conf->facture->dir_output;
1111 $object_type = 'ficheinter';
1112 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
1113 break;
1114 case 'ORDER_SUPPLIER_CANCEL':
1115 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1116 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1117 $object_type = 'order_supplier';
1118 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1119 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($langs));
1120 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1121 break;
1122 case 'ORDER_SUPPLIER_VALIDATE':
1123 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1124 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1125 $object_type = 'order_supplier';
1126 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1127 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs));
1128 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1129 break;
1130 case 'ORDER_SUPPLIER_APPROVE':
1131 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1132 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1133 $object_type = 'order_supplier';
1134 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1135 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs));
1136 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1137 break;
1138 case 'ORDER_SUPPLIER_SUBMIT':
1139 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1140 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1141 $object_type = 'order_supplier';
1142 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1143 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs));
1144 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1145 break;
1146 case 'ORDER_SUPPLIER_REFUSE':
1147 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1148 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
1149 $object_type = 'order_supplier';
1150 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
1151 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs));
1152 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
1153 break;
1154 case 'SHIPPING_VALIDATE':
1155 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1156 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
1157 $object_type = 'order_supplier';
1158 $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
1159 break;
1160 case 'EXPENSE_REPORT_VALIDATE':
1161 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1162 $dir_output = $conf->expensereport->dir_output;
1163 $object_type = 'expensereport';
1164 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
1165 break;
1166 case 'EXPENSE_REPORT_APPROVE':
1167 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1168 $dir_output = $conf->expensereport->dir_output;
1169 $object_type = 'expensereport';
1170 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
1171 break;
1172 case 'HOLIDAY_VALIDATE':
1173 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1174 $dir_output = $conf->holiday->dir_output;
1175 $object_type = 'holiday';
1176 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
1177 break;
1178 case 'HOLIDAY_APPROVE':
1179 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1180 $dir_output = $conf->holiday->dir_output;
1181 $object_type = 'holiday';
1182 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
1183 break;
1184 case 'ACTION_CREATE':
1185 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
1186 $dir_output = $conf->agenda->dir_output;
1187 $object_type = 'action';
1188 $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link);
1189 break;
1190 default:
1191 $object_type = $object->element;
1192 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
1193 $mesg = $langs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref;
1194 break;
1195 }
1196 $ref = dol_sanitizeFileName($newref);
1197 $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
1198 if (!dol_is_file($pdf_path)) {
1199 // We can't add PDF as it is not generated yet.
1200 $filepdf = '';
1201 } else {
1202 $filepdf = $pdf_path;
1203 $filename_list[] = $pdf_path;
1204 $mimetype_list[] = mime_content_type($filepdf);
1205 $mimefilename_list[] = $ref.".pdf";
1206 }
1207
1208 // Set output language
1209 $outputlangs = $langs;
1210
1211 // if an e-mail template is configured for this notification code (for instance 'SHIPPING_VALIDATE_TEMPLATE', ...),
1212 // we fetch this template by its label. Otherwise, a default message content will be sent.
1213 $mailTemplateLabel = getDolGlobalString($notifcode.'_TEMPLATE');
1214 $emailTemplate = null;
1215 if (!empty($mailTemplateLabel)) {
1216 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
1217 $formmail = new FormMail($this->db);
1218 $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $mailTemplateLabel);
1219 }
1220 if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) {
1221 if (property_exists($object, 'thirdparty') && $object->thirdparty instanceof Societe && $object->thirdparty->default_lang && $object->thirdparty->default_lang != $langs->defaultlang) {
1222 $outputlangs = new Translate('', $conf);
1223 $outputlangs->setDefaultLang($object->thirdparty->default_lang);
1224 $outputlangs->loadLangs(array('main', 'other'));
1225 }
1226 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
1227 complete_substitutions_array($substitutionarray, $outputlangs, $object);
1228 $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $outputlangs);
1229 $message = make_substitutions($emailTemplate->content, $substitutionarray, $outputlangs);
1230 } else {
1231 $message = '';
1232 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
1233 $message .= "\n";
1234 $message .= $mesg;
1235
1236 $message = nl2br($message);
1237 }
1238
1239 // Replace keyword __SUPERVISOREMAIL__
1240 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
1241 $newval = '';
1242 if ($user->fk_user > 0) {
1243 $supervisoruser = new User($this->db);
1244 $supervisoruser->fetch($user->fk_user);
1245 if ($supervisoruser->email) {
1246 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
1247 }
1248 }
1249 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
1250 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
1251 $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
1252 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
1253 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
1254 }
1255
1256 if ($sendto) {
1257 $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'subject' => &$subject, 'message' => &$message);
1258 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1259 if (empty($reshook)) {
1260 if (!empty($hookmanager->resArray['files'])) {
1261 $filename_list = $hookmanager->resArray['files']['file'];
1262 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
1263 $mimefilename_list = $hookmanager->resArray['files']['filename'];
1264 }
1265 if (!empty($hookmanager->resArray['subject'])) {
1266 $subject .= $hookmanager->resArray['subject'];
1267 }
1268 if (!empty($hookmanager->resArray['message'])) {
1269 $message .= $hookmanager->resArray['message'];
1270 }
1271 }
1272 $mailfile = new CMailFile(
1273 $subject,
1274 $sendto,
1275 $from,
1276 $message,
1277 $filename_list,
1278 $mimetype_list,
1279 $mimefilename_list,
1280 '',
1281 '',
1282 0,
1283 1,
1284 '',
1285 $trackid,
1286 '',
1287 '',
1288 'notification'
1289 );
1290
1291 if ($mailfile->sendfile()) {
1292 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
1293 $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)."')";
1294 if (!$this->db->query($sql)) {
1295 dol_print_error($this->db);
1296 }
1297 } else {
1298 $error++;
1299 $this->errors[] = $mailfile->error;
1300 }
1301 }
1302 }
1303 }
1304
1305 if (!$error) {
1306 return $num;
1307 } else {
1308 return -1 * $error;
1309 }
1310 }
1311}
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.
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 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