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 $id;
39
43 public $label;
44
45 public $extension;
46
51 public $version = 'dolibarr';
52
53 public $label_lib;
54
55 public $version_lib;
56
57 public $separator;
58
59 public $handle; // Handle fichier
60
66 public function getDriverId()
67 {
68 return $this->id;
69 }
70
76 public function getDriverLabel()
77 {
78 return $this->label;
79 }
80
86 public function getDriverDesc()
87 {
88 return $this->desc;
89 }
90
96 public function getDriverExtension()
97 {
98 return $this->extension;
99 }
100
106 public function getDriverVersion()
107 {
108 return $this->version;
109 }
110
116 public function getLibLabel()
117 {
118 return $this->label_lib;
119 }
120
126 public function getLibVersion()
127 {
128 return $this->version_lib;
129 }
130
131
132 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
140 public function open_file($file, $outputlangs)
141 {
142 // phpcs:enable
143 global $langs;
144
145 dol_syslog("ExportCsv::open_file file=".$file);
146
147 $ret = 1;
148
149 $outputlangs->load("exports");
150 $this->handle = fopen($file, "wt");
151 if (!$this->handle) {
152 $langs->load("errors");
153 $this->error = $langs->trans("ErrorFailToCreateFile", $file);
154 $ret = -1;
155 }
156
157 return $ret;
158 }
159
160 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
167 public function write_header($outputlangs)
168 {
169 // phpcs:enable
170 return 0;
171 }
172
173
174 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
184 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
185 {
186 // phpcs:enable
187 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
188
189 $selectlabel = array();
190 foreach ($array_selected_sorted as $code => $value) {
191 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
192 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
193
194 fwrite($this->handle, $newvalue.$this->separator);
195 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
196
197 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
198 $selectlabel[$code."_label"] = $newvalue."_label";
199 }
200 }
201
202 foreach ($selectlabel as $key => $value) {
203 fwrite($this->handle, $value.$this->separator);
204 }
205 fwrite($this->handle, "\n");
206 return 0;
207 }
208
209
210 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
220 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
221 {
222 // phpcs:enable
223 global $conf;
224
225 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
226
227 $this->col = 0;
228
229 $reg = array();
230 $selectlabelvalues = array();
231 foreach ($array_selected_sorted as $code => $value) {
232 if (strpos($code, ' as ') == 0) {
233 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
234 } else {
235 $alias = substr($code, strpos($code, ' as ') + 4);
236 }
237 if (empty($alias)) {
238 dol_print_error(null, 'Bad value for field with key='.$code.'. Try to redefine export.');
239 }
240
241 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
242 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
243
244 // Translation newvalue
245 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
246 $newvalue = $outputlangs->transnoentities($reg[1]);
247 }
248
249 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
250 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
251
252 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
253 $array = jsonOrUnserialize($typefield);
254 if (is_array($array) && !empty($newvalue)) {
255 $array = $array['options'];
256 $selectlabelvalues[$code."_label"] = $array[$newvalue];
257 } else {
258 $selectlabelvalues[$code."_label"] = "";
259 }
260 }
261
262 fwrite($this->handle, $newvalue.$this->separator);
263 $this->col++;
264 }
265 foreach ($selectlabelvalues as $key => $value) {
266 fwrite($this->handle, $value.$this->separator);
267 $this->col++;
268 }
269
270 fwrite($this->handle, "\n");
271 return 0;
272 }
273
274 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
281 public function write_footer($outputlangs)
282 {
283 // phpcs:enable
284 return 0;
285 }
286
287 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
293 public function close_file()
294 {
295 // phpcs:enable
296 fclose($this->handle);
297 return 0;
298 }
299
300
310 public function csvClean($newvalue, $charset)
311 {
312 global $conf;
313 $addquote = 0;
314
315 // Rule Dolibarr: No HTML
316 //print $charset.' '.$newvalue."\n";
317 //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
318 $newvalue = dol_htmlcleanlastbr($newvalue);
319 //print $charset.' '.$newvalue."\n";
320
321 // 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)
322 $oldvalue = $newvalue;
323 $newvalue = str_replace("\r", '', $newvalue);
324 $newvalue = str_replace("\n", '\n', $newvalue);
325 if (getDolGlobalString('USE_STRICT_CSV_RULES') && $oldvalue != $newvalue) {
326 // If we must use enclusure on text with CR/LF)
327 if (getDolGlobalInt('USE_STRICT_CSV_RULES') == 1) {
328 // If we use strict CSV rules (original value must remain but we add quote)
329 $newvalue = $oldvalue;
330 }
331 $addquote = 1;
332 }
333
334 // Rule 2 CSV: If value contains ", we must escape with ", and add "
335 if (preg_match('/"/', $newvalue)) {
336 $addquote = 1;
337 $newvalue = str_replace('"', '""', $newvalue);
338 }
339
340 // Rule 3 CSV: If value contains separator, we must add "
341 if (preg_match('/'.$this->separator.'/', $newvalue)) {
342 $addquote = 1;
343 }
344
345 return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
346 }
347}
Class to build export files with format CSV.
getLibLabel()
getLabelLabel
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.