要创建新模块,需要执行以下几个步骤。本教程旨在向您介绍每一个步骤,以便添加一个模块来扩展Dolibarr的功能,例如添加以下一个或多个功能:

  • 在数据库中添加新表
  • 在菜单中添加您自己的菜单项
  • 添加对象和屏幕以输入/查看新表
  • 在视图页上添加或删除选项卡/编辑对象(发票、产品、订单、事件等)
  • 为内部导出工具添加预定义的导出
  • 向主页添加新信息框/widget小组件
  • 添加新的替换变量
  • 定义新权限
  • 自动触发指定 Dolibarr 操作的代码
  • 将自定义代码插入 Dolibarr 的钩子位置
  • 为所创建的对象的引用添加编号模块(为数据自动编码的模块)
  • 添加PDF文档模板
  • 添加一个新主题/皮肤

等等...

以下章节介绍了如何以简单的方式手动完成所有这些操作。对于有经验的开发人员来说,使用MDA生成的方法也不错,详见最后一章。

使用模块生成器构建模块

从Dolibarr 9.0开始,您可以使用"模块生成器"(Module Builder)创建模块的主要文件。要激活它 :

  • 设置参数 MAIN_FEATURES_LEVEL 的值为 2 ( 设置 / 其他 )(或者修改llx_const表中该项的记录的value值为2)

 

  • 在 主页 - 设置 - 设置-模块/应用 多模块工具 中激活“模块生成器”(ModuleBuilder)

 

  • 点击屏幕右上角菜单栏中出现的'Bug'图标

 

  • 运行的ModuleBuilder界面如下:

 

外部模块模板示例

一个Dolibarr外部模块的模板/框架:Github Dolibarr模块

创建模块

以下章节描述了创建Dolibarr模块所需的操作。无论模块的用途如何,第一章描述的操作都是必须要完成的,后续章节描述的操作是否要做将取决于模块的需求。

创建模块描述符(必做)

何时:无论其用途如何,开发扩展模块时都是强制性的。从Dolibarr 9.0开始,您可以使用“模块生成器”创建模块的描述符。该工具仍处于开发和完善阶段,但其已经可以使用。

使用模块生成器创建描述符

  • 通过单击"Bug"图标启动模块生成器

 

  • 在下图“模块名称”处输入不含空格的模块名称(模块名称不得包含下划线 :_),然后单击"创建"按钮。

 

  • 已初始化具有第一个文件的模块。然后,您可以更改模块描述符的参数:

 

备选方案:手动创建描述符(不使用模块生成器)

第一步是创建模块描述文件(描述符)。为此:

  • 创建目录 htdocs/custom/mymodule/core/modules 。
  • 进入目录 htdocs/modulebuilder/template/core/modules 将文件 modMyModule.class.php 复制到目录 htdocs/custom/mymodule/core/modules 中。
  • 重命名modMyModule.class.php文件,只需修改MyModule部分即可(文件必须以mod开头)

然后,编辑此文件的内容:

  • 将所有modMyModule替换为与模块用途相对应的值。此值必须始终以“mod”开头,并且仅包含字母字符。
  • 通过 $this->numero = 100000 设置模块ID。为避免冲突,您可以参考以下页面查找已分配的ID:模块ID清单 。
  • 可能还需要修改构造函数中定义的其他变量(有关其含义,请参阅类文件代码中的注释)。

模块的描述文件(描述符)已准备就绪。请参阅下一步“测试描述符”以激活您的新模块。

测试描述符

启动Dolibarr并进入 主页->设置->模块/应用 页面,您应该会看到一个新行,其中包含新模块以及是否激活它的选项(浏览每个模块类别的所有选项卡,直到找到它)。

$this->special 的值决定了模块位于哪个类别选项卡中。

注意:如果在模块描述符中设置了属性 version ,则应在 模块/应用 页面看到包含新模块的新行以及是否激活它的选项。如果将属性 version 设置为“develop”或“experiental”,若要查看模块,必须先进入页面 “主页->设置->其他设置”,并将参数 MAIN_FEATURES_LEVEL 设置为 1 以查看“实验性”(experimental)模块,或设置为 2 以查看“开发”(development)模块。

模块的名称

模块名称不得包含下划线或下划线:_

