模块开发: Difference between revisions

no edit summary
m (Updating interlang links (links to translated versions of this page in other languages) triggered by origin English page "Module_development" update.)
No edit summary
Line 15: Line 15:
{{TemplateMainPageZh}}
{{TemplateMainPageZh}}


为开发Dolibarr模块,有以下几个步骤,本向导逐步引导你开发模块,扩展Dolibarr功能。包括以下内容 :
要创建新模块,需要执行以下几个步骤。本教程旨在向您介绍每一个步骤,以便添加一个模块来扩展Dolibarr的功能,例如添加以下一个或多个功能:


*在数据库中增加新表
*在数据库中添加新表
*增加菜单条目
*在菜单中添加您自己的菜单项
*为新表增加新的屏显
*添加对象和屏幕以输入/查看新表
*增删对象的选项卡 (发票、产品、订单、事件,...)
*添加或删除对象视图/编辑页面上的选项卡(发票、产品、订单、事件等)
*为导出工具添加预定义导出
*为导出工具添加预定义导出
*在主页增加插件(信息框)
*在主页增加插件(信息框)
Line 117: Line 117:
检查模块主文件,"init"函数中是否包括行:
检查模块主文件,"init"函数中是否包括行:


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


''规则:''
''规则:''
Line 128: Line 128:


data.sql实例
data.sql实例
<syntaxHighlight lang="sql">
<syntaxhighlight lang="sql">
  delete from llx_const where name='MYMODULE_IT_WORKS' and entity='__ENTITY__';
  delete from llx_const where name='MYMODULE_IT_WORKS' and entity='__ENTITY__';
  insert into llx_const (name, value, type, note, visible, entity) values ('MYMODULE_IT_WORKS','1','chaine','A constant vor my module',1,'__ENTITY__');
  insert into llx_const (name, value, type, note, visible, entity) values ('MYMODULE_IT_WORKS','1','chaine','A constant vor my module',1,'__ENTITY__');
</syntaxHighlight>
</syntaxhighlight>


对于MySQL数据库,文件必须是可操作的。REM:其他数据库的文件不被维护。它们将由另一个数据库的驱动程序进行读取和转换。
对于MySQL数据库,文件必须是可操作的。REM:其他数据库的文件不被维护。它们将由另一个数据库的驱动程序进行读取和转换。
Line 141: Line 141:
===生成 PHP DAO 类文件===
===生成 PHP DAO 类文件===
一旦将表创建到数据库中,就转到DEV/Skeleto目录
一旦将表创建到数据库中,就转到DEV/Skeleto目录
<syntaxHighlight lang="bash">php build_class_from_table.php tablename modulename</syntaxHighlight>
<syntaxhighlight lang="bash">php build_class_from_table.php tablename modulename</syntaxhighlight>
注意:如果命令不工作,尝试使用PHP-CLI PHP代替。
注意:如果命令不工作,尝试使用PHP-CLI PHP代替。
这将生成一个文件'''out.tablename.class.php''',它包含管理表tablename的类。
这将生成一个文件'''out.tablename.class.php''',它包含管理表tablename的类。
Line 165: Line 165:
为此,进入先前创建的模块主文件,并编辑$this->tabs数组:
为此,进入先前创建的模块主文件,并编辑$this->tabs数组:


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// Array to add new pages in new tabs or remove existing one
// Array to add new pages in new tabs or remove existing one
$this->tabs = array('objecttype:+tabname1:Title1:mylangfile@mymodule:$user->rights->mymodule->read:/mymodule/mypagetab1.php?id=__ID__'    // To add a new tab identified by code tabname1
$this->tabs = array('objecttype:+tabname1:Title1:mylangfile@mymodule:$user->rights->mymodule->read:/mymodule/mypagetab1.php?id=__ID__'    // To add a new tab identified by code tabname1
                             'objecttype:+tabname2:Title2:mylangfile@mymodule:$user->rights->mymodule->read:/mymodule/mypagetab2.php?id=__ID__',  // To add a new tab identified by code tabname2
                             'objecttype:+tabname2:Title2:mylangfile@mymodule:$user->rights->mymodule->read:/mymodule/mypagetab2.php?id=__ID__',  // To add a new tab identified by code tabname2
                             'objecttype:-tabname');                                                    // To remove an existing tab identified by code tabname
                             'objecttype:-tabname');                                                    // To remove an existing tab identified by code tabname
</syntaxHighlight>
</syntaxhighlight>


该数据包含一个字符串列表,每个字符串表示一个新选项卡。
该数据包含一个字符串列表,每个字符串表示一个新选项卡。
Line 194: Line 194:


