dolibarr 19.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 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
30
34class Notify
35{
39 public $id;
40
44 public $db;
45
49 public $error = '';
50
54 public $errors = array();
55
56 public $author;
57 public $ref;
58 public $date;
59 public $duree;
60 public $note;
61
65 public $fk_project;
66
67 // This codes actions are defined into table llx_notify_def
68 public static $arrayofnotifsupported = array(
69 'BILL_VALIDATE',
70 'BILL_PAYED',
71 'ORDER_CREATE',
72 'ORDER_VALIDATE',
73 'ORDER_CLOSE',
74 'PROPAL_VALIDATE',
75 'PROPAL_CLOSE_SIGNED',
76 'PROPAL_CLOSE_REFUSED',
77 'FICHINTER_VALIDATE',
78 'FICHINTER_CLOSE',
79 'FICHINTER_ADD_CONTACT',
80 'ORDER_SUPPLIER_VALIDATE',
81 'ORDER_SUPPLIER_APPROVE',
82 'ORDER_SUPPLIER_SUBMIT',
83 'ORDER_SUPPLIER_REFUSE',
84 'SHIPPING_VALIDATE',
85 'EXPENSE_REPORT_VALIDATE',
86 'EXPENSE_REPORT_APPROVE',
87 'HOLIDAY_VALIDATE',
88 'HOLIDAY_APPROVE',
89 'ACTION_CREATE'
90 );
91
97 public function __construct($db)
98 {
99 $this->db = $db;
100 }
101
102
112 public function confirmMessage($action, $socid, $object)
113 {
114 global $conf, $langs;
115 $langs->load("mails");
116
117 // Get full list of all notifications subscribed for $action, $socid and $object
118 $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
119
120 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_USER')) {
121 foreach ($listofnotiftodo as $val) {
122 if ($val['type'] == 'touser') {
123 unset($listofnotiftodo[$val['email']]);
124 //$listofnotiftodo = array_merge($listofnotiftodo);
125 }
126 }
127 }
128 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_CONTACT')) {
129 foreach ($listofnotiftodo as $val) {
130 if ($val['type'] == 'tocontact') {
131 unset($listofnotiftodo[$val['email']]);
132 //$listofnotiftodo = array_merge($listofnotiftodo);
133 }
134 }
135 }
136 if (getDolGlobalString('NOTIFICATION_EMAIL_DISABLE_CONFIRM_MESSAGE_FIX')) {
137 foreach ($listofnotiftodo as $val) {
138 if ($val['type'] == 'tofixedemail') {
139 unset($listofnotiftodo[$val['email']]);
140 //$listofnotiftodo = array_merge($listofnotiftodo);
141 }
142 }
143 }
144
145 $texte = '';
146 $nb = -1;
147 if (is_array($listofnotiftodo)) {
148 $nb = count($listofnotiftodo);
149 }
150 if ($nb < 0) {
151 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
152 } elseif ($nb == 0) {
153 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("NoNotificationsWillBeSent");
154 } elseif ($nb == 1) {
155 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("ANotificationsWillBeSent");
156 } elseif ($nb >= 2) {
157 $texte = img_object($langs->trans("Notifications"), 'email', 'class="pictofixedwidth"').$langs->trans("SomeNotificationsWillBeSent", $nb);
158 }
159
160 if (is_array($listofnotiftodo)) {
161 $i = 0;
162 foreach ($listofnotiftodo as $val) {
163 if ($i) {
164 $texte .= ', ';
165 } else {
166 $texte .= ' (';
167 }
168 if ($val['isemailvalid']) {
169 $texte .= $val['email'];
170 } else {
171 $texte .= $val['emaildesc'];
172 }
173 $i++;
174 }
175 if ($i) {
176 $texte .= ')';
177 }
178 }
179
180 return $texte;
181 }
182
193 public function getNotificationsArray($notifcode, $socid = 0, $object = null, $userid = 0, $scope = array('thirdparty', 'user', 'global'))
194 {
195 global $conf, $user;
196
197 $error = 0;
198 $resarray = array();
199
200 $valueforthreshold = 0;
201 if (is_object($object)) {
202 $valueforthreshold = $object->total_ht;
203 }
204
205 $sqlnotifcode = '';
206 if ($notifcode) {
207 if (is_numeric($notifcode)) {
208 $sqlnotifcode = " AND n.fk_action = ".((int) $notifcode); // Old usage
209 } else {
210 $sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
211 }
212 }
213
214 if (!$error) {
215 if ($socid >= 0 && in_array('thirdparty', $scope)) {
216 $sql = "SELECT a.code, c.email, c.rowid, c.statut as status";
217 $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
218 $sql .= " ".$this->db->prefix()."socpeople as c,";
219
220 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
221 $sql .= " ".$this->db->prefix()."societe as s";
222 $sql .= " WHERE n.fk_contact = c.rowid";
223 $sql .= " AND a.rowid = n.fk_action";
224 $sql .= " AND n.fk_soc = s.rowid";
225 $sql .= $sqlnotifcode;
226 $sql .= " AND s.entity IN (".getEntity('societe').")";
227 if ($socid > 0) {
228 $sql .= " AND s.rowid = ".((int) $socid);
229 }
230
231 dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
232
233 $resql = $this->db->query($sql);
234 if ($resql) {
235 $num = $this->db->num_rows($resql);
236 $i = 0;
237 while ($i < $num) {
238 $obj = $this->db->fetch_object($resql);
239 // we want to notify only if contact is enable
240 if ($obj && $obj->status == 1) {
241 $newval2 = trim($obj->email);
242 $isvalid = isValidEmail($newval2);
243 if (empty($resarray[$newval2])) {
244 $resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
245 }
246 }
247 $i++;
248 }
249 } else {
250 $error++;
251 $this->error = $this->db->lasterror();
252 }
253 }
254 }
255
256 if (!$error) {
257 if ($userid >= 0 && in_array('user', $scope)) {
258 $sql = "SELECT a.code, c.email, c.rowid";
259 $sql .= " FROM ".$this->db->prefix()."notify_def as n,";
260 $sql .= " ".$this->db->prefix()."user as c,";
261 $sql .= " ".$this->db->prefix()."c_action_trigger as a";
262 $sql .= " WHERE n.fk_user = c.rowid";
263 $sql .= " AND a.rowid = n.fk_action";
264 $sql .= $sqlnotifcode;
265 $sql .= " AND c.entity IN (".getEntity('user').")";
266 if ($userid > 0) {
267 $sql .= " AND c.rowid = ".((int) $userid);
268 }
269
270 dol_syslog(__METHOD__." ".$notifcode.", ".$socid, LOG_DEBUG);
271
272 $resql = $this->db->query($sql);
273 if ($resql) {
274 $num = $this->db->num_rows($resql);
275 $i = 0;
276 while ($i < $num) {
277 $obj = $this->db->fetch_object($resql);
278 if ($obj) {
279 $newval2 = trim($obj->email);
280 $isvalid = isValidEmail($newval2);
281 if (empty($resarray[$newval2])) {
282 $resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
283 }
284 }
285 $i++;
286 }
287 } else {
288 $error++;
289 $this->error = $this->db->lasterror();
290 }
291 }
292 }
293
294 if (!$error) {
295 if (in_array('global', $scope)) {
296 // List of notifications enabled for fixed email
297 foreach ($conf->global as $key => $val) {
298 if ($notifcode) {
299 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
300 continue;
301 }
302 } else {
303 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
304 continue;
305 }
306 }
307
308 $threshold = (float) $reg[1];
309 if ($valueforthreshold < $threshold) {
310 continue;
311 }
312
313 $tmpemail = explode(',', $val);
314 foreach ($tmpemail as $key2 => $val2) {
315 $newval2 = trim($val2);
316 if ($newval2 == '__SUPERVISOREMAIL__') {
317 if ($user->fk_user > 0) {
318 $tmpuser = new User($this->db);
319 $tmpuser->fetch($user->fk_user);
320 if ($tmpuser->email) {
321 $newval2 = trim($tmpuser->email);
322 } else {
323 $newval2 = '';
324 }
325 } else {
326 $newval2 = '';
327 }
328 }
329 if ($newval2) {
330 $isvalid = isValidEmail($newval2, 0);
331 if (empty($resarray[$newval2])) {
332 $resarray[$newval2] = array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
333 }
334 }
335 }
336 }
337 }
338 }
339
340 if ($error) {
341 return -1;
342 }
343
344 //var_dump($resarray);
345 return $resarray;
346 }
347
359 public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
360 {
361 global $user, $conf, $langs, $mysoc;
362 global $hookmanager;
363 global $dolibarr_main_url_root;
364 global $action;
365
366 // Complete the array Notify::$arrayofnotifsupported
367 if (!is_object($hookmanager)) {
368 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
369 $hookmanager = new HookManager($this->db);
370 }
371 $hookmanager->initHooks(array('notification'));
372
373 $parameters = array('notifcode' => $notifcode);
374 $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
375 if (empty($reshook)) {
376 if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
377 Notify::$arrayofnotifsupported = array_merge(Notify::$arrayofnotifsupported, $hookmanager->resArray['arrayofnotifsupported']);
378 }
379 }
380
381 // If the trigger code is not managed by the Notification module
382 if (!in_array($notifcode, Notify::$arrayofnotifsupported)) {
383 return 0;
384 }
385
386 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
387
388 dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object id=".$object->id);
389
390 $langs->load("other");
391
392 // Define $urlwithroot
393 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
394 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
395 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
396
397 // Define some vars
398 $application = 'Dolibarr';
399 if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
400 $application = $conf->global->MAIN_APPLICATION_TITLE;
401 }
402 $replyto = $conf->notification->email_from;
403 $object_type = '';
404 $link = '';
405 $num = 0;
406 $error = 0;
407
408 $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
409 $newref = (empty($object->newref) ? $object->ref : $object->newref);
410
411 $sql = '';
412
413 // Check notification per third party
414 if (!empty($object->socid) && $object->socid > 0) {
415 $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
416 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
417 $sql .= " FROM ".$this->db->prefix()."socpeople as c,";
418 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
419 $sql .= " ".$this->db->prefix()."notify_def as n,";
420 $sql .= " ".$this->db->prefix()."societe as s";
421 $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
422 $sql .= " AND n.fk_soc = s.rowid";
423 $sql .= " AND c.statut = 1";
424 if (is_numeric($notifcode)) {
425 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
426 } else {
427 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
428 }
429 $sql .= " AND s.rowid = ".((int) $object->socid);
430
431 $sql .= "\nUNION\n";
432 }
433
434 // Check notification per user
435 $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
436 $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type";
437 $sql .= " FROM ".$this->db->prefix()."user as c,";
438 $sql .= " ".$this->db->prefix()."c_action_trigger as a,";
439 $sql .= " ".$this->db->prefix()."notify_def as n";
440 $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
441 $sql .= " AND c.statut = 1";
442 if (is_numeric($notifcode)) {
443 $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
444 } else {
445 $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
446 }
447
448 // Check notification fixed
449 // TODO Move part found after, into a sql here
450
451
452 // Loop on all notifications enabled
453 $result = $this->db->query($sql);
454 if ($result) {
455 $num = $this->db->num_rows($result);
456 $projtitle = '';
457 if (is_object($object->project) || $object->fetch_project() > 0) {
458 $projtitle = '('.$object->project->title.')';
459 }
460
461 if ($num > 0) {
462 $i = 0;
463 while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
464 $obj = $this->db->fetch_object($result);
465
466 $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
467 $notifcodedefid = $obj->adid;
468 $trackid = '';
469 if ($obj->type_target == 'tocontactid') {
470 $trackid = 'ctc'.$obj->cid;
471 }
472 if ($obj->type_target == 'touserid') {
473 $trackid = 'use'.$obj->cid;
474 }
475
476 if (dol_strlen($obj->email)) {
477 // Set output language
478 $outputlangs = $langs;
479 if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
480 $outputlangs = new Translate('', $conf);
481 $outputlangs->setDefaultLang($obj->default_lang);
482 $outputlangs->loadLangs(array("main", "other"));
483 }
484
485 $appli = $mysoc->name;
486
487 $subject = '['.$appli.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
488
489 switch ($notifcode) {
490 case 'BILL_VALIDATE':
491 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
492 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
493 $object_type = 'facture';
494 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
495 break;
496 case 'BILL_PAYED':
497 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
498 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
499 $object_type = 'facture';
500 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
501 break;
502 case 'ORDER_VALIDATE':
503 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
504 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
505 $object_type = 'order';
506 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
507 break;
508 case 'ORDER_CLOSE':
509 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
510 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
511 $object_type = 'order';
512 $labeltouse = $conf->global->ORDER_CLOSE_TEMPLATE;
513 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderClose", $link);
514 break;
515 case 'PROPAL_VALIDATE':
516 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
517 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
518 $object_type = 'propal';
519 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
520 break;
521 case 'PROPAL_CLOSE_REFUSED':
522 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
523 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
524 $object_type = 'propal';
525 $labeltouse = $conf->global->PROPAL_CLOSE_REFUSED_TEMPLATE;
526 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedRefused", $link);
527 if (!empty($object->context['closedfromonlinesignature'])) {
528 $mesg .= ' - From online page';
529 }
530 break;
531 case 'PROPAL_CLOSE_SIGNED':
532 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
533 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
534 $object_type = 'propal';
535 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
536 if (!empty($object->context['closedfromonlinesignature'])) {
537 $mesg .= ' - From online page';
538 }
539 break;
540 case 'FICHINTER_ADD_CONTACT':
541 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
542 $dir_output = $conf->ficheinter->dir_output;
543 $object_type = 'ficheinter';
544 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
545 break;
546 case 'FICHINTER_VALIDATE':
547 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
548 $dir_output = $conf->ficheinter->dir_output;
549 $object_type = 'ficheinter';
550 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
551 break;
552 case 'FICHINTER_CLOSE':
553 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
554 $dir_output = $conf->ficheinter->dir_output;
555 $object_type = 'ficheinter';
556 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionClosed", $link);
557 break;
558 case 'ORDER_SUPPLIER_VALIDATE':
559 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
560 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
561 $object_type = 'order_supplier';
562 $labeltouse = isset($conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE : '';
563 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
564 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($outputlangs));
565 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
566 break;
567 case 'ORDER_SUPPLIER_APPROVE':
568 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
569 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
570 $object_type = 'order_supplier';
571 $labeltouse = isset($conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE : '';
572 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
573 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($outputlangs));
574 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
575 break;
576 case 'ORDER_SUPPLIER_SUBMIT':
577 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
578 $dir_output = $conf->fournisseur->commande->dir_output;
579 $object_type = 'order_supplier';
580 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
581 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($outputlangs));
582 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
583 break;
584 case 'ORDER_SUPPLIER_REFUSE':
585 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
586 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
587 $object_type = 'order_supplier';
588 $labeltouse = isset($conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE : '';
589 $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
590 $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($outputlangs));
591 $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
592 break;
593 case 'SHIPPING_VALIDATE':
594 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
595 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
596 $object_type = 'shipping';
597 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
598 break;
599 case 'EXPENSE_REPORT_VALIDATE':
600 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
601 $dir_output = $conf->expensereport->dir_output;
602 $object_type = 'expensereport';
603 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
604 break;
605 case 'EXPENSE_REPORT_APPROVE':
606 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
607 $dir_output = $conf->expensereport->dir_output;
608 $object_type = 'expensereport';
609 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
610 break;
611 case 'HOLIDAY_VALIDATE':
612 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
613 $dir_output = $conf->holiday->dir_output;
614 $object_type = 'holiday';
615 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
616 break;
617 case 'HOLIDAY_APPROVE':
618 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
619 $dir_output = $conf->holiday->dir_output;
620 $object_type = 'holiday';
621 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
622 break;
623 case 'ACTION_CREATE':
624 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
625 $dir_output = $conf->agenda->dir_output;
626 $object_type = 'action';
627 $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link);
628 break;
629 default:
630 $object_type = $object->element;
631 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
632 $template = $notifcode.'_TEMPLATE';
633 $mesg = $outputlangs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref.' '.$dir_output;
634 break;
635 }
636
637 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
638 $formmail = new FormMail($this->db);
639 $arraydefaultmessage = null;
640
641 $template = $notifcode.'_TEMPLATE';
642 $labeltouse = getDolGlobalString($template);
643 if (!empty($labeltouse)) {
644 $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $outputlangs, 0, 1, $labeltouse);
645 }
646 if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
647 if (method_exists($object, 'fetch_thirdparty') && empty($object->thirdparty)) {
648 $object->fetch_thirdparty();
649 }
650 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
651 complete_substitutions_array($substitutionarray, $outputlangs, $object);
652 $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
653 $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
654 } else {
655 $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
656 $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
657 $message .= "\n";
658 $message .= $mesg;
659 }
660
661 $ref = dol_sanitizeFileName($newref);
662 $pdf_path = $dir_output."/".$ref.".pdf";
663 if (!dol_is_file($pdf_path)||(is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) {
664 // We can't add PDF as it is not generated yet.
665 $filepdf = '';
666 } else {
667 $filepdf = $pdf_path;
668 $filename_list[] = $filepdf;
669 $mimetype_list[] = mime_content_type($filepdf);
670 $mimefilename_list[] = $ref.".pdf";
671 }
672
673 $labeltouse = !empty($labeltouse) ? $labeltouse : '';
674
675 // Replace keyword __SUPERVISOREMAIL__
676 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
677 $newval = '';
678 if ($user->fk_user > 0) {
679 $supervisoruser = new User($this->db);
680 $supervisoruser->fetch($user->fk_user);
681 if ($supervisoruser->email) {
682 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
683 }
684 }
685 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
686 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
687 $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
688 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
689 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
690 }
691
692 $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list, 'outputlangs'=>$outputlangs, 'labeltouse'=>$labeltouse);
693 if (!isset($action)) {
694 $action = '';
695 }
696
697 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
698 if (empty($reshook)) {
699 if (!empty($hookmanager->resArray['files'])) {
700 $filename_list = $hookmanager->resArray['files']['file'];
701 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
702 $mimefilename_list = $hookmanager->resArray['files']['filename'];
703 }
704 if (!empty($hookmanager->resArray['subject'])) {
705 $subject .= $hookmanager->resArray['subject'];
706 }
707 if (!empty($hookmanager->resArray['message'])) {
708 $message .= $hookmanager->resArray['message'];
709 }
710 }
711
712 $mailfile = new CMailFile(
713 $subject,
714 $sendto,
715 $replyto,
716 $message,
717 $filename_list,
718 $mimetype_list,
719 $mimefilename_list,
720 '',
721 '',
722 0,
723 -1,
724 '',
725 '',
726 $trackid,
727 '',
728 'notification'
729 );
730
731 if ($mailfile->sendfile()) {
732 if ($obj->type_target == 'touserid') {
733 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
734 $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)."')";
735 } else {
736 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
737 $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)."')";
738 }
739 if (!$this->db->query($sql)) {
740 dol_print_error($this->db);
741 }
742 } else {
743 $error++;
744 $this->errors[] = $mailfile->error;
745 }
746 } else {
747 dol_syslog("No notification sent for ".$sendto." because email is empty");
748 }
749 $i++;
750 }
751 } else {
752 dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
753 }
754 } else {
755 $error++;
756 $this->errors[] = $this->db->lasterror();
757 dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
758 return -1;
759 }
760
761 // Check notification using fixed email
762 // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and inclulde the case into previous loop of sql result
763 if (!$error) {
764 foreach ($conf->global as $key => $val) {
765 $reg = array();
766 if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
767 continue;
768 }
769
770 $sendto = $val;
771
772 $threshold = (float) $reg[1];
773 if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
774 dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
775 continue;
776 }
777
778 $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
779 if ($notifcodedefid <= 0) {
780 dol_print_error($this->db, 'Failed to get id from code');
781 }
782 $trackid = '';
783
784 $object_type = '';
785 $link = '';
786 $num++;
787
788 $appli = $mysoc->name;
789
790 $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
791
792 switch ($notifcode) {
793 case 'BILL_VALIDATE':
794 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
795 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
796 $object_type = 'facture';
797 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
798 break;
799 case 'BILL_PAYED':
800 $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
801 $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
802 $object_type = 'facture';
803 $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
804 break;
805 case 'ORDER_VALIDATE':
806 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
807 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
808 $object_type = 'order';
809 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
810 break;
811 case 'ORDER_CLOSE':
812 $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
813 $dir_output = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande');
814 $object_type = 'order';
815 $mesg = $langs->transnoentitiesnoconv("EMailTextOrderClose", $link);
816 break;
817 case 'PROPAL_VALIDATE':
818 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
819 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
820 $object_type = 'propal';
821 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
822 break;
823 case 'PROPAL_CLOSE_SIGNED':
824 $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
825 $dir_output = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal');
826 $object_type = 'propal';
827 $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
828 break;
829 case 'FICHINTER_ADD_CONTACT':
830 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
831 $dir_output = $conf->ficheinter->dir_output;
832 $object_type = 'ficheinter';
833 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
834 break;
835 case 'FICHINTER_VALIDATE':
836 $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
837 $dir_output = $conf->facture->dir_output;
838 $object_type = 'ficheinter';
839 $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
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->facture->dir_output;
844 $object_type = 'ficheinter';
845 $mesg = $langs->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 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
852 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs));
853 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
854 break;
855 case 'ORDER_SUPPLIER_APPROVE':
856 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
857 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
858 $object_type = 'order_supplier';
859 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
860 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs));
861 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
862 break;
863 case 'ORDER_SUPPLIER_SUBMIT':
864 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
865 $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object);
866 $object_type = 'order_supplier';
867 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
868 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs));
869 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
870 break;
871 case 'ORDER_SUPPLIER_REFUSE':
872 $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
873 $dir_output = $conf->fournisseur->dir_output.'/commande/';
874 $object_type = 'order_supplier';
875 $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
876 $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs));
877 $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
878 break;
879 case 'SHIPPING_VALIDATE':
880 $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
881 $dir_output = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment');
882 $object_type = 'order_supplier';
883 $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
884 break;
885 case 'EXPENSE_REPORT_VALIDATE':
886 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
887 $dir_output = $conf->expensereport->dir_output;
888 $object_type = 'expensereport';
889 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
890 break;
891 case 'EXPENSE_REPORT_APPROVE':
892 $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
893 $dir_output = $conf->expensereport->dir_output;
894 $object_type = 'expensereport';
895 $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
896 break;
897 case 'HOLIDAY_VALIDATE':
898 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
899 $dir_output = $conf->holiday->dir_output;
900 $object_type = 'holiday';
901 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
902 break;
903 case 'HOLIDAY_APPROVE':
904 $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
905 $dir_output = $conf->holiday->dir_output;
906 $object_type = 'holiday';
907 $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
908 break;
909 case 'ACTION_CREATE':
910 $link = '<a href="'.$urlwithroot.'/comm/action/card.php?id='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
911 $dir_output = $conf->agenda->dir_output;
912 $object_type = 'action';
913 $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link);
914 break;
915 default:
916 $object_type = $object->element;
917 $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type);
918 $mesg = $langs->transnoentitiesnoconv('Notify_'.$notifcode).' '.$newref;
919 break;
920 }
921 $ref = dol_sanitizeFileName($newref);
922 $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
923 if (!dol_is_file($pdf_path)) {
924 // We can't add PDF as it is not generated yet.
925 $filepdf = '';
926 } else {
927 $filepdf = $pdf_path;
928 $filename_list[] = $pdf_path;
929 $mimetype_list[] = mime_content_type($filepdf);
930 $mimefilename_list[] = $ref.".pdf";
931 }
932
933 // if an e-mail template is configured for this notification code (for instance
934 // 'SHIPPING_VALIDATE_TEMPLATE'), we fetch this template by its label. Otherwise, a default message
935 // content will be sent.
936 $mailTemplateLabel = isset($conf->global->{$notifcode.'_TEMPLATE'}) ? $conf->global->{$notifcode.'_TEMPLATE'} : '';
937 $emailTemplate = null;
938 if (!empty($mailTemplateLabel)) {
939 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
940 $formmail = new FormMail($this->db);
941 $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type.'_send', $user, $langs, 0, 1, $labeltouse);
942 }
943 if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) {
944 $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
945 complete_substitutions_array($substitutionarray, $langs, $object);
946 $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $langs);
947 $message = make_substitutions($emailTemplate->content, $substitutionarray, $langs);
948 } else {
949 $message = '';
950 $message .= $langs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
951 $message .= "\n";
952 $message .= $mesg;
953
954 $message = nl2br($message);
955 }
956
957 // Replace keyword __SUPERVISOREMAIL__
958 if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
959 $newval = '';
960 if ($user->fk_user > 0) {
961 $supervisoruser = new User($this->db);
962 $supervisoruser->fetch($user->fk_user);
963 if ($supervisoruser->email) {
964 $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
965 }
966 }
967 dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
968 $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
969 $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
970 $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
971 $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
972 }
973
974 if ($sendto) {
975 $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list, 'subject'=>&$subject, 'message'=>&$message);
976 $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
977 if (empty($reshook)) {
978 if (!empty($hookmanager->resArray['files'])) {
979 $filename_list = $hookmanager->resArray['files']['file'];
980 $mimetype_list = $hookmanager->resArray['files']['mimefile'];
981 $mimefilename_list = $hookmanager->resArray['files']['filename'];
982 }
983 if (!empty($hookmanager->resArray['subject'])) {
984 $subject .= $hookmanager->resArray['subject'];
985 }
986 if (!empty($hookmanager->resArray['message'])) {
987 $message .= $hookmanager->resArray['message'];
988 }
989 }
990 $mailfile = new CMailFile(
991 $subject,
992 $sendto,
993 $replyto,
994 $message,
995 $filename_list,
996 $mimetype_list,
997 $mimefilename_list,
998 '',
999 '',
1000 0,
1001 1,
1002 '',
1003 $trackid,
1004 '',
1005 '',
1006 'notification'
1007 );
1008
1009 if ($mailfile->sendfile()) {
1010 $sql = "INSERT INTO ".$this->db->prefix()."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
1011 $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)."')";
1012 if (!$this->db->query($sql)) {
1013 dol_print_error($this->db);
1014 }
1015 } else {
1016 $error++;
1017 $this->errors[] = $mailfile->error;
1018 }
1019 }
1020 }
1021 }
1022
1023 if (!$error) {
1024 return $num;
1025 } else {
1026 return -1 * $error;
1027 }
1028 }
1029}
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Class to manage hooks.
Class to manage notifications.
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 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.
__construct($db)
Constructor.
Class to manage translations.
Class to manage Dolibarr users.
dol_is_file($pathoffile)
Return if path is a file.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.