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)) {
73 $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);
74 dol_syslog(get_class($this).
'::run_triggers '.$this->error, LOG_ERR);
75 $this->errors[] = $this->error;
78 if (!is_object($langs)) {
79 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);
81 if (!is_object($user)) {
82 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 $user =
new User($this->
db);
87 $nbfile = $nbtotal = $nbok = $nbko = 0;
95 $dirtriggers = array_merge(array(
'/core/triggers'), $conf->modules_parts[
'triggers']);
96 foreach ($dirtriggers as $reldir) {
102 if (!is_dir($newdir)) {
106 $handle = opendir($newdir);
107 if (is_resource($handle)) {
108 $fullpathfiles = array();
109 while (($file = readdir($handle)) !==
false) {
111 if (is_readable($newdir.
"/".$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
119 if (preg_match(
'/NORUN$/i', $file)) {
124 if (strtolower($reg[2]) !=
'all') {
125 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
126 $constparam =
'MAIN_MODULE_'.strtoupper($module);
127 if (empty($conf->global->$constparam)) {
137 $modName =
"Interface".ucfirst($reg[3]);
139 if (in_array($modName, $modules)) {
140 $langs->load(
"errors");
141 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" ".$langs->trans(
"ErrorDuplicateTrigger", $newdir.
"/".$file, $fullpathfiles[$modName]), LOG_WARNING);
147 include_once $newdir.
'/'.$file;
150 dol_syslog(
'ko for '.$modName.
" ".$e->getMessage().
"\n", LOG_ERR);
153 $modules[$i] = $modName;
155 $fullpathfiles[$modName] = $newdir.
'/'.$file;
156 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
169 foreach ($orders as $key => $value) {
170 $modName = $modules[$key];
171 if (empty($modName)) {
175 $objMod =
new $modName($this->
db);
177 $dblevelbefore = $this->
db->transaction_opened;
181 if (method_exists($objMod,
'runTrigger')) {
183 $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
184 } elseif (method_exists($objMod,
'run_trigger')) {
185 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);
186 $result = $objMod->run_trigger($action, $object, $user, $langs, $conf);
188 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);
191 $dblevelafter = $this->
db->transaction_opened;
193 if ($dblevelbefore != $dblevelafter) {
194 $errormessage =
"Error, the balance begin/close of db transactions has been broken into trigger ".$modName.
" with action=".$action.
" before=".$dblevelbefore.
" after=".$dblevelafter;
195 $this->errors[] = $errormessage;
214 if (!empty($objMod->errors)) {
215 $this->errors = array_merge($this->errors, $objMod->errors);
216 } elseif (!empty($objMod->error)) {
217 $this->errors[] = $objMod->error;
222 dol_syslog(get_class($this).
"::run_triggers action=".$action.
" Failed to instantiate trigger for file '".$files[$key].
"'", LOG_ERR);
227 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);
244 global $conf, $langs, $db;
249 $iscoreorexternal = array();
254 $dirtriggers = array_merge(array(
'/core/triggers/'), $conf->modules_parts[
'triggers']);
255 if (is_array($forcedirtriggers)) {
256 $dirtriggers = $forcedirtriggers;
259 foreach ($dirtriggers as $reldir) {
264 if (!is_dir($newdir)) {
268 $handle = opendir($newdir);
269 if (is_resource($handle)) {
270 while (($file = readdir($handle)) !==
false) {
272 if (is_readable($newdir.
'/'.$file) && preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
273 if (preg_match(
'/\.back$/', $file)) {
281 $modName =
'Interface'.ucfirst($reg[3]);
283 if (in_array($modName, $modules)) {
284 $langs->load(
"errors");
285 print
'<div class="error">'.$langs->trans(
"Error").
' : '.$langs->trans(
"ErrorDuplicateTrigger", $modName,
"/htdocs/core/triggers/").
'</div>';
287 include_once $newdir.
'/'.$file;
291 $fullpath[$i] = $dir.
'/'.$file;
292 $relpath[$i] = preg_replace(
'/^\//',
'', $reldir).
'/'.$file;
293 $iscoreorexternal[$i] = ($reldir ==
'/core/triggers/' ?
'internal' :
'external');
294 $modules[$i] = $modName;
295 $orders[$i] = $part1.
'_'.$part2.
'_'.$part3;
310 foreach ($orders as $key => $value) {
311 $modName = $modules[$key];
312 if (empty($modName)) {
316 if (!class_exists($modName)) {
317 print
'Error: A trigger file was found but its class "'.$modName.
'" was not found.'.
"<br>\n";
324 $objMod =
new $modName($db);
326 if (is_subclass_of($objMod,
'DolibarrTriggers')) {
329 $disabledbymodule = 1;
333 if (preg_match(
'/NORUN$/i', $files[$key])) {
337 if (preg_match(
'/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
338 $module = preg_replace(
'/^mod/i',
'', $reg[2]);
339 $constparam =
'MAIN_MODULE_'.strtoupper($module);
340 if (strtolower($module) ==
'all') {
341 $disabledbymodule = 0;
342 } elseif (empty($conf->global->$constparam)) {
343 $disabledbymodule = 2;
345 $triggers[$j][
'module'] = strtolower($module);
349 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
350 $triggers[$j][
'file'] = $files[$key];
351 $triggers[$j][
'fullpath'] = $fullpath[$key];
352 $triggers[$j][
'relpath'] = $relpath[$key];
353 $triggers[$j][
'iscoreorexternal'] = $iscoreorexternal[$key];
354 $triggers[$j][
'version'] = $objMod->getVersion();
355 $triggers[$j][
'status'] =
img_picto($langs->trans(
"Active"),
'tick');
356 if ($disabledbyname > 0 || $disabledbymodule > 1) {
357 $triggers[$j][
'status'] =
'';
360 $text =
'<b>'.$langs->trans(
"Description").
':</b><br>';
361 $text .= $objMod->getDesc().
'<br>';
362 $text .=
'<br><b>'.$langs->trans(
"Status").
':</b><br>';
363 if ($disabledbyname == 1) {
364 $text .= $langs->trans(
"TriggerDisabledByName").
'<br>';
365 if ($disabledbymodule == 2) {
366 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
369 if ($disabledbymodule == 0) {
370 $text .= $langs->trans(
"TriggerAlwaysActive").
'<br>';
372 if ($disabledbymodule == 1) {
373 $text .= $langs->trans(
"TriggerActiveAsModuleActive", $module).
'<br>';
375 if ($disabledbymodule == 2) {
376 $text .= $langs->trans(
"TriggerDisabledAsModuleDisabled", $module).
'<br>';
380 $triggers[$j][
'picto'] = (!empty($objMod->picto)) ?
img_object(
'', $objMod->picto,
'class="valignmiddle pictomodule "') :
img_object(
'',
'generic',
'class="valignmiddle pictomodule "');
381 $triggers[$j][
'file'] = $files[$key];
382 $triggers[$j][
'fullpath'] = $fullpath[$key];
383 $triggers[$j][
'relpath'] = $relpath[$key];
384 $triggers[$j][
'status'] =
img_picto(
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers',
'warning');
387 $text =
'Error: Trigger '.$modName.
' does not extends DolibarrTriggers';
390 print $e->getMessage();
393 $triggers[$j][
'info'] = $text;
Class to manage triggers.
__construct($db)
Constructor.
run_triggers($action, $object, $user, $langs, $conf)
Function called when a Dolibarr business event occurs This function call all qualified triggers.
getTriggersList($forcedirtriggers=null)
Return list of triggers.
Class to manage Dolibarr users.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.