dolibarr 18.0.6
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 *
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
32{
36 public $id;
37
41 public $label;
42
43 public $extension;
44
49 public $version = 'dolibarr';
50
51 public $label_lib;
52
53 public $version_lib;
54
55 public $separator = "\t";
56
57 public $handle; // Handle fichier
58
59
65 public function __construct($db)
66 {
67 global $conf, $langs;
68 $this->db = $db;
69
70 $this->id = 'tsv'; // Same value then xxx in file name export_xxx.modules.php
71 $this->label = 'TSV'; // Label of driver
72 $this->desc = $langs->trans('TsvFormatDesc');
73 $this->extension = 'tsv'; // Extension for generated file by this driver
74 $this->picto = 'mime/other'; // Picto
75 $this->version = '1.15'; // Driver version
76
77 // If driver use an external library, put its name here
78 $this->label_lib = 'Dolibarr';
79 $this->version_lib = DOL_VERSION;
80 }
81
87 public function getDriverId()
88 {
89 return $this->id;
90 }
91
97 public function getDriverLabel()
98 {
99 return $this->label;
100 }
101
107 public function getDriverDesc()
108 {
109 return $this->desc;
110 }
111
117 public function getDriverExtension()
118 {
119 return $this->extension;
120 }
121
127 public function getDriverVersion()
128 {
129 return $this->version;
130 }
131
137 public function getLibLabel()
138 {
139 return $this->label_lib;
140 }
141
147 public function getLibVersion()
148 {
149 return $this->version_lib;
150 }
151
152
153 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
161 public function open_file($file, $outputlangs)
162 {
163 // phpcs:enable
164 global $langs;
165
166 dol_syslog("ExportTsv::open_file file=".$file);
167
168 $ret = 1;
169
170 $outputlangs->load("exports");
171 $this->handle = fopen($file, "wt");
172 if (!$this->handle) {
173 $langs->load("errors");
174 $this->error = $langs->trans("ErrorFailToCreateFile", $file);
175 $ret = -1;
176 }
177
178 return $ret;
179 }
180
181 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
188 public function write_header($outputlangs)
189 {
190 // phpcs:enable
191 return 0;
192 }
193
194
195 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
205 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
206 {
207 // phpcs:enable
208 $selectlabel = array();
209 foreach ($array_selected_sorted as $code => $value) {
210 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
211 $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
212
213 fwrite($this->handle, $newvalue.$this->separator);
214 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
215
216 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
217 $selectlabel[$code."_label"] = $newvalue."_label";
218 }
219 }
220 foreach ($selectlabel as $key => $value) {
221 fwrite($this->handle, $value.$this->separator);
222 }
223 fwrite($this->handle, "\n");
224 return 0;
225 }
226
227
228 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
238 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
239 {
240 // phpcs:enable
241 global $conf;
242
243 $this->col = 0;
244 $selectlabelvalues = array();
245 foreach ($array_selected_sorted as $code => $value) {
246 if (strpos($code, ' as ') == 0) {
247 $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
248 } else {
249 $alias = substr($code, strpos($code, ' as ') + 4);
250 }
251 if (empty($alias)) {
252 dol_print_error('', 'Bad value for field with code='.$code.'. Try to redefine export.');
253 }
254
255 $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
256 $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
257
258 // Translation newvalue
259 if (preg_match('/^\‍((.*)\‍)$/i', $newvalue, $reg)) {
260 $newvalue = $outputlangs->transnoentities($reg[1]);
261 }
262
263 $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
264
265 if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
266 $array = jsonOrUnserialize($typefield);
267 if (is_array($array) && !empty($newvalue)) {
268 $array = $array['options'];
269 $selectlabelvalues[$code."_label"] = $array[$newvalue];
270 } else {
271 $selectlabelvalues[$code."_label"] = "";
272 }
273 }
274
275 fwrite($this->handle, $newvalue.$this->separator);
276 $this->col++;
277 }
278 foreach ($selectlabelvalues as $key => $value) {
279 fwrite($this->handle, $value.$this->separator);
280 $this->col++;
281 }
282 fwrite($this->handle, "\n");
283 return 0;
284 }
285
286 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
293 public function write_footer($outputlangs)
294 {
295 // phpcs:enable
296 return 0;
297 }
298
299 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
305 public function close_file()
306 {
307 // phpcs:enable
308 fclose($this->handle);
309 return 0;
310 }
311
312 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
320 public function tsv_clean($newvalue, $charset)
321 {
322 // phpcs:enable
323 // Rule Dolibarr: No HTML
324 $newvalue = dol_string_nohtmltag($newvalue, 1, $charset);
325
326 // Rule 1 TSV: No CR, LF in cells
327 $newvalue = str_replace("\r", '', $newvalue);
328 $newvalue = str_replace("\n", '\n', $newvalue);
329
330 // Rule 2 TSV: If value contains tab, we must replace by space
331 if (preg_match('/'.$this->separator.'/', $newvalue)) {
332 $newvalue = str_replace("\t", " ", $newvalue);
333 }
334
335 return $newvalue;
336 }
337}
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
jsonOrUnserialize($stringtodecode)
Decode an encode string.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.