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