68 public function run_triggers($action, $object, $user, $langs, $conf)
74 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Launch run_triggers", LOG_DEBUG);
78 if (!is_object($object) || !is_object($conf)) {
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;
84 if (!is_object($langs)) {
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);
87 if (!is_object($user)) {
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);
92 $nbfile = $nbtotal = $nbok = $nbko = 0;
100 $dirtriggers = array_merge(array(
'/core/triggers'), $conf->modules_parts[
'triggers']);
101 foreach ($dirtriggers as $reldir) {
107 if (!is_dir($newdir)) {
111 $handle = opendir($newdir);
112 if (is_resource($handle)) {
113 $fullpathfiles = array();
114 while (($file = readdir($handle)) !==
false) {
116 if (is_readable($newdir.
"/".$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
124 if (preg_match(
'/NORUN$/i', $file)) {
129 if (strtolower($reg[2]) !=
'all') {
130 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
131 if (!isModEnabled(strtolower($module))) {
141 $modName =
"Interface".ucfirst($reg[3]);
143 if (in_array($modName, $modules)) {
144 $langs->load(
"errors");
145 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" ".$langs->trans(
"ErrorDuplicateTrigger", $newdir.
"/".$file, $fullpathfiles[$modName]), LOG_WARNING);
151 include_once $newdir.
'/'.$file;
154 dol_syslog(
'ko for '.$modName.
" ".$e->getMessage().
"\n", LOG_ERR);
157 $modules[$i] = $modName;
159 $fullpathfiles[$modName] = $newdir.
'/'.$file;
160 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
170 asort($orders, SORT_NATURAL);
173 foreach ($orders as $key => $value) {
174 $modName = $modules[$key];
175 if (empty($modName)) {
179 $objMod =
new $modName($this->db);
181 $dblevelbefore = $this->db->transaction_opened;
185 if (method_exists($objMod,
'runTrigger')) {
187 $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
188 } elseif (method_exists($objMod,
'run_trigger')) {
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);
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);
195 $dblevelafter = $this->db->transaction_opened;
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;
218 if (!empty($objMod->errors)) {
219 $this->errors = array_merge($this->errors, $objMod->errors);
220 } elseif (!empty($objMod->error)) {
221 $this->errors[] = $objMod->error;
226 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
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);
248 global $conf, $langs, $db;
253 $iscoreorexternal = array();
258 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
259 if (is_array($forcedirtriggers)) {
260 $dirtriggers = $forcedirtriggers;
263 foreach ($dirtriggers as $reldir) {
268 if (!is_dir($newdir)) {
272 $handle = opendir($newdir);
273 if (is_resource($handle)) {
274 while (($file = readdir($handle)) !==
false) {
276 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
277 if (preg_match(
'/\.back$/', $file)) {
285 $modName =
'Interface'.ucfirst($reg[3]);
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>';
291 include_once $newdir.
'/'.$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;
308 asort($orders, SORT_NATURAL);
314 foreach ($orders as $key => $value) {
315 $modName = $modules[$key];
316 if (empty($modName)) {
320 if (!class_exists($modName)) {
321 print
'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
328 $objMod =
new $modName($db);
330 if (is_subclass_of($objMod,
'DolibarrTriggers')) {
333 $disabledbymodule = 1;
337 if (preg_match(
'/NORUN$/i', $files[$key])) {
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;
348 $triggers[$j][
'module'] = strtolower($module);
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'] =
'';
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>';
372 if ($disabledbymodule == 0) {
373 $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
375 if ($disabledbymodule == 1) {
376 $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
378 if ($disabledbymodule == 2) {
379 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
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');
390 $text =
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers';
393 print $e->getMessage();
396 $triggers[$j][
'info'] = $text;