dolibarr  13.0.2
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
27 
28 
32 class Notify
33 {
37  public $id;
38 
42  public $db;
43 
47  public $error = '';
48 
52  public $errors = array();
53 
54  public $author;
55  public $ref;
56  public $date;
57  public $duree;
58  public $note;
59 
63  public $fk_project;
64 
65  // Les codes actions sont definis dans la table llx_notify_def
66 
67  // codes actions supported are
68  // @todo defined also into interface_50_modNotificiation_Notificiation.class.php
69  public $arrayofnotifsupported = array(
70  'BILL_VALIDATE',
71  'BILL_PAYED',
72  'ORDER_VALIDATE',
73  'PROPAL_VALIDATE',
74  'PROPAL_CLOSE_SIGNED',
75  'FICHINTER_VALIDATE',
76  'FICHINTER_ADD_CONTACT',
77  'ORDER_SUPPLIER_VALIDATE',
78  'ORDER_SUPPLIER_APPROVE',
79  'ORDER_SUPPLIER_REFUSE',
80  'SHIPPING_VALIDATE',
81  'EXPENSE_REPORT_VALIDATE',
82  'EXPENSE_REPORT_APPROVE',
83  'HOLIDAY_VALIDATE',
84  'HOLIDAY_APPROVE'
85  );
86 
87 
93  public function __construct($db)
94  {
95  $this->db = $db;
96  }
97 
98 
108  public function confirmMessage($action, $socid, $object)
109  {
110  global $langs;
111  $langs->load("mails");
112 
113  $listofnotiftodo = $this->getNotificationsArray($action, $socid, $object, 0);
114 
115  $texte = '';
116  $nb = -1;
117  if (is_array($listofnotiftodo)) {
118  $nb = count($listofnotiftodo);
119  }
120  if ($nb < 0) {
121  $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
122  } elseif ($nb == 0) {
123  $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("NoNotificationsWillBeSent");
124  } elseif ($nb == 1) {
125  $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("ANotificationsWillBeSent");
126  } elseif ($nb >= 2) {
127  $texte = img_object($langs->trans("Notifications"), 'email').' '.$langs->trans("SomeNotificationsWillBeSent", $nb);
128  }
129 
130  if (is_array($listofnotiftodo)) {
131  $i = 0;
132  foreach ($listofnotiftodo as $val) {
133  if ($i) {
134  $texte .= ', ';
135  } else {
136  $texte .= ' (';
137  }
138  if ($val['isemailvalid']) {
139  $texte .= $val['email'];
140  } else {
141  $texte .= $val['emaildesc'];
142  }
143  $i++;
144  }
145  if ($i) {
146  $texte .= ')';
147  }
148  }
149 
150  return $texte;
151  }
152 
163  public function getNotificationsArray($notifcode, $socid = 0, $object = null, $userid = 0, $scope = array('thirdparty', 'user', 'global'))
164  {
165  global $conf, $user;
166 
167  $error = 0;
168  $resarray = array();
169 
170  $valueforthreshold = 0;
171  if (is_object($object)) {
172  $valueforthreshold = $object->total_ht;
173  }
174 
175  $sqlnotifcode = '';
176  if ($notifcode) {
177  if (is_numeric($notifcode)) {
178  $sqlnotifcode = " AND n.fk_action = ".$notifcode; // Old usage
179  } else {
180  $sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
181  }
182  }
183 
184  if (!$error) {
185  if ($socid >= 0 && in_array('thirdparty', $scope)) {
186  $sql = "SELECT a.code, c.email, c.rowid";
187  $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
188  $sql .= " ".MAIN_DB_PREFIX."socpeople as c,";
189  $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
190  $sql .= " ".MAIN_DB_PREFIX."societe as s";
191  $sql .= " WHERE n.fk_contact = c.rowid";
192  $sql .= " AND a.rowid = n.fk_action";
193  $sql .= " AND n.fk_soc = s.rowid";
194  $sql .= $sqlnotifcode;
195  $sql .= " AND s.entity IN (".getEntity('societe').")";
196  if ($socid > 0) {
197  $sql .= " AND s.rowid = ".$socid;
198  }
199 
200  dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
201 
202  $resql = $this->db->query($sql);
203  if ($resql) {
204  $num = $this->db->num_rows($resql);
205  $i = 0;
206  while ($i < $num) {
207  $obj = $this->db->fetch_object($resql);
208  if ($obj) {
209  $newval2 = trim($obj->email);
210  $isvalid = isValidEmail($newval2);
211  if (empty($resarray[$newval2])) {
212  $resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
213  }
214  }
215  $i++;
216  }
217  } else {
218  $error++;
219  $this->error = $this->db->lasterror();
220  }
221  }
222  }
223 
224  if (!$error) {
225  if ($userid >= 0 && in_array('user', $scope)) {
226  $sql = "SELECT a.code, c.email, c.rowid";
227  $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
228  $sql .= " ".MAIN_DB_PREFIX."user as c,";
229  $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a";
230  $sql .= " WHERE n.fk_user = c.rowid";
231  $sql .= " AND a.rowid = n.fk_action";
232  $sql .= $sqlnotifcode;
233  $sql .= " AND c.entity IN (".getEntity('user').")";
234  if ($userid > 0) {
235  $sql .= " AND c.rowid = ".$userid;
236  }
237 
238  dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
239 
240  $resql = $this->db->query($sql);
241  if ($resql) {
242  $num = $this->db->num_rows($resql);
243  $i = 0;
244  while ($i < $num) {
245  $obj = $this->db->fetch_object($resql);
246  if ($obj) {
247  $newval2 = trim($obj->email);
248  $isvalid = isValidEmail($newval2);
249  if (empty($resarray[$newval2])) {
250  $resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
251  }
252  }
253  $i++;
254  }
255  } else {
256  $error++;
257  $this->error = $this->db->lasterror();
258  }
259  }
260  }
261 
262  if (!$error) {
263  if (in_array('global', $scope)) {
264  // List of notifications enabled for fixed email
265  foreach ($conf->global as $key => $val) {
266  if ($notifcode) {
267  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
268  continue;
269  }
270  } else {
271  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
272  continue;
273  }
274  }
275 
276  $threshold = (float) $reg[1];
277  if ($valueforthreshold < $threshold) {
278  continue;
279  }
280 
281  $tmpemail = explode(',', $val);
282  foreach ($tmpemail as $key2 => $val2) {
283  $newval2 = trim($val2);
284  if ($newval2 == '__SUPERVISOREMAIL__') {
285  if ($user->fk_user > 0) {
286  $tmpuser = new User($this->db);
287  $tmpuser->fetch($user->fk_user);
288  if ($tmpuser->email) {
289  $newval2 = trim($tmpuser->email);
290  } else {
291  $newval2 = '';
292  }
293  } else {
294  $newval2 = '';
295  }
296  }
297  if ($newval2) {
298  $isvalid = isValidEmail($newval2, 0);
299  if (empty($resarray[$newval2])) {
300  $resarray[$newval2] = array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
301  }
302  }
303  }
304  }
305  }
306  }
307 
308  if ($error) {
309  return -1;
310  }
311 
312  //var_dump($resarray);
313  return $resarray;
314  }
315 
327  public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
328  {
329  global $user, $conf, $langs, $mysoc;
330  global $hookmanager;
331  global $dolibarr_main_url_root;
332  global $action;
333 
334  if (!in_array($notifcode, $this->arrayofnotifsupported)) {
335  return 0;
336  }
337 
338  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
339  if (!is_object($hookmanager)) {
340  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
341  $hookmanager = new HookManager($this->db);
342  }
343  $hookmanager->initHooks(array('notification'));
344 
345  dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
346 
347  $langs->load("other");
348 
349  // Define $urlwithroot
350  $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
351  $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
352  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
353 
354  // Define some vars
355  $application = 'Dolibarr';
356  if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
357  $application = $conf->global->MAIN_APPLICATION_TITLE;
358  }
359  $replyto = $conf->notification->email_from;
360  $object_type = '';
361  $link = '';
362  $num = 0;
363  $error = 0;
364 
365  $oldref = (empty($object->oldref) ? $object->ref : $object->oldref);
366  $newref = (empty($object->newref) ? $object->ref : $object->newref);
367 
368  $sql = '';
369 
370  // Check notification per third party
371  if (!empty($object->socid) && $object->socid > 0) {
372  $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
373  $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
374  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
375  $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
376  $sql .= " ".MAIN_DB_PREFIX."notify_def as n,";
377  $sql .= " ".MAIN_DB_PREFIX."societe as s";
378  $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
379  $sql .= " AND n.fk_soc = s.rowid";
380  $sql .= " AND c.statut = 1";
381  if (is_numeric($notifcode)) {
382  $sql .= " AND n.fk_action = ".$notifcode; // Old usage
383  } else {
384  $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
385  }
386  $sql .= " AND s.rowid = ".$object->socid;
387 
388  $sql .= "\nUNION\n";
389  }
390 
391  // Check notification per user
392  $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
393  $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
394  $sql .= " FROM ".MAIN_DB_PREFIX."user as c,";
395  $sql .= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
396  $sql .= " ".MAIN_DB_PREFIX."notify_def as n";
397  $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
398  $sql .= " AND c.statut = 1";
399  if (is_numeric($notifcode)) {
400  $sql .= " AND n.fk_action = ".$notifcode; // Old usage
401  } else {
402  $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
403  }
404 
405  $result = $this->db->query($sql);
406  if ($result) {
407  $num = $this->db->num_rows($result);
408  $projtitle = '';
409  if (!empty($object->fk_project)) {
410  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
411  $proj = new Project($this->db);
412  $proj->fetch($object->fk_project);
413  $projtitle = '('.$proj->title.')';
414  }
415 
416  if ($num > 0) {
417  $i = 0;
418  while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode)
419  $obj = $this->db->fetch_object($result);
420 
421  $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
422  $notifcodedefid = $obj->adid;
423  $trackid = '';
424  if ($obj->type_target == 'tocontactid') {
425  $trackid = 'con'.$obj->id;
426  }
427  if ($obj->type_target == 'touserid') {
428  $trackid = 'use'.$obj->id;
429  }
430 
431  if (dol_strlen($obj->email)) {
432  // Set output language
433  $outputlangs = $langs;
434  if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) {
435  $outputlangs = new Translate('', $conf);
436  $outputlangs->setDefaultLang($obj->default_lang);
437  $outputlangs->loadLangs(array("main", "other"));
438  }
439 
440  $subject = '['.$mysoc->name.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
441 
442  switch ($notifcode) {
443  case 'BILL_VALIDATE':
444  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
445  $dir_output = $conf->facture->dir_output;
446  $object_type = 'facture';
447  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
448  break;
449  case 'BILL_PAYED':
450  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
451  $dir_output = $conf->facture->dir_output;
452  $object_type = 'facture';
453  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
454  break;
455  case 'ORDER_VALIDATE':
456  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
457  $dir_output = $conf->commande->dir_output;
458  $object_type = 'order';
459  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
460  break;
461  case 'PROPAL_VALIDATE':
462  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
463  $dir_output = $conf->propal->multidir_output[$object->entity];
464  $object_type = 'propal';
465  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
466  break;
467  case 'PROPAL_CLOSE_SIGNED':
468  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
469  $dir_output = $conf->propal->multidir_output[$object->entity];
470  $object_type = 'propal';
471  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
472  break;
473  case 'FICHINTER_ADD_CONTACT':
474  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
475  $dir_output = $conf->ficheinter->dir_output;
476  $object_type = 'ficheinter';
477  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
478  break;
479  case 'FICHINTER_VALIDATE':
480  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
481  $dir_output = $conf->ficheinter->dir_output;
482  $object_type = 'ficheinter';
483  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
484  break;
485  case 'ORDER_SUPPLIER_VALIDATE':
486  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
487  $dir_output = $conf->fournisseur->commande->dir_output;
488  $object_type = 'order_supplier';
489  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
490  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderValidatedBy", $link, $user->getFullName($outputlangs));
491  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
492  break;
493  case 'ORDER_SUPPLIER_APPROVE':
494  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
495  $dir_output = $conf->fournisseur->commande->dir_output;
496  $object_type = 'order_supplier';
497  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
498  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($outputlangs));
499  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
500  break;
501  case 'ORDER_SUPPLIER_REFUSE':
502  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
503  $dir_output = $conf->fournisseur->commande->dir_output;
504  $object_type = 'order_supplier';
505  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
506  $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextOrderRefusedBy", $link, $user->getFullName($outputlangs));
507  $mesg .= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
508  break;
509  case 'SHIPPING_VALIDATE':
510  $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'">'.$newref.'</a>';
511  $dir_output = $conf->expedition->dir_output.'/sending/';
512  $object_type = 'expedition';
513  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
514  break;
515  case 'EXPENSE_REPORT_VALIDATE':
516  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
517  $dir_output = $conf->expensereport->dir_output;
518  $object_type = 'expensereport';
519  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
520  break;
521  case 'EXPENSE_REPORT_APPROVE':
522  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
523  $dir_output = $conf->expensereport->dir_output;
524  $object_type = 'expensereport';
525  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
526  break;
527  case 'HOLIDAY_VALIDATE':
528  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
529  $dir_output = $conf->holiday->dir_output;
530  $object_type = 'holiday';
531  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
532  break;
533  case 'HOLIDAY_APPROVE':
534  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
535  $dir_output = $conf->holiday->dir_output;
536  $object_type = 'holiday';
537  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
538  break;
539  }
540  $ref = dol_sanitizeFileName($newref);
541  $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
542  if (!dol_is_file($pdf_path)) {
543  // We can't add PDF as it is not generated yet.
544  $filepdf = '';
545  } else {
546  $filepdf = $pdf_path;
547  }
548 
549  $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name)."\n";
550  $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
551  $message .= "\n";
552  $message .= $mesg;
553 
554  $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
555  if (!isset($action)) {
556  $action = '';
557  }
558 
559  $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
560  if (empty($reshook)) {
561  if (!empty($hookmanager->resArray['subject'])) {
562  $subject .= $hookmanager->resArray['subject'];
563  }
564  if (!empty($hookmanager->resArray['message'])) {
565  $message .= $hookmanager->resArray['message'];
566  }
567  }
568 
569  $mailfile = new CMailFile(
570  $subject,
571  $sendto,
572  $replyto,
573  $message,
574  $filename_list,
575  $mimetype_list,
576  $mimefilename_list,
577  '',
578  '',
579  0,
580  -1,
581  '',
582  '',
583  $trackid,
584  '',
585  'notification'
586  );
587 
588  if ($mailfile->sendfile()) {
589  if ($obj->type_target == 'touserid') {
590  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
591  $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
592  } else {
593  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
594  $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", ".$obj->cid.", '".$obj->type."', '".$object_type."', '".$obj->type_target."', ".$object->id.", '".$this->db->escape($obj->email)."')";
595  }
596  if (!$this->db->query($sql)) {
597  dol_print_error($this->db);
598  }
599  } else {
600  $error++;
601  $this->errors[] = $mailfile->error;
602  }
603  } else {
604  dol_syslog("No notification sent for ".$sendto." because email is empty");
605  }
606  $i++;
607  }
608  } else {
609  dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".(empty($object->socid) ? '' : $object->socid));
610  }
611  } else {
612  $error++;
613  $this->errors[] = $this->db->lasterror();
614  dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
615  return -1;
616  }
617 
618  // Check notification using fixed email
619  if (!$error) {
620  foreach ($conf->global as $key => $val) {
621  $reg = array();
622  if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) {
623  continue;
624  }
625 
626  $threshold = (float) $reg[1];
627  if (!empty($object->total_ht) && $object->total_ht <= $threshold) {
628  dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
629  continue;
630  }
631 
632  $param = 'NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_'.$reg[1];
633 
634  $sendto = $conf->global->$param;
635  $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
636  if ($notifcodedefid <= 0) {
637  dol_print_error($this->db, 'Failed to get id from code');
638  }
639  $trackid = '';
640 
641  $object_type = '';
642  $link = '';
643  $num++;
644 
645  $subject = '['.$mysoc->name.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');
646 
647  switch ($notifcode) {
648  case 'BILL_VALIDATE':
649  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
650  $dir_output = $conf->facture->dir_output;
651  $object_type = 'facture';
652  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link);
653  break;
654  case 'BILL_PAYED':
655  $link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'">'.$newref.'</a>';
656  $dir_output = $conf->facture->dir_output;
657  $object_type = 'facture';
658  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link);
659  break;
660  case 'ORDER_VALIDATE':
661  $link = '<a href="'.$urlwithroot.'/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
662  $dir_output = $conf->commande->dir_output;
663  $object_type = 'order';
664  $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link);
665  break;
666  case 'PROPAL_VALIDATE':
667  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
668  $dir_output = $conf->propal->multidir_output[$object->entity];
669  $object_type = 'propal';
670  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link);
671  break;
672  case 'PROPAL_CLOSE_SIGNED':
673  $link = '<a href="'.$urlwithroot.'/comm/propal/card.php?id='.$object->id.'">'.$newref.'</a>';
674  $dir_output = $conf->propal->multidir_output[$object->entity];
675  $object_type = 'propal';
676  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link);
677  break;
678  case 'FICHINTER_ADD_CONTACT':
679  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
680  $dir_output = $conf->ficheinter->dir_output;
681  $object_type = 'ficheinter';
682  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link);
683  break;
684  case 'FICHINTER_VALIDATE':
685  $link = '<a href="'.$urlwithroot.'/fichinter/card.php?id='.$object->id.'">'.$newref.'</a>';
686  $dir_output = $conf->facture->dir_output;
687  $object_type = 'ficheinter';
688  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link);
689  break;
690  case 'ORDER_SUPPLIER_VALIDATE':
691  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
692  $dir_output = $conf->fournisseur->commande->dir_output;
693  $object_type = 'order_supplier';
694  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
695  $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy", $link, $user->getFullName($langs));
696  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
697  break;
698  case 'ORDER_SUPPLIER_APPROVE':
699  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
700  $dir_output = $conf->fournisseur->commande->dir_output;
701  $object_type = 'order_supplier';
702  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
703  $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($langs));
704  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
705  break;
706  case 'ORDER_SUPPLIER_APPROVE2':
707  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
708  $dir_output = $conf->fournisseur->commande->dir_output;
709  $object_type = 'order_supplier';
710  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
711  $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy", $link, $user->getFullName($langs));
712  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
713  break;
714  case 'ORDER_SUPPLIER_REFUSE':
715  $link = '<a href="'.$urlwithroot.'/fourn/commande/card.php?id='.$object->id.'">'.$newref.'</a>';
716  $dir_output = $conf->fournisseur->dir_output.'/commande/';
717  $object_type = 'order_supplier';
718  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
719  $mesg .= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy", $link, $user->getFullName($langs));
720  $mesg .= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
721  break;
722  case 'SHIPPING_VALIDATE':
723  $link = '<a href="'.$urlwithroot.'/expedition/card.php?id='.$object->id.'">'.$newref.'</a>';
724  $dir_output = $conf->expedition->dir_output.'/sending/';
725  $object_type = 'order_supplier';
726  $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link);
727  break;
728  case 'EXPENSE_REPORT_VALIDATE':
729  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
730  $dir_output = $conf->expensereport->dir_output;
731  $object_type = 'expensereport';
732  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link);
733  break;
734  case 'EXPENSE_REPORT_APPROVE':
735  $link = '<a href="'.$urlwithroot.'/expensereport/card.php?id='.$object->id.'">'.$newref.'</a>';
736  $dir_output = $conf->expensereport->dir_output;
737  $object_type = 'expensereport';
738  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link);
739  break;
740  case 'HOLIDAY_VALIDATE':
741  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
742  $dir_output = $conf->holiday->dir_output;
743  $object_type = 'holiday';
744  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link);
745  break;
746  case 'HOLIDAY_APPROVE':
747  $link = '<a href="'.$urlwithroot.'/holiday/card.php?id='.$object->id.'">'.$newref.'</a>';
748  $dir_output = $conf->holiday->dir_output;
749  $object_type = 'holiday';
750  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link);
751  break;
752  }
753  $ref = dol_sanitizeFileName($newref);
754  $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
755  if (!dol_is_file($pdf_path)) {
756  // We can't add PDF as it is not generated yet.
757  $filepdf = '';
758  } else {
759  $filepdf = $pdf_path;
760  }
761 
762  $message .= $langs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name)."\n";
763  $message .= "\n";
764  $message .= $mesg;
765 
766  $message = nl2br($message);
767 
768  // Replace keyword __SUPERVISOREMAIL__
769  if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) {
770  $newval = '';
771  if ($user->fk_user > 0) {
772  $supervisoruser = new User($this->db);
773  $supervisoruser->fetch($user->fk_user);
774  if ($supervisoruser->email) {
775  $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
776  }
777  }
778  dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
779  $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
780  $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
781  $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
782  $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
783  }
784 
785  if ($sendto) {
786  $parameters = array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
787  $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
788  if (empty($reshook)) {
789  if (!empty($hookmanager->resArray['subject'])) {
790  $subject .= $hookmanager->resArray['subject'];
791  }
792  if (!empty($hookmanager->resArray['message'])) {
793  $message .= $hookmanager->resArray['message'];
794  }
795  }
796  $mailfile = new CMailFile(
797  $subject,
798  $sendto,
799  $replyto,
800  $message,
801  $filename_list,
802  $mimetype_list,
803  $mimefilename_list,
804  '',
805  '',
806  0,
807  1,
808  '',
809  $trackid,
810  '',
811  '',
812  'notification'
813  );
814 
815  if ($mailfile->sendfile()) {
816  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
817  $sql .= " VALUES ('".$this->db->idate(dol_now())."', ".$notifcodedefid.", ".($object->socid ? $object->socid : 'null').", null, 'email', 'tofixedemail', '".$object_type."', ".$object->id.", '".$this->db->escape($conf->global->$param)."')";
818  if (!$this->db->query($sql)) {
819  dol_print_error($this->db);
820  }
821  } else {
822  $error++;
823  $this->errors[] = $mailfile->error;
824  }
825  }
826  }
827  }
828 
829  if (!$error) {
830  return $num;
831  } else {
832  return -1 * $error;
833  }
834  }
835 }
Class to manage notifications.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
getNotificationsArray($notifcode, $socid=0, $object=null, $userid=0, $scope=array('thirdparty', 'user', 'global'))
Return number of notifications activated for action code (and third party)
confirmMessage($action, $socid, $object)
Return message that say how many notification (and to which email) will occurs on requested event...
send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
Check if notification are active for couple action/company.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage hooks.
Class to manage projects.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile-&gt;sendfile();.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class to manage translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:457
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
div float
Buy price without taxes.
Definition: style.css.php:650
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
__construct($db)
Constructor.
isValidEmail($address, $acceptsupervisorkey=0)
Return true if email syntax is ok.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.