dolibarr  7.0.0-beta
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 {
34  var $id;
35  var $label;
36  var $extension;
37  var $version;
38 
39  var $label_lib;
40  var $version_lib;
41 
42  var $separator="\t";
43 
44  var $handle; // Handle fichier
45 
46 
52  function __construct($db)
53  {
54  global $conf, $langs;
55  $this->db = $db;
56 
57  $this->id='tsv'; // Same value then xxx in file name export_xxx.modules.php
58  $this->label = 'TSV'; // Label of driver
59  $this->desc = $langs->trans('TsvFormatDesc');
60  $this->extension='tsv'; // Extension for generated file by this driver
61  $this->picto='mime/other'; // Picto
62  $this->version='1.15'; // Driver version
63 
64  // If driver use an external library, put its name here
65  $this->label_lib='Dolibarr';
66  $this->version_lib=DOL_VERSION;
67  }
68 
74  function getDriverId()
75  {
76  return $this->id;
77  }
78 
84  function getDriverLabel()
85  {
86  return $this->label;
87  }
88 
94  function getDriverDesc()
95  {
96  return $this->desc;
97  }
98 
105  {
106  return $this->extension;
107  }
108 
114  function getDriverVersion()
115  {
116  return $this->version;
117  }
118 
124  function getLibLabel()
125  {
126  return $this->label_lib;
127  }
128 
134  function getLibVersion()
135  {
136  return $this->version_lib;
137  }
138 
139 
147  function open_file($file,$outputlangs)
148  {
149  global $langs;
150 
151  dol_syslog("ExportTsv::open_file file=".$file);
152 
153  $ret=1;
154 
155  $outputlangs->load("exports");
156  $this->handle = fopen($file, "wt");
157  if (! $this->handle)
158  {
159  $langs->load("errors");
160  $this->error=$langs->trans("ErrorFailToCreateFile",$file);
161  $ret=-1;
162  }
163 
164  return $ret;
165  }
166 
173  function write_header($outputlangs)
174  {
175  return 0;
176  }
177 
178 
188  function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs,$array_types)
189  {
190  foreach($array_selected_sorted as $code => $value)
191  {
192  $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
193  $newvalue=$this->tsv_clean($newvalue,$outputlangs->charset_output);
194 
195  fwrite($this->handle,$newvalue.$this->separator);
196  }
197  fwrite($this->handle,"\n");
198  return 0;
199  }
200 
201 
211  function write_record($array_selected_sorted,$objp,$outputlangs,$array_types)
212  {
213  global $conf;
214 
215  $this->col=0;
216  foreach($array_selected_sorted as $code => $value)
217  {
218  if (strpos($code,' as ') == 0) $alias=str_replace(array('.','-','(',')'),'_',$code);
219  else $alias=substr($code, strpos($code, ' as ') + 4);
220  if (empty($alias)) dol_print_error('','Bad value for field with code='.$code.'. Try to redefine export.');
221 
222  $newvalue=$outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
223  $typefield=isset($array_types[$code])?$array_types[$code]:'';
224 
225  // Translation newvalue
226  if (preg_match('/^\((.*)\)$/i',$newvalue,$reg)) $newvalue=$outputlangs->transnoentities($reg[1]);
227 
228  $newvalue=$this->tsv_clean($newvalue,$outputlangs->charset_output);
229 
230  if (preg_match('/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
231  {
232  $array = unserialize($typefield);
233  $array = $array['options'];
234  $newvalue = $array[$newvalue];
235  }
236 
237  fwrite($this->handle,$newvalue.$this->separator);
238  $this->col++;
239  }
240  fwrite($this->handle,"\n");
241  return 0;
242  }
243 
250  function write_footer($outputlangs)
251  {
252  return 0;
253  }
254 
260  function close_file()
261  {
262  fclose($this->handle);
263  return 0;
264  }
265 
273  function tsv_clean($newvalue, $charset)
274  {
275  // Rule Dolibarr: No HTML
276  $newvalue=dol_string_nohtmltag($newvalue, 1, $charset);
277 
278  // Rule 1 TSV: No CR, LF in cells
279  $newvalue=str_replace("\r",'',$newvalue);
280  $newvalue=str_replace("\n",'\n',$newvalue);
281 
282  // Rule 2 TSV: If value contains tab, we must replace by space
283  if (preg_match('/'.$this->separator.'/',$newvalue))
284  {
285  $newvalue=str_replace("\t"," ",$newvalue);
286  }
287 
288  return $newvalue;
289  }
290 
291 }
292 
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
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.
__construct($db)
Constructor.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8')
Clean a string from all HTML tags and entities.
getLibVersion()
getLibVersion
getDriverDesc()
getDriverDesc
getDriverExtension()
getDriverExtension
getDriverVersion()
getDriverVersion