Difference between revisions of "Script development"
m |
PolyglotBot (talk | contribs) m (Import interlang links (links to translated versions of this page in other languages) from Multi Language Manager table.) |
||
(7 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
+ | <!-- BEGIN origin interlang links --> | ||
+ | <!-- You can edit this section but do NOT remove these comments | ||
+ | Links below will be automatically replicated on translated pages by PolyglotBot --> | ||
+ | [[zh:脚本开发]] | ||
+ | [[fr:Développement_de_scripts]] | ||
+ | [[es:Desarrollo_de_scripts]] | ||
+ | <!-- END interlang links --> | ||
+ | |||
+ | [[Category:Core]] | ||
{{TemplateDocDev}} | {{TemplateDocDev}} | ||
− | + | = Location = | |
The Dolibarr command line scripts must be located in the '''scripts''' directory in Dolibarr root. The scripts are then divided into sub-directories according to their vocation. A number of scripts is provided as standard. | The Dolibarr command line scripts must be located in the '''scripts''' directory in Dolibarr root. The scripts are then divided into sub-directories according to their vocation. A number of scripts is provided as standard. | ||
− | + | = Develop a new script = | |
The scripts are often made for a specific need, it is likely that you do not find the one you want. In this case, we'll explain how to develop your own script. | The scripts are often made for a specific need, it is likely that you do not find the one you want. In this case, we'll explain how to develop your own script. | ||
− | + | == Step 1 - Create the skeleton of the script == | |
The first step is to take the skeleton script available in '''dev/skeletons/skeleton_scripts.php''' and copy and renaming it in directory '''scripts/mydir/myscript.php''' | The first step is to take the skeleton script available in '''dev/skeletons/skeleton_scripts.php''' and copy and renaming it in directory '''scripts/mydir/myscript.php''' | ||
Once renaming is done, give it permissions to execute. | Once renaming is done, give it permissions to execute. | ||
On linux, this is the command: | On linux, this is the command: | ||
− | + | <source lang="bash"> | |
− | < | ||
cd scripts/mydir; | cd scripts/mydir; | ||
chmod a+rx myscript.php | chmod a+rx myscript.php | ||
− | </ | + | </source> |
Then, run it to see if you can run the script in a command line mode. For this, on Linux, type: | Then, run it to see if you can run the script in a command line mode. For this, on Linux, type: | ||
− | < | + | <source lang="bash"> |
+ | php-cli ./myscript.php | ||
+ | </source> | ||
or | or | ||
− | php ./myscript.php</ | + | <source lang="php"> |
− | You | + | php ./myscript.php |
− | < | + | </source> |
+ | You must get the following output: | ||
+ | <source lang="bash"> | ||
Usage: myscript.php param1 param2 ... | Usage: myscript.php param1 param2 ... | ||
− | </ | + | </source> |
− | + | == Step 2 - Editing script code == | |
Modify the contents of the script to perform operations that interest you. | Modify the contents of the script to perform operations that interest you. | ||
All the code that lies between the tags | All the code that lies between the tags | ||
− | < | + | <source lang="php">// ---------- START OF YOUR CODE HERE</source> |
and | and | ||
− | < | + | <source lang="php">// ---------- END OY YOUR CODE</source> |
is provided as an example. | is provided as an example. | ||
You can delete it and put the code that you want. | You can delete it and put the code that you want. | ||
Note that in this section, you can use the variable $db that is the resource to access Dolibarr database and is already initialized. The $conf object that contains Dolibarr configuration is also available. | Note that in this section, you can use the variable $db that is the resource to access Dolibarr database and is already initialized. The $conf object that contains Dolibarr configuration is also available. | ||
− | = | + | = Examples = |
− | + | Some code examples (to create orders, products...) are available into directory '''/dev/examples/'''. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | // | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 13:21, 23 July 2019
Location
The Dolibarr command line scripts must be located in the scripts directory in Dolibarr root. The scripts are then divided into sub-directories according to their vocation. A number of scripts is provided as standard.
Develop a new script
The scripts are often made for a specific need, it is likely that you do not find the one you want. In this case, we'll explain how to develop your own script.
Step 1 - Create the skeleton of the script
The first step is to take the skeleton script available in dev/skeletons/skeleton_scripts.php and copy and renaming it in directory scripts/mydir/myscript.php
Once renaming is done, give it permissions to execute. On linux, this is the command:
cd scripts/mydir;
chmod a+rx myscript.php
Then, run it to see if you can run the script in a command line mode. For this, on Linux, type:
php-cli ./myscript.php
or
php ./myscript.php
You must get the following output:
Usage: myscript.php param1 param2 ...
Step 2 - Editing script code
Modify the contents of the script to perform operations that interest you. All the code that lies between the tags
// ---------- START OF YOUR CODE HERE
and
// ---------- END OY YOUR CODE
is provided as an example. You can delete it and put the code that you want. Note that in this section, you can use the variable $db that is the resource to access Dolibarr database and is already initialized. The $conf object that contains Dolibarr configuration is also available.
Examples
Some code examples (to create orders, products...) are available into directory /dev/examples/.