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 $constparam =
'MAIN_MODULE_'.strtoupper($module);
132 if (empty($conf->global->$constparam)) {
142 $modName =
"Interface".ucfirst($reg[3]);
144 if (in_array($modName, $modules)) {
145 $langs->load(
"errors");
146 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" ".$langs->trans(
"ErrorDuplicateTrigger", $newdir.
"/".$file, $fullpathfiles[$modName]), LOG_WARNING);
152 include_once $newdir.
'/'.$file;
155 dol_syslog(
'ko for '.$modName.
" ".$e->getMessage().
"\n", LOG_ERR);
158 $modules[$i] = $modName;
160 $fullpathfiles[$modName] = $newdir.
'/'.$file;
161 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
174 foreach ($orders as $key => $value) {
175 $modName = $modules[$key];
176 if (empty($modName)) {
180 $objMod =
new $modName($this->db);
182 $dblevelbefore = $this->db->transaction_opened;
186 if (method_exists($objMod,
'runTrigger')) {
188 $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
189 } elseif (method_exists($objMod,
'run_trigger')) {
190 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);
191 $result = $objMod->run_trigger($action, $object, $user, $langs, $conf);
193 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);
196 $dblevelafter = $this->db->transaction_opened;
198 if ($dblevelbefore != $dblevelafter) {
199 $errormessage =
"Error, the balance begin/close of db transactions has been broken into trigger ".$modName.
" with action=".$action.
" before=".$dblevelbefore.
" after=".$dblevelafter;
200 $this->errors[] = $errormessage;
219 if (!empty($objMod->errors)) {
220 $this->errors = array_merge($this->errors, $objMod->errors);
221 } elseif (!empty($objMod->error)) {
222 $this->errors[] = $objMod->error;
227 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
232 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);
249 global $conf, $langs, $db;
254 $iscoreorexternal = array();
259 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
260 if (is_array($forcedirtriggers)) {
261 $dirtriggers = $forcedirtriggers;
264 foreach ($dirtriggers as $reldir) {
269 if (!is_dir($newdir)) {
273 $handle = opendir($newdir);
274 if (is_resource($handle)) {
275 while (($file = readdir($handle)) !==
false) {
277 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
278 if (preg_match(
'/\.back$/', $file)) {
286 $modName =
'Interface'.ucfirst($reg[3]);
288 if (in_array($modName, $modules)) {
289 $langs->load(
"errors");
290 print
'<div class="error">'.$langs->trans(
"Error").
' : '.$langs->trans(
"ErrorDuplicateTrigger", $modName,
"/htdocs/core/triggers/").
'</div>';
292 include_once $newdir.
'/'.$file;
296 $fullpath[$i] = $dir.
'/'.$file;
297 $relpath[$i] = preg_replace(
'/^\//',
'', $reldir).
'/'.$file;
298 $iscoreorexternal[$i] = ($reldir ==
'/core/triggers/' ?
'internal' :
'external');
299 $modules[$i] = $modName;
300 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
315 foreach ($orders as $key => $value) {
316 $modName = $modules[$key];
317 if (empty($modName)) {
321 if (!class_exists($modName)) {
322 print
'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
329 $objMod =
new $modName($db);
331 if (is_subclass_of($objMod,
'DolibarrTriggers')) {
334 $disabledbymodule = 1;
338 if (preg_match(
'/NORUN$/i', $files[$key])) {
342 if (preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
343 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
344 $constparam =
'MAIN_MODULE_'.strtoupper($module);
345 if (strtolower($module) ==
'all') {
346 $disabledbymodule = 0;
347 } elseif (empty($conf->global->$constparam)) {
348 $disabledbymodule = 2;
350 $triggers[$j][
'module'] = strtolower($module);
354 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
355 $triggers[$j][
'file'] = $files[$key];
356 $triggers[$j][
'fullpath'] = $fullpath[$key];
357 $triggers[$j][
'relpath'] = $relpath[$key];
358 $triggers[$j][
'iscoreorexternal'] = $iscoreorexternal[$key];
359 $triggers[$j][
'version'] = $objMod->getVersion();
360 $triggers[$j][
'status'] =
img_picto($langs->trans(
"Active"),
'tick');
361 if ($disabledbyname > 0 || $disabledbymodule > 1) {
362 $triggers[$j][
'status'] =
'';
365 $text =
'<b>'.$langs->trans(
"Description").
':</b><br>';
366 $text .= $objMod->getDesc().
'<br>';
367 $text .=
'<br><b>'.$langs->trans(
"Status").
':</b><br>';
368 if ($disabledbyname == 1) {
369 $text .= $langs->trans(
"TriggerDisabledByName").
'<br>';
370 if ($disabledbymodule == 2) {
371 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
374 if ($disabledbymodule == 0) {
375 $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
377 if ($disabledbymodule == 1) {
378 $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
380 if ($disabledbymodule == 2) {
381 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
385 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
386 $triggers[$j][
'file'] = $files[$key];
387 $triggers[$j][
'fullpath'] = $fullpath[$key];
388 $triggers[$j][
'relpath'] = $relpath[$key];
389 $triggers[$j][
'status'] =
img_picto(
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers',
'warning');
392 $text =
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers';
395 print $e->getMessage();
398 $triggers[$j][
'info'] = $text;