dolibarr 23.0.3
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 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
215 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
216 $newvalue = csvClean($newvalue, getDolGlobalString('EXPORT_CSV_FORCE_CHARSET'), $this->separator);
217
218 fwrite($this->handle, $newvalue.$this->separator);
219 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
220
221 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
222 $selectlabel[$code."_label"] = $newvalue."_label";
223 }
224 }
225
226 foreach ($selectlabel as $key => $value) {
227 fwrite($this->handle, $value.$this->separator);
228 }
229 fwrite($this->handle, "\n");
230 return 0;
231 }
232
233
234 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
244 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
245 {
246 // phpcs:enable
247
248 $outputlangs->charset_output = getDolGlobalString('EXPORT_CSV_FORCE_CHARSET');
249
250 $this->col = 0;
251
252 $reg = array();
253 $selectlabelvalues = array();
254 foreach ($array_selected_sorted as $code => $value) {
255 if (strpos($code, ' as ') == 0) {
256 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
257 } else {
258 $alias = substr($code, strpos($code, ' as ') + 4);
259 }
260 if (empty($alias)) {
261 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
262 continue;
263 }
264
265 $newvalue = $objp->$alias;
266 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
267
268 // Translation newvalue
269 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
270 $newvalue = $outputlangs->transnoentities($reg[1]);
271 }
272
273 // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
274 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
275 $newvalue = csvClean($newvalue, $outputlangs->charset_output, $this->separator);
276
277 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
278 $array = jsonOrUnserialize($typefield);
279 if (is_array($array) && !empty($newvalue)) {
280 $array = $array['options'];
281 $selectlabelvalues[$code."_label"] = $array[$newvalue];
282 } else {
283 $selectlabelvalues[$code."_label"] = "";
284 }
285 }
286
287 fwrite($this->handle, $newvalue.$this->separator);
288 $this->col++;
289 }
290 foreach ($selectlabelvalues as $key => $value) {
291 fwrite($this->handle, $value.$this->separator);
292 $this->col++;
293 }
294
295 fwrite($this->handle, "\n");
296 return 0;
297 }
298
299 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
306 public function write_footer($outputlangs)
307 {
308 // phpcs:enable
309 return 0;
310 }
311
312 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
318 public function close_file()
319 {
320 // phpcs:enable
321 fclose($this->handle);
322 return 0;
323 }
324}
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.
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.