dolibarr 19.0.4
exportcsv.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
25
26// avoid timeout for big export
27set_time_limit(0);
28
33{
37 public $id;
38
42 public $label;
43
44 public $extension;
45
50 public $version = 'dolibarr';
51
52 public $label_lib;
53
54 public $version_lib;
55
56 public $separator;
57
58 public $handle; // Handle fichier
59
65 public function getDriverId()
66 {
67 return $this->id;
68 }
69
75 public function getDriverLabel()
76 {
77 return $this->label;
78 }
79
85 public function getDriverDesc()
86 {
87 return $this->desc;
88 }
89
95 public function getDriverExtension()
96 {
97 return $this->extension;
98 }
99
105 public function getDriverVersion()
106 {
107 return $this->version;
108 }
109
115 public function getLibLabel()
116 {
117 return $this->label_lib;
118 }
119
125 public function getLibVersion()
126 {
127 return $this->version_lib;
128 }
129
130
131 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
139 public function open_file($file, $outputlangs)
140 {
141 // phpcs:enable
142 global $langs;
143
144 dol_syslog("ExportCsv::open_file file=".$file);
145
146 $ret = 1;
147
148 $outputlangs->load("exports");
149 $this->handle = fopen($file, "wt");
150 if (!$this->handle) {
151 $langs->load("errors");
152 $this->error = $langs->trans("ErrorFailToCreateFile", $file);
153 $ret = -1;
154 }
155
156 return $ret;
157 }
158
159 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
166 public function write_header($outputlangs)
167 {
168 // phpcs:enable
169 return 0;
170 }
171
172
173 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
183 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
184 {
185 // phpcs:enable
186 global $conf;
187
188 $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
189
190 $selectlabel = array();
191
192 foreach ($array_selected_sorted as $code => $value) {
193 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
194 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
195
196 fwrite($this->handle, $newvalue.$this->separator);
197 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
198
199 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
200 $selectlabel[$code."_label"] = $newvalue."_label";
201 }
202 }
203 foreach ($selectlabel as $key => $value) {
204 fwrite($this->handle, $value.$this->separator);
205 }
206 fwrite($this->handle, "\n");
207 return 0;
208 }
209
210
211 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
221 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
222 {
223 // phpcs:enable
224 global $conf;
225
226 $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
227
228 $this->col = 0;
229
230 $reg = array();
231 $selectlabelvalues = array();
232 foreach ($array_selected_sorted as $code => $value) {
233 if (strpos($code, ' as ') == 0) {
234 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
235 } else {
236 $alias = substr($code, strpos($code, ' as ') + 4);
237 }
238 if (empty($alias)) {
239 dol_print_error('', 'Bad value for field with key='.$code.'. Try to redefine export.');
240 }
241
242 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
243 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
244
245 // Translation newvalue
246 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
247 $newvalue = $outputlangs->transnoentities($reg[1]);
248 }
249
250 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
251 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
252
253 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
254 $array = jsonOrUnserialize($typefield);
255 if (is_array($array) && !empty($newvalue)) {
256 $array = $array['options'];
257 $selectlabelvalues[$code."_label"] = $array[$newvalue];
258 } else {
259 $selectlabelvalues[$code."_label"] = "";
260 }
261 }
262
263 fwrite($this->handle, $newvalue.$this->separator);
264 $this->col++;
265 }
266 foreach ($selectlabelvalues as $key => $value) {
267 fwrite($this->handle, $value.$this->separator);
268 $this->col++;
269 }
270
271 fwrite($this->handle, "\n");
272 return 0;
273 }
274
275 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
282 public function write_footer($outputlangs)
283 {
284 // phpcs:enable
285 return 0;
286 }
287
288 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
294 public function close_file()
295 {
296 // phpcs:enable
297 fclose($this->handle);
298 return 0;
299 }
300
301
311 public function csvClean($newvalue, $charset)
312 {
313 global $conf;
314 $addquote = 0;
315
316 // Rule Dolibarr: No HTML
317 //print $charset.' '.$newvalue."\n";
318 //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
319 $newvalue = dol_htmlcleanlastbr($newvalue);
320 //print $charset.' '.$newvalue."\n";
321
322 // 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)
323 $oldvalue = $newvalue;
324 $newvalue = str_replace("\r", '', $newvalue);
325 $newvalue = str_replace("\n", '\n', $newvalue);
326 if (getDolGlobalString('USE_STRICT_CSV_RULES') && $oldvalue != $newvalue) {
327 // If we must use enclusure on text with CR/LF)
328 if (getDolGlobalInt('USE_STRICT_CSV_RULES') == 1) {
329 // If we use strict CSV rules (original value must remain but we add quote)
330 $newvalue = $oldvalue;
331 }
332 $addquote = 1;
333 }
334
335 // Rule 2 CSV: If value contains ", we must escape with ", and add "
336 if (preg_match('/"/', $newvalue)) {
337 $addquote = 1;
338 $newvalue = str_replace('"', '""', $newvalue);
339 }
340
341 // Rule 3 CSV: If value contains separator, we must add "
342 if (preg_match('/'.$this->separator.'/', $newvalue)) {
343 $addquote = 1;
344 }
345
346 return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
347 }
348}
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.