Skip to content
Snippets Groups Projects
Commit 53b6d89e authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Work on import module

parent 4483a7e2
No related branches found
No related tags found
No related merge requests found
......@@ -62,7 +62,7 @@ class Export
/**
* \brief Load an exportable dataset
* \param user Object user making export
* \param filter Code export pour charger un lot de donnees particulier
* \param filter Load a particular dataset only
*/
function load_arrays($user,$filter='')
{
......
This diff is collapsed.
......@@ -40,16 +40,16 @@ class Import
}
/**
* \brief Load an importable dataset
* \param user Object user making export
* \param filter Code export pour charger un lot de donnees particulier
*/
function load_arrays($user,$filter='')
{
global $langs,$conf;
/**
* \brief Load description of an importable dataset
* \param user Object user making import
* \param filter Load a particular dataset only
*/
function load_arrays($user,$filter='')
{
global $langs,$conf;
dol_syslog("Import::load_arrays user=".$user->id." filter=".$filter);
dol_syslog("Import::load_arrays user=".$user->id." filter=".$filter);
//$dir=DOL_DOCUMENT_ROOT."/includes/modules";
foreach($conf->file->dol_document_root as $dirroot)
......@@ -60,199 +60,138 @@ class Import
$handle=@opendir($dir);
if ($handle)
{
// Recherche des exports disponibles
$var=True;
$i=0;
while (($file = readdir($handle))!==false)
{
if (eregi("^(mod.*)\.class\.php",$file,$reg))
{
$modulename=$reg[1];
// Defined if module is enabled
$enabled=true;
$part=strtolower(eregi_replace('^mod','',$modulename));
// Recherche des exports disponibles
$var=True;
$i=0;
while (($file = readdir($handle))!==false)
{
if (eregi("^(mod.*)\.class\.php",$file,$reg))
{
$modulename=$reg[1];
// Defined if module is enabled
$enabled=true;
$part=strtolower(eregi_replace('^mod','',$modulename));
if (empty($conf->$part->enabled)) $enabled=false;
if ($enabled)
{
{
// Chargement de la classe
$file = $dir."/".$modulename.".class.php";
$classname = $modulename;
require_once($file);
$module = new $classname($this->db);
if (is_array($module->import_code))
{
foreach($module->import_code as $r => $value)
{
if ($filter && ($filter != $module->import_code[$r])) continue;
// Test if permissions are ok
/*$perm=$module->import_permission[$r][0];
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
if ($perm[2])
{
$bool=$user->rights->$perm[0]->$perm[1]->$perm[2];
}
else
{
$bool=$user->rights->$perm[0]->$perm[1];
}
if ($perm[0]=='user' && $user->admin) $bool=true;
//print $bool." $perm[0]"."<br>";
$file = $dir."/".$modulename.".class.php";
$classname = $modulename;
require_once($file);
$module = new $classname($this->db);
if (is_array($module->import_code))
{
foreach($module->import_code as $r => $value)
{
if ($filter && ($filter != $module->import_code[$r])) continue;
// Test if permissions are ok
/*$perm=$module->import_permission[$r][0];
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
if ($perm[2])
{
$bool=$user->rights->$perm[0]->$perm[1]->$perm[2];
}
else
{
$bool=$user->rights->$perm[0]->$perm[1];
}
if ($perm[0]=='user' && $user->admin) $bool=true;
//print $bool." $perm[0]"."<br>";
*/
// Permissions ok
// if ($bool)
// {
// Charge fichier lang en rapport
$langtoload=$module->getLangFilesArray();
if (is_array($langtoload))
{
foreach($langtoload as $key)
{
$langs->load($key);
}
}
// Module
$this->array_import_module[$i]=$module;
// Permission
$this->array_import_perms[$i]=$user->admin;
// Icon
$this->array_import_icon[$i]=(isset($module->import_icon[$r])?$module->import_icon[$r]:$module->picto);
// Code du dataset export
$this->array_import_code[$i]=$module->import_code[$r];
// Libelle du dataset export
$this->array_import_label[$i]=$module->getDatasetLabel($r);
// Tableau des champ a exporter (cle=champ, valeur=libelle)
$this->array_import_fields[$i]=$module->import_fields_array[$r];
// Tableau des entites a exporter (cle=champ, valeur=entite)
$this->array_import_entities[$i]=$module->import_entities_array[$r];
// Tableau des alias a exporter (cle=champ, valeur=alias)
$this->array_import_alias[$i]=$module->import_alias_array[$r];
// Tableau des operations speciales sur champ
$this->array_import_special[$i]=$module->import_special_array[$r];
// Requete sql du dataset
$this->array_import_sql_start[$i]=$module->import_sql_start[$r];
$this->array_import_sql_end[$i]=$module->import_sql_end[$r];
//$this->array_import_sql[$i]=$module->import_sql[$r];
dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".sizeof($module->import_fields_code[$r]));
$i++;
// }
}
}
}
}
}
// Permissions ok
// if ($bool)
// {
// Charge fichier lang en rapport
$langtoload=$module->getLangFilesArray();
if (is_array($langtoload))
{
foreach($langtoload as $key)
{
$langs->load($key);
}
}
// Module
$this->array_import_module[$i]=$module;
// Permission
$this->array_import_perms[$i]=$user->admin;
// Icon
$this->array_import_icon[$i]=(isset($module->import_icon[$r])?$module->import_icon[$r]:$module->picto);
// Code du dataset export
$this->array_import_code[$i]=$module->import_code[$r];
// Libelle du dataset export
$this->array_import_label[$i]=$module->getDatasetLabel($r);
// Tableau des champ a exporter (cle=champ, valeur=libelle)
$this->array_import_fields[$i]=$module->import_fields_array[$r];
// Tableau des entites a exporter (cle=champ, valeur=entite)
$this->array_import_entities[$i]=$module->import_entities_array[$r];
// Tableau des alias a exporter (cle=champ, valeur=alias)
$this->array_import_examplevalues[$i]=$module->import_examplevalues_array[$r];
// Requete sql du dataset
$this->array_import_sql_start[$i]=$module->import_sql_start[$r];
$this->array_import_sql_end[$i]=$module->import_sql_end[$r];
//$this->array_import_sql[$i]=$module->import_sql[$r];
dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".sizeof($module->import_fields_code[$r]));
$i++;
// }
}
}
}
}
}
}
}
closedir($handle);
}
/*
* \brief Importe un fichier clients
*/
function ImportClients($file)
{
$this->nb_import_ok = 0;
$this->nb_import_ko = 0;
$this->nb_import = 0;
dol_syslog("Import::ImportClients($file)", LOG_DEBUG);
$this->ReadFile($file);
foreach ($this->lines as $this->line)
{
$societe = new Societe($this->db);
$this->SetInfosTiers($societe);
$societe->client = 1;
$societe->tva_assuj = $this->line[12];
$societe->code_client = $this->line[13];
$societe->tva_intra = $this->line[14];
$this->nb_import++;
if ( $societe->create($user) == 0)
{
dol_syslog("Import::ImportClients ".$societe->nom." SUCCESS", LOG_DEBUG);
$this->nb_import_ok++;
}
else
{
dol_syslog("Import::ImportClients ".$societe->nom." ERROR", LOG_ERR);
$this->nb_import_ko++;
}
}
closedir($handle);
}
function SetInfosTiers(&$obj)
{
$obj->nom = $this->line[0];
$obj->adresse = $this->line[1];
if (strlen(trim($this->line[2])) > 0)
$obj->adresse .= "\n". trim($this->line[2]);
if (strlen(trim($this->line[3])) > 0)
$obj->adresse .= "\n". trim($this->line[3]);
$obj->cp = $this->line[4];
$obj->ville = $this->line[5];
$obj->tel = $this->line[6];
$obj->fax = $this->line[7];
$obj->email = $this->line[8];
$obj->url = $this->line[9];
$obj->siren = $this->line[10];
$obj->siret = $this->line[11];
}
function ReadFile($file)
/**
* \brief Lance la generation du fichier example
* \param user User qui exporte
* \param model Modele d'export
* \param $headerlinefields Array of values for first line of example file
* \param $contentlinevalues Array of values for content line of example file
* \remarks Les tableaux array_export_xxx sont deja chargees pour le bon datatoexport
* aussi le parametre datatoexport est inutilise
*/
function build_example_file($user, $model, $headerlinefields, $contentlinevalues)
{
$this->errno = 0;
global $conf,$langs;
if (is_readable($file))
{
dol_syslog("Import::ReadFile Lecture du fichier $file", LOG_DEBUG);
$indice=0;
$line = 0;
$hf = fopen ($file, "r");
$line = 0;
$i=0;
dol_syslog("Import::build_example_file ".$model);
$this->lines = array();
// Creation de la classe d'import du model Import_XXX
$dir = DOL_DOCUMENT_ROOT . "/includes/modules/import/";
$file = "import_".$model.".modules.php";
$classname = "Import".$model;
require_once($dir.$file);
$objmodel = new $classname($this->db);
$outputlangs=$langs; // Lang for output
$s='';
while (!feof($hf) )
{
$cont = fgets($hf, 1024);
// Genere en-tete
$s.=$objmodel->write_header_example($outputlangs);
if (strlen(trim($cont)) > 0)
{
$this->lines[$i] = explode(";", $cont);
}
$i++;
// Genere ligne de titre
$s.=$objmodel->write_title_example($outputlangs,$headerlinefields);
}
}
else
{
$this->errno = -2;
}
// Genere ligne de titre
$s.=$objmodel->write_record_example($outputlangs,$contentlinevalues);
return $errno;
}
// Genere pied de page
$s.=$objmodel->write_footer_example($outputlangs);
return $s;
}
}
?>
......@@ -199,8 +199,6 @@ if ($step == 3 && $action == 'select_model')
* Affichage Pages des Etapes
*/
$objmodelimport=new ModeleImports();
if ($step == 1 || ! $datatoimport)
{
......@@ -338,7 +336,7 @@ if ($step == 2 && $datatoimport)
print '<tr '.$bc[$var].'>';
print '<td width="16">'.img_picto_common($key,$objmodelimport->getPicto($key)).'</td>';
print '<td>'.$objmodelimport->getDriverLabel($key).'</td>';
print '<td colspan="4" align="right"><a href="'.DOL_URL_ROOT.'/imports/emptyexample.php" target="_blank">'.$langs->trans("DownloadEmptyExample").'</a></td>';
print '<td colspan="4" align="right"><a href="'.DOL_URL_ROOT.'/imports/emptyexample.php?format='.$key.'&datatoimport='.$datatoimport.'" target="_blank">'.$langs->trans("DownloadEmptyExample").'</a></td>';
//print '<td>'.$objmodelimport->getLibLabel($key).'</td><td>'.$objmodelimport->getLibVersion($key).'</td>';
print '</tr>';
}
......@@ -432,6 +430,7 @@ if ($step == 3 && $datatoimport)
2=>array('name'=>'bb','example1'=>'valb1','example2'=>'valb2')
);
// Load targets fields in database
$fieldstarget=$objimport->array_import_fields[0];
......@@ -512,7 +511,7 @@ if ($step == 3 && $datatoimport)
print '<br>';
// Combo list of import models
// List of import models
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" name="action" value="select_model">';
......@@ -919,7 +918,7 @@ function show_elem($fieldssource,$pos,$var,$key)
if (empty($key))
{
print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
print img_picto($langs->trans("MoveBox",$pos),'uparrow','class="boxhandle" style="cursor:move;"');
print img_picto(($pos>0?$langs->trans("MoveField",$pos):''),'uparrow','class="boxhandle" style="cursor:move;"');
print '</td>';
print '<td style="font-weight: normal">';
print $langs->trans("NoFields");
......@@ -939,7 +938,7 @@ function show_elem($fieldssource,$pos,$var,$key)
//print '<td width="16">'.img_file('','').'</td>';
print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
// The image must have the class 'boxhandle' beause it's value used in DOM draggable objects to define the area used to catch the full object
print img_picto($langs->trans("MoveBox",$pos),'uparrow','class="boxhandle" style="cursor:move;"');
print img_picto($langs->trans("MoveField",$pos),'uparrow','class="boxhandle" style="cursor:move;"');
print '</td>';
print '<td style="font-weight: normal">';
print $langs->trans("Field").' '.$key.': ';
......
......@@ -68,7 +68,7 @@ class ImportCsv extends ModeleImports
$this->version_lib=DOL_VERSION;
$this->separator=',';
if (! empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
if (! empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->IMPORT_CSV_SEPARATOR_TO_USE;
}
function getDriverId()
......@@ -102,6 +102,48 @@ class ImportCsv extends ModeleImports
}
/**
* \brief Output header of an example file for this format
* \param langs Output language
*/
function write_header_example($outputlangs)
{
return '';
}
/**
* \brief Output title line of an example file for this format
* \param langs Output language
*/
function write_title_example($outputlangs,$headerlinefields)
{
$s='';
$s.=join($this->separator,$headerlinefields);
return $s."\n";
}
/**
* \brief Output record of an example file for this format
* \param langs Output language
*/
function write_record_example($outputlangs,$contentlinevalues)
{
$s='';
$s.=join($this->separator,$contentlinevalues);
return $s."\n";
}
/**
* \brief Output footer of an example file for this format
* \param langs Output language
*/
function write_footer_example($outputlangs)
{
return '';
}
/**
* \brief Open output file
* \param file Path of filename
......
<?php
/* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
/* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2007 Regis Houssin <regis@dolibarr.fr>
*
* This program is free software; you can redistribute it and/or modify
......@@ -24,7 +24,6 @@
* \brief File of parent class for import file readers
* \version $Id$
*/
require_once(DOL_DOCUMENT_ROOT.'/lib/functions.lib.php');
......
......@@ -155,6 +155,7 @@ class modProduit extends DolibarrModules
//$this->import_permission[$r]=array(array("societe","import"));
$this->import_fields_array[$r]=array('p.ref'=>"Ref",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",'p.price'=>"SellingPriceHT",'p.price_ttc'=>"SellingPriceTTC",'p.tva_tx'=>'VAT','p.envente'=>"OnSell",'p.duration'=>"Duration");
$this->import_entities_array[$r]=array('p.ref'=>"product",'p.label'=>"product",'p.description'=>"product",'p.note'=>"product",'p.price'=>"product",'p.price_ttc'=>"product",'p.tva_tx'=>'product','p.envente'=>"product",'p.duration'=>"product");
$this->import_examplevalues_array[$r]=array('p.ref'=>"PR123456",'p.label'=>"My product",'p.description'=>"This is a description example for record",'p.note'=>"Some note",'p.price'=>"100",'p.price_ttc'=>"110",'p.tva_tx'=>'10','p.envente'=>"0 or 1",'p.duration'=>"1y");
$this->import_sql_start[$r]='INSERT INTO '.MAIN_DB_PREFIX.'produit as s';
$this->import_sql_end[$r] ='';
......
......@@ -191,6 +191,7 @@ class modSociete extends DolibarrModules
$r++;
$this->export_code[$r]=$this->rights_class.'_'.$r;
$this->export_label[$r]='ExportDataset_company_1';
$this->export_icon[$r]='company';
$this->export_permission[$r]=array(array("societe","export"));
$this->export_fields_array[$r]=array('s.rowid'=>"Id",'s.nom'=>"Name",'s.prefix_comm'=>"Prefix",'s.client'=>"Customer",'s.fournisseur'=>"Supplier",'s.datec'=>"DateCreation",'s.tms'=>"DateLastModification",'s.code_client'=>"CustomerCode",'s.code_fournisseur'=>"SupplierCode",'s.address'=>"Address",'s.cp'=>"Zip",'s.ville'=>"Town",'p.libelle'=>"Country",'p.code'=>"CountryCode",'s.tel'=>"Phone",'s.fax'=>"Fax",'s.url'=>"Url",'s.email'=>"Email",'s.siret'=>"IdProf1",'s.siren'=>"IdProf2",'s.ape'=>"IdProf3",'s.idprof4'=>"IdProf4",'s.tva_intra'=>"VATIntraShort",'s.capital'=>"Capital",'s.note'=>"Note",'t.libelle'=>"ThirdPartyType",'ce.code'=>"Effectif","cfj.libelle"=>"JuridicalStatus",'s.fk_prospectlevel'=>'ProspectLevel','fk_stcomm'=>'ProspectStatus');
$this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>"company",'s.prefix_comm'=>"company",'s.client'=>"company",'s.fournisseur'=>"company",'s.datec'=>"company",'s.tms'=>"company",'s.code_client'=>"company",'s.code_fournisseur'=>"company",'s.address'=>"company",'s.cp'=>"company",'s.ville'=>"company",'p.libelle'=>"company",'p.code'=>"company",'s.tel'=>"company",'s.fax'=>"company",'s.url'=>"company",'s.email'=>"company",'s.siret'=>"company",'s.siren'=>"company",'s.ape'=>"company",'s.idprof4'=>"company",'s.tva_intra'=>"company",'s.capital'=>"company",'s.note'=>"company",'t.libelle'=>"company",'ce.code'=>"company","cfj.libelle"=>"company",'s.fk_prospectlevel'=>'company','fk_stcomm'=>'company');
......@@ -229,9 +230,11 @@ class modSociete extends DolibarrModules
$r++;
$this->import_code[$r]=$this->rights_class.'_'.$r;
$this->import_label[$r]='ImportDataset_company_1';
$this->import_icon[$r]='company';
//$this->import_permission[$r]=array(array("societe","export"));
$this->import_fields_array[$r]=array('s.nom'=>"Name",'s.prefix_comm'=>"Prefix",'s.client'=>"Customer",'s.fournisseur'=>"Supplier",'s.datec'=>"DateCreation",'s.tms'=>"DateLastModification",'s.code_client'=>"CustomerCode",'s.code_fournisseur'=>"SupplierCode",'s.address'=>"Address",'s.cp'=>"Zip",'s.ville'=>"Town",'p.libelle'=>"Country",'p.code'=>"CountryCode",'s.tel'=>"Phone",'s.fax'=>"Fax",'s.url'=>"Url",'s.email'=>"Email",'s.siret'=>"IdProf1",'s.siren'=>"IdProf2",'s.ape'=>"IdProf3",'s.idprof4'=>"IdProf4",'s.tva_intra'=>"VATIntraShort",'s.capital'=>"Capital",'s.note'=>"Note",'t.libelle'=>"ThirdPartyType",'ce.code'=>"Effectif","cfj.libelle"=>"JuridicalStatus",'s.fk_prospectlevel'=>'ProspectLevel','fk_stcomm'=>'ProspectStatus');
$this->import_entities_array[$r]=array('s.nom'=>"company",'s.prefix_comm'=>"company",'s.client'=>"company",'s.fournisseur'=>"company",'s.datec'=>"company",'s.tms'=>"company",'s.code_client'=>"company",'s.code_fournisseur'=>"company",'s.address'=>"company",'s.cp'=>"company",'s.ville'=>"company",'p.libelle'=>"company",'p.code'=>"company",'s.tel'=>"company",'s.fax'=>"company",'s.url'=>"company",'s.email'=>"company",'s.siret'=>"company",'s.siren'=>"company",'s.ape'=>"company",'s.idprof4'=>"company",'s.tva_intra'=>"company",'s.capital'=>"company",'s.note'=>"company",'t.libelle'=>"company",'ce.code'=>"company","cfj.libelle"=>"company",'s.fk_prospectlevel'=>'company','fk_stcomm'=>'company');
$this->import_fields_array[$r]=array('s.nom'=>"Name",'s.prefix_comm'=>"Prefix",'s.client'=>"Customer",'s.fournisseur'=>"Supplier",'s.datec'=>"DateCreation",'s.code_client'=>"CustomerCode",'s.code_fournisseur'=>"SupplierCode",'s.address'=>"Address",'s.cp'=>"Zip",'s.ville'=>"Town",'p.code'=>"CountryCode",'s.tel'=>"Phone",'s.fax'=>"Fax",'s.url'=>"Url",'s.email'=>"Email",'s.siret'=>"IdProf1",'s.siren'=>"IdProf2",'s.ape'=>"IdProf3",'s.idprof4'=>"IdProf4",'s.tva_intra'=>"VATIntraShort",'s.capital'=>"Capital",'s.note'=>"Note",'s.fk_type'=>"ThirdPartyType",'s.fk_effectif'=>"Effectif","s.fk_status"=>"JuridicalStatus",'s.fk_prospectlevel'=>'ProspectLevel','s.fk_stcomm'=>'ProspectStatus');
$this->import_entities_array[$r]=array('s.nom'=>"company",'s.prefix_comm'=>"company",'s.client'=>"company",'s.fournisseur'=>"company",'s.datec'=>"company",'s.code_client'=>"company",'s.code_fournisseur'=>"company",'s.address'=>"company",'s.cp'=>"company",'s.ville'=>"company",'p.code'=>"company",'s.tel'=>"company",'s.fax'=>"company",'s.url'=>"company",'s.email'=>"company",'s.siret'=>"company",'s.siren'=>"company",'s.ape'=>"company",'s.idprof4'=>"company",'s.tva_intra'=>"company",'s.capital'=>"company",'s.note'=>"company",'s.fk_type'=>"company",'s.fk_effectif'=>"company","s.fk_status"=>"company",'s.fk_prospectlevel'=>'company','s.fk_stcomm'=>'company');
$this->import_examplevalues_array[$r]=array('s.nom'=>"A company",'s.prefix_comm'=>"comp",'s.client'=>'0 or 1','s.fournisseur'=>'0 or 1','s.datec'=>dol_print_date(mktime(),'YYYY-MM-DD'),'s.code_client'=>"CU01-0001",'s.code_fournisseur'=>"SU01-0001",'s.address'=>"61 jump street",'s.cp'=>"123456",'s.ville'=>"Big town",'p.code'=>"US",'s.tel'=>"0101010101",'s.fax'=>"0101010102",'s.url'=>"http://mycompany.com",'s.email'=>"test@mycompany.com",'s.siret'=>"",'s.siren'=>"",'s.ape'=>"",'s.idprof4'=>"",'s.tva_intra'=>"FR0123456789",'s.capital'=>"10000",'s.note'=>"This is an example of note for record",'s.fk_type'=>"XXX",'s.fk_effectif'=>"YYY","s.fk_status"=>"ZZZ",'s.fk_prospectlevel'=>'AAA','s.fk_stcomm'=>'BBB');
$this->import_sql_start[$r]='INSERT INTO '.MAIN_DB_PREFIX.'societe as s';
$this->import_sql_end[$r] ='';
......
......@@ -62,4 +62,5 @@ DownloadEmptyExample=Download example of empty source file
ChooseFileToImport=Upload file then click on picto %s to select file as source import file...
FieldsInSourceFile=Fields in source file
FieldsInTargetDatabase=Target fields in Dolibarr database
NoFields=No fields
\ No newline at end of file
NoFields=No fields
MoveField=Move field column number %s
\ No newline at end of file
......@@ -62,4 +62,5 @@ ChooseFileToImport=Ajouter le fichier à importer puis cliquez sur le picto %s p
DownloadEmptyExample=Télécharger fichier vierge exemple
FieldsInSourceFile=Champs dans le fichier source
FieldsInTargetDatabase=Champs cibles dans la base Dolibarr
NoFields=Aucun champ
\ No newline at end of file
NoFields=Aucun champ
MoveField=Déplacer champ colonne numéro %s
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment