714
edits
PolyglotBot (talk | contribs) m (Adding interlang links (links to translated versions of this page in other languages) triggered by origin English page "Triggers" update.) |
No edit summary |
||
| Line 10: | Line 10: | ||
{{TemplateDocDevZh}} | {{TemplateDocDevZh}} | ||
= | =将您的代码添加到业务事件= | ||
为了能够触发自定义代码以响应Dolibarr事件(创建/修改/删除、公司/发票/产品/用户或其他),Dolibarr提供了一种业务触发机制。例如,此机制允许您自定义工作流,以便Dolibarr的管理事件可以反映到另一个应用程序中。也没有什么可以阻止使用它来改变Dolibarr本身的行为:例如,在验证发票后自动创建合同、在删除记录时填写日志文件... | |||
请注意,触发器是一项开发功能,仅限于CRUD业务事件(创建|读取|更新|删除)。还有另一个功能允许开发人员将自定义代码注入应用程序,名为 [[Hooks system]] 。 | |||
因此,要添加将由触发器触发的自己的代码,过程如下: | |||
1) | 1)'''htdocs/modulebuilder/template/core/triggers/interface_99_modMyModule_MyModuleTriggers.class.php'''u文件复制一份,命名为 '''interface_99_all_''Xxx''.class.php''' 或 '''interface_99_mod''MyModule_Xxx''.class.php''' | ||
其中 '''''Xxx''''' 是您选择的以大写字母开头的字符串;'''99'''是触发器的优先级号(01是最高优先级);如果您希望触发器仅在 myModule 处于活动状态时才应激活,则'''''MyModule'''''是模块的名称;如果我们希望触发器始终处于活动状态,我们将使用'''all'''代替'''mod''MyModule'''''。此新文件必须保留在同一目录中。 | |||
注意:可用于 '''mod''MyModule''''' 的值在目录 ''htdocs/core/modules'' 中可见。 | |||
'''警告:所有'''这些参数都是检测触发器所必需的!例如,如果您忘记了优先级号,将无法检测到该触发器! | |||
例如,我们可以将我们的新触发器命名为 ''htdocs/core/triggers/interface_99_modFacture_Myworkflow.class.php'' | |||
通过创建一个名为本例的文件,我们的新触发器将在每个Dolibarr业务事件中触发,前提是发票模块处于活动状态。 | |||
''' | '''注意''':使用Dolibarr 3.2+,还可以将触发器文件放置在模块的子文件夹中。例如:如果模块位于htdocs/mymodule/中,则可以将触发器放置在htdocs/mymodule/core/triggers/中。但在这种情况下,必须将触发器声明到模块的描述符文件(如: '''/mymodule/core/modules/modMyModule.class.php''')中,才会找到触发器。 | ||
为此,请在数组 module_parts 中添加 'triggers' => 1 | |||
' | |||
<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 47: | Line 40: | ||
// 'substitutions' => 0, | // 'substitutions' => 0, | ||
// 'menus' => 0); | // 'menus' => 0); | ||
</ | </syntaxhighlight> | ||
然后停用并重新激活模块。这将在 [[Table llx_const|表llx_const]] 中添加一行记录,告诉Dolibarr必须在模块的目录 '''htdocs/mymodule/core/triggers/''' 中查找触发器。2) Edit the file ''interface_99_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceDemo'' by ''InterfaceMyworkflow'' | |||
Then go on page Home -> Admin Tools -> About Dolibarr ->Triggers. | Then go on page Home -> Admin Tools -> About Dolibarr ->Triggers. | ||
| Line 60: | Line 51: | ||
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 78: | Line 69: | ||
... | ... | ||
} | } | ||
</ | </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 111: | Line 102: | ||
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 | ||
//$this->context = array('paramX'=>$valueX); | //$this->context = array('paramX'=>$valueX); | ||
| Line 117: | Line 108: | ||
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 143: | Line 134: | ||
... | ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
=Conclusion= | =Conclusion= | ||
edits