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