dolibarr  9.0.0
notify.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
6  *
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 <http://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  function __construct($db)
94  {
95  $this->db = $db;
96  }
97 
98 
108  function confirmMessage($action,$socid,$object)
109  {
110  global $langs;
111  $langs->load("mails");
112 
113  $listofnotiftodo=$this->getNotificationsArray($action,$socid,$object,0);
114 
115  $nb=-1;
116  if (is_array($listofnotiftodo)) $nb=count($listofnotiftodo);
117  if ($nb < 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ErrorFailedToGetListOfNotificationsToSend");
118  if ($nb == 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("NoNotificationsWillBeSent");
119  if ($nb == 1) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ANotificationsWillBeSent");
120  if ($nb >= 2) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("SomeNotificationsWillBeSent",$nb);
121 
122  if (is_array($listofnotiftodo))
123  {
124  $i=0;
125  foreach ($listofnotiftodo as $key => $val)
126  {
127  if ($i) $texte.=', ';
128  else $texte.=' (';
129  if ($val['isemailvalid']) $texte.=$val['email'];
130  else $texte.=$val['emaildesc'];
131  $i++;
132  }
133  if ($i) $texte.=')';
134  }
135 
136  return $texte;
137  }
138 
149  function getNotificationsArray($notifcode, $socid=0, $object=null, $userid=0, $scope=array('thirdparty', 'user', 'global'))
150  {
151  global $conf, $user;
152 
153  $error=0;
154  $resarray=array();
155 
156  $valueforthreshold = 0;
157  if (is_object($object)) $valueforthreshold = $object->total_ht;
158 
159  if (! $error)
160  {
161  if ($socid >= 0 && in_array('thirdparty', $scope))
162  {
163  $sql = "SELECT a.code, c.email, c.rowid";
164  $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
165  $sql.= " ".MAIN_DB_PREFIX."socpeople as c,";
166  $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
167  $sql.= " ".MAIN_DB_PREFIX."societe as s";
168  $sql.= " WHERE n.fk_contact = c.rowid";
169  $sql.= " AND a.rowid = n.fk_action";
170  $sql.= " AND n.fk_soc = s.rowid";
171  if ($notifcode)
172  {
173  if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
174  else $sql.= " AND a.code = '".$notifcode."'"; // New usage
175  }
176  $sql.= " AND s.entity IN (".getEntity('societe').")";
177  if ($socid > 0) $sql.= " AND s.rowid = ".$socid;
178 
179  dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
180 
181  $resql = $this->db->query($sql);
182  if ($resql)
183  {
184  $num = $this->db->num_rows($resql);
185  $i=0;
186  while ($i < $num)
187  {
188  $obj = $this->db->fetch_object($resql);
189  if ($obj)
190  {
191  $newval2=trim($obj->email);
192  $isvalid=isValidEmail($newval2);
193  if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'tocontact', 'code'=>trim($obj->code), 'emaildesc'=>'Contact id '.$obj->rowid, 'email'=>$newval2, 'contactid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
194  }
195  $i++;
196  }
197  }
198  else
199  {
200  $error++;
201  $this->error=$this->db->lasterror();
202  }
203  }
204  }
205 
206  if (! $error)
207  {
208  if ($userid >= 0 && in_array('user', $scope))
209  {
210  $sql = "SELECT a.code, c.email, c.rowid";
211  $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
212  $sql.= " ".MAIN_DB_PREFIX."user as c,";
213  $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a";
214  $sql.= " WHERE n.fk_user = c.rowid";
215  $sql.= " AND a.rowid = n.fk_action";
216  if ($notifcode)
217  {
218  if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
219  else $sql.= " AND a.code = '".$notifcode."'"; // New usage
220  }
221  $sql.= " AND c.entity IN (".getEntity('user').")";
222  if ($userid > 0) $sql.= " AND c.rowid = ".$userid;
223 
224  dol_syslog(__METHOD__." ".$notifcode.", ".$socid."", LOG_DEBUG);
225 
226  $resql = $this->db->query($sql);
227  if ($resql)
228  {
229  $num = $this->db->num_rows($resql);
230  $i=0;
231  while ($i < $num)
232  {
233  $obj = $this->db->fetch_object($resql);
234  if ($obj)
235  {
236  $newval2=trim($obj->email);
237  $isvalid=isValidEmail($newval2);
238  if (empty($resarray[$newval2])) $resarray[$newval2] = array('type'=> 'touser', 'code'=>trim($obj->code), 'emaildesc'=>'User id '.$obj->rowid, 'email'=>$newval2, 'userid'=>$obj->rowid, 'isemailvalid'=>$isvalid);
239  }
240  $i++;
241  }
242  }
243  else
244  {
245  $error++;
246  $this->error=$this->db->lasterror();
247  }
248  }
249  }
250 
251  if (! $error)
252  {
253  if (in_array('global', $scope))
254  {
255  // List of notifications enabled for fixed email
256  foreach($conf->global as $key => $val)
257  {
258  if ($notifcode)
259  {
260  if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
261  }
262  else
263  {
264  if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
265  }
266 
267  $threshold = (float) $reg[1];
268  if ($valueforthreshold < $threshold) continue;
269 
270  $tmpemail=explode(',',$val);
271  foreach($tmpemail as $key2 => $val2)
272  {
273  $newval2=trim($val2);
274  if ($newval2 == '__SUPERVISOREMAIL__')
275  {
276  if ($user->fk_user > 0)
277  {
278  $tmpuser=new User($this->db);
279  $tmpuser->fetch($user->fk_user);
280  if ($tmpuser->email) $newval2=trim($tmpuser->email);
281  else $newval2='';
282  }
283  else $newval2='';
284  }
285  if ($newval2)
286  {
287  $isvalid=isValidEmail($newval2, 0);
288  if (empty($resarray[$newval2])) $resarray[$newval2]=array('type'=> 'tofixedemail', 'code'=>trim($key), 'emaildesc'=>trim($val2), 'email'=>$newval2, 'isemailvalid'=>$isvalid);
289  }
290  }
291  }
292  }
293  }
294 
295  if ($error) return -1;
296 
297  //var_dump($resarray);
298  return $resarray;
299  }
300 
312  function send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
313  {
314  global $user,$conf,$langs,$mysoc;
315  global $hookmanager;
316  global $dolibarr_main_url_root;
317 
318  if (! in_array($notifcode, $this->arrayofnotifsupported)) return 0;
319 
320  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
321  if (! is_object($hookmanager))
322  {
323  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
324  $hookmanager=new HookManager($this->db);
325  }
326  $hookmanager->initHooks(array('notification'));
327 
328  dol_syslog(get_class($this)."::send notifcode=".$notifcode.", object=".$object->id);
329 
330  $langs->load("other");
331 
332  // Define $urlwithroot
333  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
334  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
335  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
336 
337  // Define some vars
338  $application = 'Dolibarr';
339  if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $application = $conf->global->MAIN_APPLICATION_TITLE;
340  $replyto = $conf->notification->email_from;
341  $object_type = '';
342  $link = '';
343  $num = 0;
344 
345  $oldref=(empty($object->oldref)?$object->ref:$object->oldref);
346  $newref=(empty($object->newref)?$object->ref:$object->newref);
347 
348  $sql = '';
349 
350  // Check notification per third party
351  if ($object->socid > 0)
352  {
353  $sql.= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,";
354  $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
355  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
356  $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
357  $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
358  $sql.= " ".MAIN_DB_PREFIX."societe as s";
359  $sql.= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
360  $sql.= " AND n.fk_soc = s.rowid";
361  if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
362  else $sql.= " AND a.code = '".$notifcode."'"; // New usage
363  $sql .= " AND s.rowid = ".$object->socid;
364 
365  $sql.= "\nUNION\n";
366  }
367 
368  // Check notification per user
369  $sql.= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,";
370  $sql.= " a.rowid as adid, a.label, a.code, n.rowid, n.type";
371  $sql.= " FROM ".MAIN_DB_PREFIX."user as c,";
372  $sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
373  $sql.= " ".MAIN_DB_PREFIX."notify_def as n";
374  $sql.= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action";
375  if (is_numeric($notifcode)) $sql.= " AND n.fk_action = ".$notifcode; // Old usage
376  else $sql.= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage
377 
378  $result = $this->db->query($sql);
379  if ($result)
380  {
381  $num = $this->db->num_rows($result);
382  $projtitle='';
383  if (! empty($object->fk_project))
384  {
385  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
386  $proj = new Project($this->db);
387  $proj->fetch($object->fk_project);
388  $projtitle='('.$proj->title.')';
389  }
390 
391  if ($num > 0)
392  {
393  $i = 0;
394  while ($i < $num && ! $error) // For each notification couple defined (third party/actioncode)
395  {
396  $obj = $this->db->fetch_object($result);
397 
398  $sendto = dolGetFirstLastname($obj->firstname,$obj->lastname) . " <".$obj->email.">";
399  $notifcodedefid = $obj->adid;
400 
401  if (dol_strlen($obj->email))
402  {
403  // Set output language
404  $outputlangs = $langs;
405  if ($obj->default_lang && $obj->default_lang != $langs->defaultlang)
406  {
407  $outputlangs = new Translate('', $conf);
408  $outputlangs->setDefaultLang($obj->default_lang);
409  $outputlangs->loadLangs(array("main","other"));
410  }
411 
412  $subject = '['.$mysoc->name.'] '.$outputlangs->transnoentitiesnoconv("DolibarrNotification").($projtitle?' '.$projtitle:'');
413 
414  switch ($notifcode) {
415  case 'BILL_VALIDATE':
416  $link='/compta/facture/card.php?facid='.$object->id;
417  $dir_output = $conf->facture->dir_output;
418  $object_type = 'facture';
419  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated",$newref);
420  break;
421  case 'BILL_PAYED':
422  $link='/compta/facture/card.php?facid='.$object->id;
423  $dir_output = $conf->facture->dir_output;
424  $object_type = 'facture';
425  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed",$newref);
426  break;
427  case 'ORDER_VALIDATE':
428  $link='/commande/card.php?id='.$object->id;
429  $dir_output = $conf->commande->dir_output;
430  $object_type = 'order';
431  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated",$newref);
432  break;
433  case 'PROPAL_VALIDATE':
434  $link='/comm/propal/card.php?id='.$object->id;
435  $dir_output = $conf->propal->multidir_output[$object->entity];
436  $object_type = 'propal';
437  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated",$newref);
438  break;
439  case 'PROPAL_CLOSE_SIGNED':
440  $link='/comm/propal/card.php?id='.$object->id;
441  $dir_output = $conf->propal->multidir_output[$object->entity];
442  $object_type = 'propal';
443  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned",$newref);
444  break;
445  case 'FICHINTER_ADD_CONTACT':
446  $link='/fichinter/card.php?id='.$object->id;
447  $dir_output = $conf->ficheinter->dir_output;
448  $object_type = 'ficheinter';
449  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$newref);
450  break;
451  case 'FICHINTER_VALIDATE':
452  $link='/fichinter/card.php?id='.$object->id;
453  $dir_output = $conf->ficheinter->dir_output;
454  $object_type = 'ficheinter';
455  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated",$newref);
456  break;
457  case 'ORDER_SUPPLIER_VALIDATE':
458  $link='/fourn/commande/card.php?id='.$object->id;
459  $dir_output = $conf->fournisseur->commande->dir_output;
460  $object_type = 'order_supplier';
461  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
462  $mesg.= $outputlangs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$newref,$user->getFullName($langs));
463  $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
464  break;
465  case 'ORDER_SUPPLIER_APPROVE':
466  $link='/fourn/commande/card.php?id='.$object->id;
467  $dir_output = $conf->fournisseur->commande->dir_output;
468  $object_type = 'order_supplier';
469  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
470  $mesg.= $outputlangs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$newref,$user->getFullName($langs));
471  $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
472  break;
473  case 'ORDER_SUPPLIER_REFUSE':
474  $link='/fourn/commande/card.php?id='.$object->id;
475  $dir_output = $conf->fournisseur->commande->dir_output;
476  $object_type = 'order_supplier';
477  $mesg = $outputlangs->transnoentitiesnoconv("Hello").",\n\n";
478  $mesg.= $outputlangs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$newref,$user->getFullName($langs));
479  $mesg.= "\n\n".$outputlangs->transnoentitiesnoconv("Sincerely").".\n\n";
480  break;
481  case 'SHIPPING_VALIDATE':
482  $dir_output = $conf->expedition->dir_output.'/sending/';
483  $object_type = 'order_supplier';
484  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
485  break;
486  case 'EXPENSE_REPORT_VALIDATE':
487  $dir_output = $conf->expensereport->dir_output;
488  $object_type = 'expensereport';
489  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated",$newref);
490  break;
491  case 'EXPENSE_REPORT_APPROVE':
492  $dir_output = $conf->expensereport->dir_output;
493  $object_type = 'expensereport';
494  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved",$newref);
495  break;
496  case 'HOLIDAY_VALIDATE':
497  $dir_output = $conf->holiday->dir_output;
498  $object_type = 'holiday';
499  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated",$newref);
500  break;
501  case 'HOLIDAY_APPROVE':
502  $dir_output = $conf->holiday->dir_output;
503  $object_type = 'holiday';
504  $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved",$newref);
505  break;
506  }
507  $ref = dol_sanitizeFileName($newref);
508  $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
509  if (! dol_is_file($pdf_path))
510  {
511  // We can't add PDF as it is not generated yet.
512  $filepdf = '';
513  }
514  else
515  {
516  $filepdf = $pdf_path;
517  }
518 
519  $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
520  $message.= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
521  $message.= "\n";
522  $message.= $mesg;
523  if ($link) $message.= "\n" . $urlwithroot . $link;
524 
525  $parameters=array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
526  $reshook=$hookmanager->executeHooks('formatNotificationMessage',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
527  if (empty($reshook))
528  {
529  if (! empty($hookmanager->resArray['subject'])) $subject.=$hookmanager->resArray['subject'];
530  if (! empty($hookmanager->resArray['message'])) $message.=$hookmanager->resArray['message'];
531  }
532 
533  $mailfile = new CMailFile(
534  $subject,
535  $sendto,
536  $replyto,
537  $message,
538  $filename_list,
539  $mimetype_list,
540  $mimefilename_list,
541  '',
542  '',
543  0,
544  -1
545  );
546 
547  if ($mailfile->sendfile())
548  {
549  if ($obj->type_target == 'touserid') {
550  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)";
551  $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)."')";
552  }
553  else {
554  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)";
555  $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)."')";
556  }
557  if (! $this->db->query($sql))
558  {
559  dol_print_error($this->db);
560  }
561  }
562  else
563  {
564  $error++;
565  $this->errors[]=$mailfile->error;
566  }
567  }
568  else
569  {
570  dol_syslog("No notification sent for ".$sendto." because email is empty");
571  }
572  $i++;
573  }
574  }
575  else
576  {
577  dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = ".$object->socid);
578  }
579  }
580  else
581  {
582  $error++;
583  $this->errors[]=$this->db->lasterror();
584  dol_syslog("Failed to get list of notification to send ".$this->db->lasterror(), LOG_ERR);
585  return -1;
586  }
587 
588  // Check notification using fixed email
589  if (! $error)
590  {
591  foreach($conf->global as $key => $val)
592  {
593  if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) continue;
594 
595  $threshold = (float) $reg[1];
596  if (!empty($object->total_ht) && $object->total_ht <= $threshold)
597  {
598  dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification");
599  continue;
600  }
601 
602  $param='NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_'.$reg[1];
603 
604  $sendto = $conf->global->$param;
605  $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid');
606  if ($notifcodedefid <= 0) dol_print_error($this->db, 'Failed to get id from code');
607 
608  $object_type = '';
609  $link = '';
610  $num++;
611 
612  $subject = '['.$mysoc->name.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle?' '.$projtitle:'');
613 
614  switch ($notifcode) {
615  case 'BILL_VALIDATE':
616  $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '">' . $newref . '</a>';
617  $dir_output = $conf->facture->dir_output;
618  $object_type = 'facture';
619  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$link);
620  break;
621  case 'BILL_PAYED':
622  $link ='<a href="' . $urlwithroot . '/compta/facture/card.php?facid='.$object->id . '">' . $newref . '</a>';
623  $dir_output = $conf->facture->dir_output;
624  $object_type = 'facture';
625  $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed",$link);
626  break;
627  case 'ORDER_VALIDATE':
628  $link = '<a href="' . $urlwithroot . '/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
629  $dir_output = $conf->commande->dir_output;
630  $object_type = 'order';
631  $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$link);
632  break;
633  case 'PROPAL_VALIDATE':
634  $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
635  $dir_output = $conf->propal->multidir_output[$object->entity];
636  $object_type = 'propal';
637  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$link);
638  break;
639  case 'PROPAL_CLOSE_SIGNED':
640  $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id='.$object->id . '">' . $newref . '</a>';
641  $dir_output = $conf->propal->multidir_output[$object->entity];
642  $object_type = 'propal';
643  $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned",$link);
644  break;
645  case 'FICHINTER_ADD_CONTACT':
646  $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id='.$object->id . '">' . $newref . '</a>';
647  $dir_output = $conf->facture->dir_output;
648  $object_type = 'ficheinter';
649  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact",$link);
650  break;
651  case 'FICHINTER_VALIDATE':
652  $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id='.$object->id . '">' . $newref . '</a>';
653  $dir_output = $conf->facture->dir_output;
654  $object_type = 'ficheinter';
655  $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$link);
656  break;
657  case 'ORDER_SUPPLIER_VALIDATE':
658  $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
659  $dir_output = $conf->fournisseur->commande->dir_output;
660  $object_type = 'order_supplier';
661  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
662  $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderValidatedBy",$link,$user->getFullName($langs));
663  $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
664  break;
665  case 'ORDER_SUPPLIER_APPROVE':
666  $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
667  $dir_output = $conf->fournisseur->commande->dir_output;
668  $object_type = 'order_supplier';
669  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
670  $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$link,$user->getFullName($langs));
671  $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
672  break;
673  case 'ORDER_SUPPLIER_APPROVE2':
674  $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
675  $dir_output = $conf->fournisseur->commande->dir_output;
676  $object_type = 'order_supplier';
677  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
678  $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$link,$user->getFullName($langs));
679  $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
680  break;
681  case 'ORDER_SUPPLIER_REFUSE':
682  $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id='.$object->id . '">' . $newref . '</a>';
683  $dir_output = $conf->fournisseur->dir_output.'/commande/';
684  $object_type = 'order_supplier';
685  $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
686  $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$link,$user->getFullName($langs));
687  $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
688  break;
689  case 'SHIPPING_VALIDATE':
690  $dir_output = $conf->expedition->dir_output.'/sending/';
691  $object_type = 'order_supplier';
692  $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated",$newref);
693  break;
694  case 'EXPENSE_REPORT_VALIDATE':
695  $dir_output = $conf->expensereport->dir_output;
696  $object_type = 'expensereport';
697  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated",$newref);
698  break;
699  case 'EXPENSE_REPORT_APPROVE':
700  $dir_output = $conf->expensereport->dir_output;
701  $object_type = 'expensereport';
702  $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved",$newref);
703  break;
704  case 'HOLIDAY_VALIDATE':
705  $dir_output = $conf->holiday->dir_output;
706  $object_type = 'holiday';
707  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated",$newref);
708  break;
709  case 'HOLIDAY_APPROVE':
710  $dir_output = $conf->holiday->dir_output;
711  $object_type = 'holiday';
712  $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved",$newref);
713  break;
714  }
715  $ref = dol_sanitizeFileName($newref);
716  $pdf_path = $dir_output."/".$ref."/".$ref.".pdf";
717  if (! dol_is_file($pdf_path))
718  {
719  // We can't add PDF as it is not generated yet.
720  $filepdf = '';
721  }
722  else
723  {
724  $filepdf = $pdf_path;
725  }
726 
727  $message = $langs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
728  $message.= $langs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
729  $message.= "\n";
730  $message.= $mesg;
731  //if ($link) $message.= "\n" . $urlwithroot . $link; // link already added around the ref into the text
732 
733  $message = nl2br($message);
734 
735  // Replace keyword __SUPERVISOREMAIL__
736  if (preg_match('/__SUPERVISOREMAIL__/', $sendto))
737  {
738  $newval='';
739  if ($user->fk_user > 0)
740  {
741  $supervisoruser=new User($this->db);
742  $supervisoruser->fetch($user->fk_user);
743  if ($supervisoruser->email) $newval=trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname).' <'.$supervisoruser->email.'>');
744  }
745  dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with ".$newval);
746  $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto);
747  $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
748  $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string
749  $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string
750  }
751 
752  if ($sendto)
753  {
754  $parameters=array('notifcode'=>$notifcode, 'sendto'=>$sendto, 'replyto'=>$replyto, 'file'=>$filename_list, 'mimefile'=>$mimetype_list, 'filename'=>$mimefilename_list);
755  $reshook=$hookmanager->executeHooks('formatNotificationMessage',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
756  if (empty($reshook))
757  {
758  if (! empty($hookmanager->resArray['subject'])) $subject.=$hookmanager->resArray['subject'];
759  if (! empty($hookmanager->resArray['message'])) $message.=$hookmanager->resArray['message'];
760  }
761  $mailfile = new CMailFile(
762  $subject,
763  $sendto,
764  $replyto,
765  $message,
766  $filename_list,
767  $mimetype_list,
768  $mimefilename_list,
769  '',
770  '',
771  0,
772  1
773  );
774 
775  if ($mailfile->sendfile())
776  {
777  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)";
778  $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)."')";
779  if (! $this->db->query($sql))
780  {
781  dol_print_error($this->db);
782  }
783  }
784  else
785  {
786  $error++;
787  $this->errors[]=$mailfile->error;
788  }
789  }
790  }
791  }
792 
793  if (! $error) return $num;
794  else return -1 * $error;
795  }
796 }
Class to manage notifications.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
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...
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
send($notifcode, $object, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array())
Check if notification are active for couple action/company.
Class to manage hooks.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage projects.
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); $mailfile->sendfile();.
Class to manage translations.
dol_now($mode='gmt')
Return date for now.
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:451
__construct($db)
Constructor.
isValidEmail($address, $acceptsupervisorkey=0)
Return true if email syntax is ok.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
div float
Buy price without taxes.
Definition: style.css.php:564
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.