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