8
edits
mNo edit summary Tag: 2017 source edit |
m (Update class name as per Dolibarr 18.0.2) |
||
| Line 39: | Line 39: | ||
'''Note''': If you develop a full module, you can also place the trigger file in your own module's subdirectly. Eg: if your module resides in htdocs/mymodule/, then you can place your triggers inside htdocs/mymodule/core/triggers/. But for this, you must declare the trigger into your module descriptor file (eg: '''/mymodule/core/modules/modMyModule.class.php'''). For this, add into this file triggers->1 into module_parts array: | '''Note''': If you develop a full module, you can also place the trigger file in your own module's subdirectly. Eg: if your module resides in htdocs/mymodule/, then you can place your triggers inside htdocs/mymodule/core/triggers/. But for this, you must declare the trigger into your module descriptor file (eg: '''/mymodule/core/modules/modMyModule.class.php'''). For this, add into this file triggers->1 into module_parts array: | ||
< | <syntaxhighlight lang="PHP"> | ||
// Defined all module parts (triggers, login, substitutions, menus, etc...) (0=disable,1=enable) | // Defined all module parts (triggers, login, substitutions, menus, etc...) (0=disable,1=enable) | ||
$this->module_parts = array('triggers' => 1); | $this->module_parts = array('triggers' => 1); | ||
| Line 46: | Line 46: | ||
// 'substitutions' => 0, | // 'substitutions' => 0, | ||
// 'menus' => 0); | // 'menus' => 0); | ||
</ | </syntaxhighlight> | ||
Then disable and reenable your module. This will add a record into [[Table llx_const]] to tell Dolibarr that a trigger file must be searched in the module directory '''htdocs/mymodule/core/triggers/''' | Then disable and reenable your module. This will add a record into [[Table llx_const]] to tell Dolibarr that a trigger file must be searched in the module directory '''htdocs/mymodule/core/triggers/''' | ||
2) Edit the file ''interface_99_modMyModule_Myworkflow.class.php'' to rename class '' | 2) Edit the file ''interface_99_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceMyModuleTriggers'' by ''InterfaceMyworkflow'' | ||
Then go on page Home -> Admin Tools -> About Dolibarr ->Triggers. | Then go on page Home -> Admin Tools -> About Dolibarr ->Triggers. | ||
| Line 59: | Line 59: | ||
This function is called at each Dolibarr business event. Put your code according to events on which you want to run. Each event is identified by a code (see following chapter to get full list of codes). So you can react or no after an event by doing just a test on variable '''$action''': | This function is called at each Dolibarr business event. Put your code according to events on which you want to run. Each event is identified by a code (see following chapter to get full list of codes). So you can react or no after an event by doing just a test on variable '''$action''': | ||
< | <syntaxhighlight lang="PHP"> | ||
function run_trigger($action,$object,$user,$langs,$conf) | function run_trigger($action,$object,$user,$langs,$conf) | ||
{ | { | ||
| Line 77: | Line 77: | ||
... | ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
You can do what you want into this coding part, if you check that function run_trigger returns a return code according to following rule: | You can do what you want into this coding part, if you check that function run_trigger returns a return code according to following rule: | ||
| Line 110: | Line 110: | ||
To manage events other than those above, you must modify the Dolibarr code to add the following sequence in the business methods of the classes of the objects handled: | To manage events other than those above, you must modify the Dolibarr code to add the following sequence in the business methods of the classes of the objects handled: | ||
< | <syntaxhighlight lang="PHP"> | ||
// Call trigger | // Call trigger | ||
$result = $this->call_trigger('CODE_TRIGGER', $user); | $result = $this->call_trigger('CODE_TRIGGER', $user); | ||
if ($result <0) $error++; | if ($result <0) $error++; | ||
// End call triggers | // End call triggers | ||
</ | </syntaxhighlight> | ||
To add context information (for example to dissociate 2 different cases called with the same Trigger code), it is possible to place this information in the properties ->context of the object on which the trigger is called. Example: | To add context information (for example to dissociate 2 different cases called with the same Trigger code), it is possible to place this information in the properties ->context of the object on which the trigger is called. Example: | ||
< | <syntaxhighlight lang="PHP"> | ||
// Call trigger | // Call trigger | ||
$this->context = array('paramX'=>$valueX, 'anyparamofyourchoice'=>$anyvalue); | $this->context = array('paramX'=>$valueX, 'anyparamofyourchoice'=>$anyvalue); | ||
| Line 125: | Line 125: | ||
if ($result < 0) $error++; | if ($result < 0) $error++; | ||
// End call triggers | // End call triggers | ||
</ | </syntaxhighlight> | ||
It will then be possible to add in the run_trigger method of any trigger file, an if which allows you to manage this code. The run_trigger method would then be of the form: | It will then be possible to add in the run_trigger method of any trigger file, an if which allows you to manage this code. The run_trigger method would then be of the form: | ||
< | <syntaxhighlight lang="PHP"> | ||
function run_trigger($action,$object,$user,$lang,$conf) | function run_trigger($action,$object,$user,$lang,$conf) | ||
{ | { | ||
| Line 151: | Line 151: | ||
... | ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
'''Please note the following before adding additional calls to triggers:''' | '''Please note the following before adding additional calls to triggers:''' | ||
edits