Module development: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 12: Line 12:
* Define new permissions
* Define new permissions
* Execute code automatically triggered by a particular Dolibarr action
* Execute code automatically triggered by a particular Dolibarr action
* Add your numbering module
* Add a new document template
etc...
etc...
All those operations are possible with version 2.4 or more of Dolibarr.
All those operations are possible with version 2.4 or more of Dolibarr.
Line 17: Line 19:
Following chapters presents how to do all of this manually with an easy way. For experienced developers, a method to do this using MDA generation is also on the work. See last chapter for this.
Following chapters presents how to do all of this manually with an easy way. For experienced developers, a method to do this using MDA generation is also on the work. See last chapter for this.


= [[File:Art.png]] Create a descriptor of Module (required) =
= [[File:Art.png]] Create your module =
== Create a descriptor of Module (required) ==
'''When''': Required as soon as an addon is developed, whatever is its goal.
'''When''': Required as soon as an addon is developed, whatever is its goal.


== Create your module descriptor ==
=== Create your module descriptor ===
The first step is to create a file descriptor for the module. To do this go into the directory dev/skeletons and copy the file modMyModule.class.php in the directory htdocs/includes/modules.
The first step is to create a file descriptor for the module. To do this go into the directory dev/skeletons and copy the file modMyModule.class.php in the directory htdocs/includes/modules.
Then change the contents of this file to replace:
Then change the contents of this file to replace:
Line 30: Line 33:
Your file descriptor for your module is ready.
Your file descriptor for your module is ready.


== Test your descriptor ==
=== Test your descriptor ===


Launch Dolibarr and go to page '''Setup->module'''? You must see a new line with your new module and the possibility to activate it or not (browse all the tabs of each category of modules to find it). It's value of $this->special that determines in which tab will be your module.
Launch Dolibarr and go to page '''Setup->module'''? You must see a new line with your new module and the possibility to activate it or not (browse all the tabs of each category of modules to find it). It's value of $this->special that determines in which tab will be your module.


= [[File:Art.png]] Tree of path for new module files (required) =
== Tree of path for new module files (required) ==
Create a directory 'mymodule' that will receive all files of your module. This directory must be inside the '''htdocs''' directory and will contains following subdirectories (only first line is mandatory):
Create a directory 'mymodule' that will receive all files of your module. This directory must be inside the '''htdocs''' directory and will contains following subdirectories (only first line is mandatory):


Line 48: Line 51:
* htdocs/mymodule/themes/mytheme if module provide its own skin
* htdocs/mymodule/themes/mytheme if module provide its own skin


= [[File:Art.png]] Create your SQL tables and the PHP DAO class (optional) =
== Create your SQL tables and the PHP DAO class (optional) ==
'''When''': If your module needs to manage data of its own
'''When''': If your module needs to manage data of its own


== Create your .sql file ==
=== Create your .sql file ===
If your module is designed to manage data of its own, which are not available in the standard version of Dolibarr, it is necessary to define SQL tables to store the data.
If your module is designed to manage data of its own, which are not available in the standard version of Dolibarr, it is necessary to define SQL tables to store the data.


Line 75: Line 78:
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.


== Test your .sql files ==
=== Test your .sql files ===


Once the files ready, you can return to Dolibarr to disable the module, dropper tables in database and reactivate the module. The tables should be recreated by the activation of the module. If this is not the case, check your scripts by passing them by hand, or check the Dolibarr logs.
Once the files ready, you can return to Dolibarr to disable the module, dropper tables in database and reactivate the module. The tables should be recreated by the activation of the module. If this is not the case, check your scripts by passing them by hand, or check the Dolibarr logs.


== Generate the PHP DAO class ==
=== Generate the PHP DAO class ===
Once you have created your tables into the database, move to the dev/skeletons directory and run the script
Once you have created your tables into the database, move to the dev/skeletons directory and run the script


