dolibarr 19.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 *
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
26require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27include_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 $result = $notify->send($action, $object);
98
99 if ($result < 0) {
100 $this->errors = array_merge($this->errors, empty($notify->error) ? array() : array($notify->error), empty($notify->errors) ? array() : $notify->errors);
101 }
102
103 return $result;
104 }
105
111 public function getListOfManagedEvents()
112 {
113 global $conf, $action;
114 global $hookmanager;
115
116 if (!is_object($hookmanager)) {
117 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
118 $hookmanager = new HookManager($this->db);
119 }
120 $hookmanager->initHooks(array('notification'));
121
122 $parameters = array();
123 $object = new stdClass();
124 $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
125 if (empty($reshook)) {
126 if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
127 $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
128 }
129 }
130
131 $ret = array();
132
133
134 $sql = "SELECT rowid, code, contexts, label, description, elementtype";
135 $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
136 $sql .= $this->db->order("rang, elementtype, code");
137
138 dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
139 $resql = $this->db->query($sql);
140 if ($resql) {
141 $num = $this->db->num_rows($resql);
142 $i = 0;
143 while ($i < $num) {
144 $obj = $this->db->fetch_object($resql);
145
146 $qualified = 0;
147 // Check is this event is supported by notification module
148 if (in_array($obj->code, $this->listofmanagedevents)) {
149 $qualified = 1;
150 }
151 // Check if module for this event is active
152 if ($qualified) {
153 //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
154 $element = $obj->elementtype;
155
156 // Exclude events if related module is disabled
157 if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
158 $qualified = 0;
159 } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
160 $qualified = 0;
161 } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
162 $qualified = 0;
163 } elseif ($element == 'shipping' && !isModEnabled('expedition')) {
164 $qualified = 0;
165 } elseif ($element == 'member' && !isModEnabled('adherent')) {
166 $qualified = 0;
167 } elseif (($element == 'expense_report' || $element == 'expensereport') && !isModEnabled('expensereport')) {
168 $qualified = 0;
169 } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
170 $qualified = 0;
171 }
172 }
173
174 if ($qualified) {
175 $ret[] = array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'contexts'=>$obj->contexts, 'label'=>$obj->label, 'description'=>$obj->description, 'elementtype'=>$obj->elementtype);
176 }
177
178 $i++;
179 }
180 } else {
181 dol_print_error($this->db);
182 }
183
184 return $ret;
185 }
186}
Class to stock current configuration.
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.
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='', $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:124