触发系统: Difference between revisions

Jump to navigation Jump to search
no edit summary
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}}


=Execute your own code on a Dolibarr business event=
=将您的代码添加到业务事件=
To execute personalized code after a Dolibarr event (create/modify/remove a third party/invoice/product or other), Dolibarr offers a mechanism of business triggers. This mechanism allows you to personalize a work-flow to have Dolibarr business events being synchronized into another application for example.
为了能够触发自定义代码以响应Dolibarr事件(创建/修改/删除、公司/发票/产品/用户或其他),Dolibarr提供了一种业务触发机制。例如,此机制允许您自定义工作流,以便Dolibarr的管理事件可以反映到另一个应用程序中。也没有什么可以阻止使用它来改变Dolibarr本身的行为:例如,在验证发票后自动创建合同、在删除记录时填写日志文件...
You can also use triggers to modify the behavior of Dolibarr itself: for example, to create a contract automatically when an invoice is validated, a fill a log file when a record is deleted, ...


Note that triggers is a feature for developers that is limited to business CRUD events (Create | Read | Update | Delete). There is another feature for developers to inject custom code in the application called the [[Hooks system]].
请注意,触发器是一项开发功能,仅限于CRUD业务事件(创建|读取|更新|删除)。还有另一个功能允许开发人员将自定义代码注入应用程序,名为 [[Hooks system]] 。


So, to add your own code that will be triggered by a Dolibarr trigger, this is the process:
因此,要添加将由触发器触发的自己的代码,过程如下:


1) Copy the template trigger file '''htdocs/modulebuilder/template/core/triggers/interface_99_modMyModule_MyModuleTriggers.class.php''' under the new name:
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'''


*'''interface_99_all_''Xxx''.class.php'''
其中 '''''Xxx''''' 是您选择的以大写字母开头的字符串;'''99'''是触发器的优先级号(01是最高优先级);如果您希望触发器仅在 myModule 处于活动状态时才应激活,则'''''MyModule'''''是模块的名称;如果我们希望触发器始终处于活动状态,我们将使用'''all'''代替'''mod''MyModule'''''。此新文件必须保留在同一目录中。


or
注意:可用于 '''mod''MyModule''''' 的值在目录 ''htdocs/core/modules'' 中可见。


*'''interface_99_mod''MyModule_Xxx''.class.php'''
'''警告:所有'''这些参数都是检测触发器所必需的!例如,如果您忘记了优先级号,将无法检测到该触发器!


where ''Xxx'' is a string of your choice starting with uppercase character, ''99'' is the priority number for the execution of your trigger (1 being the top priority) and ''MyModule'' is the name of a module if you want that trigger is activated only if module MyModule is activated. If you want the trigger to be always active, use ''all'' instead of  modMyModule.
例如,我们可以将我们的新触发器命名为 ''htdocs/core/triggers/interface_99_modFacture_Myworkflow.class.php''
This new file must be saved inside same directory.


Note: Value you can use for mod MyModule are values found in directory
通过创建一个名为本例的文件,我们的新触发器将在每个Dolibarr业务事件中触发,前提是发票模块处于活动状态。
''htdocs/includes/modules''.


'''WARNING''': All those parameters are necessary for the trigger to be detected and executed! Eg: if you forget the priority number, your trigger won't be detected!
'''注意''':使用Dolibarr 3.2+,还可以将触发器文件放置在模块的子文件夹中。例如:如果模块位于htdocs/mymodule/中,则可以将触发器放置在htdocs/mymodule/core/triggers/中。但在这种情况下,必须将触发器声明到模块的描述符文件(如: '''/mymodule/core/modules/modMyModule.class.php''')中,才会找到触发器。


For example, you can name your new trigger file:
为此,请在数组 module_parts 中添加 'triggers' => 1
''htdocs/includes/triggers/interface_99_modFacture_Myworkflow.class.php''


By creating such a file with a name like the example, your new trigger file will be executed each time a Dolibarr business event occurs but only if the module Facture is enabled.
<syntaxhighlight lang="PHP">
 
'''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 47: Line 40:
//              'substitutions' => 0,
//              'substitutions' => 0,
//              'menus' => 0);
//              'menus' => 0);
</syntaxHighlight>
</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/'''
 


2) Edit the file ''interface_99_modMyModule_Myworkflow.class.php'' to rename class ''InterfaceDemo'' by ''InterfaceMyworkflow''
然后停用并重新激活模块。这将在 [[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">
<syntaxhighlight lang="PHP">
   function run_trigger($action,$object,$user,$langs,$conf)
   function run_trigger($action,$object,$user,$langs,$conf)
   {
   {
Line 78: Line 69:
         ...
         ...
   }
   }
</syntaxHighlight>
</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">
<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>
</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">
<syntaxhighlight lang="PHP">
function run_trigger($action,$object,$user,$lang,$conf)
function run_trigger($action,$object,$user,$lang,$conf)
{
{
Line 143: Line 134:
       ...
       ...
}
}
</syntaxHighlight>
</syntaxhighlight>


=Conclusion=
=Conclusion=
714

edits

Navigation menu