Module development: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
Tag: 2017 source edit
mNo edit summary
Tag: 2017 source edit
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


<source lang="php">$this->_load_tables('/mymodule/sql/');</source>
<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
<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>


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 111: Line 111:
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


<source lang="bash">php build_class_from_table.php tablename modulename</source>
<syntaxHighlight lang="bash">php build_class_from_table.php tablename modulename</syntaxHighlight>
Note: If the command does not work, try to use php-cli php instead.
Note: If the command does not work, try to use php-cli php instead.


Line 129: Line 129:
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:


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


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 160: Line 160:


For each object type, there are two files to include with line
For each object type, there are two files to include with line
<source lang="php">require_once($url_fichier) ;</source>
<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 181: Line 181:


''Example :''
''Example :''
<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. 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 193: Line 193:


The resulting array has the following structure
The resulting array has the following structure
<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 199: Line 199:
$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>


''Example :''
''Example :''
<source lang="php">
<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
</source>
</syntaxHighlight>


'''4. show tabs into your page'''
'''4. show tabs into your page'''
Line 210: Line 210:
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().


<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 220: Line 220:
//            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 234: Line 234:
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.  
<source lang="php">
<syntaxHighlight lang="php">
// Load Dolibarr environment
// Load Dolibarr environment
$res=0;
$res=0;
Line 249: Line 249:
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>
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 266: Line 266:


''Example :''
''Example :''
<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>


*To include classes provided with Dolibarr, use the following syntax:
*To include classes provided with Dolibarr, use the following syntax:


''Example :''
''Example :''
<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>


===Add some fields into existing forms===
===Add some fields into existing forms===
Line 305: Line 305:
To make an insert, update or delete:
To make an insert, update or delete:


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


To read:
To read:


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


===Define style of your pages===
===Define style of your pages===
Line 349: Line 349:
===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">
<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>
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 358: Line 358:


To get value after the POST of your form, the command is:
To get value after the POST of your form, the command is:
<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>


==Add your own setup page (optional)==
==Add your own setup page (optional)==
Line 375: Line 375:
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:
<source lang="php">
<syntaxHighlight lang="php">
$this->config_page_url = array("mymodule_setuppage.php");
$this->config_page_url = array("mymodule_setuppage.php");
</source>
</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:
<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>


===Test your page===
===Test your page===
Line 395: Line 395:


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 430: Line 430:
'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 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 442: Line 442:
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 457: Line 457:
$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 465: Line 465:
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>


==Define your own box (optional)==
==Define your own box (optional)==
Line 477: Line 477:


''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 483: Line 483:
$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 515: Line 515:
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
<source lang="php">
<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'));
</source>
</syntaxHighlight>


===Test your style sheet===
===Test your style sheet===
Line 533: Line 533:


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>


==Run your own code on any Dolibarr event (optional)==
==Run your own code on any Dolibarr event (optional)==
Line 584: Line 584:
*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 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").

Navigation menu