Line 91: Line 94:
A file '''out.tablename_script.php''' has been generated and contains a sample code to use the class for each of the 4 CRUD methods.
A file '''out.tablename_script.php''' has been generated and contains a sample code to use the class for each of the 4 CRUD methods.


= [[File:Art.png]] The tab management =
== The tab management ==


== Add or removed your own tabs on object sheets (optional) ==
=== Add or removed your own tabs on object sheets (optional) ===
'''When''': To add a tab on an object (invoices, orders, proposals, member ...)
'''When''': To add a tab on an object (invoices, orders, proposals, member ...)


Line 130: Line 133:
To feed the contents of the tab with data from the database, see the next chapter.
To feed the contents of the tab with data from the database, see the next chapter.


== Show tabs navigation into your own pages ==
=== Show tabs navigation into your own pages ===
'''When''': To show all standard tabs of an object (produit, tiers, etc.) on your page.
'''When''': To show all standard tabs of an object (produit, tiers, etc.) on your page.


Line 198: Line 201:
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.


= [[File:Art.png]] Create your own PHP screens (optional) =
== Create your own PHP screens (optional) ==
'''When''': If the purpose of your module is to add features that require new screens.
'''When''': If the purpose of your module is to add features that require new screens.


== Create a PHP screen ==
=== Create a PHP screen ===
You must then create your PHP pages that will show/edit data from tables using the skeleton templates provided as an example in the directory '''dev/skeletons''' (For the development of a script from the command line, see [[Script development]]).
You must then create your PHP pages that will show/edit data from tables using the skeleton templates provided as an example in the directory '''dev/skeletons''' (For the development of a script from the command line, see [[Script development]]).


Line 221: Line 224:
Then enter your code to display the page.
Then enter your code to display the page.


== Database access ==
=== Database access ===
If you need to edit some data in database inside you own table, use the PHP class generated before.
If you need to edit some data in database inside you own table, use the PHP class generated before.


Line 260: Line 263:
</source>
</source>


== Define style of your pages ==
=== Define style of your pages ===
To have the look of your own pages to match the Dolibarr theme, it is necessary to use the Dolibarr CSS styles.
To have the look of your own pages to match the Dolibarr theme, it is necessary to use the Dolibarr CSS styles.


Line 270: Line 273:
* class="'''button'''" on HTML fields with type ''input type="submit"''.
* class="'''button'''" on HTML fields with type ''input type="submit"''.


== 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:
<source lang="php">
<source lang="php">
Line 286: Line 289:
</source>
</source>


= [[File:Art.png]] Add your own setup page (optional) =
== Add your own setup page (optional) ==
'''When''': If your module need to ask user several parameters.
'''When''': If your module need to ask user several parameters.


== Create your page to edit parameters ==
=== Create your page to edit parameters ===
If your module need several parameters to be setup, you must create a page to edit options (that will be saved into [[Table llx_const|table '''llx_const''']]).
If your module need several parameters to be setup, you must create a page to edit options (that will be saved into [[Table llx_const|table '''llx_const''']]).
Create a page named '''mymodule_setuppage.php''' that show possibles options and update them into table from a form.
Create a page named '''mymodule_setuppage.php''' that show possibles options and update them into table from a form.
Line 299: Line 302:
</source>
</source>


== Test your page ==
=== Test your page ===
Go into page '''Home->Setup->Modules''', you should see a picture at the end of the line of your module to reach your setup page. Click on it, you should be able to view/edit parameters from your page.
Go into page '''Home->Setup->Modules''', you should see a picture at the end of the line of your module to reach your setup page. Click on it, you should be able to view/edit parameters from your page.


= [[File:Art.png]] Define your entries in menu (optional) =
== Define your entries in menu (optional) ==
'''When''': If you have created PHP pages, it is necessary that those pages can be reached from menu entries in Dolibarr menu.
'''When''': If you have created PHP pages, it is necessary that those pages can be reached from menu entries in Dolibarr menu.


