dolibarr  18.0.0
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  if (empty($conf->notification) || !isModEnabled('notification')) {
70  return 0; // Module not active, we do nothing
71  }
72 
73  // If the trigger code is not managed by the Notification module
74  if (!in_array($action, $this->listofmanagedevents)) {
75  return 0;
76  }
77 
78  dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
79 
80  $notify = new Notify($this->db);
81  $notify->send($action, $object);
82 
83  return 1;
84  }
85 
86 
92  public function getListOfManagedEvents()
93  {
94  global $conf, $action;
95  global $hookmanager;
96 
97  if (!is_object($hookmanager)) {
98  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
99  $hookmanager = new HookManager($this->db);
100  }
101  $hookmanager->initHooks(array('notification'));
102 
103  $parameters = array();
104  $object = new stdClass();
105  $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
106  if (empty($reshook)) {
107  if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
108  $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
109  }
110  }
111 
112  $ret = array();
113 
114 
115  $sql = "SELECT rowid, code, contexts, label, description, elementtype";
116  $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
117  $sql .= $this->db->order("rang, elementtype, code");
118 
119  dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
120  $resql = $this->db->query($sql);
121  if ($resql) {
122  $num = $this->db->num_rows($resql);
123  $i = 0;
124  while ($i < $num) {
125  $obj = $this->db->fetch_object($resql);
126 
127  $qualified = 0;
128  // Check is this event is supported by notification module
129  if (in_array($obj->code, $this->listofmanagedevents)) {
130  $qualified = 1;
131  }
132  // Check if module for this event is active
133  if ($qualified) {
134  //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
135  $element = $obj->elementtype;
136 
137  // Exclude events if related module is disabled
138  if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
139  $qualified = 0;
140  } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
141  $qualified = 0;
142  } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
143  $qualified = 0;
144  } elseif ($element == 'shipping' && !isModEnabled('expedition')) {
145  $qualified = 0;
146  } elseif ($element == 'member' && !isModEnabled('adherent')) {
147  $qualified = 0;
148  } elseif (($element == 'expense_report' || $element == 'expensereport') && !isModEnabled('expensereport')) {
149  $qualified = 0;
150  } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
151  $qualified = 0;
152  }
153  }
154 
155  if ($qualified) {
156  $ret[] = array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'contexts'=>$obj->contexts, 'label'=>$obj->label, 'description'=>$obj->description, 'elementtype'=>$obj->elementtype);
157  }
158 
159  $i++;
160  }
161  } else {
162  dol_print_error($this->db);
163  }
164 
165  return $ret;
166  }
167 }
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