dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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
27require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
28
29
34{
38 public $db;
39
43 public $dir; // Directory with all core and external triggers files
44
48 public $lastmoduleerror;
49
53 public $errors = array();
54
60 public function __construct($db)
61 {
62 $this->db = $db;
63 }
64
65 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
77 public function run_triggers($action, $object, $user, $langs, $conf)
78 {
79 // phpcs:enable
80
81 if (getDolGlobalInt('MAIN_TRIGGER_DEBUG')) {
82 // This his too much verbose, enabled if const enabled only
83 dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_triggers", LOG_DEBUG);
84 }
85
86 // Check parameters
87 if (!is_object($object) || !is_object($conf)) { // Error
88 $error = 'function run_triggers called with wrong parameters action='.$action.' object='.((string) (int) is_object($object)).' user='.((string) (int) is_object($user)).' langs='.((string) (int) is_object($langs)).' conf='.((string) (int) is_object($conf));
89 dol_syslog(get_class($this).'::run_triggers '.$error, LOG_ERR);
90 $this->errors[] = $error;
91 return -1;
92 }
93 if (!is_object($langs)) { // Warning
94 dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.((string) (int) is_object($object)).' user='.((string) (int) is_object($user)).' langs='.((string) (int) is_object($langs)).' conf='.((string) (int) is_object($conf)), LOG_WARNING);
95 }
96 if (!is_object($user)) { // Warning
97 dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.((string) (int) is_object($object)).' user='.((string) (int) is_object($user)).' langs='.((string) (int) is_object($langs)).' conf='.((string) (int) is_object($conf)), LOG_WARNING);
98 $user = new User($this->db);
99 }
100
101 $nbfile = $nbtotal = $nbok = $nbko = 0;
102 $this->lastmoduleerror = '';
103
104 $files = array();
105 $modules = array();
106 $orders = array();
107 $i = 0;
108
109 $dirtriggers = array_merge(array('/core/triggers'), $conf->modules_parts['triggers']);
110 foreach ($dirtriggers as $reldir) {
111 $dir = dol_buildpath($reldir, 0);
112 $newdir = dol_osencode($dir);
113 //print "xx".$dir;exit;
114
115 // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
116 if (!is_dir($newdir)) {
117 continue;
118 }
119
120 $handle = opendir($newdir);
121 if (is_resource($handle)) {
122 $fullpathfiles = array();
123 '@phan-var-force array<string,string> $fullpathfiles';
124 while (($file = readdir($handle)) !== false) {
125 $reg = array();
126 if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
127 $part1 = $reg[1];
128 $part2 = $reg[2];
129 $part3 = $reg[3];
130
131 $nbfile++;
132
133 // Check if trigger file is disabled by name
134 if (preg_match('/NORUN$/i', $file)) {
135 continue;
136 }
137 // Check if trigger file is for a particular module
138 $qualified = true;
139 if (strtolower($reg[2]) != 'all') {
140 $module = preg_replace('/^mod/i', '', $reg[2]);
141 if (!isModEnabled(strtolower($module))) {
142 $qualified = false;
143 }
144 }
145
146 if (!$qualified) {
147 //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
148 continue;
149 }
150
151 $modName = "Interface".ucfirst($reg[3]);
152 //print "file=$file - modName=$modName\n";
153 if (array_key_exists($modName, $fullpathfiles)) { // $modules = list of modName already loaded, fullpathfiles[$modName] is alsoset
154 $langs->load("errors");
155 dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
156 continue;
157 }
158
159 try {
160 //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
161 include_once $newdir.'/'.$file;
162 //print 'Done for '.$modName."\n";
163 } catch (Exception $e) {
164 dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
165 }
166
167 $modules[$i] = $modName;
168 $files[$i] = $file;
169 $fullpathfiles[$modName] = $newdir.'/'.$file;
170 $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
171
172 $i++;
173 }
174 }
175
176 closedir($handle);
177 }
178 }
179
180 asort($orders, SORT_NATURAL);
181
182 // Loop on each trigger
183 foreach ($orders as $key => $value) {
184 $modName = $modules[$key];
185 if (empty($modName)) {
186 continue;
187 }
188
189 $objMod = new $modName($this->db);
190 '@phan-var-force DolibarrTriggers $objMod';
191 if ($objMod) {
192 $dblevelbefore = $this->db->transaction_opened;
193
194 $result = 0;
195
196 if (method_exists($objMod, 'runTrigger')) { // New method to implement
197 //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG);
198 $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
199 } elseif (method_exists($objMod, 'run_trigger')) { // Deprecated method
200 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);
201 // @phan-suppress-next-line PhanUndeclaredMethod
202 $result = $objMod->run_trigger($action, $object, $user, $langs, $conf);
203 } else {
204 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);
205 }
206
207 $dblevelafter = $this->db->transaction_opened;
208
209 if ($dblevelbefore != $dblevelafter) {
210 $errormessage = "Error, the balance begin/close of db transactions has been broken into trigger ".$modName." with action=".$action." before=".$dblevelbefore." after=".$dblevelafter;
211 $this->errors[] = $errormessage;
212 dol_syslog($errormessage, LOG_ERR);
213 $result = -1;
214 }
215
216 if ($result > 0) {
217 // Action OK
218 $nbtotal++;
219 $nbok++;
220 }
221 if ($result == 0) {
222 // Aucune action faite
223 $nbtotal++;
224 }
225 if ($result < 0) {
226 // Action KO
227 //dol_syslog("Error in trigger ".$action." - result = ".$result." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
228 $nbtotal++;
229 $nbko++;
230 $this->lastmoduleerror = $modName;
231 if (!empty($objMod->errors)) {
232 $this->errors = array_merge($this->errors, $objMod->errors);
233 } elseif (!empty($objMod->error)) {
234 $this->errors[] = $objMod->error;
235 }
236 //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
237 }
238 } else {
239 dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
240 }
241 }
242
243 if ($nbko) {
244 dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko.($this->lastmoduleerror ? " - Last module in error: ".$this->lastmoduleerror : "")." - Nb of error string returned in this->errors = ".count($this->errors), LOG_ERR);
245 return -$nbko;
246 } else {
247 //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
248 return $nbok;
249 }
250 }
251
259 public function getTriggersList($forcedirtriggers = null)
260 {
261 global $conf, $langs, $db;
262
263 $files = array();
264 $fullpath = array();
265 $relpath = array();
266 $iscoreorexternal = array();
267 $modules = array();
268 $orders = array();
269 $i = 0;
270
271 $dirtriggers = array_merge(array('/core/triggers/'), $conf->modules_parts['triggers']);
272 if (is_array($forcedirtriggers)) {
273 $dirtriggers = $forcedirtriggers;
274 }
275
276 foreach ($dirtriggers as $reldir) {
277 $dir = dol_buildpath($reldir, 0);
278 $newdir = dol_osencode($dir);
279
280 // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
281 if (!is_dir($newdir)) {
282 continue;
283 }
284
285 $handle = opendir($newdir);
286 if (is_resource($handle)) {
287 while (($file = readdir($handle)) !== false) {
288 $reg = array();
289 if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
290 if (preg_match('/\.back$/', $file)) {
291 continue;
292 }
293
294 $part1 = $reg[1];
295 $part2 = $reg[2];
296 $part3 = $reg[3];
297
298 $modName = 'Interface'.ucfirst($reg[3]);
299 //print "file=$file"; print "modName=$modName"; exit;
300 if (in_array($modName, $modules)) {
301 $langs->load("errors");
302 print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger", $modName, "/htdocs/core/triggers/").'</div>';
303 } else {
304 include_once $newdir.'/'.$file;
305 }
306
307 $files[$i] = $file;
308 $fullpath[$i] = $dir.'/'.$file;
309 $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
310 $iscoreorexternal[$i] = ($reldir == '/core/triggers/' ? 'internal' : 'external');
311 $modules[$i] = $modName;
312 $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
313
314 $i++;
315 }
316 }
317 closedir($handle);
318 }
319 }
320
321 asort($orders, SORT_NATURAL);
322
323 $triggers = array();
324 $j = 0;
325
326 // Loop on each trigger
327 foreach ($orders as $key => $value) {
328 $modName = $modules[$key];
329 if (empty($modName)) {
330 continue;
331 }
332
333 if (!class_exists($modName)) {
334 print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
335 continue;
336 }
337
338 $text = '';
339
340 try {
341 $objMod = new $modName($db);
342 '@phan-var-force DolibarrTriggers $objMod';
343
344 if (is_subclass_of($objMod, 'DolibarrTriggers')) {
345 // Define disabledbyname and disabledbymodule
346 $disabledbyname = 0;
347 $disabledbymodule = 1;
348 $module = '';
349
350 // Check if trigger file is disabled by name
351 if (preg_match('/NORUN$/i', $files[$key])) {
352 $disabledbyname = 1;
353 }
354 // Check if trigger file is for a particular module
355 if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
356 $module = preg_replace('/^mod/i', '', $reg[2]);
357 if (strtolower($module) == 'all') {
358 $disabledbymodule = 0;
359 } elseif (!isModEnabled(strtolower($module))) {
360 $disabledbymodule = 2;
361 }
362 $triggers[$j]['module'] = strtolower($module);
363 }
364
365 // We set info of modules
366 $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
367 $triggers[$j]['file'] = $files[$key];
368 $triggers[$j]['fullpath'] = $fullpath[$key];
369 $triggers[$j]['relpath'] = $relpath[$key];
370 $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
371 $triggers[$j]['version'] = $objMod->getVersion();
372 $triggers[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
373 if ($disabledbyname > 0 || $disabledbymodule > 1) {
374 $triggers[$j]['status'] = '';
375 }
376
377 $text = '<b>'.$langs->trans("Description").':</b><br>';
378 $text .= $objMod->getDesc().'<br>';
379 $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
380 if ($disabledbyname == 1) {
381 $text .= $langs->trans("TriggerDisabledByName").'<br>';
382 if ($disabledbymodule == 2) {
383 $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
384 }
385 } else {
386 if ($disabledbymodule == 0) {
387 $text .= $langs->trans("TriggerAlwaysActive").'<br>';
388 }
389 if ($disabledbymodule == 1) {
390 $text .= $langs->trans("TriggerActiveAsModuleActive", $module).'<br>';
391 }
392 if ($disabledbymodule == 2) {
393 $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
394 }
395 }
396 } else {
397 $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
398 $triggers[$j]['file'] = $files[$key];
399 $triggers[$j]['fullpath'] = $fullpath[$key];
400 $triggers[$j]['relpath'] = $relpath[$key];
401 $triggers[$j]['status'] = img_picto('Error: Trigger '.$modName.' does not extends DolibarrTriggers', 'warning');
402
403 //print 'Error: Trigger '.$modName.' does not extends DolibarrTriggers<br>';
404 $text = 'Error: Trigger '.$modName.' does not extend DolibarrTriggers';
405 }
406 } catch (Exception $e) {
407 print $e->getMessage();
408 }
409
410 $triggers[$j]['info'] = $text;
411 $j++;
412 }
413 return $triggers;
414 }
415}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.