dolibarr 25.0.0-alpha
import_csv.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
5 * Copyright (C) 2012-2016 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * or see https://www.gnu.org/
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.class.php';
32
33
38{
42 public $db;
43
47 public $id;
48
53 public $version = 'dolibarr';
54
58 public $label_lib;
59
63 public $version_lib;
64
68 public $separator;
69
73 public $file; // Path of file
74
78 public $handle; // File handle
79
80 public $cacheconvert = array(); // Array to cache list of value found after a conversion
81
82 public $cachefieldtable = array(); // Array to cache list of value found into fields@tables
83
84 public $nbinsert = 0; // # of insert done during the import
85
86 public $nbupdate = 0; // # of update done during the import
87
88 public $charset = '';
89
93 public $col;
94
95
102 public function __construct($db, $datatoimport)
103 {
104 global $langs;
105
106 parent::__construct();
107 $this->db = $db;
108
109 $this->separator = (GETPOST('separator') ? GETPOST('separator') : getDolGlobalString('IMPORT_CSV_SEPARATOR_TO_USE', ','));
110 $this->enclosure = '"';
111 $this->escape = '"';
112
113 $this->id = 'csv'; // Same value then xxx in file name export_xxx.modules.php
114 $this->label = 'Csv'; // Label of driver
115 $this->desc = $langs->trans("CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
116 $this->extension = 'csv'; // Extension for generated file by this driver
117 $this->picto = 'mime/other'; // Picto
118 $this->version = '1.34'; // Driver version
119 $this->phpmin = array(7, 0); // Minimum version of PHP required by module
120
121 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
122 if (versioncompare($this->phpmin, versionphparray()) > 0) {
123 dol_syslog("Module need a higher PHP version");
124 $this->error = "Module need a higher PHP version";
125 return;
126 }
127
128 // If driver use an external library, put its name here
129 $this->label_lib = 'Dolibarr';
130 $this->version_lib = DOL_VERSION;
131
132 $this->datatoimport = $datatoimport;
133 if (preg_match('/^societe_/', $datatoimport)) {
134 $this->thirdpartyobject = new Societe($this->db);
135 }
136 }
137
138
139 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
146 public function write_header_example($outputlangs)
147 {
148 // phpcs:enable
149 return '';
150 }
151
152 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
160 public function write_title_example($outputlangs, $headerlinefields)
161 {
162 // phpcs:enable
163 $s = implode($this->separator, array_map('cleansep', $headerlinefields));
164 return $s."\n";
165 }
166
167 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
175 public function write_record_example($outputlangs, $contentlinevalues)
176 {
177 // phpcs:enable
178 $s = implode($this->separator, array_map('cleansep', $contentlinevalues));
179 return $s."\n";
180 }
181
182 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
189 public function write_footer_example($outputlangs)
190 {
191 // phpcs:enable
192 return '';
193 }
194
195
196 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
203 public function import_open_file($file)
204 {
205 // phpcs:enable
206 global $langs;
207 $ret = 1;
208
209 dol_syslog(get_class($this)."::open_file file=".$file);
210
211 // fgetcsv() only accepts one byte for the separator and for the enclosure (it throws a ValueError otherwise)
212 foreach (array('separator' => $this->separator, 'enclosure' => $this->enclosure) as $name => $value) {
213 if (strlen((string) $value) != 1) {
214 $langs->load("errors");
215 $this->error = $langs->trans("ErrorBadValueForParameter", (string) $value, $name);
216 return -1;
217 }
218 }
219
220 $handle = fopen(dol_osencode($file), "r");
221 if (!$handle) {
222 $langs->load("errors");
223 $this->error = $langs->trans("ErrorFailToOpenFile", $file);
224 $ret = -1;
225 } else {
226 // Skip the UTF-8 BOM that Excel adds at the beginning of a file, or it would be part of the first value
227 if (fread($handle, 3) !== "\xEF\xBB\xBF") {
228 rewind($handle);
229 }
230 $this->handle = $handle;
231 $this->file = $file;
232 }
233
234 return $ret;
235 }
236
237
238 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245 public function import_get_nb_of_lines($file)
246 {
247 // phpcs:enable
248 return dol_count_nb_of_line($file);
249 }
250
251
252 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
258 public function import_read_header()
259 {
260 // phpcs:enable
261 return 0;
262 }
263
264
265 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271 public function import_read_record()
272 {
273 // phpcs:enable
274 $arrayres = fgetcsv($this->handle, 0, $this->separator, $this->enclosure, $this->escape);
275
276 // End of file
277 if ($arrayres === false) {
278 return false;
279 }
280
281 //var_dump($this->handle);
282 //var_dump($arrayres);exit;
283 $newarrayres = array();
284 $key = 1; // Default value to ensure $key is declared
285
286 if ($arrayres && is_array($arrayres)) {
287 foreach ($arrayres as $key => $val) {
288 if (getDolGlobalString('IMPORT_CSV_FORCE_CHARSET')) { // Forced charset
289 if (strtolower(getDolGlobalString('IMPORT_CSV_FORCE_CHARSET')) == 'utf8') {
290 $newarrayres[$key]['val'] = $val;
291 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
292 } else {
293 $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
294 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
295 }
296 } else { // Autodetect format (UTF8 or ISO)
297 if (utf8_check($val)) {
298 $newarrayres[$key]['val'] = $val;
299 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
300 } else {
301 $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
302 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
303 }
304 }
305
306 $newarrayres[$key]['val'] = trim($newarrayres[$key]['val']);
307 }
308
309 $this->col = count($newarrayres);
310 }
311
312 return $newarrayres;
313 }
314
315 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
321 public function import_close_file()
322 {
323 // phpcs:enable
324 fclose($this->handle);
325 return 0;
326 }
327
328
329 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
341 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
342 {
343 // phpcs:enable
344 return $this->commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, 0);
345 }
346}
347
354function cleansep($value)
355{
356 return str_replace(array(',', ';'), '/', $value);
357}
versionphparray()
Return version PHP.
versioncompare($versionarray1, $versionarray2)
Compare 2 versions (stored into 2 arrays), to know if a version (a,b,c) is lower than (x,...
Definition admin.lib.php:79
Class to import CSV files.
write_header_example($outputlangs)
Output header of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
__construct($db, $datatoimport)
Constructor.
import_read_record()
Return array of next record in input file.
write_record_example($outputlangs, $contentlinevalues)
Output record of an example file for this format.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_close_file()
Close file handle.
import_read_header()
Input header line from file.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
import_open_file($file)
Open input file.
Parent class for import file readers.
commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, $recordpositionbase=0)
Shared implementation of import_insert for CSV/XLSX.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_count_nb_of_line($file)
Count number of lines in a file.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
cleansep($value)
Clean a string from separator.
$conf browser version
Definition main.inc.php:229