dolibarr  19.0.0-dev
interface_50_modNotification_Notification.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2011 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2013-2014 Marcos GarcĂ­a <marcosgdf@gmail.com>
5  * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
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/triggers/dolibarrtriggers.class.php';
27 include_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
28 
29 
34 {
35  public $listofmanagedevents = array();
36 
42  public function __construct($db)
43  {
44  $this->db = $db;
45 
46  $this->name = preg_replace('/^Interface/i', '', get_class($this));
47  $this->family = "notification";
48  $this->description = "Triggers of this module send Email notifications according to Notification module setup.";
49  // 'development', 'experimental', 'dolibarr' or version
50  $this->version = self::VERSION_DOLIBARR;
51  $this->picto = 'email';
52 
53  $this->listofmanagedevents = Notify::$arrayofnotifsupported;
54  }
55 
67  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
68  {
69  global $hookmanager;
70 
71  if (empty($conf->notification) || !isModEnabled('notification')) {
72  return 0; // Module not active, we do nothing
73  }
74 
75  if (!is_object($hookmanager)) {
76  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
77  $hookmanager = new HookManager($this->db);
78  }
79  $hookmanager->initHooks(array('notification'));
80 
81  $parameters = array();
82  $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
83  if (empty($reshook)) {
84  if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
85  $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
86  }
87  }
88 
89  // If the trigger code is not managed by the Notification module
90  if (!in_array($action, $this->listofmanagedevents)) {
91  return 0;
92  }
93 
94  dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
95 
96  $notify = new Notify($this->db);
97  $notify->send($action, $object);
98 
99  return 1;
100  }
101 
107  public function getListOfManagedEvents()
108  {
109  global $conf, $action;
110  global $hookmanager;
111 
112  if (!is_object($hookmanager)) {
113  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
114  $hookmanager = new HookManager($this->db);
115  }
116  $hookmanager->initHooks(array('notification'));
117 
118  $parameters = array();
119  $object = new stdClass();
120  $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
121  if (empty($reshook)) {
122  if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
123  $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
124  }
125  }
126 
127  $ret = array();
128 
129 
130  $sql = "SELECT rowid, code, contexts, label, description, elementtype";
131  $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
132  $sql .= $this->db->order("rang, elementtype, code");
133 
134  dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
135  $resql = $this->db->query($sql);
136  if ($resql) {
137  $num = $this->db->num_rows($resql);
138  $i = 0;
139  while ($i < $num) {
140  $obj = $this->db->fetch_object($resql);
141 
142  $qualified = 0;
143  // Check is this event is supported by notification module
144  if (in_array($obj->code, $this->listofmanagedevents)) {
145  $qualified = 1;
146  }
147  // Check if module for this event is active
148  if ($qualified) {
149  //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
150  $element = $obj->elementtype;
151 
152  // Exclude events if related module is disabled
153  if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
154  $qualified = 0;
155  } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
156  $qualified = 0;
157  } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
158  $qualified = 0;
159  } elseif ($element == 'shipping' && !isModEnabled('expedition')) {
160  $qualified = 0;
161  } elseif ($element == 'member' && !isModEnabled('adherent')) {
162  $qualified = 0;
163  } elseif (($element == 'expense_report' || $element == 'expensereport') && !isModEnabled('expensereport')) {
164  $qualified = 0;
165  } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
166  $qualified = 0;
167  }
168  }
169 
170  if ($qualified) {
171  $ret[] = array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'contexts'=>$obj->contexts, 'label'=>$obj->label, 'description'=>$obj->description, 'elementtype'=>$obj->elementtype);
172  }
173 
174  $i++;
175  }
176  } else {
177  dol_print_error($this->db);
178  }
179 
180  return $ret;
181  }
182 }
Class to stock current configuration.
Definition: conf.class.php:34
Class that all the triggers must extend.
Class to manage hooks.
Class of triggers for notification module.
getListOfManagedEvents()
Return list of events managed by notification module.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
Class to manage notifications.
Class to manage translations.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:123