26 require_once DOL_DOCUMENT_ROOT.
'/core/triggers/dolibarrtriggers.class.php';
44 public $errors = array();
68 public function run_triggers($action, $object, $user, $langs, $conf)
72 if (!is_object($object) || !is_object($conf))
74 $this->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);
75 dol_syslog(get_class($this).
'::run_triggers '.$this->error, LOG_ERR);
76 $this->errors[] = $this->error;
79 if (!is_object($langs))
81 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);
83 if (!is_object($user))
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);
86 $user =
new User($this->
db);
90 $nbfile = $nbtotal = $nbok = $nbko = 0;
98 $dirtriggers = array_merge(array(
'/core/triggers'), $conf->modules_parts[
'triggers']);
99 foreach ($dirtriggers as $reldir)
106 if (!is_dir($newdir))
continue;
108 $handle = opendir($newdir);
109 if (is_resource($handle))
111 $fullpathfiles = array();
112 while (($file = readdir($handle)) !==
false)
115 if (is_readable($newdir.
"/".$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg))
124 if (preg_match(
'/NORUN$/i', $file))
continue;
127 if (strtolower($reg[2]) !=
'all')
129 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
130 $constparam =
'MAIN_MODULE_'.strtoupper($module);
131 if (empty($conf->global->$constparam)) $qualified =
false;
140 $modName =
"Interface".ucfirst($reg[3]);
142 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;
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)
176 $modName = $modules[$key];
177 if (empty($modName))
continue;
179 $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;
222 if (!empty($objMod->errors)) $this->errors = array_merge($this->errors, $objMod->errors);
223 elseif (!empty($objMod->error)) $this->errors[] = $objMod->error;
227 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
233 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);
250 global $conf, $langs, $db;
255 $iscoreorexternal = array();
260 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
261 if (is_array($forcedirtriggers))
263 $dirtriggers = $forcedirtriggers;
266 foreach ($dirtriggers as $reldir)
272 if (!is_dir($newdir))
continue;
274 $handle = opendir($newdir);
275 if (is_resource($handle))
277 while (($file = readdir($handle)) !==
false)
280 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg))
282 if (preg_match(
'/\.back$/', $file))
continue;
288 $modName =
'Interface'.ucfirst($reg[3]);
290 if (in_array($modName, $modules))
292 $langs->load(
"errors");
293 print '<div class="error">'.$langs->trans(
"Error").
' : '.$langs->trans(
"ErrorDuplicateTrigger", $modName,
"/htdocs/core/triggers/").
'</div>';
295 include_once $newdir.
'/'.$file;
299 $fullpath[$i] = $dir.
'/'.$file;
300 $relpath[$i] = preg_replace(
'/^\//',
'', $reldir).
'/'.$file;
301 $iscoreorexternal[$i] = ($reldir ==
'/core/triggers/' ?
'internal' :
'external');
302 $modules[$i] = $modName;
303 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
318 foreach ($orders as $key => $value)
320 $modName = $modules[$key];
321 if (empty($modName))
continue;
323 if (!class_exists($modName))
325 print 'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
332 $objMod =
new $modName($db);
334 if (is_subclass_of($objMod,
'DolibarrTriggers'))
338 $disabledbymodule = 1;
342 if (preg_match(
'/NORUN$/i', $files[$key])) $disabledbyname = 1;
344 if (preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg))
346 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
347 $constparam =
'MAIN_MODULE_'.strtoupper($module);
348 if (strtolower($module) ==
'all') $disabledbymodule = 0;
349 elseif (empty($conf->global->$constparam)) $disabledbymodule = 2;
350 $triggers[$j][
'module'] = strtolower($module);
354 $triggers[$j][
'picto'] = $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) $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)
368 $text .= $langs->trans(
"TriggerDisabledByName").
'<br>';
369 if ($disabledbymodule == 2) $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
371 if ($disabledbymodule == 0) $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
372 if ($disabledbymodule == 1) $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
373 if ($disabledbymodule == 2) $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
376 print 'Error: Trigger '.$modName.
' does not extends DolibarrTriggers<br>';
379 print $e->getMessage();
382 $triggers[$j][
'info'] = $text;
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Class to manage Dolibarr users.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
$conf db
API class for accounts.
__construct($db)
Constructor.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
run_triggers($action, $object, $user, $langs, $conf)
Function called when a Dolibarr business event occurs This function call all qualified triggers...
print
Draft customers invoices.
getTriggersList($forcedirtriggers=null)
Return list of triggers.
Class to manage triggers.