== Define your menu entries ==
=== Define your menu entries ===
For this, you must define into the module descriptor, the array this->menu that declare all menus added by your module.
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.
This array contains entries that will appear once your module is activated.
Line 349: Line 352:
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.


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


= [[File:Art.png]] Add your own permissions (optional) =
== Add your own permissions (optional) ==
'''When''': If you want to add new permissions.
'''When''': If you want to add new permissions.


Line 382: Line 385:
</source>
</source>


= [[File:Art.png]] Define you own box (optional) =
== Define you own box (optional) ==
'''When''': If your module need to provide one or several new boxes to show on home page.
'''When''': If your module need to provide one or several new boxes to show on home page.


== Define your box ==
=== Define your box ===
For this, modify the array $this->boxes into the module descriptor file.
For this, modify the array $this->boxes into the module descriptor file.
All you have to do is to add one line for each box file you will create into directory '''htdocs/includes/boxes'''
All you have to do is to add one line for each box file you will create into directory '''htdocs/includes/boxes'''
Line 400: Line 403:
Then create files mybox0.php, mybox1.php... by copying an example from an existing box file (into directory htdocs/include/boxes).
Then create files mybox0.php, mybox1.php... by copying an example from an existing box file (into directory htdocs/include/boxes).


== Test if your box is detected by Dolibarr ==
=== Test if your box is detected by Dolibarr ===
Disable and enabled module.
Disable and enabled module.


Line 407: Line 410:
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.
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.


= [[File:Art.png]] Define your own export (optional) =
== 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).
'''When''': If your module provide new predefined database export profiles (for its own tables or for already existing tables of another module).


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


== Test your export ==
=== Test your export ===
Go into menu Tools -> Export. Your export will appear in the list of available predefined exports (if your module is enabled).
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 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.
Choose on field and try to build an export file. If ok, try again with all fields.


= [[File:Art.png]] Define your CSS styles (optional) =
== 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).
'''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 and declare your style sheet ===
Create a style sheet CSS named '''mymodule.css''' and put it into directory '''htdocs/mymodule'''. You can have only one CSS file per module.
Create a style sheet CSS named '''mymodule.css''' 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''.php.css'''. Create your own CSS file only if you need absolutely to add not already existing styles.
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''.php.css'''. Create your own CSS file only if you need absolutely to add not already existing styles.
Line 428: Line 431:
For example '/mymodule/myfile.css'.
For example '/mymodule/myfile.css'.


== Test your style sheet ==
=== Test your style sheet ===
Disable and enable your module.
Disable and enable your module.


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


= [[File:Art.png]] Add your Javascript functions (optional) =
== Add your Javascript functions (optional) ==
'''When''': If in your PHP pages, you use javascript functions not available in Dolibarr (in file lib_head.js)
'''When''': If in your PHP pages, you use javascript functions not available in Dolibarr (in file lib_head.js)


Line 448: Line 451:
</source>
</source>


= [[File:Art.png]] Run your own code on any Dolibarr event (optional) =
== Run your own code on any Dolibarr event (optional) ==
'''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'''.
'''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'''.


Line 454: Line 457:
and [[Interfaces_from_foreign_systems_toward_Dolibarr]]
and [[Interfaces_from_foreign_systems_toward_Dolibarr]]


= [[File:Art.png]] Some coding rules and predefined functions =
== Add your own numbering rules ==
When: When you need a new rule for generated ref of elements that is not covered by existing rules.
 
TODO
 
= Some coding rules and predefined functions =
The coding rules to be followed are defined in the [[Developer documentation]], under section "General Information - Language and standards development".
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".
A lot of predefined features for developers are also available and described into page [[Developer documentation]] under section "Technical components of Dolibarr".


= [[File:Art.png]] Using MDA =
= Using MDA =
A method to generate a functional module from a UML model has been developed. More information on the page [[UML2Dolibarr - Build a module using MDA]].
A method to generate a functional module from a UML model has been developed. More information on the page [[UML2Dolibarr - Build a module using MDA]].


Navigation menu