Module development: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
Line 21: Line 21:
Then change the contents of this file to replace:
Then change the contents of this file to replace:


    * all "modMyModule" with a value which corresponds to the vocation of your module. This value must always start with 'mod'.
* all "modMyModule" with a value which corresponds to the vocation of your module. This value must always start with 'mod'.
    * $this->numero = 10000 by a number of free module (Go to Home page -> System Information -> Dolibarr to know id the list of modules used).
* $this->numero = 10000 by a number of free module (Go to Home page -> System Information -> Dolibarr to know id the list of modules used).
    * $this->const_name = 'MAIN_MODULE_MYMODULE' by $this-> const_name = 'MAIN_MODULE_XXX' where XXX must match the value chosen to replace MyModule and put in capitalized.
* $this->const_name = 'MAIN_MODULE_MYMODULE' by $this-> const_name = 'MAIN_MODULE_XXX' where XXX must match the value chosen to replace MyModule and put in capitalized.
    * $dir = DOL_DOCUMENT_ROOT.'/mysql/tables/mymodule/'; by $dir = DOL_DOCUMENT_ROOT.'/mysql/tables where xxxxxxxx is the name of your module (without spaces).
* $dir = DOL_DOCUMENT_ROOT.'/mysql/tables/mymodule/'; by $dir = DOL_DOCUMENT_ROOT.'/mysql/tables where xxxxxxxx is the name of your module (without spaces).
    * Modify any other variables defined in the constructor (see comment in the code skeleton for their meaning).  
* Modify any other variables defined in the constructor (see comment in the code skeleton for their meaning).  


Your file descriptor for your module is ready.
Your file descriptor for your module is ready.
Line 43: Line 43:
Create a subdirectory called "sql" into directory of your module (eg htdocs/mymodule/sql) to put the sql scripts that you will create. Then in the "load_tables" function of your file descriptor module, modify the line
Create a subdirectory called "sql" into directory of your module (eg htdocs/mymodule/sql) to put the sql scripts that you will create. Then in the "load_tables" function of your file descriptor module, modify the line


<pre>$dir = DOL_DOCUMENT_ROOT.'/mymodule/sql/';</pre>
<source lang="php">$dir = DOL_DOCUMENT_ROOT.'/mymodule/sql/';</source>
by
by
<pre>$dir = DOL_DOCUMENT_ROOT.'/valuechooseformymodule/sql/';</pre>
<source lang="php">$dir = DOL_DOCUMENT_ROOT.'/valuechooseformymodule/sql/';</source>


''Rules to follow:''
''Rules to follow:''
Line 53: Line 53:


Example of content of a file data.sql
Example of content of a file data.sql
<source 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>


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 65: Line 67:
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


<pre>php build_class_from_table.php tablename</pre>
<source lang="bash">php build_class_from_table.php tablename</source>
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 79: Line 81:
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:


