模块开发: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Line 54: Line 54:
=== 创建模块主文件 ===
=== 创建模块主文件 ===
创建模块主文件.  
创建模块主文件.  
* 创建目录 '''/htdocs/''mymodule''/core/modules'''. 从目录dev/skeletons拷贝文件modMyModule.class.php到新建目录。
* 创建目录 '''/htdocs/''mymodule''/core/modules'''. 从目录/modulebuilder/template/core/modules拷贝文件modMyModule.class.php到新建目录。


将 modMyModule.class.php 改名,名称对应于您的模块。 名称以 'mod'开头,以.class.php结尾。 (例如:要创建模块 'NewName', 对应的模块名为'modNewName.class.php').
将 modMyModule.class.php 改名,名称对应于您的模块。名称以 'mod'开头,以.class.php结尾。 (例如:要创建模块 'NewName', 对应的模块名为'modNewName.class.php').
接下来修改此文件的内容如下:
接下来修改此文件的内容如下:
* 替换所有 "modMyModule",对应于模块名称。 此值须以'mod'开头且仅含字符 (例如,模块为 'NewName', 替换所有 "modMyModule" 为 "modNewName")。
* 替换所有 "modMyModule",对应于模块名称。 此值须以'mod'开头且仅含字符 (例如,模块为 'NewName', 替换所有 "modMyModule" 为 "modNewName")。
Line 86: Line 86:


<source lang="php">$this->_load_tables('/mymodule/sql/');</source>
<source lang="php">$this->_load_tables('/mymodule/sql/');</source>


''规则:''
''规则:''
* 新建表的命令在文件 '''llx_matable.sql''' , 或者还有 '''llx_matable.key.sql''' (参见 '''install/mysql/tables''' 实例).
* 新建表的命令在文件 '''llx_matable.sql'''中, 或者还有 '''llx_matable.key.sql''' (参见 '''install/mysql/tables''' 实例).
 
* 管理数据:在目录 '''/mymodule/sql/'''中创建文件 '''data.sql''' 文件中包括增删模改数据的命令。
* 管理数据:在目录 '''/mymodule/sql/'''中创建文件 '''data.sql''' 文件中包括增删模改数据的命令。
* 不要使用双引号 (如使用 'chaine' 而不使用 "chaine") 因为双引号有特殊意义(PostGreSQL)。
* 不要使用双引号 (如使用 'chaine' 而不使用 "chaine") 因为双引号有特殊意义(PostGreSQL)。
Line 106: Line 104:


=== 生成 PHP DAO 类文件 ===
=== 生成 PHP DAO 类文件 ===
Once you have created your tables into the database, move to the dev/skeletons directory and run the script
一旦将表创建到数据库中,就转到DEV/Skeleto目录
 
<source lang="bash">php build_class_from_table.php tablename modulename</source>
<source lang="bash">php build_class_from_table.php tablename modulename</source>
Note: If the command does not work, try to use php-cli php instead.
注意:如果命令不工作,尝试使用PHP-CLI PHP代替。
这将生成一个文件'''out.tablename.class.php''',它包含管理表tablename的类。
在这个类中,您可以找到CRUD方法(Create / Read / Update / Delete),在其中进行插入、获取(选择)、更新、删除表中的行。
从文件名中删除“out”,并将文件放在“模块”的子目录中(例如,在htdocs/mymodule/class中)。


This will generate a file '''out.tablename.class.php''' that contains the class to manage table tablename.
已经生成了一个文件'''out.tablename_script.php''',并包含一个示例代码,使用了4个CRUD方法中的每一个。
In this class, you can find the CRUD methods (Create / Read / Update / Delete) already working to do an insert, a fetch (select), an update, a delete of a row in table.
Remove just the "out" from the file name and put the file in a subdirectory of '''htdocs''' specific to your module (in htdocs/mymodule for example).


A file '''out.tablename_script.php''' has been generated and contains a sample code to use the class for each of the 4 CRUD methods.
''自从Dolibarr 6.0开,不再提供文件dev/skeletons/build_class_from_table.php。要从一个表生成一个类,您应该使用ModuleBuilder模块。


'''Since Dolibarr 6.0, the file dev/skeletons/build_class_from_table.php is no more provided. To build a class from a table you should use the ModuleBuilder modules provided as standard module.'''
=== 使用ModuleBuilder生成PHP DAO类文件 ===
* 使用ModuleBuilder生成PHP DAO类文件,简单至在下图中选对'对象'选项卡,输入'对象名',然后点击创建,即可生成相关一切文档。
[[File:新对象.png|800px]]


参见[[ModuleBuilder用户手册]]
== 选项卡管理(可选) ==
== 选项卡管理(可选) ==


