模块开发: Difference between revisions

m
no edit summary
Tag: 2017 source edit
mNo edit summary
Tag: 2017 source edit
Line 116: Line 116:
检查模块主文件,"init"函数中是否包括行:
检查模块主文件,"init"函数中是否包括行:


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


''规则:''
''规则:''
Line 125: Line 127:


data.sql实例
data.sql实例
<source 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__');
</source>
</syntaxHighlight>


对于MySQL数据库,文件必须是可操作的。REM:其他数据库的文件不被维护。它们将由另一个数据库的驱动程序进行读取和转换。
对于MySQL数据库,文件必须是可操作的。REM:其他数据库的文件不被维护。它们将由另一个数据库的驱动程序进行读取和转换。
Line 138: Line 140:
===生成 PHP DAO 类文件===
===生成 PHP DAO 类文件===
一旦将表创建到数据库中,就转到DEV/Skeleto目录
一旦将表创建到数据库中,就转到DEV/Skeleto目录
<source lang="bash">php build_class_from_table.php tablename modulename</source>
<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 162: Line 164:
为此,进入先前创建的模块主文件,并编辑$this->tabs数组:
为此,进入先前创建的模块主文件,并编辑$this->tabs数组:


<source 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
</source>
</syntaxHighlight>


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


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


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


''例如:''
''例如:''
<source 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
</source>
</syntaxHighlight>


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


返回的数组为:
返回的数组为:
<source 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 230: Line 232:
$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
</source>
</syntaxHighlight>


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


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


<source 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 251: Line 253:
//            service
//            service
//            company
//            company
</source>
</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 266: Line 268:


编辑该文件,以包括正确的 '''main.inc.php'''文件。
编辑该文件,以包括正确的 '''main.inc.php'''文件。
<source lang="php">
<syntaxHighlight lang="php">
// Load Dolibarr environment
// Load Dolibarr environment
$res=0;
$res=0;
Line 281: Line 283:
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");
</source>
</syntaxHighlight>
注: 你可能增加更多的 "../" 根据你的模块目录的深度。
注: 你可能增加更多的 "../" 根据你的模块目录的深度。
在3.2之后,为所有版本的Dolibarr开发的模块应该能够在不同的文件夹中移动,而不是“htdocs”,比如“htdocs/custom”,而不必修改模块的源代码,这就是为什么我们要做几个包含,所以这个规则必须被所有模块应用。
在3.2之后,为所有版本的Dolibarr开发的模块应该能够在不同的文件夹中移动,而不是“htdocs”,比如“htdocs/custom”,而不必修改模块的源代码,这就是为什么我们要做几个包含,所以这个规则必须被所有模块应用。
Line 297: Line 299:


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


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


''例如 :''
''例如 :''
<source 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';
</source>
</syntaxHighlight>


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


<source 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
</source>
</syntaxHighlight>


读:
读:


<source lang="php">
<syntaxHighlight lang="php">
$resql=$db->query("My select request");
$resql=$db->query("My select request");
if ($resql)
if ($resql)
Line 363: Line 365:
}
}
}
}
</source>
</syntaxHighlight>


===页面风格===
===页面风格===
Line 377: Line 379:
===使用Dolibarr日期选择器===
===使用Dolibarr日期选择器===
如果您的页面使用Dolibarr日期选择器(日历弹出),请使用以下行:
如果您的页面使用Dolibarr日期选择器(日历弹出),请使用以下行:
<source 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");
</source>
</syntaxHighlight>
字符串mykey将标识表单中的日期选择器。如果在同一页中使用多个日期选择器,则必须使用不同的值。
字符串mykey将标识表单中的日期选择器。如果在同一页中使用多个日期选择器,则必须使用不同的值。


Line 388: Line 390:


要获得表单POST的值,命令是:
要获得表单POST的值,命令是:
<source 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);
</source>
</syntaxHighlight>


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


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


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


===测试页面===
===测试页面===
Line 430: Line 432:


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:
<source 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 465: Line 467:
'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++;
</source>
</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 477: Line 479:
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  
<source lang="php">
<syntaxHighlight lang="php">
$this->rights_class = 'mymodule'
$this->rights_class = 'mymodule'
</source>
</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.


<source 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 491: Line 493:
$this->rights[$r][5] = 'subaction';
$this->rights[$r][5] = 'subaction';
$r++;
$r++;
</source>
</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 498: Line 500:
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:


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


==信息窗 (可选)==
==信息窗 (可选)==
Line 510: Line 512:


''Example:''
''Example:''
<source 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 516: Line 518:
$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'
</source>
</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 548: Line 550:
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
<source 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'));
</source>
</syntaxHighlight>


===Test your style sheet===
===Test your style sheet===
Line 566: Line 568:


Example for page /htdocs/mymodule/mypage.php :
Example for page /htdocs/mymodule/mypage.php :
<source 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);  
</source>
</syntaxHighlight>


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


修改以下变量的值以修改作者名称
修改以下变量的值以修改作者名称
<source 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';
</source>
</syntaxHighlight>


=一些编码规则和预定义函数=
=一些编码规则和预定义函数=
Line 627: Line 629:
*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):


<source lang="bash">
<syntaxHighlight lang="bash">
perl makepack-dolibarrmodule.pl
perl makepack-dolibarrmodule.pl
</source>
</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:


<source lang="bash">
<syntaxHighlight lang="bash">
tar -xvf mymodule.zip
tar -xvf mymodule.zip
</source>
</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").
Line 642: Line 644:
**If quality is enough and license permits it, code might be added inside main Dolibarr sources (except if you disagree for that).
**If quality is enough and license permits it, code might be added inside main Dolibarr sources (except if you disagree for that).


=Delistor上的外部模块的启用/激活条件=
=Dolistor上的外部模块的启用/激活条件=


See [[Module_Dolistore_Validation_Rules|Validation Rules]]
See [[Module_Dolistore_Validation_Rules|Validation Rules]]