dolibarr 24.0.0-beta
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 ini_set('auto_detect_line_endings', 1); // For MAC compatibility
212
213 $handle = fopen(dol_osencode($file), "r");
214 if (!$handle) {
215 $langs->load("errors");
216 $this->error = $langs->trans("ErrorFailToOpenFile", $file);
217 $ret = -1;
218 } else {
219 $this->handle = $handle;
220 $this->file = $file;
221 }
222
223 return $ret;
224 }
225
226
227 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
234 public function import_get_nb_of_lines($file)
235 {
236 // phpcs:enable
237 return dol_count_nb_of_line($file);
238 }
239
240
241 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
247 public function import_read_header()
248 {
249 // phpcs:enable
250 return 0;
251 }
252
253
254 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
260 public function import_read_record()
261 {
262 // phpcs:enable
263 $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
264
265 // End of file
266 if ($arrayres === false) {
267 return false;
268 }
269
270 //var_dump($this->handle);
271 //var_dump($arrayres);exit;
272 $newarrayres = array();
273 $key = 1; // Default value to ensure $key is declared
274
275 if ($arrayres && is_array($arrayres)) {
276 foreach ($arrayres as $key => $val) {
277 if (getDolGlobalString('IMPORT_CSV_FORCE_CHARSET')) { // Forced charset
278 if (strtolower(getDolGlobalString('IMPORT_CSV_FORCE_CHARSET')) == 'utf8') {
279 $newarrayres[$key]['val'] = $val;
280 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
281 } else {
282 $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
283 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
284 }
285 } else { // Autodetect format (UTF8 or ISO)
286 if (utf8_check($val)) {
287 $newarrayres[$key]['val'] = $val;
288 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
289 } else {
290 $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
291 $newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it's null
292 }
293 }
294
295 $newarrayres[$key]['val'] = trim($newarrayres[$key]['val']);
296 }
297
298 $this->col = count($newarrayres);
299 }
300
301 return $newarrayres;
302 }
303
304 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
310 public function import_close_file()
311 {
312 // phpcs:enable
313 fclose($this->handle);
314 return 0;
315 }
316
317
318 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
330 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
331 {
332 // phpcs:enable
333 return $this->commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, 0);
334 }
335}
336
343function cleansep($value)
344{
345 return str_replace(array(',', ';'), '/', $value);
346}
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:72
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_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=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.