dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17 */
18 
26 require_once DOL_DOCUMENT_ROOT .'/core/modules/export/modules_export.php';
27 
28 
32 class ExportTsv extends ModeleExports
33 {
37  public $id;
38 
42  public $label;
43 
44  public $extension;
45 
50  public $version = 'dolibarr';
51 
52  public $label_lib;
53 
54  public $version_lib;
55 
56  public $separator="\t";
57 
58  public $handle; // Handle fichier
59 
60 
66  function __construct($db)
67  {
68  global $conf, $langs;
69  $this->db = $db;
70 
71  $this->id='tsv'; // Same value then xxx in file name export_xxx.modules.php
72  $this->label = 'TSV'; // Label of driver
73  $this->desc = $langs->trans('TsvFormatDesc');
74  $this->extension='tsv'; // Extension for generated file by this driver
75  $this->picto='mime/other'; // Picto
76  $this->version='1.15'; // Driver version
77 
78  // If driver use an external library, put its name here
79  $this->label_lib='Dolibarr';
80  $this->version_lib=DOL_VERSION;
81  }
82 
88  function getDriverId()
89  {
90  return $this->id;
91  }
92 
98  function getDriverLabel()
99  {
100  return $this->label;
101  }
102 
108  function getDriverDesc()
109  {
110  return $this->desc;
111  }
112 
119  {
120  return $this->extension;
121  }
122 
128  function getDriverVersion()
129  {
130  return $this->version;
131  }
132 
138  function getLibLabel()
139  {
140  return $this->label_lib;
141  }
142 
148  function getLibVersion()
149  {
150  return $this->version_lib;
151  }
152 
153 
154  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
162  function open_file($file,$outputlangs)
163  {
164  // phpcs:enable
165  global $langs;
166 
167  dol_syslog("ExportTsv::open_file file=".$file);
168 
169  $ret=1;
170 
171  $outputlangs->load("exports");
172  $this->handle = fopen($file, "wt");
173  if (! $this->handle)
174  {
175  $langs->load("errors");
176  $this->error=$langs->trans("ErrorFailToCreateFile",$file);
177  $ret=-1;
178  }
179 
180  return $ret;
181  }
182 
183  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
190  function write_header($outputlangs)
191  {
192  // phpcs:enable
193  return 0;
194  }
195 
196 
197  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
207  function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs,$array_types)
208  {
209  // phpcs:enable
210  foreach($array_selected_sorted as $code => $value)
211  {
212  $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
213  $newvalue=$this->tsv_clean($newvalue,$outputlangs->charset_output);
214 
215  fwrite($this->handle,$newvalue.$this->separator);
216  }
217  fwrite($this->handle,"\n");
218  return 0;
219  }
220 
221 
222  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
232  function write_record($array_selected_sorted,$objp,$outputlangs,$array_types)
233  {
234  // phpcs:enable
235  global $conf;
236 
237  $this->col=0;
238  foreach($array_selected_sorted as $code => $value)
239  {
240  if (strpos($code,' as ') == 0) $alias=str_replace(array('.','-','(',')'),'_',$code);
241  else $alias=substr($code, strpos($code, ' as ') + 4);
242  if (empty($alias)) dol_print_error('','Bad value for field with code='.$code.'. Try to redefine export.');
243 
244  $newvalue=$outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
245  $typefield=isset($array_types[$code])?$array_types[$code]:'';
246 
247  // Translation newvalue
248  if (preg_match('/^\((.*)\)$/i',$newvalue,$reg)) $newvalue=$outputlangs->transnoentities($reg[1]);
249 
250  $newvalue=$this->tsv_clean($newvalue,$outputlangs->charset_output);
251 
252  if (preg_match('/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
253  {
254  $array = unserialize($typefield);
255  $array = $array['options'];
256  $newvalue = $array[$newvalue];
257  }
258 
259  fwrite($this->handle,$newvalue.$this->separator);
260  $this->col++;
261  }
262  fwrite($this->handle,"\n");
263  return 0;
264  }
265 
266  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
273  function write_footer($outputlangs)
274  {
275  // phpcs:enable
276  return 0;
277  }
278 
279  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
285  function close_file()
286  {
287  // phpcs:enable
288  fclose($this->handle);
289  return 0;
290  }
291 
292  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
300  function tsv_clean($newvalue, $charset)
301  {
302  // phpcs:enable
303  // Rule Dolibarr: No HTML
304  $newvalue=dol_string_nohtmltag($newvalue, 1, $charset);
305 
306  // Rule 1 TSV: No CR, LF in cells
307  $newvalue=str_replace("\r",'',$newvalue);
308  $newvalue=str_replace("\n",'\n',$newvalue);
309 
310  // Rule 2 TSV: If value contains tab, we must replace by space
311  if (preg_match('/'.$this->separator.'/',$newvalue))
312  {
313  $newvalue=str_replace("\t"," ",$newvalue);
314  }
315 
316  return $newvalue;
317  }
318 }
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
Parent class for export modules.
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
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
open_file($file, $outputlangs)
Open output file.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getDriverId()
getDriverId
getDriverLabel()
getDriverLabel
close_file()
Close file handle.
Class to build export files with format TSV.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0)
Clean a string from all HTML tags and entities.
__construct($db)
Constructor.
getLibVersion()
getLibVersion
getDriverDesc()
getDriverDesc
getDriverExtension()
getDriverExtension
getDriverVersion()
getDriverVersion
$version
Dolibarr version of the loaded document string.