<pre>
<source lang="php">
// Array to add new pages in new tabs
// Array to add new pages in new tabs
$this->tabs = array('product:ComposedProducts:@composition:/composition/show_composition.php?id=__ID__');
$this->tabs = array('product:ComposedProducts:@composition:/composition/show_composition.php?id=__ID__');
Line 92: Line 94:
// 'propal'          to add a tab in propal view
// 'propal'          to add a tab in propal view
// 'member'          to add a tab in fundation member view
// 'member'          to add a tab in fundation member view
</pre>
</source>


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 122: Line 124:
Copy, into that directory, the file '''skeletons_page.php''' that will serve as a starting point for your page file and also file '''pre.inc.php'''.
Copy, into that directory, the file '''skeletons_page.php''' that will serve as a starting point for your page file and also file '''pre.inc.php'''.
Edit the file '''pre.inc.php''' to have a correct relative path to the main.inc.php file.
Edit the file '''pre.inc.php''' to have a correct relative path to the main.inc.php file.
<pre>
<source lang="php">
include("../../main.inc.php)";
include("../../main.inc.php)";
</pre>
</source>
This depends on the depth of the directory in which the file is pre.inc.php (Add or remove "../").  
This depends on the depth of the directory in which the file is pre.inc.php (Add or remove "../").  
It's in the main.inc.php file that is loaded technical environment variables and permissions. The following variables are objects positioned in this file:
It's in the main.inc.php file that is loaded technical environment variables and permissions. The following variables are objects positioned in this file:
Line 142: Line 144:
To make an insert, update or delete:
To make an insert, update or delete:


<pre>
<source 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
</pre>
</source>


To read:
To read:


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


== Define style of your pages ==
== Define style of your pages ==
Line 186: Line 188:
== Use the Dolibarr date selector ==
== Use the Dolibarr date selector ==
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">
  $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>
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 193: Line 197:


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


= [[File:Art.png]] Add your own setup page (optionnal)  =
= [[File:Art.png]] Add your own setup page (optionnal)  =
Line 207: Line 211:
Put this page into directory '''/admin'''.
Put this page into directory '''/admin'''.
Then, into the descriptor file of your module, modify the variable config_page_url to set name of this PHP page (without the path that is not necesary if page is inside directory admin).
Then, into the descriptor file of your module, modify the variable config_page_url to set name of this PHP page (without the path that is not necesary if page is inside directory admin).
<pre>
<source lang="php">
$this->config_page_url = array("mymodule_setuppage.php");
$this->config_page_url = array("mymodule_setuppage.php");
</pre>
</source>


== Test your page ==
== Test your page ==
Line 223: Line 227:


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:
<pre>
<source 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 257: Line 261:
'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++;
</pre>
</source>


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.
Line 270: Line 274:
La définition des permissions que gèrera votre module se fait dans le fichier descripteur créé dans la première étape.
La définition des permissions que gèrera votre module se fait dans le fichier descripteur créé dans la première étape.
Modifier la ligne $this->rights_class = 'facture' par
Modifier la ligne $this->rights_class = 'facture' par
<pre>
<source lang="php">
$this->rights_class = 'monmodule'
$this->rights_class = 'monmodule'
</pre>
</source>


Ensuite remplissez le tableau $this->rights avec autant d'entrée que de permissions différentes à gérer.
Ensuite remplissez le tableau $this->rights avec autant d'entrée que de permissions différentes à gérer.


<pre>
<source lang="php">
$this->rights[$r][0] = 10001;
$this->rights[$r][0] = 10001;
$this->rights[$r][1] = 'Libelle par défaut de ma permission';
$this->rights[$r][1] = 'Libelle par défaut de ma permission';
Line 283: Line 287:
$this->rights[$r][5] = 'sousaction';
$this->rights[$r][5] = 'sousaction';
$r++;
$r++;
</pre>
</source>


Dans $this->rights[$r][0], mettre un id de permission non déjà pris (Voir dans le menu '''Infos Système''' sur une installation de Dolibarr opérationnelle pour connaitre les id déjà utilisés.
Dans $this->rights[$r][0], mettre un id de permission non déjà pris (Voir dans le menu '''Infos Système''' sur une installation de Dolibarr opérationnelle pour connaitre les id déjà utilisés.
Line 303: Line 307:


''Exemple:''
''Exemple:''
<pre>
<source lang="php">
this->boxes[0][1]='mabox0.php'
this->boxes[0][1]='mabox0.php'
this->boxes[1][1]='mabox1.php'
this->boxes[1][1]='mabox1.php'
Line 309: Line 313:
...
...
this->boxes[n][1]='maboxn.php'
this->boxes[n][1]='maboxn.php'
</pre>
</source>


Ensuite creer les fichiers mabox0.php, mabox1.php... en prenant exemple sur des box existantes (dans le répertoire htdocs/include/boxes
Ensuite creer les fichiers mabox0.php, mabox1.php... en prenant exemple sur des box existantes (dans le répertoire htdocs/include/boxes
Line 366: Line 370:


* 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):
<pre>
<source lang="bash">
perl makepack-dolibarrmodule.pl
perl makepack-dolibarrmodule.pl
</pre>
</source>
The script asks you the name of your module, its major and minor version. A file '''mymodule.tgz ''' 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.tgz ''' 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:
<pre>
<source lang="bash">
tar -xvf monmodule.tgz
tar -xvf monmodule.tgz
</pre>
</source>


* If you want your module to be public, you can submit it (the tgz file) on Dolibarr web site on download area: http://www.dolibarr.org -> downloads -> contrib -> submit a file (you must create an account and be logged to have link appeared).
* If you want your module to be public, you can submit it (the tgz file) on Dolibarr web site on download area: http://www.dolibarr.org -> downloads -> contrib -> submit a file (you must create an account and be logged to have link appeared).

Navigation menu