31,029
edits
| Line 5: | Line 5: | ||
{{ToTranslate}} | {{ToTranslate}} | ||
To execute personalized code after a Dolibarr event (create/modify/remove a third party/invoice/product or other), Dolibarr offers a mecanism of business triggers. This mecanism allows you to personalize a workflow to have Dolibarr business events being synchronized into another application for example. | |||
You can also use triggers to modify the behaviour of Dolibarr itself: for example, to create automatically a contract when an invoice is validated. | |||
So, to add your own code that will be triggered by a Dolibarr trigger, this is the way to process: | |||
1) | 1) Copy file '''htdocs/includes/triggers/interface_all_Demo.class.php''' under the new name: | ||
* '''interface_all_''Xxx''.class.php''' | * '''interface_all_''Xxx''.class.php''' | ||
or | |||
* '''interface_mod'' | * '''interface_mod''MyModule_Xxx''.class.php''' | ||
where ''Xxx'' is a string of your choice startint with uppercase character and ''MyModule'' is the name of a module if you want that trigger is activated only if module MyModyle is activated. If you want the trigger to be always active, use ''all'' instead of modMyModule. | |||
This new file must be saved inside same directory. | |||
Note: Value you can use for modMyModule are values found in directory | |||
''htdocs/includes/modules''. | ''htdocs/includes/modules''. | ||
For example, you can name your new trigger file: | |||
''htdocs/includes/triggers/ | ''htdocs/includes/triggers/interface_modFacture_Myworkflow.class.php'' | ||
By creating such a file with name as in the example, your new trigger file will be executed each time a Dolibarr business event occurs but only if the module Facture is enabled. | |||
2) | 2) Edit the file ''interface_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceDemo'' by ''InterfaceMyworkflow'' | ||
Then go on page Home-> System Infos -> Dolibarr -> Triggers. | |||
Your new trigger file must appears in the list without any error message. This means preceding actions have been done successfully. | |||
3) | 3) Return now to edition of file to add your own code inside function ''run_trigger''. | ||
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''': | |||
<source lang="php"> | <source lang="php"> | ||
function run_trigger($action,$object,$user,$lang,$conf) | function run_trigger($action,$object,$user,$lang,$conf) | ||
{ | { | ||
// | // Put here code to execute after an action | ||
// | // The type of Dolibarr business event is stored into $action | ||
// | // Data of action are stored into object $object | ||
// | // Setup, user and language are saved into $conf, $user and $lang | ||
if ($action == 'COMPANY_CREATE') | if ($action == 'COMPANY_CREATE') | ||
{ | { | ||
| Line 56: | Line 56: | ||
</source> | </source> | ||
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: | |||
<0 | <0 if KO, 0 if nothing is done, >0 if OK | ||
You can also use in this function the preinitialized following variables: | |||
* '''$object''' | * '''$object''' is the object on which action is done (see next chapter) | ||
* '''$user''' | * '''$user''' is the object containg all information of Dolibarr user that realizes the business event. | ||
* '''$langs''' | * '''$langs''' is the object that contains the user language and translations strings | ||
* '''$conf''' | * '''$conf''' is the object that contains all the Dolibarr configuration. | ||
4) Une fois le code réalisé, il n'y a plus qu'à tester, en provoquant l'évènement déclencheur dans Dolibarr. Attention, l'appel au '''run_trigger''' et encapsuler dans un transaction. Si votre trigger renvoie un code ko, la fonction appelante peut annuler la transaction (ceci dépend de la fonction appelante). | 4) Une fois le code réalisé, il n'y a plus qu'à tester, en provoquant l'évènement déclencheur dans Dolibarr. Attention, l'appel au '''run_trigger''' et encapsuler dans un transaction. Si votre trigger renvoie un code ko, la fonction appelante peut annuler la transaction (ceci dépend de la fonction appelante). | ||