80 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Launch run_triggers", LOG_DEBUG);
84 if (!is_object(
$object) || !is_object($conf)) {
85 $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));
86 dol_syslog(get_class($this).
'::run_triggers '.$error, LOG_ERR);
87 $this->errors[] = $error;
90 if (!is_object($langs)) {
91 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);
93 if (!is_object($user)) {
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 $user =
new User($this->db);
98 $nbfile = $nbtotal = $nbok = $nbko = 0;
99 $this->lastmoduleerror =
'';
106 $dirtriggers = array_merge(array(
'/core/triggers'), $conf->modules_parts[
'triggers']);
107 foreach ($dirtriggers as $reldir) {
113 if (!is_dir($newdir)) {
117 $handle = opendir($newdir);
118 if (is_resource($handle)) {
119 $fullpathfiles = array();
120 while (($file = readdir($handle)) !==
false) {
122 if (is_readable($newdir.
"/".$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
130 if (preg_match(
'/NORUN$/i', $file)) {
135 if (strtolower($reg[2]) !=
'all') {
136 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
137 if (!isModEnabled(strtolower($module))) {
147 $modName =
"Interface".ucfirst($reg[3]);
149 if (in_array($modName, $modules)) {
150 $langs->load(
"errors");
151 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" ".$langs->trans(
"ErrorDuplicateTrigger", $newdir.
"/".$file, $fullpathfiles[$modName]), LOG_WARNING);
157 include_once $newdir.
'/'.$file;
160 dol_syslog(
'ko for '.$modName.
" ".$e->getMessage().
"\n", LOG_ERR);
163 $modules[$i] = $modName;
165 $fullpathfiles[$modName] = $newdir.
'/'.$file;
166 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
176 asort($orders, SORT_NATURAL);
179 foreach ($orders as $key => $value) {
180 $modName = $modules[$key];
181 if (empty($modName)) {
185 $objMod =
new $modName($this->db);
187 $dblevelbefore = $this->db->transaction_opened;
191 if (method_exists($objMod,
'runTrigger')) {
193 $result = $objMod->runTrigger($action,
$object, $user, $langs, $conf);
194 } elseif (method_exists($objMod,
'run_trigger')) {
195 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);
196 $result = $objMod->run_trigger($action,
$object, $user, $langs, $conf);
198 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);
201 $dblevelafter = $this->db->transaction_opened;
203 if ($dblevelbefore != $dblevelafter) {
204 $errormessage =
"Error, the balance begin/close of db transactions has been broken into trigger ".$modName.
" with action=".$action.
" before=".$dblevelbefore.
" after=".$dblevelafter;
205 $this->errors[] = $errormessage;
224 $this->lastmoduleerror = $modName;
225 if (!empty($objMod->errors)) {
226 $this->errors = array_merge($this->errors, $objMod->errors);
227 } elseif (!empty($objMod->error)) {
228 $this->errors[] = $objMod->error;
233 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
238 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);
255 global $conf, $langs, $db;
260 $iscoreorexternal = array();
265 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
266 if (is_array($forcedirtriggers)) {
267 $dirtriggers = $forcedirtriggers;
270 foreach ($dirtriggers as $reldir) {
275 if (!is_dir($newdir)) {
279 $handle = opendir($newdir);
280 if (is_resource($handle)) {
281 while (($file = readdir($handle)) !==
false) {
283 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
284 if (preg_match(
'/\.back$/', $file)) {
292 $modName =
'Interface'.ucfirst($reg[3]);
294 if (in_array($modName, $modules)) {
295 $langs->load(
"errors");
296 print
'<div class="error">'.$langs->trans(
"Error").
' : '.$langs->trans(
"ErrorDuplicateTrigger", $modName,
"/htdocs/core/triggers/").
'</div>';
298 include_once $newdir.
'/'.$file;
302 $fullpath[$i] = $dir.
'/'.$file;
303 $relpath[$i] = preg_replace(
'/^\//',
'', $reldir).
'/'.$file;
304 $iscoreorexternal[$i] = ($reldir ==
'/core/triggers/' ?
'internal' :
'external');
305 $modules[$i] = $modName;
306 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
315 asort($orders, SORT_NATURAL);
321 foreach ($orders as $key => $value) {
322 $modName = $modules[$key];
323 if (empty($modName)) {
327 if (!class_exists($modName)) {
328 print
'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
335 $objMod =
new $modName($db);
337 if (is_subclass_of($objMod,
'DolibarrTriggers')) {
340 $disabledbymodule = 1;
344 if (preg_match(
'/NORUN$/i', $files[$key])) {
348 if (preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
349 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
350 if (strtolower($module) ==
'all') {
351 $disabledbymodule = 0;
352 } elseif (!isModEnabled(strtolower($module))) {
353 $disabledbymodule = 2;
355 $triggers[$j][
'module'] = strtolower($module);
359 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
360 $triggers[$j][
'file'] = $files[$key];
361 $triggers[$j][
'fullpath'] = $fullpath[$key];
362 $triggers[$j][
'relpath'] = $relpath[$key];
363 $triggers[$j][
'iscoreorexternal'] = $iscoreorexternal[$key];
364 $triggers[$j][
'version'] = $objMod->getVersion();
365 $triggers[$j][
'status'] =
img_picto($langs->trans(
"Active"),
'tick');
366 if ($disabledbyname > 0 || $disabledbymodule > 1) {
367 $triggers[$j][
'status'] =
'';
370 $text =
'<b>'.$langs->trans(
"Description").
':</b><br>';
371 $text .= $objMod->getDesc().
'<br>';
372 $text .=
'<br><b>'.$langs->trans(
"Status").
':</b><br>';
373 if ($disabledbyname == 1) {
374 $text .= $langs->trans(
"TriggerDisabledByName").
'<br>';
375 if ($disabledbymodule == 2) {
376 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
379 if ($disabledbymodule == 0) {
380 $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
382 if ($disabledbymodule == 1) {
383 $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
385 if ($disabledbymodule == 2) {
386 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
390 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
391 $triggers[$j][
'file'] = $files[$key];
392 $triggers[$j][
'fullpath'] = $fullpath[$key];
393 $triggers[$j][
'relpath'] = $relpath[$key];
394 $triggers[$j][
'status'] =
img_picto(
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers',
'warning');
397 $text =
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers';
400 print $e->getMessage();
403 $triggers[$j][
'info'] = $text;