dolibarr 19.0.3
interfaces.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4 * Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27
28
33{
37 public $db;
38
39 public $dir; // Directory with all core and external triggers files
40
44 public $errors = array();
45
51 public function __construct($db)
52 {
53 $this->db = $db;
54 }
55
56 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
68 public function run_triggers($action, $object, $user, $langs, $conf)
69 {
70 // phpcs:enable
71
72 if (getDolGlobalInt('MAIN_TRIGGER_DEBUG')) {
73 // This his too much verbose, enabled if const enabled only
74 dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_triggers", LOG_DEBUG);
75 }
76
77 // Check parameters
78 if (!is_object($object) || !is_object($conf)) { // Error
79 $error = 'function run_triggers called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf);
80 dol_syslog(get_class($this).'::run_triggers '.$error, LOG_ERR);
81 $this->errors[] = $error;
82 return -1;
83 }
84 if (!is_object($langs)) { // Warning
85 dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
86 }
87 if (!is_object($user)) { // Warning
88 dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
89 $user = new User($this->db);
90 }
91
92 $nbfile = $nbtotal = $nbok = $nbko = 0;
93
94 $files = array();
95 $modules = array();
96 $orders = array();
97 $i = 0;
98
99
100 $dirtriggers = array_merge(array('/core/triggers'), $conf->modules_parts['triggers']);
101 foreach ($dirtriggers as $reldir) {
102 $dir = dol_buildpath($reldir, 0);
103 $newdir = dol_osencode($dir);
104 //print "xx".$dir;exit;
105
106 // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
107 if (!is_dir($newdir)) {
108 continue;
109 }
110
111 $handle = opendir($newdir);
112 if (is_resource($handle)) {
113 $fullpathfiles = array();
114 while (($file = readdir($handle)) !== false) {
115 $reg = array();
116 if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
117 $part1 = $reg[1];
118 $part2 = $reg[2];
119 $part3 = $reg[3];
120
121 $nbfile++;
122
123 // Check if trigger file is disabled by name
124 if (preg_match('/NORUN$/i', $file)) {
125 continue;
126 }
127 // Check if trigger file is for a particular module
128 $qualified = true;
129 if (strtolower($reg[2]) != 'all') {
130 $module = preg_replace('/^mod/i', '', $reg[2]);
131 if (!isModEnabled(strtolower($module))) {
132 $qualified = false;
133 }
134 }
135
136 if (!$qualified) {
137 //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
138 continue;
139 }
140
141 $modName = "Interface".ucfirst($reg[3]);
142 //print "file=$file - modName=$modName\n";
143 if (in_array($modName, $modules)) { // $modules = list of modName already loaded
144 $langs->load("errors");
145 dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
146 continue;
147 }
148
149 try {
150 //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
151 include_once $newdir.'/'.$file;
152 //print 'Done for '.$modName."\n";
153 } catch (Exception $e) {
154 dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
155 }
156
157 $modules[$i] = $modName;
158 $files[$i] = $file;
159 $fullpathfiles[$modName] = $newdir.'/'.$file;
160 $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
161
162 $i++;
163 }
164 }
165
166 closedir($handle);
167 }
168 }
169
170 asort($orders, SORT_NATURAL);
171
172 // Loop on each trigger
173 foreach ($orders as $key => $value) {
174 $modName = $modules[$key];
175 if (empty($modName)) {
176 continue;
177 }
178
179 $objMod = new $modName($this->db);
180 if ($objMod) {
181 $dblevelbefore = $this->db->transaction_opened;
182
183 $result = 0;
184
185 if (method_exists($objMod, 'runTrigger')) { // New method to implement
186 //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG);
187 $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
188 } elseif (method_exists($objMod, 'run_trigger')) { // Deprecated method
189 dol_syslog(get_class($this)."::run_triggers action=".$action." Launch old method run_trigger (rename your trigger into runTrigger) for file '".$files[$key]."'", LOG_WARNING);
190 $result = $objMod->run_trigger($action, $object, $user, $langs, $conf);
191 } else {
192 dol_syslog(get_class($this)."::run_triggers action=".$action." A trigger was declared for class ".get_class($objMod)." but method runTrigger was not found", LOG_ERR);
193 }
194
195 $dblevelafter = $this->db->transaction_opened;
196
197 if ($dblevelbefore != $dblevelafter) {
198 $errormessage = "Error, the balance begin/close of db transactions has been broken into trigger ".$modName." with action=".$action." before=".$dblevelbefore." after=".$dblevelafter;
199 $this->errors[] = $errormessage;
200 dol_syslog($errormessage, LOG_ERR);
201 $result = -1;
202 }
203
204 if ($result > 0) {
205 // Action OK
206 $nbtotal++;
207 $nbok++;
208 }
209 if ($result == 0) {
210 // Aucune action faite
211 $nbtotal++;
212 }
213 if ($result < 0) {
214 // Action KO
215 //dol_syslog("Error in trigger ".$action." - result = ".$result." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
216 $nbtotal++;
217 $nbko++;
218 if (!empty($objMod->errors)) {
219 $this->errors = array_merge($this->errors, $objMod->errors);
220 } elseif (!empty($objMod->error)) {
221 $this->errors[] = $objMod->error;
222 }
223 //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
224 }
225 } else {
226 dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
227 }
228 }
229
230 if ($nbko) {
231 dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko." - Nb of error string returned in this->errors = ".count($this->errors), LOG_ERR);
232 return -$nbko;
233 } else {
234 //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
235 return $nbok;
236 }
237 }
238
246 public function getTriggersList($forcedirtriggers = null)
247 {
248 global $conf, $langs, $db;
249
250 $files = array();
251 $fullpath = array();
252 $relpath = array();
253 $iscoreorexternal = array();
254 $modules = array();
255 $orders = array();
256 $i = 0;
257
258 $dirtriggers = array_merge(array('/core/triggers/'), $conf->modules_parts['triggers']);
259 if (is_array($forcedirtriggers)) {
260 $dirtriggers = $forcedirtriggers;
261 }
262
263 foreach ($dirtriggers as $reldir) {
264 $dir = dol_buildpath($reldir, 0);
265 $newdir = dol_osencode($dir);
266
267 // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
268 if (!is_dir($newdir)) {
269 continue;
270 }
271
272 $handle = opendir($newdir);
273 if (is_resource($handle)) {
274 while (($file = readdir($handle)) !== false) {
275 $reg = array();
276 if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
277 if (preg_match('/\.back$/', $file)) {
278 continue;
279 }
280
281 $part1 = $reg[1];
282 $part2 = $reg[2];
283 $part3 = $reg[3];
284
285 $modName = 'Interface'.ucfirst($reg[3]);
286 //print "file=$file"; print "modName=$modName"; exit;
287 if (in_array($modName, $modules)) {
288 $langs->load("errors");
289 print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger", $modName, "/htdocs/core/triggers/").'</div>';
290 } else {
291 include_once $newdir.'/'.$file;
292 }
293
294 $files[$i] = $file;
295 $fullpath[$i] = $dir.'/'.$file;
296 $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
297 $iscoreorexternal[$i] = ($reldir == '/core/triggers/' ? 'internal' : 'external');
298 $modules[$i] = $modName;
299 $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
300
301 $i++;
302 }
303 }
304 closedir($handle);
305 }
306 }
307
308 asort($orders, SORT_NATURAL);
309
310 $triggers = array();
311 $j = 0;
312
313 // Loop on each trigger
314 foreach ($orders as $key => $value) {
315 $modName = $modules[$key];
316 if (empty($modName)) {
317 continue;
318 }
319
320 if (!class_exists($modName)) {
321 print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
322 continue;
323 }
324
325 $text = '';
326
327 try {
328 $objMod = new $modName($db);
329
330 if (is_subclass_of($objMod, 'DolibarrTriggers')) {
331 // Define disabledbyname and disabledbymodule
332 $disabledbyname = 0;
333 $disabledbymodule = 1;
334 $module = '';
335
336 // Check if trigger file is disabled by name
337 if (preg_match('/NORUN$/i', $files[$key])) {
338 $disabledbyname = 1;
339 }
340 // Check if trigger file is for a particular module
341 if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
342 $module = preg_replace('/^mod/i', '', $reg[2]);
343 if (strtolower($module) == 'all') {
344 $disabledbymodule = 0;
345 } elseif (!isModEnabled(strtolower($module))) {
346 $disabledbymodule = 2;
347 }
348 $triggers[$j]['module'] = strtolower($module);
349 }
350
351 // We set info of modules
352 $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
353 $triggers[$j]['file'] = $files[$key];
354 $triggers[$j]['fullpath'] = $fullpath[$key];
355 $triggers[$j]['relpath'] = $relpath[$key];
356 $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
357 $triggers[$j]['version'] = $objMod->getVersion();
358 $triggers[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
359 if ($disabledbyname > 0 || $disabledbymodule > 1) {
360 $triggers[$j]['status'] = '';
361 }
362
363 $text = '<b>'.$langs->trans("Description").':</b><br>';
364 $text .= $objMod->getDesc().'<br>';
365 $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
366 if ($disabledbyname == 1) {
367 $text .= $langs->trans("TriggerDisabledByName").'<br>';
368 if ($disabledbymodule == 2) {
369 $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
370 }
371 } else {
372 if ($disabledbymodule == 0) {
373 $text .= $langs->trans("TriggerAlwaysActive").'<br>';
374 }
375 if ($disabledbymodule == 1) {
376 $text .= $langs->trans("TriggerActiveAsModuleActive", $module).'<br>';
377 }
378 if ($disabledbymodule == 2) {
379 $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
380 }
381 }
382 } else {
383 $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
384 $triggers[$j]['file'] = $files[$key];
385 $triggers[$j]['fullpath'] = $fullpath[$key];
386 $triggers[$j]['relpath'] = $relpath[$key];
387 $triggers[$j]['status'] = img_picto('Error: Trigger '.$modName.' does not extends DolibarrTriggers', 'warning');
388
389 //print 'Error: Trigger '.$modName.' does not extends DolibarrTriggers<br>';
390 $text = 'Error: Trigger '.$modName.' does not extends DolibarrTriggers';
391 }
392 } catch (Exception $e) {
393 print $e->getMessage();
394 }
395
396 $triggers[$j]['info'] = $text;
397 $j++;
398 }
399 return $triggers;
400 }
401}
Class to manage triggers.
__construct($db)
Constructor.
run_triggers($action, $object, $user, $langs, $conf)
Function called when a Dolibarr business event occurs This function call all qualified triggers.
getTriggersList($forcedirtriggers=null)
Return list of triggers.
Class to manage Dolibarr users.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.