83 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Launch run_triggers", LOG_DEBUG);
87 if (!is_object(
$object) || !is_object($conf)) {
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;
93 if (!is_object($langs)) {
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);
96 if (!is_object($user)) {
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);
101 $nbfile = $nbtotal = $nbok = $nbko = 0;
102 $this->lastmoduleerror =
'';
109 $dirtriggers = array_merge(array(
'/core/triggers'), $conf->modules_parts[
'triggers']);
110 foreach ($dirtriggers as $reldir) {
116 if (!is_dir($newdir)) {
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) {
126 if (is_readable($newdir.
"/".$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
134 if (preg_match(
'/NORUN$/i', $file)) {
139 if (strtolower($reg[2]) !=
'all') {
140 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
141 if (!isModEnabled(strtolower($module))) {
151 $modName =
"Interface".ucfirst($reg[3]);
153 if (array_key_exists($modName, $fullpathfiles)) {
154 $langs->load(
"errors");
155 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" ".$langs->trans(
"ErrorDuplicateTrigger", $newdir.
"/".$file, $fullpathfiles[$modName]), LOG_WARNING);
161 include_once $newdir.
'/'.$file;
164 dol_syslog(
'ko for '.$modName.
" ".$e->getMessage().
"\n", LOG_ERR);
167 $modules[$i] = $modName;
169 $fullpathfiles[$modName] = $newdir.
'/'.$file;
170 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
180 asort($orders, SORT_NATURAL);
183 foreach ($orders as $key => $value) {
184 $modName = $modules[$key];
185 if (empty($modName)) {
189 $objMod =
new $modName($this->db);
190 '@phan-var-force DolibarrTriggers $objMod';
192 $dblevelbefore = $this->db->transaction_opened;
196 if (method_exists($objMod,
'runTrigger')) {
198 $result = $objMod->runTrigger($action,
$object, $user, $langs, $conf);
199 } elseif (method_exists($objMod,
'run_trigger')) {
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);
202 $result = $objMod->run_trigger($action,
$object, $user, $langs, $conf);
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);
207 $dblevelafter = $this->db->transaction_opened;
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;
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;
239 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
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);
261 global $conf, $langs, $db;
266 $iscoreorexternal = array();
271 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
272 if (is_array($forcedirtriggers)) {
273 $dirtriggers = $forcedirtriggers;
276 foreach ($dirtriggers as $reldir) {
281 if (!is_dir($newdir)) {
285 $handle = opendir($newdir);
286 if (is_resource($handle)) {
287 while (($file = readdir($handle)) !==
false) {
289 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
290 if (preg_match(
'/\.back$/', $file)) {
298 $modName =
'Interface'.ucfirst($reg[3]);
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>';
304 include_once $newdir.
'/'.$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;
321 asort($orders, SORT_NATURAL);
327 foreach ($orders as $key => $value) {
328 $modName = $modules[$key];
329 if (empty($modName)) {
333 if (!class_exists($modName)) {
334 print
'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
341 $objMod =
new $modName($db);
342 '@phan-var-force DolibarrTriggers $objMod';
344 if (is_subclass_of($objMod,
'DolibarrTriggers')) {
347 $disabledbymodule = 1;
351 if (preg_match(
'/NORUN$/i', $files[$key])) {
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;
362 $triggers[$j][
'module'] = strtolower($module);
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'] =
'';
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>';
386 if ($disabledbymodule == 0) {
387 $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
389 if ($disabledbymodule == 1) {
390 $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
392 if ($disabledbymodule == 2) {
393 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
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');
404 $text =
'Error: Trigger '.$modName.
' does not extend DolibarrTriggers';
407 print $e->getMessage();
410 $triggers[$j][
'info'] = $text;