dolibarr  7.0.0-beta
export_csv.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
25 require_once DOL_DOCUMENT_ROOT .'/core/modules/export/modules_export.php';
26 
27 
31 class ExportCsv extends ModeleExports
32 {
33  var $id;
34  var $label;
35  var $extension;
36  var $version;
37 
38  var $label_lib;
39  var $version_lib;
40 
41  var $separator;
42 
43  var $handle; // Handle fichier
44 
45 
51  function __construct($db)
52  {
53  global $conf,$langs;
54  $this->db = $db;
55 
56  $this->separator=',';
57  if (! empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
58  $this->escape='"';
59  $this->enclosure='"';
60 
61  $this->id='csv'; // Same value then xxx in file name export_xxx.modules.php
62  $this->label = 'CSV'; // Label of driver
63  $this->desc=$langs->trans("CSVFormatDesc",$this->separator,$this->enclosure,$this->escape);
64  $this->extension='csv'; // Extension for generated file by this driver
65  $this->picto='mime/other'; // Picto
66  $this->version='1.32'; // Driver version
67 
68  // If driver use an external library, put its name here
69  $this->label_lib='Dolibarr';
70  $this->version_lib=DOL_VERSION;
71 
72  }
73 
79  function getDriverId()
80  {
81  return $this->id;
82  }
83 
89  function getDriverLabel()
90  {
91  return $this->label;
92  }
93 
99  function getDriverDesc()
100  {
101  return $this->desc;
102  }
103 
110  {
111  return $this->extension;
112  }
113 
119  function getDriverVersion()
120  {
121  return $this->version;
122  }
123 
129  function getLibLabel()
130  {
131  return $this->label_lib;
132  }
133 
139  function getLibVersion()
140  {
141  return $this->version_lib;
142  }
143 
144 
152  function open_file($file,$outputlangs)
153  {
154  global $langs;
155 
156  dol_syslog("ExportCsv::open_file file=".$file);
157 
158  $ret=1;
159 
160  $outputlangs->load("exports");
161  $this->handle = fopen($file, "wt");
162  if (! $this->handle)
163  {
164  $langs->load("errors");
165  $this->error=$langs->trans("ErrorFailToCreateFile",$file);
166  $ret=-1;
167  }
168 
169  return $ret;
170  }
171 
178  function write_header($outputlangs)
179  {
180  return 0;
181  }
182 
183 
193  function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs,$array_types)
194  {
195  global $conf;
196 
197  if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
198  {
199  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
200  }
201  else
202  {
203  $outputlangs->charset_output = 'ISO-8859-1';
204  }
205 
206  foreach($array_selected_sorted as $code => $value)
207  {
208  $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
209  $newvalue=$this->csvClean($newvalue,$outputlangs->charset_output);
210 
211  fwrite($this->handle,$newvalue.$this->separator);
212  }
213  fwrite($this->handle,"\n");
214  return 0;
215  }
216 
217 
227  function write_record($array_selected_sorted,$objp,$outputlangs,$array_types)
228  {
229  global $conf;
230 
231  if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
232  {
233  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
234  }
235  else
236  {
237  $outputlangs->charset_output = 'ISO-8859-1';
238  }
239 
240  $this->col=0;
241  foreach($array_selected_sorted as $code => $value)
242  {
243  if (strpos($code,' as ') == 0) $alias=str_replace(array('.','-','(',')'),'_',$code);
244  else $alias=substr($code, strpos($code, ' as ') + 4);
245  if (empty($alias)) dol_print_error('','Bad value for field with key='.$code.'. Try to redefine export.');
246 
247  $newvalue=$outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
248  $typefield=isset($array_types[$code])?$array_types[$code]:'';
249 
250  // Translation newvalue
251  if (preg_match('/^\((.*)\)$/i',$newvalue,$reg)) $newvalue=$outputlangs->transnoentities($reg[1]);
252 
253  $newvalue=$this->csvClean($newvalue,$outputlangs->charset_output);
254 
255  if (preg_match('/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
256  {
257  $array = unserialize($typefield);
258  $array = $array['options'];
259  $newvalue = $array[$newvalue];
260  }
261 
262  fwrite($this->handle,$newvalue.$this->separator);
263  $this->col++;
264  }
265 
266  fwrite($this->handle,"\n");
267  return 0;
268  }
269 
276  function write_footer($outputlangs)
277  {
278  return 0;
279  }
280 
286  function close_file()
287  {
288  fclose($this->handle);
289  return 0;
290  }
291 
292 
302  public function csvClean($newvalue, $charset)
303  {
304  global $conf;
305  $addquote=0;
306 
307 
308  // Rule Dolibarr: No HTML
309  //print $charset.' '.$newvalue."\n";
310  //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
311  $newvalue=dol_htmlcleanlastbr($newvalue);
312  //print $charset.' '.$newvalue."\n";
313 
314  // Rule 1 CSV: No CR, LF in cells (except if USE_STRICT_CSV_RULES is on, we can keep record as it is but we must add quotes)
315  $oldvalue=$newvalue;
316  $newvalue=str_replace("\r",'',$newvalue);
317  $newvalue=str_replace("\n",'\n',$newvalue);
318  if (! empty($conf->global->USE_STRICT_CSV_RULES) && $oldvalue != $newvalue)
319  {
320  // If strict use of CSV rules, we just add quote
321  $newvalue=$oldvalue;
322  $addquote=1;
323  }
324 
325  // Rule 2 CSV: If value contains ", we must escape with ", and add "
326  if (preg_match('/"/',$newvalue))
327  {
328  $addquote=1;
329  $newvalue=str_replace('"','""',$newvalue);
330  }
331 
332  // Rule 3 CSV: If value contains separator, we must add "
333  if (preg_match('/'.$this->separator.'/',$newvalue))
334  {
335  $addquote=1;
336  }
337 
338  return ($addquote?'"':'').$newvalue.($addquote?'"':'');
339  }
340 
341 }
342 
Parent class for export modules.
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
write_header($outputlangs)
Output header into file.
getLibLabel()
getLabelLabel
getLibVersion()
getLibVersion
getDriverId()
getDriverId
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
open_file($file, $outputlangs)
Open output file.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
csvClean($newvalue, $charset)
Clean a cell to respect rules of CSV file cells Note: It uses $this->separator Note: We keep this fun...
getDriverVersion()
getDriverVersion
__construct($db)
Constructor.
Class to build export files with format CSV.
getDriverLabel()
getDriverLabel
write_footer($outputlangs)
Output footer into file.
close_file()
Close file handle.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
getDriverExtension()
getDriverExtension
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
getDriverDesc()
getDriverDesc