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