Line 124: Line 125:
'''何时''': 增加对象选项卡 (对象是指‘发票’、‘订单’、‘提议’、‘会员’等等)
'''何时''': 增加对象选项卡 (对象是指‘发票’、‘订单’、‘提议’、‘会员’等等)


To do this go into the file descriptor of module previously created and edit the $this->tabs array:
为此,进入先前创建的模块主文件,并编辑$this->tabs数组:


<source lang="php">
<source lang="php">
Line 133: Line 134:
</source>
</source>


The table should contain a list of strings, each string representing a new tab.
该数据包含一个字符串列表,每个字符串表示一个新选项卡。
The format of the string consisting of 5 parts separated by ":"
字符串由":"分隔的5个部分组成。
* Part 1: The element type (objecttype) where the tab should appear is a value from the following:
* Part 1: 选项卡出现在的对象类型(objecttype),只能是以下值:
{{TemplateModuleTabs}}
{{TemplateModuleTabs}}
* Part 2: A code to identify tab to add (start with +) or to remove (start with -)
* Part 2: 标识选项卡是添加(以+开始)或移除(以-start)的代码
* Part 3: The title of the tab. This can be a hard read or better code translation in a file lang.
* Part 3: 选项卡的标题。这可以是lang文件中的译文代码或者是一个直接的字符串。
* Part 4: The name of the file "*.lang" which contains correspondence between the code translation and language to display. If this name is followed with @mymodule, Dolibarr will search translation file "*.lang" within the module, so htdocs/mymodule/langs/code_CODE/mylangfile.lang, otherwise Dolibarr will look for file htdocs/langs/code_CODE/mylangfile.lang  
* Part 4: 文件“*.lang”的名称,包含译文代码和要显示的语言之间的对应关系。如果这个名字跟随 @mymodule,Dolibarr将搜索模块内的翻译文件“*.lang”,即"htdocs/mymodule/langs/code_CODE/mylangfile.lang"。否则Dolibarr将查找文件"htdocs/langs/code_CODE/mylangfile.lang"
* Part 5: A condition to test if tab must be visible. Put 1 to be always visible.
* Part 5: 测试选项卡是否必须可见的条件。为1则一直可见。
* Part 6: The url of the page to display when you click on the tab. The __ID__ string will be replaced automatically by the Id of the element concerned.
* Part 6: 单击选项卡时要显示的页面的URL。字符串__ID__将由相关元素的ID自动替换。


To feed the contents of the tab with data from the database, see the next chapter.
要用数据库中的数据来填充选项卡的内容,请参阅下一章。


=== 在你的页上显示选项卡导航 ===
=== 在你的页上显示选项卡导航 ===
'''When''': To show all standard tabs of an object (produit, tiers, etc.) on your page.
'''何时''': 在页面上显示对象的所有标准选项卡(产品、第三方等)。


You must follow these steps :
你必须遵循这些步骤:


'''1. Include files you need into your file'''
'''1. 包含文件中需要的文件'''


For each object type, there are two files to include with line
对于每个对象类型,有两个文件要包含。
<source lang="php">require_once($url_fichier) ;</source>
<source lang="php">require_once($url_fichier) ;</source>


This is example of files to include (DOL_DOCUMENT_ROOT is often dolibarr/htdocs/) :
包含示例 (DOL_DOCUMENT_ROOT 代表 dolibarr/htdocs/) :
* Object thirdparty (thirdparty) :
* 对象第三方(thirdparty) :
** DOL_DOCUMENT_ROOT/societe/class/societe.class.php
** DOL_DOCUMENT_ROOT/societe/class/societe.class.php
** DOL_DOCUMENT_ROOT/core/lib/company.lib.php
** DOL_DOCUMENT_ROOT/core/lib/company.lib.php
* Object product (product) :
* 对象产品(product) :
** DOL_DOCUMENT_ROOT/product/class/product.class.php
** DOL_DOCUMENT_ROOT/product/class/product.class.php
** DOL_DOCUMENT_ROOT/core/lib/product.lib.php
** DOL_DOCUMENT_ROOT/core/lib/product.lib.php
* Object invoice (invoice) :
* 对象发票 (invoice) :
** DOL_DOCUMENT_ROOT/compta/facture/class/facture.class.php
** DOL_DOCUMENT_ROOT/compta/facture/class/facture.class.php
** DOL_DOCUMENT_ROOT/core/lib/invoice.lib.php
** DOL_DOCUMENT_ROOT/core/lib/invoice.lib.php
...
...


'''2. Create and load the object to show into your tab'''
'''2. 创建并加载对象以显示在您的选项卡中'''


