dolibarr 20.0.0
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 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
187
188 $selectlabel = array();
189 foreach ($array_selected_sorted as $code => $value) {
190 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
191 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
192
193 fwrite($this->handle, $newvalue.$this->separator);
194 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
195
196 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
197 $selectlabel[$code."_label"] = $newvalue."_label";
198 }
199 }
200
201 foreach ($selectlabel as $key => $value) {
202 fwrite($this->handle, $value.$this->separator);
203 }
204 fwrite($this->handle, "\n");
205 return 0;
206 }
207
208
209 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
219 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
220 {
221 // phpcs:enable
222 global $conf;
223
224 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
225
226 $this->col = 0;
227
228 $reg = array();
229 $selectlabelvalues = array();
230 foreach ($array_selected_sorted as $code => $value) {
231 if (strpos($code, ' as ') == 0) {
232 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
233 } else {
234 $alias = substr($code, strpos($code, ' as ') + 4);
235 }
236 if (empty($alias)) {
237 dol_print_error(null, 'Bad value for field with key='.$code.'. Try to redefine export.');
238 }
239
240 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
241 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
242
243 // Translation newvalue
244 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
245 $newvalue = $outputlangs->transnoentities($reg[1]);
246 }
247
248 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
249 $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
250
251 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
252 $array = jsonOrUnserialize($typefield);
253 if (is_array($array) && !empty($newvalue)) {
254 $array = $array['options'];
255 $selectlabelvalues[$code."_label"] = $array[$newvalue];
256 } else {
257 $selectlabelvalues[$code."_label"] = "";
258 }
259 }
260
261 fwrite($this->handle, $newvalue.$this->separator);
262 $this->col++;
263 }
264 foreach ($selectlabelvalues as $key => $value) {
265 fwrite($this->handle, $value.$this->separator);
266 $this->col++;
267 }
268
269 fwrite($this->handle, "\n");
270 return 0;
271 }
272
273 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
280 public function write_footer($outputlangs)
281 {
282 // phpcs:enable
283 return 0;
284 }
285
286 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
292 public function close_file()
293 {
294 // phpcs:enable
295 fclose($this->handle);
296 return 0;
297 }
298
299
309 public function csvClean($newvalue, $charset)
310 {
311 global $conf;
312 $addquote = 0;
313
314 // Rule Dolibarr: No HTML
315 //print $charset.' '.$newvalue."\n";
316 //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
317 $newvalue = dol_htmlcleanlastbr($newvalue);
318 //print $charset.' '.$newvalue."\n";
319
320 // 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)
321 $oldvalue = $newvalue;
322 $newvalue = str_replace("\r", '', $newvalue);
323 $newvalue = str_replace("\n", '\n', $newvalue);
324 if (getDolGlobalString('USE_STRICT_CSV_RULES') && $oldvalue != $newvalue) {
325 // If we must use enclusure on text with CR/LF)
326 if (getDolGlobalInt('USE_STRICT_CSV_RULES') == 1) {
327 // If we use strict CSV rules (original value must remain but we add quote)
328 $newvalue = $oldvalue;
329 }
330 $addquote = 1;
331 }
332
333 // Rule 2 CSV: If value contains ", we must escape with ", and add "
334 if (preg_match('/"/', $newvalue)) {
335 $addquote = 1;
336 $newvalue = str_replace('"', '""', $newvalue);
337 }
338
339 // Rule 3 CSV: If value contains separator, we must add "
340 if (preg_match('/'.$this->separator.'/', $newvalue)) {
341 $addquote = 1;
342 }
343
344 return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
345 }
346}
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.