dolibarr 21.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 * 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 = $array_export_fields_label[$code];
212
213 // Label may be stored HTML encoded (for example extrafield labels saved through Translate::trans()),
214 // so decode entities and remove HTML to keep the header consistent with the data cells.
215 $newvalue = dol_string_nohtmltag($newvalue);
216 $decodedlabel = $newvalue;
217
218 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
219 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
220 $newvalue = csvClean($newvalue, $outputlangs->charset_output, $this->separator);
221
222 fwrite($this->handle, $newvalue.$this->separator);
223 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
224
225 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
226 // Append the "_label" suffix before cleaning so the derived column stays valid CSV
227 // even when the decoded label contains the separator or a quote.
228 $selectlabel[$code."_label"] = csvClean($decodedlabel."_label", $outputlangs->charset_output, $this->separator);
229 }
230 }
231
232 foreach ($selectlabel as $key => $value) {
233 fwrite($this->handle, $value.$this->separator);
234 }
235 fwrite($this->handle, "\n");
236 return 0;
237 }
238
239
240 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
250 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
251 {
252 // phpcs:enable
253
254 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
255
256 $this->col = 0;
257
258 $reg = array();
259 $selectlabelvalues = array();
260 foreach ($array_selected_sorted as $code => $value) {
261 if (strpos($code, ' as ') == 0) {
262 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
263 } else {
264 $alias = substr($code, strpos($code, ' as ') + 4);
265 }
266 if (empty($alias)) {
267 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
268 continue;
269 }
270
271 $newvalue = $objp->$alias;
272 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
273
274 // Translation newvalue
275 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
276 $newvalue = $outputlangs->transnoentities($reg[1]);
277 }
278
279 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
280 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
281 $newvalue = csvClean($newvalue, $outputlangs->charset_output, $this->separator);
282
283 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
284 $array = jsonOrUnserialize($typefield);
285 if (is_array($array) && !empty($newvalue)) {
286 $array = $array['options'];
287 $selectlabelvalues[$code."_label"] = $array[$newvalue];
288 } else {
289 $selectlabelvalues[$code."_label"] = "";
290 }
291 }
292
293 fwrite($this->handle, $newvalue.$this->separator);
294 $this->col++;
295 }
296 foreach ($selectlabelvalues as $key => $value) {
297 fwrite($this->handle, $value.$this->separator);
298 $this->col++;
299 }
300
301 fwrite($this->handle, "\n");
302 return 0;
303 }
304
305 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
312 public function write_footer($outputlangs)
313 {
314 // phpcs:enable
315 return 0;
316 }
317
318 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
324 public function close_file()
325 {
326 // phpcs:enable
327 fclose($this->handle);
328 return 0;
329 }
330}
Class to build export files with format CSV.
getLibLabel()
getLibLabel
getDriverId()
getDriverId
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.
csvClean($newvalue, $charset='', $separator='')
Clean a cell to respect rules of CSV file cells.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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.