dolibarr  9.0.0
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 {
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;
56 
57  public $handle; // Handle fichier
58 
59 
65  function __construct($db)
66  {
67  global $conf, $langs;
68  $this->db = $db;
69 
70  $this->separator=',';
71  if (! empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) $this->separator=$conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
72  $this->escape='"';
73  $this->enclosure='"';
74 
75  $this->id='csv'; // Same value then xxx in file name export_xxx.modules.php
76  $this->label = 'CSV'; // Label of driver
77  $this->desc=$langs->trans("CSVFormatDesc",$this->separator,$this->enclosure,$this->escape);
78  $this->extension='csv'; // Extension for generated file by this driver
79  $this->picto='mime/other'; // Picto
80  $this->version='1.32'; // Driver version
81 
82  // If driver use an external library, put its name here
83  $this->label_lib='Dolibarr';
84  $this->version_lib=DOL_VERSION;
85  }
86 
92  function getDriverId()
93  {
94  return $this->id;
95  }
96 
102  function getDriverLabel()
103  {
104  return $this->label;
105  }
106 
112  function getDriverDesc()
113  {
114  return $this->desc;
115  }
116 
123  {
124  return $this->extension;
125  }
126 
132  function getDriverVersion()
133  {
134  return $this->version;
135  }
136 
142  function getLibLabel()
143  {
144  return $this->label_lib;
145  }
146 
152  function getLibVersion()
153  {
154  return $this->version_lib;
155  }
156 
157 
158  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
166  function open_file($file,$outputlangs)
167  {
168  // phpcs:enable
169  global $langs;
170 
171  dol_syslog("ExportCsv::open_file file=".$file);
172 
173  $ret=1;
174 
175  $outputlangs->load("exports");
176  $this->handle = fopen($file, "wt");
177  if (! $this->handle)
178  {
179  $langs->load("errors");
180  $this->error=$langs->trans("ErrorFailToCreateFile",$file);
181  $ret=-1;
182  }
183 
184  return $ret;
185  }
186 
187  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
194  function write_header($outputlangs)
195  {
196  // phpcs:enable
197  return 0;
198  }
199 
200 
201  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
211  function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs,$array_types)
212  {
213  // phpcs:enable
214  global $conf;
215 
216  if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
217  {
218  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
219  }
220  else
221  {
222  $outputlangs->charset_output = 'ISO-8859-1';
223  }
224 
225  foreach($array_selected_sorted as $code => $value)
226  {
227  $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
228  $newvalue=$this->csvClean($newvalue,$outputlangs->charset_output);
229 
230  fwrite($this->handle,$newvalue.$this->separator);
231  }
232  fwrite($this->handle,"\n");
233  return 0;
234  }
235 
236 
237  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
247  function write_record($array_selected_sorted,$objp,$outputlangs,$array_types)
248  {
249  // phpcs:enable
250  global $conf;
251 
252  if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
253  {
254  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
255  }
256  else
257  {
258  $outputlangs->charset_output = 'ISO-8859-1';
259  }
260 
261  $this->col=0;
262  foreach($array_selected_sorted as $code => $value)
263  {
264  if (strpos($code,' as ') == 0) $alias=str_replace(array('.','-','(',')'),'_',$code);
265  else $alias=substr($code, strpos($code, ' as ') + 4);
266  if (empty($alias)) dol_print_error('','Bad value for field with key='.$code.'. Try to redefine export.');
267 
268  $newvalue=$outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
269  $typefield=isset($array_types[$code])?$array_types[$code]:'';
270 
271  // Translation newvalue
272  if (preg_match('/^\((.*)\)$/i',$newvalue,$reg)) $newvalue=$outputlangs->transnoentities($reg[1]);
273 
274  $newvalue=$this->csvClean($newvalue,$outputlangs->charset_output);
275 
276  if (preg_match('/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
277  {
278  $array = unserialize($typefield);
279  $array = $array['options'];
280  $newvalue = $array[$newvalue];
281  }
282 
283  fwrite($this->handle,$newvalue.$this->separator);
284  $this->col++;
285  }
286 
287  fwrite($this->handle,"\n");
288  return 0;
289  }
290 
291  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
298  function write_footer($outputlangs)
299  {
300  // phpcs:enable
301  return 0;
302  }
303 
304  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
310  function close_file()
311  {
312  // phpcs:enable
313  fclose($this->handle);
314  return 0;
315  }
316 
317 
327  public function csvClean($newvalue, $charset)
328  {
329  global $conf;
330  $addquote=0;
331 
332 
333  // Rule Dolibarr: No HTML
334  //print $charset.' '.$newvalue."\n";
335  //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
336  $newvalue=dol_htmlcleanlastbr($newvalue);
337  //print $charset.' '.$newvalue."\n";
338 
339  // 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)
340  $oldvalue=$newvalue;
341  $newvalue=str_replace("\r",'',$newvalue);
342  $newvalue=str_replace("\n",'\n',$newvalue);
343  if (! empty($conf->global->USE_STRICT_CSV_RULES) && $oldvalue != $newvalue)
344  {
345  // If strict use of CSV rules, we just add quote
346  $newvalue=$oldvalue;
347  $addquote=1;
348  }
349 
350  // Rule 2 CSV: If value contains ", we must escape with ", and add "
351  if (preg_match('/"/',$newvalue))
352  {
353  $addquote=1;
354  $newvalue=str_replace('"','""',$newvalue);
355  }
356 
357  // Rule 3 CSV: If value contains separator, we must add "
358  if (preg_match('/'.$this->separator.'/',$newvalue))
359  {
360  $addquote=1;
361  }
362 
363  return ($addquote?'"':'').$newvalue.($addquote?'"':'');
364  }
365 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
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
$version
Dolibarr version of the loaded document string.
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