新模块的树视图(必需)

以下是组织模块文件时要遵循的树结构(如果使用ModuleBuilder生成文件,则自动采用此树结构)。

注:zip 文件也必须符合此规则,只有第二行是强制性的。

  • mymodule/* ---------------- 包含php页面(请注意,您也可以添加您选择的任何其他子目录)。注意:如果您的模块是一个metapackage(一个以zip的形式嵌入其他模块的模块),则必须在此处放置一个文件 metapackage.conf。
  • mymodule/build/ ----------- 可以包含用于编译或构建模块安装包而开发的任何文件
  • mymodule/core/modules/ ---- 包含模块描述符文件 modMyModule.class.php
  • mymodule/core/triggers ---- 包含模块提供的触发器
  • mymodule/admin/ ----------- 包含设置模块的页面
  • mymodule/class/ ----------- 包含模块提供的PHP类文件
  • mymodule/css -------------- 包含模块提供的CSS文件
  • mymodule/js --------------- 包含模块提供的javascript文件
  • mymodule/docs ------------- 提供doc和licence文件
  • mymodule/img -------------- 包含模块提供的图片文件
  • mymodule/langs/xx_XX ------ 包含xx_XX语言的语言文件(至少要放一个 en_US 语言文件)
  • mymodule/lib -------------- 包含模块提供和使用的库文件
  • mymodule/scripts ---------- 提供命令行工具或脚本。注意:命令行脚本的第一行必须是:#!/usr/bin/env php
  • mymodule/sql -------------- 包含模块提供的用于添加新表或索引的SQL文件
  • mymodule/themes/mytheme --- 如果模块提供了自己的主题/皮肤

一个好的Dolibarr扩展模块的模板/框架:GitHub Dolibarr Module Modèle

创建数据表和 PHP DAO 类 (可选)

何时:如果您的模块需要管理自己的数据

创建 .sql 文件

如果您的模块要处理在Dolibarr标准版中不存在的数据,则需要自定义数据库表来存储这些数据。

在模块的目录中创建sql子目录(mymodule/sql),用于放置要创建的sql脚本(使用ModuleBuilder,在"对象"选项卡中创建新对象后,将自动执行此步骤)。

接下来,检查模块描述符文件,在 init 函数中是否包括行:

$this->_load_tables('/mymodule/sql/');

并且未被注释掉。

应遵循的规则:

  • 根据每个表1个文件的原则添加表的创建命令文件,命名为 llx_mytable.sql(用于表),可能还附带 llx_mytable.key.sql 文件(用于索引或外键)。示例请参阅 install/mysql/tables 中的现有文件。
  • SQL字段的推荐类型和名称在 [[1]] 页中定义。
  • 管理数据:对于添加/操作数据的命令,它们都必须位于同一目录 /mymodule/sql/ 中名为 data.sql 的文件中。
  • 不要使用双引号(如 "string"),因为双引号(")在 PostGreSQL 中有特殊的含义

data.sql文件内容示例:

 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__');

文件中的SQL语句必须对 mysql 数据库有效。请注意:不需要维护其他数据库类型的文件。它们由数据库驱动程序动态读取和转换。

测试 .sql 文件

文件准备就绪后,您可以返回Dolibarr的模块管理页面,禁用模块,删除数据库表(如果它们已经存在),然后重新激活模块。正常情况下,应该可以通过激活模块来重新创建表。否则,请手动检查脚本,或查看Dolibarr日志。

生成 PHP DAO 表访问类

创建对象时,ModuleBuilder会自动生成对象的PHP DAO类文件。

如果不使用模块生成器,您将在模块框架文件htdocs/modulebuilder/templates/class/myobject.class.php 中找到示例。

在这个类中,已经有可调用的 CRUD(Create/Read/Update/Delete)方法来执行表行数据的插入、获取(select)、更新和删除。修改此文件以使用正确的模块名和表名,并将该文件放在模块的 class 子目录中。若使用 ModuleBuilder 只需点击几下鼠标即可完成此操作。

使用ModuleBuilder生成PHP DAO类文件

  • 使用ModuleBuilder生成PHP DAO类文件,简单至在下图中选择'对象'选项卡,输入'对象名',然后点击创建,即可生成相关一切文档。

 

参见ModuleBuilder用户手册

选项卡显示(可选)

添加或删除对象表单上的选项卡

何时:在标准对象(发票、订单、报价单、会员…)的表单中添加您自己的选项卡

要执行此操作,请打开之前创建的模块描述符文件并编辑 $this->tabs 数组:

	// 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
                            '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

该表应包含字符串列表,每个字符串表示一个新选项卡。字符串格式由6个部分组成,由“:”分隔

  • 第1部分:选项卡的元素类型(objecttype),只能是下列值之一:
    • 'categories_x'-------在类别视图中添加选项卡(将 'x' 替换为类别类型:0=产品;1=供应商;2=客户/准客户;3=会员)
    • 'contact'------------在联系人/地址视图中添加选项卡
    • 'contract'-----------在合同视图中添加选项卡
    • 'group'--------------在用户组视图中添加选项卡
    • 'intervention'-------在现场服务视图中添加选项卡
    • 'invoice'------------在客户发票视图中添加选项卡
    • 'invoice_supplier'---在供应商发票视图中添加选项卡
    • 'member'-------------在会员视图中添加选项卡
    • 'opensurveypoll'-----在调查视图中添加选项卡
    • 'order'--------------在客户订单视图中添加选项卡
    • 'order_supplier'-----在供应商订单视图中添加选项卡
    • 'payment'------------在客户付款视图中添加选项卡
    • 'payment_supplier'---在供应商付款视图中添加选项卡
    • 'product'------------在产品视图中添加选项卡
    • 'propal'-------------在报价单视图中添加选项卡
    • 'project'------------在项目视图中添加选项卡
    • 'stock'--------------在库存视图中添加选项卡
    • 'thirdparty'---------在合作方视图中添加选项卡
    • 'user'---------------在用户视图中添加选项卡
  • 第2部分:用于标识要添加(以+开头)或删除(以-开头)的选项卡的codeName
  • 第3部分:选项卡的标题。这可以是硬编码的字符串,或者是更好的,lang文件中的翻译代码。
  • 第4部分:文件“*.lang”的名称(不带.lang扩展名),此文件内有翻译代码和要显示的语言字符串之间的对应关系。如果这个文件名后面跟@mymodule,则Dolibarr将在模块自己的 lang 目录中查找翻译文件,即htdocs/mymodule/langs/code_CODE/mymodule.lang,否则Dolibarr将查找翻译文件htdocs/langs/code_CODE/mylangfile.lang。
  • 第5部分:判断选项卡是否可见的条件。设置为“1”,会使其始终可见。
  • 第6部分:单击选项卡时要显示的页面的URL。字符串“__ID__”将自动替换为相关元素的ID。

要使声明生效,必须禁用并重新激活该模块。

要使用来自数据库的数据填充选项卡的内容,请参阅下一章。

为了获取现有选项卡的名称“tabname”,您必须检查文件“core/lib/module.lib.php”的函数“product_prepare_head”中使用的名称,该文件对应于“$head[$h][2]”部分。

将对象的标准选项卡添加到其自己的表单页面

何时:在您自己的表单中添加标准对象(产品、合作方…)的选项卡

必须遵循以下步骤:

1. 在代码中包含定义相关函数的文件

对于每种对象选项卡,都有两个文件需要使用下列语句:

require_once($url_fichier) ;

以下是要包含的文件示例(DOL_DOCUMENT_ROOT 对应于 dolibarr/htdocs/ 文件夹):

  • 合作方(thirdparty) :
    • DOL_DOCUMENT_ROOT/societe/class/societe.class.php
    • DOL_DOCUMENT_ROOT/core/lib/company.lib.php
  • 产品(product) :
    • DOL_DOCUMENT_ROOT/product/class/product.class.php
    • DOL_DOCUMENT_ROOT/core/lib/product.lib.php
  • 发票 (invoice) :
    • DOL_DOCUMENT_ROOT/compta/facture/class/facture.class.php
    • DOL_DOCUMENT_ROOT/core/lib/invoice.lib.php

...

2. 创建并加载要在自己的表单中显示的标准对象

创建所需类的对象,并从数据库中获取对象数据。为此,您必须使用相应类的fetch()函数,并将从URL(/mytab.php?id=1)获取的对象id作为参数传递。

例如:

$id=GETPOST('id','int');
$ref=GETPOST('ref','alpha');
$product = new Product($db) ;
$result = $product->fetch($id,$ref) ; // Test $result to check the database read is ok

3. 检索与所选实体对应的选项卡列表

使用函数XXX_prepare_head($obj)(其中XXX是实体名称)创建一个包含要显示的选项卡定义的数组。要传递的参数是要显示选项卡的对象。

返回的数组由以下内容组成:

$head        // Array of tabs
$head[$h]    // Element to describe one tab.
$head[$h][0] // Url of page to show when you click on tab
$head[$h][1] // Title of tab
$head[$h][2] // Code name to identify the tab

示例:

$head = product_prepare_head($product, $user) ; // 参数 $user 是要显示选项卡的对象,这就是"产品"对象为"user"对象准备的所有可用的选项卡的数组。

某些函数上不存在$user参数

4. 在您的页面上显示选项卡

使用dol_fiche_head()函数,该函数显示由 XX_prepare_head() 返回的 $head 数组中包含的选项卡。

dol_fiche_head($links, $active='0', $title='', $notab=0, $picto='')
//$links  // Tableau des onglets, appelé $head plus haut.
//$active // Onglet actif (mettre le nom de l'onglet défini dans votre fichier de module, ou un nom contenu dans $head[$h][2]). Cet onglet sera mis en surbrillance
//$title  // Title to show (shown into a special tab that you can't click)
//$notab
//$picto  // Name of image to show into title. Possible values are:
//            product
//            service
//            company

此函数将显示所需的选项卡,并打开一个html元素< div class="" >,该元素对应于选项卡下方的显示区域(如果参数 $notab=0)。要关闭显示区域,只需在PHP代码中使用关闭元素</div>。

注:相关的更多细节,请参阅 Doxygen文档 或直接参考 Dolibarr 代码。

创建或修改 PHP 屏显 (可选)

何时: 如果模块的目的是为了修改屏显。

创建新 PHP 屏显

然后,您必须创建PHP页面,使用目录modulebuilder/myobject_page.php中提供的框架模板来显示/编辑表中的数据。 (从命令行开发脚本, 见 Script development).

要创建一个新的用户屏幕,创建一个模块名的子目录(若无) (例如 htdocs/mymodule) 来存储创建的屏幕。

复制 myobject_page.php 做为屏幕页。

编辑该文件,以包括正确的 main.inc.php文件。

// Load Dolibarr environment
$res=0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include($_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php");
// Try main.inc.php into web root detected using web root caluclated from SCRIPT_FILENAME
$tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1;
while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; }
if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include(substr($tmp, 0, ($i+1))."/main.inc.php");
if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php");
// Try main.inc.php using relative path
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 && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php");
if (! $res) die("Include of main fails");

注: 你可能增加更多的 "../" 根据你的模块目录的深度。 在3.2之后,为所有版本的Dolibarr开发的模块应该能够在不同的文件夹中移动,而不是“htdocs”,比如“htdocs/custom”,而不必修改模块的源代码,这就是为什么我们要做几个包含,所以这个规则必须被所有模块应用。

在main.inc.php文件中,加载了技术环境变量和权限。以下变量是位于该文件中的对象:

  • $user 包含用户+ 权限。
  • $conf 包含 Dolibarr 设置
  • $db 它包含一个打开的数据库连接处理程序。
  • $langs 包含用户语言

然后输入代码显示页面。

  • 包含模块专属的类或库,使用Dolibarr函数dol_include_once()(而不是直接使用 include_once):

例如 :

dol_include_once('/mymodule/class/myclass.class.php', 'MyClass');
  • 若要包含Dolibarr提供的类,请使用以下语法:

例如 :

require_once DOL_DOCUMENT_ROOT.'/core/class/doli.class.php';

为现存表单增加新字段

您可能需要提供一个模块,该模块将更多字段添加到一些元素的表单(输入和查看)中。

一个较累的解决方案(但不是很糟糕)是替换所有用于创建元素的页面(这意味着禁用“新元素”菜单条目并添加您的,禁用“显示”元素,用一个选项卡替换该元素,该选项卡是您自己的完整页面(从原始复制/粘贴),以完成与原始页面相同的操作,修改以添加字段并将添加的数据存储到自己的表中)。

转到菜单 #Define your entries in menu (optional)和#The tab management (optional),如果这个解决方案适合你(这个解决方案是更强大的,因为你可以改变你想要的一切进入页面)。

我们将在这里描述另一种解决方案,它只在现有字段结束时“添加字段”,使用元素“category”作为示例,但您可以将此教程转换为发票、提案…

  • 首先,添加一个由您的模块所拥有的表,以存储新字段的值。此表有一个名为“rowid”的列(将包含与元素表的字段rowid相同的值+每一个要添加的新字段的列。然后为这个新表创建一个CRUD(创建/读取/更新/删除)方法类。对于这两个任务,请回到前一章#创建数据表和表的PHP类文件 (可选)。
  • 下一步是在模块中添加一个钩子,以将新字段添加到窗体中。有关使用钩子的通用文档,请参见Hooks_system#Implement_the_Hook章节。

如果您遵循本教程,以便能够将字段添加到类别窗体中,您必须这样做:

  Page waiting to complete. To complete, create an account, go back and clic on "Modify".
  Page en attente d'être complété. Pour compléter, créez un compte, revenez et cliquez sur "Modifier".
  Página a completar. Para completarla, cree una cuenta, vuelva a la página y haga clic en "editar"
  Seite wartet auf Vervollständigung. Um zu helfen, erstelle ein Konto, gehe zurück und klicke auf "Bearbeiten".
  待完成,欲帮助完成,注册帐号,点击“编辑"
  ページは未完成の状態です。完成させるにはアカウントを作成し、ページに戻って「編集」をクリックして下さい。

增/换部分字段(需钩子支持)

要知道如何使用Dolibarr现有的钩子来添加/替换Dolibarr 钩子代码,参考 钩子系统#实现钩子 。

数据通道

如果需要在自己的表中编辑数据库中的一些数据,请使用的PHP类。

如果您要访问没有专用PHP类的表(例如,如果您想获得具有特定连接或筛选器的记录列表)。在这种情况下,这是要遵循的代码示例:

进行插入、更新或删除:

$db->begin();   // Start transaction
$db->query("My SQL request insert, update or delete");
$db->commit();       // Validate transaction
or $db->rollback()  // Cancel transaction

读:

$resql=$db->query("My select request");
if ($resql)
{
	$num = $db->num_rows($resql);
	$i = 0;
	if ($num)
	{
		while ($i < $num)
		{
			$obj = $db->fetch_object($resql);
			if ($obj)
			{
				// You can use here results
				print $obj->field1;
				print $obj->field2;
			}
			$i++;
		}
	}
}

页面风格

为了让你自己的页面看起来与Dolibarr主题相匹配,必须使用Dolibarr CSS样式。

例如,您可以使用:

  • class="liste_titre" on tags tr and td for the head row of a table.
  • class="pair" or class="impair" on tags tr and td of other rows of a table.
  • class="flat" on all input fields (input, select, textarea...).
  • class="button" on HTML fields with type input type="submit".

使用Dolibarr日期选择器

如果您的页面使用Dolibarr日期选择器(日历弹出),请使用以下行:

 $form=new Form($db);
 $form->select_date('','mykey',0,0,0,"myform");

字符串mykey将标识表单中的日期选择器。如果在同一页中使用多个日期选择器,则必须使用不同的值。

字符串myform是包含选择器的表单的名称(在HTML页面中的form name="myform")中找到的值。

这意味着日期选择器必须包含在HTML表单中。

要获得表单POST的值,命令是:

$mydate = dol_mktime(12, 0 , 0, $_POST['mykeymonth'], $_POST['mykeyday'], $_POST['mykeyyear']);
print strftime('%A %d %B %Y', $mydate);

新增设置页 (可选)

何时: 如果模块需要用户提供的参数,以正确设置模块。

创建页面以编辑参数

如果模块需要设置几个参数,则必须创建一个页面来编辑选项。 (这些制作存储于 表 llx_const).

新建 mymodule_setuppage.php 这显示了可能的选项的表单,将它们更新到上面提到的表中。

有必要从目录/admin 的页面中获取一个示例,它将向您展示读取/保存参数的方法。

把这个PHP页面放到你的模块的/admin (例如mymodule/admin/mymodule_setuppage.php)的目录中。

修改模块主文件

然后,在模块主文件中,将变量“config_page_url”修改为PHP页面的设置名称。

如果页面位于目录admin/中,则不需要路径,例如:

$this->config_page_url = array("mymodule_setuppage.php");

如果页面在目录 mymodule/admin/中,则像这样:

$this->config_page_url = array("mymodule_setuppage.php@mymodule");

测试页面

进入首页->安装>模块,您应该在您的模块行的末尾看到一个图片,以转到您的设置页面。点击它,你就可以从页面中查看/编辑参数。

增加菜单条目 (可选)

何时: 如果您创建了PHP页面,则必须从Dolibarr菜单中的菜单条目中访问这些页面。

增加菜单条目

For this, you must define into the module descriptor, the array this->menu that declare all menus added by your module. This array contains entries that will appear once your module is activated. The example module descriptor file modMyModule.class.php contains an example to declare a top menu and also its left menu entries.

This is the example of code to declare your own menu entries in a module descriptor file:

// Main menu entries
$this->menu = array();			// List of menus to add
$r=0;

// Add here entries to declare new menus
// Example to declare the Top Menu entry:
$this->menu[$r]=array(	'fk_menu'=>0,			// Put 0 if this is a top menu
			'type'=>'top',			// This is a Top menu entry
			'titre'=>'MyModule top menu',
			'mainmenu'=>'mymodule',
			'leftmenu'=>'mymodule',
			'url'=>'/mymodule/pagetop.php',
			'langs'=>'mylangfile',	// Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
			'position'=>100,
			'enabled'=>'1',			// Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
			'perms'=>'1',			// Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
			'target'=>'',
			'user'=>2);				// 0=Menu for internal users, 1=external users, 2=both
$r++;

// Example to declare a Left Menu entry:
$this->menu[$r]=array(	'fk_menu'=>'fk_mainmenu=xxx',		    // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode of parent menu
			'type'=>'left',			        // This is a Left menu entry
			'titre'=>'MyModule left menu 1',
			'mainmenu'=>'xxx',
                        'leftmenu'=>'yyy',
			'url'=>'/mymodule/pagelevel1.php',
			'langs'=>'mylangfile',	// Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
			'position'=>100,
			'enabled'=>'1',			// Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
			'perms'=>'1',			// Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
			'target'=>'',
			'user'=>2);				// 0=Menu for internal users,1=external users, 2=both
$r++;

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.

测试菜单

Disable and reenable your module under Dolibarr module setup page, menus entries must appear (if property 'enabled' is declaration array is ok).

新增权限 (可选)

When: If you want to add new permissions.

The way to define permissions that will manage your module is done inside the module descriptor created previously. Modify line

$this->rights_class = '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.

$this->rights[$r][0] = 10001;
$this->rights[$r][1] = 'Label by default of permission';
$this->rights[$r][3] = 1;
$this->rights[$r][4] = 'action';
$this->rights[$r][5] = 'subaction';
$r++;

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][3], put 1 if this permission must be granted automatically by default to any new created user. Into $this->rights[$r][1], put a label by default for permission (This label will be used if no translation can be found into the file admin.lang). 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:

if ($user->rights->mymodule->action->subaction) ...

信息窗 (可选)

When: If your module need to provide one or several new boxes to show on home page.

信息窗

For this, modify the array $this->boxes into the module descriptor file. All you have to do is to add 2 lines for each box file you will create into directory htdocs/mymodule/core/boxes

Example:

$this->boxes[0]['file']='mybox0.php@mymodule'
$this->boxes[0]['note']='My box 0'
 ...
$this->boxes[n]['file']='myboxn.php@mymodule'
$this->boxes[n]['note']='My box n'

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).

Test if your box is detected by Dolibarr

Disable and enabled module.

Go into menu Home - Setup - Boxes.

Your boxes must appear in the list of boxes you can activated. Activate the box and go on home page to see if the box is showing correctly.

Define your own export (optional)

When: If your module provide new predefined database export profiles (for its own tables or for already existing tables of another module).

导出

For this uncomment and modify arrays $this->export_xxx in your module descriptor file.

Test your export

Go into menu Tools -> Export. Your export will appear in the list of available predefined exports (if your module is enabled). Choose it, you must see list of all possible fields defined in the export arrays. Choose on field and try to build an export file. If ok, try again with all fields.

Define your CSS styles (optional)

When: If in your PHP pages, you use class styles that are not defined inside Dolibarr themes CSS (not recommanded).

Create and declare your style sheet

Create a style sheet CSS named mymodule.css or mymodule.css.php and put it into directory htdocs/mymodule. You can have only one CSS file per module. Remember it's better to use existing styles already available by default into Dolibarr (The CSS file used by Dolibarr is file themes/mytheme/themename.css.php. Create your own CSS file only if you need absolutely to add not already existing styles.

Once your style sheet is ready, declare it into your module descriptor file by modifying the property $this->modules_parts. Value to put here must be a relative URL to your CSS file. For example

$this->module_parts = array('css' => array('/mymodule/css/mymodule.css.php'));

Test your style sheet

Disable and enable your module.

Go on home page (index.php). Show the HTML source of the page.

You should see into the HTML header, a line that declare your style sheet.

增加JS函数功能 (可选)

When: If in your PHP pages, you use javascript functions not available in Dolibarr (in file lib_head.js)

If you need to use your own javascript functions inside your PHP pages, it is necessary to have your functions, defined into your javascript file htdocs/mymodule/js/mymodule.js, included into the HTML HEAD section. To ask Dolibarr that forge this header to include your own javascript file, you must just provide it to the llxHeader() function called by your page.

Example for page /htdocs/mymodule/mypage.php :

$morejs=array("/mymodule/js/mymodule.js");
llxHeader('','Titre','','','','',$morejs,'',0,0);

执行Dolibarr事件代码 (可选)

When: If you want to execute your own code once a common Dolibarr event has occurred (example: I want to update a table in my module when an invoice is created into Dolibarr), you must create a triggers.

See also Interfaces_Dolibarr_toward_foreign_systems and Interfaces_from_foreign_systems_toward_Dolibarr

钩子代码 (可选)

When: When you want to add code or replace Dolibarr code into another situation than a business event (See previous chapter for adding code during a business event).

See page Hooks_system.


添加编码规则

When: When you need a new rule for generated ref of elements that is not covered by existing rules.

See page Create numbering module.

创建文档模板 (optional)

When: When you want to personalized your own generated PDF or ODT documents.

Note: To add this feature you don't need to create a module descriptor.

Documentation to add your own template is available on page Create_a_PDF_document_template or Create_an_ODT_document_template.

创建主题 (optional)

When: When you want to personalized your colors/fonts/images.

Note: To add this feature you don't need to create a module descriptor.

To add your own skin, read page Skins.

修改开发模块作者名

在myModule.class.php中查找editor_name

修改以下变量的值以修改作者名称

$this->editor_name = 'Roger@QQ:12464313';
$this->editor_url = 'https://www.ivnun.cn';

一些编码规则和预定义函数

The coding rules to be followed are defined in the Developer documentation, under section "General Information - Language and standards development".

A lot of predefined features for developers are also available and described into page Developer documentation under section "Technical components of Dolibarr".

创建一个安装包来分发和安装您的模块

This process must be used to generate a package to submit it on the http://www.dolistore.com market place. But you can use it to have a package easy to distribute on your on network.

  • Go into the directory /build and copy the file makepack-dolibarrmodules.conf into makepack-mymodule.conf. Warning, this directory may not be provided in some packages of stable versions. If so, it can be retrieved from the snapshot available for download on the Dolibarr website in area "Development version" (taken in this case the whole build directory which is autonomous and independent directory).

Edit this file to add list of file names for all new files you created for your module (module descriptor, new sql tables files, php page, images, etc...)

  • Run the script via Perl (need the Perl version 5.0 or later):
perl makepack-dolibarrmodule.pl

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:
tar -xvf mymodule.zip
  • If you want your module to be public, you can submit it (the zip file) on Dolibarr market place web site: DoliStore.com (you must create an account first and be logged to use the link "Submit a module/product").
    • If your module was build correctly, file will be validated later.
    • If quality is enough and license permits it, code might be added inside main Dolibarr sources (except if you disagree for that).

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

See Validation Rules