dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/modules/export/modules_export.php';
25 
26 // avoid timeout for big export
27 set_time_limit(0);
28 
32 class ExportCsv 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;
57 
58  public $handle; // Handle fichier
59 
60 
66  public function __construct($db)
67  {
68  global $conf, $langs;
69  $this->db = $db;
70 
71  $this->separator = ',';
72  if (!empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) $this->separator = $conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
73  $this->escape = '"';
74  $this->enclosure = '"';
75 
76  $this->id = 'csv'; // Same value then xxx in file name export_xxx.modules.php
77  $this->label = 'CSV'; // Label of driver
78  $this->desc = $langs->trans("CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
79  $this->extension = 'csv'; // Extension for generated file by this driver
80  $this->picto = 'mime/other'; // Picto
81  $this->version = '1.32'; // Driver version
82 
83  // If driver use an external library, put its name here
84  $this->label_lib = 'Dolibarr';
85  $this->version_lib = DOL_VERSION;
86  }
87 
93  public function getDriverId()
94  {
95  return $this->id;
96  }
97 
103  public function getDriverLabel()
104  {
105  return $this->label;
106  }
107 
113  public function getDriverDesc()
114  {
115  return $this->desc;
116  }
117 
123  public function getDriverExtension()
124  {
125  return $this->extension;
126  }
127 
133  public function getDriverVersion()
134  {
135  return $this->version;
136  }
137 
143  public function getLibLabel()
144  {
145  return $this->label_lib;
146  }
147 
153  public function getLibVersion()
154  {
155  return $this->version_lib;
156  }
157 
158 
159  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
167  public function open_file($file, $outputlangs)
168  {
169  // phpcs:enable
170  global $langs;
171 
172  dol_syslog("ExportCsv::open_file file=".$file);
173 
174  $ret = 1;
175 
176  $outputlangs->load("exports");
177  $this->handle = fopen($file, "wt");
178  if (!$this->handle)
179  {
180  $langs->load("errors");
181  $this->error = $langs->trans("ErrorFailToCreateFile", $file);
182  $ret = -1;
183  }
184 
185  return $ret;
186  }
187 
188  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
195  public function write_header($outputlangs)
196  {
197  // phpcs:enable
198  return 0;
199  }
200 
201 
202  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
212  public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
213  {
214  // phpcs:enable
215  global $conf;
216 
217  if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
218  {
219  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
220  } else {
221  $outputlangs->charset_output = 'ISO-8859-1';
222  }
223 
224  foreach ($array_selected_sorted as $code => $value)
225  {
226  $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
227  $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
228 
229  fwrite($this->handle, $newvalue.$this->separator);
230  }
231  fwrite($this->handle, "\n");
232  return 0;
233  }
234 
235 
236  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
246  public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
247  {
248  // phpcs:enable
249  global $conf;
250 
251  if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET))
252  {
253  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
254  } else {
255  $outputlangs->charset_output = 'ISO-8859-1';
256  }
257 
258  $this->col = 0;
259 
260  $reg = array();
261 
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.ScopeNotCamelCaps
298  public function write_footer($outputlangs)
299  {
300  // phpcs:enable
301  return 0;
302  }
303 
304  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
310  public 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  // Rule Dolibarr: No HTML
333  //print $charset.' '.$newvalue."\n";
334  //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
335  $newvalue = dol_htmlcleanlastbr($newvalue);
336  //print $charset.' '.$newvalue."\n";
337 
338  // 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)
339  $oldvalue = $newvalue;
340  $newvalue = str_replace("\r", '', $newvalue);
341  $newvalue = str_replace("\n", '\n', $newvalue);
342  if (!empty($conf->global->USE_STRICT_CSV_RULES) && $oldvalue != $newvalue)
343  {
344  // If strict use of CSV rules, we just add quote
345  $newvalue = $oldvalue;
346  $addquote = 1;
347  }
348 
349  // Rule 2 CSV: If value contains ", we must escape with ", and add "
350  if (preg_match('/"/', $newvalue))
351  {
352  $addquote = 1;
353  $newvalue = str_replace('"', '""', $newvalue);
354  }
355 
356  // Rule 3 CSV: If value contains separator, we must add "
357  if (preg_match('/'.$this->separator.'/', $newvalue))
358  {
359  $addquote = 1;
360  }
361 
362  return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
363  }
364 }
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
open_file($file, $outputlangs)
Open output file.
$conf db
API class for accounts.
Definition: inc.php:54
csvClean($newvalue, $charset)
Clean a cell to respect rules of CSV file cells Note: It uses $this-&gt;separator Note: We keep this fun...
getDriverVersion()
getDriverVersion
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__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.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDriverExtension()
getDriverExtension
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
getDriverDesc()
getDriverDesc