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