Difference between revisions of "Script development"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
(Created page with '{{TemplateDocDev}} {{ToTranslate}} == Localisation == Les scripts en ligne de commande de Dolibarr doivent être situés dans le répertoire '''scripts''' de Dolibarr. Les scrip...')
 
m (Import interlang links (links to translated versions of this page in other languages) from Multi Language Manager table.)
 
(9 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}}
{{ToTranslate}}
 
 
== Localisation ==
 
Les scripts en ligne de commande de Dolibarr doivent être situés dans le répertoire '''scripts''' de Dolibarr. Les scripts sont ensuite répartis dans des sous-répertoires en fonction de leur vocation.
 
Un certain nombre de scripts est donc fourni en standard.
 
 
== Développer un nouveau script ==
 
Les scripts étant souvent réalisés pour un besoin spécifique, il y a de fortes chances que vous ne trouviez pas celui qui vous intéresse. Dans ce cas, nous allons vous expliquer comment développer votre propre script.
 
 
=== Etape 1 - Création du squelette du script ===
 
La première étape est de reprendre le squelette de script disponible en '''dev/skeletons/skeleton_scripts.php''' et de le recopier en renommant, ce script dans le répertoire '''scripts/monrep/monscript.php'''
 
 
Une fois le script renommé, donnez lui les droits en exécution par la commande:
 
<pre>
 
cd scripts/monrep;
 
chmod a+rx monscript.php
 
</pre>
 
Ensuite, lancez le pour voir si vous pouvez exécuter un script en mode ligne de commande. Pour cela, taper:
 
<pre>php-cli ./monscript.php
 
ou
 
php ./monscript.php</pre>
 
Vous devriez obtenir le résultat suivant:
 
<pre>
 
Usage: monscript.php param1 param2 ...
 
</pre>
 
 
=== Etape 2 - Edition du code du script ===
 
Modifier le contenu du script pour réaliser les opérations qui vous intéressent.
 
Tout le code qui se trouve entre les balises
 
<pre>// ---------- START OF YOUR CODE HERE</pre>
 
et
 
<pre>// ---------- END OY YOUR CODE</pre>
 
est fourni à titre d'exemple.
 
Vous pouvez le supprimer et y mettre le code qui vous intéresse.
 
Notez que dans cette portion, vous pouvez utiliser la variable $db qui est la ressource d'accès à la base Dolibarr et qui est déjà initialisée.
 
L'objet $conf qui contient la configuration Dolibarr est également disponible.
 
 
==== Exemple insertion d'un produit ====
 
Par exemple pour insérer un produit dans la base dolibarr, vous pouvez y placer le code suivant:
 
<pre>
 
// Inclusion classe métier utilisateur et product
 
require_once(DOL_DOCUMENT_ROOT."/user.class.php");
 
require_once(DOL_DOCUMENT_ROOT."/product.class.php");
 
  
// Création d'une instance utilisateur
+
= Location =
$user=new User($db);
+
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.
  
// Création d'une instance de product
+
= Develop a new script =
$myproduct=new Product($db);
+
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.
  
// Définition des propriétés de l'instance utilisateur
+
== Step 1 - Create the skeleton of the script ==
$user->id = 0;
+
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'''
  
// Définition des propriétés de l'instance product
+
Once renaming is done, give it permissions to execute.
$myproduct->ref                = '1234';
+
On linux, this is the command:
$myproduct->libelle            = 'libelle';
+
<source lang="bash">
$myproduct->price              = '10';
+
cd scripts/mydir;
$myproduct->price_base_type    = 'HT';
+
chmod a+rx myscript.php
$myproduct->tva_tx            = '19.6';
+
</source>
$myproduct->type               = 0;
+
Then, run it to see if you can run the script in a command line mode. For this, on Linux, type:
$myproduct->status            = 1;
+
<source lang="bash">
$myproduct->description        = 'Description';
+
php-cli ./myscript.php
$myproduct->note              = 'Note';
+
</source>
$myproduct->weight            = 10;
+
or
$myproduct->weight_units      = 0;
+
<source lang="php">
 +
php ./myscript.php
 +
</source>
 +
You must get the following output:
 +
<source lang="bash">
 +
Usage: myscript.php param1 param2 ...
 +
</source>
  
// Création du produit en base
+
== Step 2 - Editing script code ==
$idproduct = $myproduct->create($user);
+
Modify the contents of the script to perform operations that interest you.
 +
All the code that lies between the tags
 +
<source lang="php">// ---------- START OF YOUR CODE HERE</source>
 +
and
 +
<source lang="php">// ---------- END OY YOUR CODE</source>
 +
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.
  
// Gestion erreur
+
= Examples =
if ($idproduct < 0) dolibarr_print_error($db,$myproduct->error);
+
Some code examples (to create orders, products...) are available into directory '''/dev/examples/'''.
else print "Produit $idproduct cree.\n";
 
</pre>
 

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