对于每个对象类型,有两个文件要包含。
对于每个对象类型,有两个文件要包含。
<syntaxHighlight lang="php">require_once($url_fichier) ;</syntaxHighlight>
<syntaxhighlight lang="PHP">require_once($url_fichier) ;</syntaxhighlight>


包含示例 (DOL_DOCUMENT_ROOT 代表 dolibarr/htdocs/) :
包含示例 (DOL_DOCUMENT_ROOT 代表 dolibarr/htdocs/) :
Line 215: Line 215:


''例如:''
''例如:''
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$id=GETPOST('id','int');
$id=GETPOST('id','int');
$ref=GETPOST('ref','alpha');
$ref=GETPOST('ref','alpha');
$product = new Product($db) ;
$product = new Product($db) ;
$result = $product->fetch($id,$ref) ; // Test $result to check the database read is ok
$result = $product->fetch($id,$ref) ; // Test $result to check the database read is ok
</syntaxHighlight>
</syntaxhighlight>


'''3. 为你的对象类型获取所有可显示的选项卡列表'''
'''3. 为你的对象类型获取所有可显示的选项卡列表'''
Line 227: Line 227:


返回的数组为:
返回的数组为:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$head        // Array of tabs
$head        // Array of tabs
$head[$h]    // Element to describe one tab.
$head[$h]    // Element to describe one tab.
Line 233: Line 233:
$head[$h][1] // Title of tab
$head[$h][1] // Title of tab
$head[$h][2] // Code name to identify the tab
$head[$h][2] // Code name to identify the tab
</syntaxHighlight>
</syntaxhighlight>


''例如 :''
''例如 :''
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$head = product_prepare_head($product, $user) ; // 参数 $user 是要显示选项卡的对象,这就是"产品"对象为"user"对象准备的所有可用的选项卡的数组。
$head = product_prepare_head($product, $user) ; // 参数 $user 是要显示选项卡的对象,这就是"产品"对象为"user"对象准备的所有可用的选项卡的数组。
</syntaxHighlight>
</syntaxhighlight>


'''4. 在你的页面上显示选项卡'''
'''4. 在你的页面上显示选项卡'''
Line 244: Line 244:
使用函数dol_fiche_head()显示由函数XX_prepare_head()返回的$head数组定义的定义的所有选项卡。
使用函数dol_fiche_head()显示由函数XX_prepare_head()返回的$head数组定义的定义的所有选项卡。


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
dol_fiche_head($links, $active='0', $title='', $notab=0, $picto='')
dol_fiche_head($links, $active='0', $title='', $notab=0, $picto='')
//$links  // Tableau des onglets, appelé $head plus haut.
//$links  // Tableau des onglets, appelé $head plus haut.
Line 254: Line 254:
//            service
//            service
//            company
//            company
</syntaxHighlight>
</syntaxhighlight>


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.
Line 269: Line 269:


编辑该文件,以包括正确的 '''main.inc.php'''文件。
编辑该文件,以包括正确的 '''main.inc.php'''文件。
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// Load Dolibarr environment
// Load Dolibarr environment
$res=0;
$res=0;
Line 284: Line 284:
if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php");
if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php");
if (! $res) die("Include of main fails");
if (! $res) die("Include of main fails");
</syntaxHighlight>
</syntaxhighlight>
注: 你可能增加更多的 "../" 根据你的模块目录的深度。
注: 你可能增加更多的 "../" 根据你的模块目录的深度。
在3.2之后,为所有版本的Dolibarr开发的模块应该能够在不同的文件夹中移动,而不是“htdocs”,比如“htdocs/custom”,而不必修改模块的源代码,这就是为什么我们要做几个包含,所以这个规则必须被所有模块应用。
在3.2之后,为所有版本的Dolibarr开发的模块应该能够在不同的文件夹中移动,而不是“htdocs”,比如“htdocs/custom”,而不必修改模块的源代码,这就是为什么我们要做几个包含,所以这个规则必须被所有模块应用。
Line 300: Line 300:


''例如 :''
''例如 :''
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
dol_include_once('/mymodule/class/myclass.class.php', 'MyClass');
dol_include_once('/mymodule/class/myclass.class.php', 'MyClass');
</syntaxHighlight>
</syntaxhighlight>


*若要包含Dolibarr提供的类,请使用以下语法:
*若要包含Dolibarr提供的类,请使用以下语法:


''例如 :''
''例如 :''
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
require_once DOL_DOCUMENT_ROOT.'/core/class/doli.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doli.class.php';
</syntaxHighlight>
</syntaxhighlight>


