模塊開發
要創建新模塊,需要執行以下幾個步驟。本教程旨在向您介紹每一個步驟,以便添加一個模塊來擴展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類文件,簡單至在下圖中選擇'對象'選項卡,輸入'對象名',然後點擊創建,即可生成相關一切文檔。
選項卡顯示(可選)
添加或刪除對象表單上的選項卡
何時:在標準對象(發票、訂單、商業提案、成員…)的表單中添加您自己的選項卡
要執行此操作,請打開之前創建的模塊描述符文件並編輯 $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. 創建並加載對象以顯示在您的選項卡中
通過從url(ie : /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"对象准备的所有可用的选项卡的数组。
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
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.
創建或修改 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章節。
如果您遵循本教程,以便能夠將欄位添加到類別窗體中,您必須這樣做:
增/換部分欄位(需鉤子支持)
要知道如何使用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