dolibarr  13.0.2
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  {
174  $langs->load("errors");
175  $this->error = $langs->trans("ErrorFailToCreateFile", $file);
176  $ret = -1;
177  }
178 
179  return $ret;
180  }
181 
182  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
189  public function write_header($outputlangs)
190  {
191  // phpcs:enable
192  return 0;
193  }
194 
195 
196  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
206  public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
207  {
208  // phpcs:enable
209  foreach ($array_selected_sorted as $code => $value)
210  {
211  $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
212  $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
213 
214  fwrite($this->handle, $newvalue.$this->separator);
215  }
216  fwrite($this->handle, "\n");
217  return 0;
218  }
219 
220 
221  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
231  public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
232  {
233  // phpcs:enable
234  global $conf;
235 
236  $this->col = 0;
237  foreach ($array_selected_sorted as $code => $value)
238  {
239  if (strpos($code, ' as ') == 0) $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
240  else $alias = substr($code, strpos($code, ' as ') + 4);
241  if (empty($alias)) dol_print_error('', 'Bad value for field with code='.$code.'. Try to redefine export.');
242 
243  $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
244  $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
245 
246  // Translation newvalue
247  if (preg_match('/^\((.*)\)$/i', $newvalue, $reg)) $newvalue = $outputlangs->transnoentities($reg[1]);
248 
249  $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output);
250 
251  if (preg_match('/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
252  {
253  $array = unserialize($typefield);
254  $array = $array['options'];
255  $newvalue = $array[$newvalue];
256  }
257 
258  fwrite($this->handle, $newvalue.$this->separator);
259  $this->col++;
260  }
261  fwrite($this->handle, "\n");
262  return 0;
263  }
264 
265  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
272  public function write_footer($outputlangs)
273  {
274  // phpcs:enable
275  return 0;
276  }
277 
278  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
284  public function close_file()
285  {
286  // phpcs:enable
287  fclose($this->handle);
288  return 0;
289  }
290 
291  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
299  public function tsv_clean($newvalue, $charset)
300  {
301  // phpcs:enable
302  // Rule Dolibarr: No HTML
303  $newvalue = dol_string_nohtmltag($newvalue, 1, $charset);
304 
305  // Rule 1 TSV: No CR, LF in cells
306  $newvalue = str_replace("\r", '', $newvalue);
307  $newvalue = str_replace("\n", '\n', $newvalue);
308 
309  // Rule 2 TSV: If value contains tab, we must replace by space
310  if (preg_match('/'.$this->separator.'/', $newvalue)) {
311  $newvalue = str_replace("\t", " ", $newvalue);
312  }
313 
314  return $newvalue;
315  }
316 }
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title 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.
write_header($outputlangs)
Output header into file.
write_footer($outputlangs)
Output footer into file.
tsv_clean($newvalue, $charset)
Clean a cell to respect rules of TSV file cells.
getLibLabel()
getLibLabel
$conf db
API class for accounts.
Definition: inc.php:54
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
open_file($file, $outputlangs)
Open output file.
getDriverId()
getDriverId
getDriverLabel()
getDriverLabel
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
close_file()
Close file handle.
Class to build export files with format TSV.
__construct($db)
Constructor.
getLibVersion()
getLibVersion
getDriverDesc()
getDriverDesc
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDriverExtension()
getDriverExtension
getDriverVersion()
getDriverVersion