===为现存表单增加新字段===
===为现存表单增加新字段===
Line 336: Line 336:
进行插入、更新或删除:
进行插入、更新或删除:


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$db->begin();  // Start transaction
$db->begin();  // Start transaction
$db->query("My SQL request insert, update or delete");
$db->query("My SQL request insert, update or delete");
$db->commit();      // Validate transaction
$db->commit();      // Validate transaction
or $db->rollback()  // Cancel transaction
or $db->rollback()  // Cancel transaction
</syntaxHighlight>
</syntaxhighlight>


读:
读:


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$resql=$db->query("My select request");
$resql=$db->query("My select request");
if ($resql)
if ($resql)
Line 366: Line 366:
}
}
}
}
</syntaxHighlight>
</syntaxhighlight>


===页面风格===
===页面风格===
Line 380: Line 380:
===使用Dolibarr日期选择器===
===使用Dolibarr日期选择器===
如果您的页面使用Dolibarr日期选择器(日历弹出),请使用以下行:
如果您的页面使用Dolibarr日期选择器(日历弹出),请使用以下行:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
  $form=new Form($db);
  $form=new Form($db);
  $form->select_date('','mykey',0,0,0,"myform");
  $form->select_date('','mykey',0,0,0,"myform");
</syntaxHighlight>
</syntaxhighlight>
字符串mykey将标识表单中的日期选择器。如果在同一页中使用多个日期选择器,则必须使用不同的值。
字符串mykey将标识表单中的日期选择器。如果在同一页中使用多个日期选择器,则必须使用不同的值。


Line 391: Line 391:


要获得表单POST的值,命令是:
要获得表单POST的值,命令是:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$mydate = dol_mktime(12, 0 , 0, $_POST['mykeymonth'], $_POST['mykeyday'], $_POST['mykeyyear']);
$mydate = dol_mktime(12, 0 , 0, $_POST['mykeymonth'], $_POST['mykeyday'], $_POST['mykeyyear']);
print strftime('%A %d %B %Y', $mydate);
print strftime('%A %d %B %Y', $mydate);
</syntaxHighlight>
</syntaxhighlight>


==新增设置页 (可选)==
==新增设置页 (可选)==
Line 412: Line 412:


如果页面位于目录'''admin/'''中,则不需要路径,例如:
如果页面位于目录'''admin/'''中,则不需要路径,例如:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->config_page_url = array("mymodule_setuppage.php");
$this->config_page_url = array("mymodule_setuppage.php");
</syntaxHighlight>
</syntaxhighlight>


如果页面在目录 '''mymodule/admin/'''中,则像这样:
如果页面在目录 '''mymodule/admin/'''中,则像这样:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->config_page_url = array("mymodule_setuppage.php@mymodule");
$this->config_page_url = array("mymodule_setuppage.php@mymodule");
</syntaxHighlight>
</syntaxhighlight>


===测试页面===
===测试页面===
Line 433: Line 433:


This is the example of code to declare your own menu entries in a module descriptor file:
This is the example of code to declare your own menu entries in a module descriptor file:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// Main menu entries
// Main menu entries
$this->menu = array(); // List of menus to add
$this->menu = array(); // List of menus to add
Line 468: Line 468:
'user'=>2); // 0=Menu for internal users,1=external users, 2=both
'user'=>2); // 0=Menu for internal users,1=external users, 2=both
$r++;
$r++;
</syntaxHighlight>
</syntaxhighlight>


To show menu or not depending on a permission, modify property '''perms''' in array. See chapter on permissions later to see how to add permissions.
To show menu or not depending on a permission, modify property '''perms''' in array. See chapter on permissions later to see how to add permissions.
Line 480: Line 480:
The way to define permissions that will manage your module is done inside the module descriptor created previously.
The way to define permissions that will manage your module is done inside the module descriptor created previously.
Modify line  
Modify line  
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->rights_class = 'mymodule'
$this->rights_class = 'mymodule'
</syntaxHighlight>
</syntaxhighlight>
with correct value for mymodule.
with correct value for mymodule.


Then you must fill the array $this->rights with as many entries than different permissions you need to manage.
Then you must fill the array $this->rights with as many entries than different permissions you need to manage.


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->rights[$r][0] = 10001;
$this->rights[$r][0] = 10001;
$this->rights[$r][1] = 'Label by default of permission';
$this->rights[$r][1] = 'Label by default of permission';
Line 494: Line 494:
$this->rights[$r][5] = 'subaction';
$this->rights[$r][5] = 'subaction';
$r++;
$r++;
</syntaxHighlight>
</syntaxhighlight>


