dolibarr 21.0.0-alpha
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 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 $conf, $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 $outputlangs->charset_output = getDolGlobalString('EXPORT_TSV_FORCE_CHARSET');
222
223 $selectlabel = array();
224 foreach ($array_selected_sorted as $code => $value) {
225 if (strpos($code, ' as ') == 0) {
226 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
227 } else {
228 $alias = substr($code, strpos($code, ' as ') + 4);
229 }
230 if (empty($alias)) {
231 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
232 continue;
233 }
234
235 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
236 $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
237
238 fwrite($this->handle, $newvalue.$this->separator);
239 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
240
241 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
242 $selectlabel[$code."_label"] = $newvalue."_label";
243 }
244 }
245 foreach ($selectlabel as $key => $value) {
246 fwrite($this->handle, $value.$this->separator);
247 }
248 fwrite($this->handle, "\n");
249 return 0;
250 }
251
252
253 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
263 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
264 {
265 // phpcs:enable
266
267 $outputlangs->charset_output = getDolGlobalString('EXPORT_TSV_FORCE_CHARSET');
268
269 $this->col = 0;
270
271 $reg = array();
272 $selectlabelvalues = array();
273 foreach ($array_selected_sorted as $code => $value) {
274 if (strpos($code, ' as ') == 0) {
275 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
276 } else {
277 $alias = substr($code, strpos($code, ' as ') + 4);
278 }
279 if (empty($alias)) {
280 dol_syslog('Bad value for field with code='.$code.'. Try to redefine export.', LOG_WARNING);
281 continue;
282 }
283
284 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
285 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
286
287 // Translation newvalue
288 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
289 $newvalue = $outputlangs->transnoentities($reg[1]);
290 }
291
292 $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
293
294 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
295 $array = jsonOrUnserialize($typefield);
296 if (is_array($array) && !empty($newvalue)) {
297 $array = $array['options'];
298 $selectlabelvalues[$code."_label"] = $array[$newvalue];
299 } else {
300 $selectlabelvalues[$code."_label"] = "";
301 }
302 }
303
304 fwrite($this->handle, $newvalue.$this->separator);
305 $this->col++;
306 }
307 foreach ($selectlabelvalues as $key => $value) {
308 fwrite($this->handle, $value.$this->separator);
309 $this->col++;
310 }
311
312 fwrite($this->handle, "\n");
313 return 0;
314 }
315
316 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
323 public function write_footer($outputlangs)
324 {
325 // phpcs:enable
326 return 0;
327 }
328
329 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
335 public function close_file()
336 {
337 // phpcs:enable
338 fclose($this->handle);
339 return 0;
340 }
341
342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
350 public function tsv_clean($newvalue, $charset)
351 {
352 // phpcs:enable
353
354 // Rule Dolibarr: No HTML
355 $newvalue = dol_string_nohtmltag($newvalue, 1, $charset);
356
357 // Rule 1 TSV: No CR, LF in cells
358 $newvalue = str_replace("\r", '', $newvalue);
359 $newvalue = str_replace("\n", '\n', $newvalue);
360
361 // Rule 2 TSV: If value contains tab, we must replace by space
362 if (preg_match('/'.$this->separator.'/', $newvalue)) {
363 $newvalue = str_replace("\t", " ", $newvalue);
364 }
365
366 return $newvalue;
367 }
368}
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
getLibLabel()
getLibLabel
getLibVersion()
getLibVersion
open_file($file, $outputlangs)
Open output file.
__construct($db)
Constructor.
getDriverId()
getDriverId
tsv_clean($newvalue, $charset)
Clean a cell to respect rules of TSV file cells.
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)
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.