Create instance of object of the correct class and load object from database by using the fetch method of object en providing to this method the id you get from url (ie : /mytab.php?id=1).
通过从url(ie : /mytab.php?id=1)获取的id值,使用对象的获取方法,从数据库中创建正确的类和加载对象的实例。


''Example :''
''例如:''
<source lang="php">
<source lang="php">
$id=GETPOST('id','int');
$id=GETPOST('id','int');
Line 179: Line 180:
</source>
</source>


'''3. Get list of all tabs to show for your object type'''
'''3. 为你的对象类型获取所有可显示的选项卡列表'''


Use fonction XXX_prepare_head($obj), where XXX is name of object. This will return an array with all definition of tab entries to show. Parameter to put into this method is the instance of object you want tabs.
使用函数XXX_prepare_head($obj),其中XXX是对象的名称。这将返回一个数组,包含所有选项卡条目的定义。要放入该方法的参数是要显示选项卡的对象实例。


The resulting array has the following structure
返回的数组为:
<source lang="php">
<source lang="php">
$head        // Array of tabs
$head        // Array of tabs
Line 192: Line 193:
</source>
</source>


''Example :''
''例如 :''
<source lang="php">
<source lang="php">
$head = product_prepare_head($product, $user) ; // parameter $user is present for some function anly
$head = product_prepare_head($product, $user) ; // 参数 $user 是要显示选项卡的对象,这就是"产品"对象为"user"对象准备的所有可用的选项卡的数组。
</source>
</source>


'''4. show tabs into your page'''
'''4. 在你的页面上显示选项卡'''


Use function dol_fiche_head() to show all tabs defined into array $head returned by XX_prepare_head().
使用函数dol_fiche_head()显示由函数XX_prepare_head()返回的$head数组定义的定义的所有选项卡。


<source lang="php">
<source lang="php">
Line 215: Line 216:
This function will show required tabs and open an html element ''< div class="" >'' that correspond to the area under the tabs. To close area of a tab, just use ''< /div >'' into your PHP page.
This function will show required tabs and open an html element ''< div class="" >'' that correspond to the area under the tabs. To close area of a tab, just use ''< /div >'' into your PHP page.


== Create or modify PHP screens (optional) ==
== 创建或修改 PHP 屏显 (可选) ==
'''When''': If the purpose of your module is to add features that require new or modified screens.
'''何时''': 如果模块的目的是为了修改屏显。


=== 创建新 PHP 屏显 ===
=== 创建新 PHP 屏显 ===
You must then create your PHP pages that will show/edit data from tables using the skeleton templates provided as an example in the directory '''modulebuilder/myobject_page.php''' (For the development of a script from the command line, see [[Script development]]).
然后,您必须创建PHP页面,使用目录'''modulebuilder/myobject_page.php'''中提供的框架模板来显示/编辑表中的数据。 (从命令行开发脚本, 见 [[Script development]]).
 
要创建一个新的用户屏幕,创建一个模块名的子目录(若无)  (例如 '''htdocs/mymodule''') 来存储创建的屏幕。


To create a new user screen, create a subdirectory of '''htdocs''' (if not already done) for your module (in '''htdocs/mymodule''' for example) to store the pages you will create.
复制 '''myobject_page.php''' 做为屏幕页。


Copy, into that directory, the file '''myobject_page.phpp''' that will serve as a starting point for your page file.
编辑该文件,以包括正确的 '''main.inc.php'''文件。
Edit the file to have a correct relative path to the main.inc.php file.
<source lang="php">
<source lang="php">
// Load Dolibarr environment
// Load Dolibarr environment
Line 241: Line 243:
if (! $res) die("Include of main fails");
if (! $res) die("Include of main fails");
</source>
</source>
Note that you may add more "../" depending on the depth of the file relative to your module directory tree.
注: that you may add more "../" depending on the depth of the file relative to your module directory tree.
The modules developed for all version of Dolibarr after the 3.2 should be able to be moved in a different folder than "htdocs", like "htdocs/custom", without having to modify the source code of the module, that's why we make several includes, so this rule must be applied by all modules.
The modules developed for all version of Dolibarr after the 3.2 should be able to be moved in a different folder than "htdocs", like "htdocs/custom", without having to modify the source code of the module, that's why we make several includes, so this rule must be applied by all modules.


Line 265: Line 267:
</source>
</source>


=== 现存表单增加新字段 ===
=== 为现存表单增加新字段 ===
You may want to provide a module that add more fields into forms (input and view) of some elements.  
You may want to provide a module that add more fields into forms (input and view) of some elements.  


513

edits

Navigation menu