Fix: Import example file was wrong

This commit is contained in:
Laurent Destailleur 2011-06-07 10:36:30 +00:00
parent 8fe14361c3
commit 777c1ff575
2 changed files with 215 additions and 212 deletions

View File

@ -1,22 +1,22 @@
<?php <?php
/* Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009-2010 Regis Houssin <regis@dolibarr.fr> * Copyright (C) 2009-2010 Regis Houssin <regis@dolibarr.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* or see http://www.gnu.org/ * or see http://www.gnu.org/
*/ */
/** /**
* \file htdocs/includes/modules/import/import_csv.modules.php * \file htdocs/includes/modules/import/import_csv.modules.php
@ -35,150 +35,144 @@ require_once(DOL_DOCUMENT_ROOT ."/includes/modules/import/modules_import.php");
*/ */
class ImportCsv extends ModeleImports class ImportCsv extends ModeleImports
{ {
var $id; var $id;
var $error; var $error;
var $errors=array(); var $errors=array();
var $label; var $label;
var $extension; var $extension;
var $version; var $version;
var $label_lib; var $label_lib;
var $version_lib; var $version_lib;
var $separator; var $separator;
var $handle; // Handle fichier var $handle; // Handle fichier
var $cachefieldtable=array(); // Array to cache list of value into fields@tables var $cachefieldtable=array(); // Array to cache list of value into fields@tables
/** /**
* \brief Constructeur * \brief Constructeur
* \param db Handler acces base de donnee * \param db Handler acces base de donnee
*/ */
function ImportCsv($db) function ImportCsv($db)
{ {
global $conf,$langs; global $conf,$langs;
$this->db = $db; $this->db = $db;
$this->separator=','; $this->separator=','; // Change also function cleansep
if (! empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->IMPORT_CSV_SEPARATOR_TO_USE; if (! empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->IMPORT_CSV_SEPARATOR_TO_USE;
$this->enclosure='"'; $this->enclosure='"';
$this->escape='"'; $this->escape='"';
$this->id='csv'; // Same value then xxx in file name export_xxx.modules.php $this->id='csv'; // Same value then xxx in file name export_xxx.modules.php
$this->label='Csv'; // Label of driver $this->label='Csv'; // Label of driver
$this->desc=$langs->trans("CSVFormatDesc",$this->separator,$this->enclosure,$this->escape); $this->desc=$langs->trans("CSVFormatDesc",$this->separator,$this->enclosure,$this->escape);
$this->extension='csv'; // Extension for generated file by this driver $this->extension='csv'; // Extension for generated file by this driver
$this->picto='mime/other'; // Picto $this->picto='mime/other'; // Picto
$ver=explode(' ','$Revision$'); $ver=explode(' ','$Revision$');
$this->version=$ver[2]; // Driver version $this->version=$ver[2]; // Driver version
// If driver use an external library, put its name here // If driver use an external library, put its name here
$this->label_lib='Dolibarr'; $this->label_lib='Dolibarr';
$this->version_lib=DOL_VERSION; $this->version_lib=DOL_VERSION;
} }
function getDriverId() function getDriverId()
{ {
return $this->id; return $this->id;
} }
function getDriverLabel() function getDriverLabel()
{ {
return $this->label; return $this->label;
} }
function getDriverDesc() function getDriverDesc()
{ {
return $this->desc; return $this->desc;
} }
function getDriverExtension() function getDriverExtension()
{ {
return $this->extension; return $this->extension;
} }
function getDriverVersion() function getDriverVersion()
{ {
return $this->version; return $this->version;
} }
function getLibLabel() function getLibLabel()
{ {
return $this->label_lib; return $this->label_lib;
} }
function getLibVersion() function getLibVersion()
{ {
return $this->version_lib; return $this->version_lib;
} }
/** /**
* Output header of an example file for this format * Output header of an example file for this format
* @param outputlangs Output language * @param outputlangs Output language
*/ */
function write_header_example($outputlangs) function write_header_example($outputlangs)
{ {
return ''; return '';
} }
/** /**
* Output title line of an example file for this format * Output title line of an example file for this format
* @param outputlangs Output language * @param outputlangs Output language
*/ */
function write_title_example($outputlangs,$headerlinefields) function write_title_example($outputlangs,$headerlinefields)
{ {
$func = function($value) { $s.=join($this->separator,array_map('cleansep',$headerlinefields));
return str_replace($this->separator,'/',$value); return $s."\n";
}; }
$s.=join($this->separator,array_map($headerlinefields));
return $s."\n";
}
/** /**
* Output record of an example file for this format * Output record of an example file for this format
* @param outputlangs Output language * @param outputlangs Output language
*/ */
function write_record_example($outputlangs,$contentlinevalues) function write_record_example($outputlangs,$contentlinevalues)
{ {
$func = function($value) { $s=join($this->separator,array_map('cleansep',$contentlinevalues));
return str_replace($this->separator,'/',$value); return $s."\n";
}; }
$s=join($this->separator,array_map($contentlinevalues));
return $s."\n";
}
/** /**
* Output footer of an example file for this format * Output footer of an example file for this format
* @param outputlangs Output language * @param outputlangs Output language
*/ */
function write_footer_example($outputlangs) function write_footer_example($outputlangs)
{ {
return ''; return '';
} }
/** /**
* Open input file * Open input file
* @param file Path of filename * @param file Path of filename
* @return int <0 if KO, >=0 if OK * @return int <0 if KO, >=0 if OK
*/ */
function import_open_file($file) function import_open_file($file)
{ {
global $langs; global $langs;
$ret=1; $ret=1;
dol_syslog("ImportCsv::open_file file=".$file); dol_syslog("ImportCsv::open_file file=".$file);
ini_set('auto_detect_line_endings',1); // For MAC compatibility ini_set('auto_detect_line_endings',1); // For MAC compatibility
$newfile=utf8_check($file)?utf8_decode($file):$file; // fopen need ISO file name $newfile=utf8_check($file)?utf8_decode($file):$file; // fopen need ISO file name
$this->handle = fopen($newfile, "r"); $this->handle = fopen($newfile, "r");
if (! $this->handle) if (! $this->handle)
{ {
$langs->load("errors"); $langs->load("errors");
$this->error=$langs->trans("ErrorFailToOpenFile",$file); $this->error=$langs->trans("ErrorFailToOpenFile",$file);
@ -190,15 +184,15 @@ class ImportCsv extends ModeleImports
} }
return $ret; return $ret;
} }
/** /**
* \brief Input header line from file * \brief Input header line from file
*/ */
function import_read_header() function import_read_header()
{ {
return 0; return 0;
} }
/** /**
@ -206,42 +200,42 @@ class ImportCsv extends ModeleImports
* \return Array Array of field values. Data are UTF8 encoded. * \return Array Array of field values. Data are UTF8 encoded.
* [0] => (['val']=>val, ['type']=>-1=null,0=blank,1=string) * [0] => (['val']=>val, ['type']=>-1=null,0=blank,1=string)
*/ */
function import_read_record() function import_read_record()
{ {
global $conf; global $conf;
$arrayres=array(); $arrayres=array();
if (version_compare(phpversion(), '5.3') < 0) if (version_compare(phpversion(), '5.3') < 0)
{ {
$arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure); $arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure);
} }
else else
{ {
$arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure,$this->escape); $arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure,$this->escape);
} }
//var_dump($this->handle); //var_dump($this->handle);
//var_dump($arrayres);exit; //var_dump($arrayres);exit;
$newarrayres=array(); $newarrayres=array();
if ($arrayres && is_array($arrayres)) if ($arrayres && is_array($arrayres))
{ {
foreach($arrayres as $key => $val) foreach($arrayres as $key => $val)
{ {
if (! empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) // Forced charset if (! empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) // Forced charset
{ {
if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8') if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8')
{ {
$newarrayres[$key]['val']=$val; $newarrayres[$key]['val']=$val;
$newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null $newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null
} }
else else
{ {
$newarrayres[$key]['val']=utf8_encode($val); $newarrayres[$key]['val']=utf8_encode($val);
$newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null $newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null
} }
} }
else // Autodetect format (UTF8 or ISO) else // Autodetect format (UTF8 or ISO)
{ {
if (utf8_check($val)) if (utf8_check($val))
{ {
$newarrayres[$key]['val']=$val; $newarrayres[$key]['val']=$val;
@ -252,54 +246,54 @@ class ImportCsv extends ModeleImports
$newarrayres[$key]['val']=utf8_encode($val); $newarrayres[$key]['val']=utf8_encode($val);
$newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null $newarrayres[$key]['type']=(dol_strlen($val)?1:-1); // If empty we considere it's null
} }
} }
} }
$this->col=sizeof($newarrayres); $this->col=sizeof($newarrayres);
} }
return $newarrayres; return $newarrayres;
} }
/** /**
* \brief Close file handle * \brief Close file handle
*/ */
function import_close_file() function import_close_file()
{ {
fclose($this->handle); fclose($this->handle);
return 0; return 0;
} }
/** /**
* Insert a record into database * Insert a record into database
* @param arrayrecord Array of field values * @param arrayrecord Array of field values
* @param array_match_file_to_database * @param array_match_file_to_database
* @param objimport * @param objimport
* @param maxfields Max number of fiels to use * @param maxfields Max number of fiels to use
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid) function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid)
{ {
global $langs,$conf,$user; global $langs,$conf,$user;
$error=0; $error=0;
$warning=0; $warning=0;
$this->errors=array(); $this->errors=array();
$this->warnings=array(); $this->warnings=array();
//dol_syslog("import_csv.modules maxfields=".$maxfields." importid=".$importid); //dol_syslog("import_csv.modules maxfields=".$maxfields." importid=".$importid);
//var_dump($array_match_file_to_database); //var_dump($array_match_file_to_database);
//var_dump($arrayrecord); //var_dump($arrayrecord);
$array_match_database_to_file=array_flip($array_match_file_to_database); $array_match_database_to_file=array_flip($array_match_file_to_database);
$sort_array_match_file_to_database=$array_match_file_to_database; $sort_array_match_file_to_database=$array_match_file_to_database;
ksort($sort_array_match_file_to_database); ksort($sort_array_match_file_to_database);
//var_dump($sort_array_match_file_to_database); //var_dump($sort_array_match_file_to_database);
if (sizeof($arrayrecord) == 0 || if (sizeof($arrayrecord) == 0 ||
(sizeof($arrayrecord) == 1 && empty($arrayrecord[0]['val']))) (sizeof($arrayrecord) == 1 && empty($arrayrecord[0]['val'])))
{ {
//print 'W'; //print 'W';
$this->warnings[$warning]['lib']=$langs->trans('EmptyLine'); $this->warnings[$warning]['lib']=$langs->trans('EmptyLine');
@ -353,53 +347,53 @@ class ImportCsv extends ModeleImports
else { else {
if (! empty($objimport->array_import_regex[0][$val])) if (! empty($objimport->array_import_regex[0][$val]))
{ {
// If test is "Must exist in a field@table" // If test is "Must exist in a field@table"
if (preg_match('/^(.*)@(.*)$/',$objimport->array_import_regex[0][$val],$reg)) if (preg_match('/^(.*)@(.*)$/',$objimport->array_import_regex[0][$val],$reg))
{ {
$field=$reg[1]; $field=$reg[1];
$table=$reg[2]; $table=$reg[2];
if (! is_array($this->cachefieldtable[$field.'@'.$table])) // If content of field@table no already loaded into cache if (! is_array($this->cachefieldtable[$field.'@'.$table])) // If content of field@table no already loaded into cache
{ {
$sql="SELECT ".$field." as aliasfield FROM ".$table; $sql="SELECT ".$field." as aliasfield FROM ".$table;
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
$num=$this->db->num_rows($resql); $num=$this->db->num_rows($resql);
$i=0; $i=0;
while ($i < $num) while ($i < $num)
{ {
$obj=$this->db->fetch_object($resql); $obj=$this->db->fetch_object($resql);
if ($obj) $this->cachefieldtable[$field.'@'.$table][]=$obj->aliasfield; if ($obj) $this->cachefieldtable[$field.'@'.$table][]=$obj->aliasfield;
$i++; $i++;
} }
} }
else else
{ {
dol_print_error($this->db); dol_print_error($this->db);
} }
} }
// Now we check in cache // Now we check in cache
if (! in_array($newval,$this->cachefieldtable[$field.'@'.$table])) if (! in_array($newval,$this->cachefieldtable[$field.'@'.$table]))
{ {
$this->errors[$error]['lib']=$langs->trans('ErrorFieldValueNotIn',$key,$newval,$field,$table); $this->errors[$error]['lib']=$langs->trans('ErrorFieldValueNotIn',$key,$newval,$field,$table);
$this->errors[$error]['type']='FOREIGNKEY'; $this->errors[$error]['type']='FOREIGNKEY';
$error++; $error++;
} }
} }
// If test is just a static regex // If test is just a static regex
else if (! preg_match('/'.$objimport->array_import_regex[0][$val].'/i',$newval)) else if (! preg_match('/'.$objimport->array_import_regex[0][$val].'/i',$newval))
{ {
$this->errors[$error]['lib']=$langs->trans('ErrorWrongValueForField',$key,$newval,$objimport->array_import_regex[0][$val]); $this->errors[$error]['lib']=$langs->trans('ErrorWrongValueForField',$key,$newval,$objimport->array_import_regex[0][$val]);
$this->errors[$error]['type']='REGEX'; $this->errors[$error]['type']='REGEX';
$errorforthistable++; $errorforthistable++;
$error++; $error++;
} }
} }
// Other tests // Other tests
// ... // ...
} }
} }
$i++; $i++;
@ -430,7 +424,7 @@ class ImportCsv extends ModeleImports
$sql.=') VALUES('.$listvalues.", '".$importid."'"; $sql.=') VALUES('.$listvalues.", '".$importid."'";
if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id; if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id;
$sql.=')'; $sql.=')';
dol_syslog("import_csv.modules sql=".$sql); dol_syslog("import_csv.modules sql=".$sql);
//print '> '.join(',',$arrayrecord); //print '> '.join(',',$arrayrecord);
//print 'sql='.$sql; //print 'sql='.$sql;
@ -466,4 +460,12 @@ class ImportCsv extends ModeleImports
} }
/**
* Clean a string from separator
*/
function cleansep($value)
{
return str_replace(',','/',$value);
};
?> ?>

View File

@ -289,8 +289,9 @@ ExportCardToFormat=Export card to format
ContactNotLinkedToCompany=Contact not linked to any third party ContactNotLinkedToCompany=Contact not linked to any third party
DolibarrLogin=Dolibarr login DolibarrLogin=Dolibarr login
NoDolibarrAccess=No Dolibarr access NoDolibarrAccess=No Dolibarr access
ExportDataset_company_1=Companies/foundations and properties ExportDataset_company_1=Third parties (Companies/foundations) and properties
ExportDataset_company_2=Contacts and properties ExportDataset_company_2=Contacts and properties
ImportDataset_company_1=Third parties (Companies/foundations) and properties
PriceLevel=Price level PriceLevel=Price level
DeliveriesAddress=Delivery addresses DeliveriesAddress=Delivery addresses
DeliveryAddress=Delivery address DeliveryAddress=Delivery address