Line 7:
Line 7:
So, to add your own code that will be triggered by a Dolibarr trigger, this is the way to process:
So, to add your own code that will be triggered by a Dolibarr trigger, this is the way to process:
−
+
1) Copy the trigger file '''htdocs/core/triggers/interface_all_Demo.class.php''' under the new name:
−
1) (only for Dolibarr >= v3.2.0) in your module descriptor file (eg: '''/mymodule/core/modules/modMyModule.class.php'''), add triggers->1 into module_parts array:
−
<source lang="php">
−
// 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,
−
// 'login' => 0,
−
// 'substitutions' => 0,
−
// 'menus' => 0);
−
</source>
−
Then disable and reenable your module. This will add a record into [[Table llx_const]] to say Dolibarr a trigger file must be searched into module directory '''htdocs/mymodule/core/triggers/'''
−
−
−
2) Copy the trigger file '''htdocs/core/triggers/interface_all_Demo.class.php''' under the new name:
* '''interface_all_''Xxx''.class.php'''
* '''interface_all_''Xxx''.class.php'''
or
or
Line 34:
Line 21:
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.
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.
−
'''Note''': starting from Dolibarr v3.2.x, you have to place triggers inside the '''core/''' folder instead of includes/ (eg: htdocs/core/triggers/interface_modMyModule_Xxx.class.php).
+
'''Note''': Before Dolibarr 3.2, you have to place triggers inside the '''includes/triggers/''' folder instead of '''core/triggers/''' (eg: htdocs/includes/triggers/interface_modMyModule_Xxx.class.php).
−
'''Note2''': You can also place the triggers 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/
+
'''Note2''': With Dolibarr 3.2+, you can also place the triggers 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:
+
<source lang="php">
+
// 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,
+
// 'login' => 0,
+
// 'substitutions' => 0,
+
// 'menus' => 0);
+
</source>
+
Then disable and reenable your module. This will add a record into [[Table llx_const]] to say Dolibarr a trigger file must be searched into module directory '''htdocs/mymodule/core/triggers/'''
−
3) Edit the file ''interface_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceDemo'' by ''InterfaceMyworkflow''
+
2) Edit the file ''interface_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceDemo'' by ''InterfaceMyworkflow''
Then go on page Home-> System Infos -> Dolibarr -> Triggers.
Then go on page Home-> System Infos -> Dolibarr -> Triggers.
Line 45:
Line 41:
−
4) Return now to edition of file to add your own code inside function ''run_trigger''.
+
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''':
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''':
Line 78:
Line 74:
* '''$conf''' is the object that contains all the Dolibarr configuration.
* '''$conf''' is the object that contains all the Dolibarr configuration.
−
5) Once your trigger file has been developed, last action is to test by using Dolibarr to execute the business event that should trigger your code. Warning, call of function '''run_trigger''' is encapsulated into a transaction. If your code return a KO code, the calling function may rollback the transaction (this depends on calling function).
+
+
4) Once your trigger file has been developed, last action is to test by using Dolibarr to execute the business event that should trigger your code. Warning, call of function '''run_trigger''' is encapsulated into a transaction. If your code return a KO code, the calling function may rollback the transaction (this depends on calling function).
Add log information inside your function '''run_trigger''' to know if your code is correctly triggered and runs correctly. For this, you can use the function
Add log information inside your function '''run_trigger''' to know if your code is correctly triggered and runs correctly. For this, you can use the function
dol_syslog("my log message", LOG_DEBUG);
dol_syslog("my log message", LOG_DEBUG);