dolibarr 23.0.3
export_tsv.modules.php
1<?php
2/* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
26require_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
27
28// avoid timeout for big export
29set_time_limit(0);
30
35{
39 public $label;
40
44 public $extension;
45
50 public $version = 'dolibarr';
51
55 public $label_lib;
56
60 public $version_lib;
61
65 public $separator = "\t";
66
70 public $handle;
71
72
78 public function __construct($db)
79 {
80 global $langs;
81 $this->db = $db;
82
83 $this->id = 'tsv'; // Same value then xxx in file name export_xxx.modules.php
84 $this->label = 'TSV'; // Label of driver
85 $this->desc = $langs->trans('TsvFormatDesc');
86 $this->extension = 'tsv'; // Extension for generated file by this driver
87 $this->picto = 'mime/other'; // Picto
88 $this->version = '1.15'; // Driver version
89
90 // If driver use an external library, put its name here
91 $this->label_lib = 'Dolibarr';
92 $this->version_lib = DOL_VERSION;
93 }
94
100 public function getDriverId()
101 {
102 return $this->id;
103 }
104
110 public function getDriverLabel()
111 {
112 return $this->label;
113 }
114
120 public function getDriverDesc()
121 {
122 return $this->desc;
123 }
124
130 public function getDriverExtension()
131 {
132 return $this->extension;
133 }
134
140 public function getDriverVersion()
141 {
142 return $this->version;
143 }
144
150 public function getLibLabel()
151 {
152 return $this->label_lib;
153 }
154
160 public function getLibVersion()
161 {
162 return $this->version_lib;
163 }
164
165
166 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
174 public function open_file($file, $outputlangs)
175 {
176 // phpcs:enable
177 global $langs;
178
179 dol_syslog("ExportTsv::open_file file=".$file);
180
181 $ret = 1;
182
183 $outputlangs->load("exports");
184 $this->handle = fopen($file, "wt");
185 if (!$this->handle) {
186 $langs->load("errors");
187 $this->error = $langs->trans("ErrorFailToCreateFile", $file);
188 $ret = -1;
189 }
190
191 return $ret;
192 }
193
194 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
201 public function write_header($outputlangs)
202 {
203 // phpcs:enable
204 return 0;
205 }
206
207
208 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
218 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
219 {
220 // phpcs:enable
221 $selectlabel = array();
222 foreach ($array_selected_sorted as $code => $value) {
223 if (strpos($code, ' as ') == 0) {
224 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
225 } else {
226 $alias = substr($code, strpos($code, ' as ') + 4);
227 }
228 if (empty($alias)) {
229 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
230 continue;
231 }
232
233 $newvalue = $array_export_fields_label[$code];
234 if ($newvalue) {
235 $newvalue = $outputlangs->transnoentitiesnoconv($newvalue);
236 }
237
238 $newvalue = $this->tsv_clean($newvalue, getDolGlobalString('EXPORT_TSV_FORCE_CHARSET'));
239
240 fwrite($this->handle, $newvalue.$this->separator);
241 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
242
243 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
244 $selectlabel[$code."_label"] = $newvalue."_label";
245 }
246 }
247 foreach ($selectlabel as $key => $value) {
248 fwrite($this->handle, $value.$this->separator);
249 }
250 fwrite($this->handle, "\n");
251 return 0;
252 }
253
254
255 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
265 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
266 {
267 // phpcs:enable
268
269 $outputlangs->charset_output = getDolGlobalString('EXPORT_TSV_FORCE_CHARSET');
270
271 $this->col = 0;
272
273 $reg = array();
274 $selectlabelvalues = array();
275 foreach ($array_selected_sorted as $code => $value) {
276 if (strpos($code, ' as ') == 0) {
277 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
278 } else {
279 $alias = substr($code, strpos($code, ' as ') + 4);
280 }
281 if (empty($alias)) {
282 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
283 continue;
284 }
285
286 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
287 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
288
289 // Translation newvalue
290 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
291 $newvalue = $outputlangs->transnoentities($reg[1]);
292 }
293
294 $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
295
296 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
297 $array = jsonOrUnserialize($typefield);
298 if (is_array($array) && !empty($newvalue)) {
299 $array = $array['options'];
300 $selectlabelvalues[$code."_label"] = $array[$newvalue];
301 } else {
302 $selectlabelvalues[$code."_label"] = "";
303 }
304 }
305
306 fwrite($this->handle, $newvalue.$this->separator);
307 $this->col++;
308 }
309 foreach ($selectlabelvalues as $key => $value) {
310 fwrite($this->handle, $value.$this->separator);
311 $this->col++;
312 }
313
314 fwrite($this->handle, "\n");
315 return 0;
316 }
317
318 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
325 public function write_footer($outputlangs)
326 {
327 // phpcs:enable
328 return 0;
329 }
330
331 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
337 public function close_file()
338 {
339 // phpcs:enable
340 fclose($this->handle);
341 return 0;
342 }
343
344 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
352 public function tsv_clean($newvalue, $charset = '')
353 {
354 // phpcs:enable
355 global $langs;
356
357 if (empty($charset)) {
358 $charset = getDolGlobalString('EXPORT_TSV_FORCE_CHARSET');
359 }
360
361 $newvalue = $langs->convToOutputCharset($newvalue, 'UTF-8', $charset); // newvalue is now encoded into $charset
362
363
364 // Rule Dolibarr: No HTML
365 $newvalue = dol_string_nohtmltag($newvalue, 1, $charset);
366
367 // Rule 1 TSV: No CR, LF in cells
368 $newvalue = str_replace("\r", '', $newvalue);
369 $newvalue = str_replace("\n", '\n', $newvalue);
370
371 // Rule 2 TSV: If value contains tab, we must replace by space
372 if (preg_match('/'.$this->separator.'/', $newvalue)) {
373 $newvalue = str_replace("\t", " ", $newvalue);
374 }
375
376 return $newvalue;
377 }
378}
Class to build export files with format TSV.
write_header($outputlangs)
Output header into file.
getDriverExtension()
getDriverExtension
getDriverLabel()
getDriverLabel
close_file()
Close file handle.
write_footer($outputlangs)
Output footer into file.
getDriverVersion()
getDriverVersion
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
getDriverDesc()
getDriverDesc
tsv_clean($newvalue, $charset='')
Clean a cell to respect rules of TSV file cells.
getLibLabel()
getLibLabel
getLibVersion()
getLibVersion
open_file($file, $outputlangs)
Open output file.
__construct($db)
Constructor.
getDriverId()
getDriverId
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
Parent class for export modules.
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.