Into $this->rights[$r][0], put a permission id not already used (See into menu '''System info''' on a working installation of Dolibarr to know list of id already used by standard modules.
Into $this->rights[$r][0], put a permission id not already used (See into menu '''System info''' on a working installation of Dolibarr to know list of id already used by standard modules.
Line 501: Line 501:
Into $this->rights[$r][4] and $this->rights[$r][5], put an action and subaction string without spaces. You will be able to test if a user has the permission in your PHP source code with the sequence:
Into $this->rights[$r][4] and $this->rights[$r][5], put an action and subaction string without spaces. You will be able to test if a user has the permission in your PHP source code with the sequence:


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
if ($user->rights->mymodule->action->subaction) ...
if ($user->rights->mymodule->action->subaction) ...
</syntaxHighlight>
</syntaxhighlight>


==信息窗 (可选)==
==信息窗 (可选)==
Line 513: Line 513:


''Example:''
''Example:''
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->boxes[0]['file']='mybox0.php@mymodule'
$this->boxes[0]['file']='mybox0.php@mymodule'
$this->boxes[0]['note']='My box 0'
$this->boxes[0]['note']='My box 0'
Line 519: Line 519:
$this->boxes[n]['file']='myboxn.php@mymodule'
$this->boxes[n]['file']='myboxn.php@mymodule'
$this->boxes[n]['note']='My box n'
$this->boxes[n]['note']='My box n'
</syntaxHighlight>
</syntaxhighlight>


Then create files '''htdocs/mymodule/core/boxes/mybox0.php''', '''htdocs/mymodule/core/boxes/mybox1.php'''... by copying an example from an existing box file (found into directory '''htdocs/core/boxes''').
Then create files '''htdocs/mymodule/core/boxes/mybox0.php''', '''htdocs/mymodule/core/boxes/mybox1.php'''... by copying an example from an existing box file (found into directory '''htdocs/core/boxes''').
Line 551: Line 551:
Value to put here must be a relative URL to your CSS file.
Value to put here must be a relative URL to your CSS file.
For example
For example
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->module_parts = array('css' => array('/mymodule/css/mymodule.css.php'));
$this->module_parts = array('css' => array('/mymodule/css/mymodule.css.php'));
</syntaxHighlight>
</syntaxhighlight>


===Test your style sheet===
===Test your style sheet===
Line 569: Line 569:


Example for page /htdocs/mymodule/mypage.php :
Example for page /htdocs/mymodule/mypage.php :
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$morejs=array("/mymodule/js/mymodule.js");
$morejs=array("/mymodule/js/mymodule.js");
llxHeader('','Titre','','','','',$morejs,'',0,0);  
llxHeader('','Titre','','','','',$morejs,'',0,0);  
</syntaxHighlight>
</syntaxhighlight>


==执行Dolibarr事件代码 (可选)==
==执行Dolibarr事件代码 (可选)==
Line 610: Line 610:


修改以下变量的值以修改作者名称
修改以下变量的值以修改作者名称
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
$this->editor_name = 'Roger@QQ:12464313';
$this->editor_name = 'Roger@QQ:12464313';
$this->editor_url = 'https://www.ivnun.cn';
$this->editor_url = 'https://www.ivnun.cn';
</syntaxHighlight>
</syntaxhighlight>


=一些编码规则和预定义函数=
=一些编码规则和预定义函数=
Line 630: Line 630:
*Run the script via Perl (need the Perl version 5.0 or later):
*Run the script via Perl (need the Perl version 5.0 or later):


<syntaxHighlight lang="bash">
<syntaxhighlight lang="bash">
perl makepack-dolibarrmodule.pl
perl makepack-dolibarrmodule.pl
</syntaxHighlight>
</syntaxhighlight>
The script asks you the name of your module, its major and minor version. A file '''mymodule.zip ''' will then be manufactured containing your module ready to be deployed.
The script asks you the name of your module, its major and minor version. A file '''mymodule.zip ''' will then be manufactured containing your module ready to be deployed.


*The person who receives your module must then place the file in the root directory of a Dolibarr installation and perform the command:
*The person who receives your module must then place the file in the root directory of a Dolibarr installation and perform the command:


<syntaxHighlight lang="bash">
<syntaxhighlight lang="bash">
tar -xvf mymodule.zip
tar -xvf mymodule.zip
</syntaxHighlight>
</syntaxhighlight>


*If you want your module to be public, you can submit it (the zip file) on Dolibarr market place web site: {{LinkToPluginDownload}} (you must create an account first and be logged to use the link "Submit a module/product").
*If you want your module to be public, you can submit it (the zip file) on Dolibarr market place web site: {{LinkToPluginDownload}} (you must create an account first and be logged to use the link "Submit a module/product").
715

edits