dolibarr 21.0.0-alpha
exportcsv.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
26
27// avoid timeout for big export
28set_time_limit(0);
29
34{
38 public $label;
39
43 public $extension;
44
49 public $version = 'dolibarr';
50
54 public $label_lib;
55
59 public $version_lib;
60
64 public $separator;
65
69 public $handle; // Handle fichier
70
76 public function getDriverId()
77 {
78 return $this->id;
79 }
80
86 public function getDriverLabel()
87 {
88 return $this->label;
89 }
90
96 public function getDriverDesc()
97 {
98 return $this->desc;
99 }
100
106 public function getDriverExtension()
107 {
108 return $this->extension;
109 }
110
116 public function getDriverVersion()
117 {
118 return $this->version;
119 }
120
126 public function getLibLabel()
127 {
128 return $this->label_lib;
129 }
130
136 public function getLibVersion()
137 {
138 return $this->version_lib;
139 }
140
141
142 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
150 public function open_file($file, $outputlangs)
151 {
152 // phpcs:enable
153 global $langs;
154
155 dol_syslog("ExportCsv::open_file file=".$file);
156
157 $ret = 1;
158
159 $outputlangs->load("exports");
160 $this->handle = fopen($file, "wt");
161 if (!$this->handle) {
162 $langs->load("errors");
163 $this->error = $langs->trans("ErrorFailToCreateFile", $file);
164 $ret = -1;
165 }
166
167 return $ret;
168 }
169
170 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
177 public function write_header($outputlangs)
178 {
179 // phpcs:enable
180 return 0;
181 }
182
183
184 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
194 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
195 {
196 // phpcs:enable
197 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
198
199 $selectlabel = array();
200 foreach ($array_selected_sorted as $code => $value) {
201 if (strpos($code, ' as ') == 0) {
202 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
203 } else {
204 $alias = substr($code, strpos($code, ' as ') + 4);
205 }
206 if (empty($alias)) {
207 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
208 continue;
209 }
210
211 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
212 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
213
214 fwrite($this->handle, $newvalue.$this->separator);
215 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
216
217 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
218 $selectlabel[$code."_label"] = $newvalue."_label";
219 }
220 }
221
222 foreach ($selectlabel as $key => $value) {
223 fwrite($this->handle, $value.$this->separator);
224 }
225 fwrite($this->handle, "\n");
226 return 0;
227 }
228
229
230 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
240 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
241 {
242 // phpcs:enable
243
244 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
245
246 $this->col = 0;
247
248 $reg = array();
249 $selectlabelvalues = array();
250 foreach ($array_selected_sorted as $code => $value) {
251 if (strpos($code, ' as ') == 0) {
252 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
253 } else {
254 $alias = substr($code, strpos($code, ' as ') + 4);
255 }
256 if (empty($alias)) {
257 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
258 continue;
259 }
260
261 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
262 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
263
264 // Translation newvalue
265 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
266 $newvalue = $outputlangs->transnoentities($reg[1]);
267 }
268
269 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
270 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
271
272 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
273 $array = jsonOrUnserialize($typefield);
274 if (is_array($array) && !empty($newvalue)) {
275 $array = $array['options'];
276 $selectlabelvalues[$code."_label"] = $array[$newvalue];
277 } else {
278 $selectlabelvalues[$code."_label"] = "";
279 }
280 }
281
282 fwrite($this->handle, $newvalue.$this->separator);
283 $this->col++;
284 }
285 foreach ($selectlabelvalues as $key => $value) {
286 fwrite($this->handle, $value.$this->separator);
287 $this->col++;
288 }
289
290 fwrite($this->handle, "\n");
291 return 0;
292 }
293
294 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
301 public function write_footer($outputlangs)
302 {
303 // phpcs:enable
304 return 0;
305 }
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
313 public function close_file()
314 {
315 // phpcs:enable
316 fclose($this->handle);
317 return 0;
318 }
319
320
330 public function csvClean($newvalue, $charset)
331 {
332 $addquote = 0;
333
334 // Rule Dolibarr: No HTML
335 //print $charset.' '.$newvalue."\n";
336 //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
337 $newvalue = dol_htmlcleanlastbr($newvalue);
338 //print $charset.' '.$newvalue."\n";
339
340 // Rule 1 CSV: No CR, LF in cells (except if USE_STRICT_CSV_RULES is 1, we can keep record as it is but we must add quotes)
341 $oldvalue = $newvalue;
342 $newvalue = str_replace("\r", '', $newvalue);
343 $newvalue = str_replace("\n", '\n', $newvalue);
344 if (getDolGlobalString('USE_STRICT_CSV_RULES') && $oldvalue != $newvalue) {
345 // If we must use enclusure on text with CR/LF)
346 if (getDolGlobalInt('USE_STRICT_CSV_RULES') == 1) {
347 // If we use strict CSV rules (original value must remain but we add quote)
348 $newvalue = $oldvalue;
349 }
350 $addquote = 1;
351 }
352
353 // Rule 2 CSV: If value contains ", we must escape with ", and add "
354 if (preg_match('/"/', $newvalue)) {
355 $addquote = 1;
356 $newvalue = str_replace('"', '""', $newvalue);
357 }
358
359 // Rule 3 CSV: If value contains separator, we must add "
360 if (preg_match('/'.$this->separator.'/', $newvalue)) {
361 $addquote = 1;
362 }
363
364 return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
365 }
366}
Class to build export files with format CSV.
getLibLabel()
getLibLabel
getDriverId()
getDriverId
csvClean($newvalue, $charset)
Clean a cell to respect rules of CSV file cells Note: It uses $this->separator Note: We keep this fun...
open_file($file, $outputlangs)
Open output file.
getDriverExtension()
getDriverExtension
write_footer($outputlangs)
Output footer into file.
getDriverVersion()
getDriverVersion
write_header($outputlangs)
Output header into file.
getLibVersion()
getLibVersion
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
close_file()
Close file handle.
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
getDriverLabel()
getDriverLabel
getDriverDesc()
getDriverDesc
Parent class for export modules.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
jsonOrUnserialize($stringtodecode)
Decode an encode string.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.