dolibarr  16.0.5
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 
25 require_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
26 
27 
31 class ExportTsv extends ModeleExports
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  foreach ($array_selected_sorted as $code => $value) {
209  $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
210  $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
211 
212  fwrite($this->handle, $newvalue.$this->separator);
213  }
214  fwrite($this->handle, "\n");
215  return 0;
216  }
217 
218 
219  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
229  public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
230  {
231  // phpcs:enable
232  global $conf;
233 
234  $this->col = 0;
235  foreach ($array_selected_sorted as $code => $value) {
236  if (strpos($code, ' as ') == 0) {
237  $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
238  } else {
239  $alias = substr($code, strpos($code, ' as ') + 4);
240  }
241  if (empty($alias)) {
242  dol_print_error('', 'Bad value for field with code='.$code.'. Try to redefine export.');
243  }
244 
245  $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
246  $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
247 
248  // Translation newvalue
249  if (preg_match('/^\((.*)\)$/i', $newvalue, $reg)) {
250  $newvalue = $outputlangs->transnoentities($reg[1]);
251  }
252 
253  $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
254 
255  if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
256  $array = json_decode($typefield, true);
257  $array = $array['options'];
258  $newvalue = $array[$newvalue];
259  }
260 
261  fwrite($this->handle, $newvalue.$this->separator);
262  $this->col++;
263  }
264  fwrite($this->handle, "\n");
265  return 0;
266  }
267 
268  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
275  public function write_footer($outputlangs)
276  {
277  // phpcs:enable
278  return 0;
279  }
280 
281  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
287  public function close_file()
288  {
289  // phpcs:enable
290  fclose($this->handle);
291  return 0;
292  }
293 
294  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
302  public function tsv_clean($newvalue, $charset)
303  {
304  // phpcs:enable
305  // Rule Dolibarr: No HTML
306  $newvalue = dol_string_nohtmltag($newvalue, 1, $charset);
307 
308  // Rule 1 TSV: No CR, LF in cells
309  $newvalue = str_replace("\r", '', $newvalue);
310  $newvalue = str_replace("\n", '\n', $newvalue);
311 
312  // Rule 2 TSV: If value contains tab, we must replace by space
313  if (preg_match('/'.$this->separator.'/', $newvalue)) {
314  $newvalue = str_replace("\t", " ", $newvalue);
315  }
316 
317  return $newvalue;
318  }
319 }
ExportTsv\getDriverVersion
getDriverVersion()
getDriverVersion
Definition: export_tsv.modules.php:127
db
$conf db
API class for accounts.
Definition: inc.php:41
ExportTsv\write_header
write_header($outputlangs)
Output header into file.
Definition: export_tsv.modules.php:188
ExportTsv\tsv_clean
tsv_clean($newvalue, $charset)
Clean a cell to respect rules of TSV file cells.
Definition: export_tsv.modules.php:302
ExportTsv\write_title
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
Definition: export_tsv.modules.php:205
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
ExportTsv
Class to build export files with format TSV.
Definition: export_tsv.modules.php:31
ExportTsv\open_file
open_file($file, $outputlangs)
Open output file.
Definition: export_tsv.modules.php:161
ExportTsv\write_footer
write_footer($outputlangs)
Output footer into file.
Definition: export_tsv.modules.php:275
ExportTsv\getLibLabel
getLibLabel()
getLibLabel
Definition: export_tsv.modules.php:137
ExportTsv\getDriverLabel
getDriverLabel()
getDriverLabel
Definition: export_tsv.modules.php:97
ExportTsv\getDriverId
getDriverId()
getDriverId
Definition: export_tsv.modules.php:87
ExportTsv\write_record
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
Definition: export_tsv.modules.php:229
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:6694
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ExportTsv\getLibVersion
getLibVersion()
getLibVersion
Definition: export_tsv.modules.php:147
ExportTsv\close_file
close_file()
Close file handle.
Definition: export_tsv.modules.php:287
ExportTsv\getDriverDesc
getDriverDesc()
getDriverDesc
Definition: export_tsv.modules.php:107
ExportTsv\__construct
__construct($db)
Constructor.
Definition: export_tsv.modules.php:65
ModeleExports
Parent class for export modules.
Definition: modules_export.php:31
ExportTsv\getDriverExtension
getDriverExtension()
getDriverExtension
Definition: export_tsv.modules.php:117