3
edits
Tag: 2017 source edit |
m (→Create a Module descriptor for your Module (required): Path to template) |
||
| Line 34: | Line 34: | ||
From Dolibarr 9.0 it is possible to create the main pages of your module with the Module Builder. To activate it: | From Dolibarr 9.0 it is possible to create the main pages of your module with the Module Builder. To activate it: | ||
* Go into the list of module and enable the module "Module Builder", | *Go into the list of module and enable the module "Module Builder", | ||
[[File:Mod_builder_2.png|400px]] | [[File:Mod_builder_2.png|400px]] | ||
* Then click on the "bug" image in the top right of screen. | *Then click on the "bug" image in the top right of screen. | ||
[[File:Mod_builder_3.png|400px]] | [[File:Mod_builder_3.png|400px]] | ||
| Line 54: | Line 54: | ||
The first step is to create a file descriptor for the module. | The first step is to create a file descriptor for the module. | ||
*Create directory '''/htdocs/''mymodule''/core/modules'''. Then copy the file modMyModule.class.php from the directory | *Create directory '''/htdocs/''mymodule''/core/modules'''. Then copy the file modMyModule.class.php from the directory htdocs/modulebuilder/template into this directory '''htdocs/''mymodule''/core/modules'''. Change the name of the file modMyModule.class.php to the name corresponding to the purpose of your module. Name of the file must start with initial 'mod' and end with .class.php (for example if you want to give the name 'NewName' to this file, then the name should be 'modNewName.class.php'). | ||
Next modify the contents of this file as follows: | Next modify the contents of this file as follows: | ||
| Line 85: | Line 85: | ||
Then check into your descriptor file, into function '''init''' that the line | Then check into your descriptor file, into function '''init''' that the line | ||
< | <syntaxhighlight lang="PHP">$this->_load_tables('/mymodule/sql/');</syntaxhighlight> | ||
is not commented. | is not commented. | ||
| Line 97: | Line 97: | ||
Example of content of a file data.sql | Example of content of a file data.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> | ||
Files must be operational for the database mysql. Rem: The files of other databases are not maintained. They will be read and converted on the fly by the driver of the other database. | Files must be operational for the database mysql. Rem: The files of other databases are not maintained. They will be read and converted on the fly by the driver of the other database. | ||
| Line 122: | Line 122: | ||
To do this go into the file descriptor of module previously created and edit the $this->tabs array: | To do this go into the file descriptor of module previously created and edit the $this->tabs array: | ||
< | <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> | ||
The table should contain a list of strings, each string representing a new tab. | The table should contain a list of strings, each string representing a new tab. | ||
| Line 153: | Line 153: | ||
For each object type, there are two files to include with line | For each object type, there are two files to include with line | ||
< | <syntaxhighlight lang="PHP">require_once($url_fichier) ;</syntaxhighlight> | ||
This is example of files to include (DOL_DOCUMENT_ROOT is often dolibarr/htdocs/) : | This is example of files to include (DOL_DOCUMENT_ROOT is often dolibarr/htdocs/) : | ||
| Line 174: | Line 174: | ||
''Example :'' | ''Example :'' | ||
< | <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> | ||
'''3. Get a list of all tabs to show for your object type''' | '''3. Get a list of all tabs to show for your object type''' | ||
| Line 186: | Line 186: | ||
The resulting array has the following structure | The resulting array has the following structure | ||
< | <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 192: | Line 192: | ||
$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> | ||
''Example :'' | ''Example :'' | ||
< | <syntaxhighlight lang="PHP"> | ||
$head = product_prepare_head($product, $user) ; // parameter $user is present for some function anly | $head = product_prepare_head($product, $user) ; // parameter $user is present for some function anly | ||
</ | </syntaxhighlight> | ||
'''4. show tabs into your page''' | '''4. show tabs into your page''' | ||
| Line 203: | Line 203: | ||
Use function dol_fiche_head() to show all tabs defined into array $head returned by XX_prepare_head(). | Use function dol_fiche_head() to show all tabs defined into array $head returned by XX_prepare_head(). | ||
< | <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 213: | Line 213: | ||
// service | // service | ||
// company | // company | ||
</ | </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 227: | Line 227: | ||
Copy, into that directory, the file '''myobject_page.phpp''' that will serve as a starting point for your page file. | Copy, into that directory, the file '''myobject_page.phpp''' that will serve as a starting point for your page file. | ||
Edit the file to have a correct relative path to the main.inc.php file. | Edit the file to have a correct relative path to the main.inc.php file. | ||
< | <syntaxhighlight lang="PHP"> | ||
// Load Dolibarr environment | // Load Dolibarr environment | ||
$res=0; | $res=0; | ||
| Line 242: | Line 242: | ||
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> | ||
As you can see, there are several tries to load the main.inc.php (or master.inc.php). The goal is to succeed in the most cases as possible. The minimum is 2 lines: one to try to load the master/main.inc.php into the dolibarr root directory and another one to try to load the file to support the case when the module is deployed into the "custom" directory. But you can have more to deal more situation. The provided example should be able to load the file main/master.inc.php in nearly all situation/setups. | As you can see, there are several tries to load the main.inc.php (or master.inc.php). The goal is to succeed in the most cases as possible. The minimum is 2 lines: one to try to load the master/main.inc.php into the dolibarr root directory and another one to try to load the file to support the case when the module is deployed into the "custom" directory. But you can have more to deal more situation. The provided example should be able to load the file main/master.inc.php in nearly all situation/setups. | ||
| Line 259: | Line 259: | ||
''Example :'' | ''Example :'' | ||
< | <syntaxhighlight lang="PHP"> | ||
dol_include_once('/mymodule/class/myclass.class.php', 'MyClass'); | dol_include_once('/mymodule/class/myclass.class.php', 'MyClass'); | ||
</ | </syntaxhighlight> | ||
*To include classes provided with Dolibarr, use the following syntax: | *To include classes provided with Dolibarr, use the following syntax: | ||
''Example :'' | ''Example :'' | ||
< | <syntaxhighlight lang="PHP"> | ||
require_once DOL_DOCUMENT_ROOT.'/core/class/doli.class.php'; | require_once DOL_DOCUMENT_ROOT.'/core/class/doli.class.php'; | ||
</ | </syntaxhighlight> | ||
===Add some fields into existing forms=== | ===Add some fields into existing forms=== | ||
| Line 293: | Line 293: | ||
To make an insert, update or delete: | To make an insert, update or delete: | ||
< | <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> | ||
To read: | To read: | ||
< | <syntaxhighlight lang="PHP"> | ||
$resql=$db->query("My select request"); | $resql=$db->query("My select request"); | ||
if ($resql) | if ($resql) | ||
| Line 323: | Line 323: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
===Define style of your pages=== | ===Define style of your pages=== | ||
| Line 337: | Line 337: | ||
===Use the Dolibarr date picker=== | ===Use the Dolibarr date picker=== | ||
If you want to use the Dolibarr date selector (with its calendar popup) into your pages, use the following line: | If you want to use the Dolibarr date selector (with its calendar popup) into your pages, use the following line: | ||
< | <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> | ||
The string mykey will identify the date selector in the form. You must use different values if you use several date selectors in same page. | The string mykey will identify the date selector in the form. You must use different values if you use several date selectors in same page. | ||
The string myform is the name of the FORM in which the selector is included (value found in the form name="myform" in HTML page). | The string myform is the name of the FORM in which the selector is included (value found in the form name="myform" in HTML page). | ||
| Line 346: | Line 346: | ||
To get value after the POST of your form, the command is: | To get value after the POST of your form, the command is: | ||
< | <syntaxhighlight lang="PHP"> | ||
$mydate = dol_mktime(12, 0 , 0, $GETPOST('mykeymonth', 'int'), GETPOST('mykeyday', 'int'), GETPOST('mykeyyear', 'int')); | $mydate = dol_mktime(12, 0 , 0, $GETPOST('mykeymonth', 'int'), GETPOST('mykeyday', 'int'), GETPOST('mykeyyear', 'int')); | ||
print strftime('%A %d %B %Y', $mydate); | print strftime('%A %d %B %Y', $mydate); | ||
</ | </syntaxhighlight> | ||
==Add your own setup page (optional)== | ==Add your own setup page (optional)== | ||
| Line 363: | Line 363: | ||
Then, within the descriptor file of your module, modify the variable '''config_page_url''' to set the name of this PHP page. | Then, within the descriptor file of your module, modify the variable '''config_page_url''' to set the name of this PHP page. | ||
If the page is in the directory '''admin/''', then the path is not required, e.g. like this: | If the page is in the directory '''admin/''', then the path is not required, e.g. like this: | ||
< | <syntaxhighlight lang="PHP"> | ||
$this->config_page_url = array("mymodule_setuppage.php"); | $this->config_page_url = array("mymodule_setuppage.php"); | ||
</ | </syntaxhighlight> | ||
If the page is in the directory '''mymodule/admin/''', then like this: | If the page is in the directory '''mymodule/admin/''', then like this: | ||
< | <syntaxhighlight lang="PHP"> | ||
$this->config_page_url = array("mymodule_setuppage.php@mymodule"); | $this->config_page_url = array("mymodule_setuppage.php@mymodule"); | ||
</ | </syntaxhighlight> | ||
===Test your page=== | ===Test your page=== | ||
| Line 383: | Line 383: | ||
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"> | ||
// Main menu entries | // Main menu entries | ||
$this->menu = array(); // List of menus to add | $this->menu = array(); // List of menus to add | ||
| Line 418: | Line 418: | ||
'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> | ||
To show the menu or not depending on a permission, modify property '''perms''' in the array. See chapter on permissions later to see how to add permissions. | To show the menu or not depending on a permission, modify property '''perms''' in the array. See chapter on permissions later to see how to add permissions. | ||
| Line 430: | Line 430: | ||
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"> | ||
$this->rights_class = 'mymodule' | $this->rights_class = 'mymodule' | ||
</ | </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"> | ||
$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 445: | Line 445: | ||
$this->rights[$r][5] = 'subaction'; | $this->rights[$r][5] = 'subaction'; | ||
$r++; | $r++; | ||
</ | </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 453: | Line 453: | ||
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"> | ||
if ($user->rights->mymodule->action->subaction) ... | if ($user->rights->mymodule->action->subaction) ... | ||
</ | </syntaxhighlight> | ||
==Define your own box (optional)== | ==Define your own box (optional)== | ||
| Line 465: | Line 465: | ||
''Example:'' | ''Example:'' | ||
< | <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 471: | Line 471: | ||
$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> | ||
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 503: | Line 503: | ||
Value to put here must be an array of relative URLs to your CSS files. | Value to put here must be an array of relative URLs to your CSS files. | ||
For example | For example | ||
< | <syntaxhighlight lang="PHP"> | ||
$this->module_parts = array('css' => array('/mymodule/css/mymodule.css.php','/mymodule/css/myother.css')); | $this->module_parts = array('css' => array('/mymodule/css/mymodule.css.php','/mymodule/css/myother.css')); | ||
</ | </syntaxhighlight> | ||
===Test your style sheet=== | ===Test your style sheet=== | ||
| Line 521: | Line 521: | ||
Example for page /htdocs/mymodule/mypage.php : | Example for page /htdocs/mymodule/mypage.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> | ||
==Run your own code on any Dolibarr event (optional)== | ==Run your own code on any Dolibarr event (optional)== | ||
| Line 572: | Line 572: | ||
*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"> | ||
perl makepack-dolibarrmodule.pl | perl makepack-dolibarrmodule.pl | ||
</ | </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"> | ||
tar -xvf mymodule.zip | tar -xvf mymodule.zip | ||
</ | </syntaxhighlight> | ||
*If you want your module to be public, you can submit it (the zip file) on Dolibarr marketplace website: {{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 marketplace website: {{LinkToPluginDownload}} (you must create an account first and be logged to use the link "Submit a module/product"). | ||
edits