dolibarr 20.0.4
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
28include_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
29
30
35{
36 public $listofmanagedevents = array();
37
43 public function __construct($db)
44 {
45 $this->db = $db;
46
47 $this->name = preg_replace('/^Interface/i', '', get_class($this));
48 $this->family = "notification";
49 $this->description = "Triggers of this module send Email notifications according to Notification module setup.";
50 $this->version = self::VERSIONS['prod'];
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 $resultSend = $notify->send($action, $object);
98 if ($resultSend < 0) {
99 if (!isset($this->errors)) {
100 $this->errors = [];
101 }
102 $this->errors = array_merge($this->errors, $notify->errors);
103 return $resultSend;
104 }
105
106 return 1;
107 }
108
114 public function getListOfManagedEvents()
115 {
116 global $conf, $action;
117 global $hookmanager;
118
119 if (!is_object($hookmanager)) {
120 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
121 $hookmanager = new HookManager($this->db);
122 }
123 $hookmanager->initHooks(array('notification'));
124
125 $parameters = array();
126 $object = new stdClass();
127 $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
128 if (empty($reshook)) {
129 if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
130 $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
131 }
132 }
133
134 $ret = array();
135
136
137 $sql = "SELECT rowid, code, contexts, label, description, elementtype";
138 $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
139 $sql .= $this->db->order("rang, elementtype, code");
140
141 dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
142 $resql = $this->db->query($sql);
143 if ($resql) {
144 $num = $this->db->num_rows($resql);
145 $i = 0;
146 while ($i < $num) {
147 $obj = $this->db->fetch_object($resql);
148
149 $qualified = 0;
150 // Check is this event is supported by notification module
151 if (in_array($obj->code, $this->listofmanagedevents)) {
152 $qualified = 1;
153 }
154 // Check if module for this event is active
155 if ($qualified) {
156 //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
157 $element = $obj->elementtype;
158
159 // Exclude events if related module is disabled
160 if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
161 $qualified = 0;
162 } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
163 $qualified = 0;
164 } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
165 $qualified = 0;
166 } elseif ($element == 'shipping' && !isModEnabled('shipping')) {
167 $qualified = 0;
168 } elseif ($element == 'member' && !isModEnabled('member')) {
169 $qualified = 0;
170 } elseif (($element == 'expense_report' || $element == 'expensereport') && !isModEnabled('expensereport')) {
171 $qualified = 0;
172 } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
173 $qualified = 0;
174 }
175 }
176
177 if ($qualified) {
178 $ret[] = array('rowid' => $obj->rowid, 'code' => $obj->code, 'contexts' => $obj->contexts, 'label' => $obj->label, 'description' => $obj->description, 'elementtype' => $obj->elementtype);
179 }
180
181 $i++;
182 }
183 } else {
184 dol_print_error($this->db);
185 }
186
187 return $ret;
188 }
189}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to stock current configuration.
Class that all triggers must inherit.
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 Dolibarr business event is done.
Class to manage the table of subscription to notifications.
Class to manage translations.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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:140