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
Line 2: Line 2:
 
{{ToTranslate}}
 
{{ToTranslate}}
  
== Localisation ==
+
== Location ==
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.
+
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.
Un certain nombre de scripts est donc fourni en standard.
 
  
== Développer un nouveau script ==
+
== Develop a new 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.
+
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.
  
=== Etape 1 - Création du squelette du script ===
+
=== Step 1 - Create the skeletonn of the 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'''
+
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'''
  
Une fois le script renommé, donnez lui les droits en exécution par la commande:
+
Once renaming is done, give it permissions to execute.
 +
On linux, this is the command:
 +
onnez lui les droits en exécution par la commande:
 
<pre>
 
<pre>
cd scripts/monrep;
+
cd scripts/mydir;
chmod a+rx monscript.php
+
chmod a+rx myscript.php
 
</pre>
 
</pre>
Ensuite, lancez le pour voir si vous pouvez exécuter un script en mode ligne de commande. Pour cela, taper:
+
Then, run it to see if you can run the script in a command line mode. For this, on Linux, type:
<pre>php-cli ./monscript.php
+
<pre>php-cli ./myscript.php
ou
+
or
php ./monscript.php</pre>
+
php ./myscript.php</pre>
Vous devriez obtenir le résultat suivant:
+
You muste get the following output:
 
<pre>
 
<pre>
Usage: monscript.php param1 param2 ...
+
Usage: myscript.php param1 param2 ...
 
</pre>
 
</pre>
  
=== Etape 2 - Edition du code du script ===
+
=== Step 2 - Editing script code ===
Modifier le contenu du script pour réaliser les opérations qui vous intéressent.
+
Modify the contents of the script to perform operations that interest you.  
Tout le code qui se trouve entre les balises
+
All the code that lies between the tags
 
<pre>// ---------- START OF YOUR CODE HERE</pre>
 
<pre>// ---------- START OF YOUR CODE HERE</pre>
et
+
and
 
<pre>// ---------- END OY YOUR CODE</pre>
 
<pre>// ---------- END OY YOUR CODE</pre>
est fourni à titre d'exemple.
+
is provided as an example.  
Vous pouvez le supprimer et y mettre le code qui vous intéresse.
+
You can delete it and put the code that you want.  
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.
+
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.
L'objet $conf qui contient la configuration Dolibarr est également disponible.
 
  
==== Exemple insertion d'un produit ====
+
==== Example to insert a product ====
Par exemple pour insérer un produit dans la base dolibarr, vous pouvez y placer le code suivant:
+
For example to insert a product in the database Dolibarr, you can place the following code:
 
<pre>
 
<pre>
// Inclusion classe métier utilisateur et product
+
// Include mother classes for user and product
 
require_once(DOL_DOCUMENT_ROOT."/user.class.php");
 
require_once(DOL_DOCUMENT_ROOT."/user.class.php");
 
require_once(DOL_DOCUMENT_ROOT."/product.class.php");
 
require_once(DOL_DOCUMENT_ROOT."/product.class.php");
  
// Création d'une instance utilisateur
+
// Create a new user instance
 
$user=new User($db);
 
$user=new User($db);
  
// Création d'une instance de product
+
// Create a new product instance
 
$myproduct=new Product($db);
 
$myproduct=new Product($db);
  
// Définition des propriétés de l'instance utilisateur
+
// Define properties of user
 
$user->id = 0;
 
$user->id = 0;
  
// Définition des propriétés de l'instance product
+
// Define properties of product
 
$myproduct->ref                = '1234';
 
$myproduct->ref                = '1234';
 
$myproduct->libelle            = 'libelle';
 
$myproduct->libelle            = 'libelle';
Line 66: Line 66:
 
$myproduct->weight_units      = 0;
 
$myproduct->weight_units      = 0;
  
// Création du produit en base
+
// Create product in database
 
$idproduct = $myproduct->create($user);
 
$idproduct = $myproduct->create($user);
  
// Gestion erreur
+
// Error management
if ($idproduct < 0) dolibarr_print_error($db,$myproduct->error);
+
if ($idproduct < 0) dol_print_error($db,$myproduct->error);
else print "Produit $idproduct cree.\n";
+
else print "Product $idproduct created.\n";
 
</pre>
 
</pre>

Revision as of 10:12, 3 July 2009

En verysmall.png Page waiting for translation. To translate, create an account, go back and clic on "Modify".
Fr verysmall.png Page en attente de traduction. Pour traduire, créez un compte, revenez et cliquez sur "Modifier".
Es verysmall.png Página a traducir. Para traducirla, cree una cuenta, vuelva a la página y haga clic en "editar".
It verysmall.png Pagina da tradurre. Per tradurla, crea un utente, torna indietro e clicca su "modifica".
Pt verysmall.png Página a aguardar por tradução. Para traduzir, crie uma conta, volte atrás e clique em "Modificar".
De verysmall.png Seite wartet auf Übersetzung. Um Übersetzung zu erstellen, richte einen Account ein, geh zurück und klicke auf "bearbeiten".
Zh verysmall.png 页面等待翻译。若要翻译本页,先创建一个帐户、登录并返回本页后单击“编辑”。

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 skeletonn 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: onnez lui les droits en exécution par la commande:

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

Example to insert a product

For example to insert a product in the database Dolibarr, you can place the following code:

// Include mother classes for user and product
require_once(DOL_DOCUMENT_ROOT."/user.class.php");
require_once(DOL_DOCUMENT_ROOT."/product.class.php");

// Create a new user instance
$user=new User($db);

// Create a new product instance
$myproduct=new Product($db);

// Define properties of user
$user->id = 0;

// Define properties of product
$myproduct->ref                = '1234';
$myproduct->libelle            = 'libelle';
$myproduct->price              = '10';
$myproduct->price_base_type    = 'HT';
$myproduct->tva_tx             = '19.6';
$myproduct->type               = 0;
$myproduct->status             = 1;
$myproduct->description        = 'Description';
$myproduct->note               = 'Note';
$myproduct->weight             = 10;
$myproduct->weight_units       = 0;

// Create product in database
$idproduct = $myproduct->create($user);

// Error management
if ($idproduct < 0) dol_print_error($db,$myproduct->error);
else print "Product $idproduct created.\n";