From 15d7266abf468d637c46bd94334aadfdaa5ec84b Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Fri, 28 Sep 2012 10:23:04 +0200 Subject: [PATCH 1/2] import : add csv options (separator and enclosure) --- .../modules/import/import_csv.modules.php | 3 +- htdocs/imports/import.php | 61 +++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index dce9949a47c..0dc91744d1e 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -1,6 +1,7 @@ * Copyright (C) 2009-2012 Regis Houssin + * Copyright (C) 2012 Christophe Battarel * * 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 @@ -633,7 +634,7 @@ class ImportCsv extends ModeleImports */ function cleansep($value) { - return str_replace(',','/',$value); + return str_replace($this->separator,'/',$value); }; ?> diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index c79126cbed9..0312814a7fc 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -1,6 +1,7 @@ * Copyright (C) 2005-2009 Regis Houssin + * Copyright (C) 2012 Christophe Battarel * * 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 @@ -75,6 +76,8 @@ $import_name = GETPOST('import_name'); $hexa = GETPOST('hexa'); $importmodelid = GETPOST('importmodelid'); $excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 0); +$separator = (GETPOST('separator') ? GETPOST('separator') : (! empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE)?$conf->global->IMPORT_CSV_SEPARATOR_TO_USE:',')); +$enclosure = (GETPOST('enclosure') ? GETPOST('enclosure') : '"'); $objimport=new Import($db); $objimport->load_arrays($user,($step==1?'':$datatoimport)); @@ -324,6 +327,8 @@ if ($step == 1 || ! $datatoimport) $param=''; if ($excludefirstline) $param.='&excludefirstline=1'; + if ($separator) $param.='&separator='.urlencode($separator); + if ($enclosure) $param.='&enclosure='.urlencode($enclosure); llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones'); @@ -388,6 +393,8 @@ if ($step == 2 && $datatoimport) { $param='&datatoimport='.$datatoimport; if ($excludefirstline) $param.='&excludefirstline=1'; + if ($separator) $param.='&separator='.urlencode($separator); + if ($enclosure) $param.='&enclosure='.urlencode($enclosure); llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones'); @@ -459,6 +466,8 @@ if ($step == 3 && $datatoimport) { $param='&datatoimport='.$datatoimport.'&format='.$format; if ($excludefirstline) $param.='&excludefirstline=1'; + if ($separator) $param.='&separator='.urlencode($separator); + if ($enclosure) $param.='&enclosure='.urlencode($enclosure); $liste=$objmodelimport->liste_modeles($db); @@ -532,6 +541,8 @@ if ($step == 3 && $datatoimport) print ''; print ''; print ''; + print ''; + print ''; print ''; print "\n"; @@ -599,6 +610,10 @@ if ($step == 4 && $datatoimport) $classname = "Import".ucfirst($model); require_once $dir.$file; $obj = new $classname($db,$datatoimport); + if ($model == 'csv') { + $obj->separator = $separator; + $obj->enclosure = $enclosure; + } // Load source fields in input file $fieldssource=array(); @@ -667,6 +682,8 @@ if ($step == 4 && $datatoimport) $param='&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport); if ($excludefirstline) $param.='&excludefirstline=1'; + if ($separator) $param.='&separator='.urlencode($separator); + if ($enclosure) $param.='&enclosure='.urlencode($enclosure); llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones'); @@ -702,6 +719,25 @@ if ($step == 4 && $datatoimport) print $form->textwithpicto($objmodelimport->getDriverLabel($format),$text); print ''; + // Separator and enclosure + if ($model == 'csv') { + print ''.$langs->trans("CsvOptions").''; + print ''; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print $langs->trans("Separator").' : '; + print ''; + print '    '.$langs->trans("Enclosure").' : '; + print ''; + print ''; + print '
'; + print ''; + } + // File to import print ''.$langs->trans("FileToImport").''; print ''; @@ -726,6 +762,8 @@ if ($step == 4 && $datatoimport) print ''; print ''; print ''; + print ''; + print ''; print '
'; print $langs->trans("SelectImportFields",img_picto('','uparrow','')).' '; $htmlother->select_import_model($importmodelid,'importmodelid',$datatoimport,1); @@ -1002,6 +1040,9 @@ if ($step == 4 && $datatoimport) print ''; print ''; print ''; + print ''; + print ''; + print ''; print ''; print ''; @@ -1062,6 +1103,10 @@ if ($step == 5 && $datatoimport) $classname = "Import".ucfirst($model); require_once $dir.$file; $obj = new $classname($db,$datatoimport); + if ($model == 'csv') { + $obj->separator = $separator; + $obj->enclosure = $enclosure; + } // Load source fields in input file $fieldssource=array(); @@ -1082,9 +1127,11 @@ if ($step == 5 && $datatoimport) $nboflines=dol_count_nb_of_line($conf->import->dir_temp.'/'.$filetoimport); - $param='&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines; - $param2='&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines; - if ($excludefirstline) $param.='&excludefirstline=1'; + $param='&leftmenu=import&step=5&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines.'&separator='.urlencode($separator).'&enclosure='.urlencode($enclosure); + $param2 = $param; + if ($excludefirstline) { + $param.='&excludefirstline=1'; + } llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones'); @@ -1144,7 +1191,7 @@ if ($step == 5 && $datatoimport) print ''; @@ -1391,6 +1438,10 @@ if ($step == 6 && $datatoimport) $classname = "Import".ucfirst($model); require_once $dir.$file; $obj = new $classname($db,$datatoimport); + if ($model == 'csv') { + $obj->separator = $separator; + $obj->enclosure = $enclosure; + } // Load source fields in input file $fieldssource=array(); @@ -1413,6 +1464,8 @@ if ($step == 6 && $datatoimport) $param='&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines; if ($excludefirstline) $param.='&excludefirstline=1'; + if ($separator) $param.='&separator='.urlencode($separator); + if ($enclosure) $param.='&enclosure='.urlencode($enclosure); llxHeader('',$langs->trans("NewImport"),'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones'); From 9f15a11b100f54ce410ad1db5c30583b4956d6e6 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Fri, 28 Sep 2012 10:28:17 +0200 Subject: [PATCH 2/2] update lang files for import (csv options) --- htdocs/langs/en_US/exports.lang | 5 ++++- htdocs/langs/fr_FR/exports.lang | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index a9dcefd568e..02a04cb3c1a 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -115,4 +115,7 @@ CSVFormatDesc=Comma Separated Value file format (.csv).
This is a text Excel95FormatDesc=Excel file format (.xls)
This is native Excel 95 format (BIFF5). Excel2007FormatDesc=Excel file format (.xlsx)
This is native Excel 2007 format (SpreadsheetML). TsvFormatDesc=Tab Separated Value file format (.tsv)
This is a text file format where fields are separated by a tabulator [tab]. -ExportFieldAutomaticallyAdded=Field %s was automatically added. It will avoid you to have similar lines to be treated as duplicate records (with this field added, all ligne will own its own id and will differ). \ No newline at end of file +ExportFieldAutomaticallyAdded=Field %s was automatically added. It will avoid you to have similar lines to be treated as duplicate records (with this field added, all ligne will own its own id and will differ). +CsvOptions=Csv Options +Separator=Separator +Enclosure=Enclosure \ No newline at end of file diff --git a/htdocs/langs/fr_FR/exports.lang b/htdocs/langs/fr_FR/exports.lang index 19eb4fe9571..3c99687cd29 100644 --- a/htdocs/langs/fr_FR/exports.lang +++ b/htdocs/langs/fr_FR/exports.lang @@ -116,3 +116,6 @@ Excel95FormatDesc=Format Excel (.xls).
Format Excel 95 (BIFF5). Excel2007FormatDesc=Format Excel (.xls).
Format standard Excel 2007 (SpreadsheetML). TsvFormatDesc=Format de fichier à Valeurs Séparées par des Tabulations (.tsv).
C'est un fichier texte dont les champs sont séparés par des tabulations [tab]. ExportFieldAutomaticallyAdded=Le champ %s a été ajouté automatiquement car il évitera que des lignes identiques soient considérées comme des doublons (avec ce champ, aucune ligne ne sera identique mais aura un id propre). +CsvOptions=Options du fichier Csv +Separator=Séparateur +Enclosure=Délimiteur de champs
'; print ''; + print ' onClick="javascript: window.location=\''.$_SERVER["PHP_SELF"].'?excludefirstline='.($excludefirstline?'0':'1').$param2.'\';">'; print ' '.$langs->trans("DoNotImportFirstLine"); print '