dolibarr  18.0.0-alpha
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)) {
73  $this->separator = $conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
74  }
75  $this->escape = '"';
76  $this->enclosure = '"';
77 
78  $this->id = 'csv'; // Same value then xxx in file name export_xxx.modules.php
79  $this->label = 'CSV'; // Label of driver
80  $this->desc = $langs->trans("CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
81  $this->extension = 'csv'; // Extension for generated file by this driver
82  $this->picto = 'mime/other'; // Picto
83  $this->version = '1.32'; // Driver version
84 
85  // If driver use an external library, put its name here
86  $this->label_lib = 'Dolibarr';
87  $this->version_lib = DOL_VERSION;
88  }
89 
95  public function getDriverId()
96  {
97  return $this->id;
98  }
99 
105  public function getDriverLabel()
106  {
107  return $this->label;
108  }
109 
115  public function getDriverDesc()
116  {
117  return $this->desc;
118  }
119 
125  public function getDriverExtension()
126  {
127  return $this->extension;
128  }
129 
135  public function getDriverVersion()
136  {
137  return $this->version;
138  }
139 
145  public function getLibLabel()
146  {
147  return $this->label_lib;
148  }
149 
155  public function getLibVersion()
156  {
157  return $this->version_lib;
158  }
159 
160 
161  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
169  public function open_file($file, $outputlangs)
170  {
171  // phpcs:enable
172  global $langs;
173 
174  dol_syslog("ExportCsv::open_file file=".$file);
175 
176  $ret = 1;
177 
178  $outputlangs->load("exports");
179  $this->handle = fopen($file, "wt");
180  if (!$this->handle) {
181  $langs->load("errors");
182  $this->error = $langs->trans("ErrorFailToCreateFile", $file);
183  $ret = -1;
184  }
185 
186  return $ret;
187  }
188 
189  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
196  public function write_header($outputlangs)
197  {
198  // phpcs:enable
199  return 0;
200  }
201 
202 
203  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
213  public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
214  {
215  // phpcs:enable
216  global $conf;
217 
218  if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) {
219  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
220  } else {
221  $outputlangs->charset_output = 'ISO-8859-1';
222  }
223  $selectlabel = array();
224 
225  foreach ($array_selected_sorted as $code => $value) {
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  $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
231 
232  if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
233  $selectlabel[$code."_label"] = $newvalue."_label";
234  }
235  }
236  foreach ($selectlabel as $key => $value) {
237  fwrite($this->handle, $value.$this->separator);
238  }
239  fwrite($this->handle, "\n");
240  return 0;
241  }
242 
243 
244  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
254  public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
255  {
256  // phpcs:enable
257  global $conf;
258 
259  if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) {
260  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
261  } else {
262  $outputlangs->charset_output = 'ISO-8859-1';
263  }
264 
265  $this->col = 0;
266 
267  $reg = array();
268  $selectlabelvalues = array();
269  foreach ($array_selected_sorted as $code => $value) {
270  if (strpos($code, ' as ') == 0) {
271  $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
272  } else {
273  $alias = substr($code, strpos($code, ' as ') + 4);
274  }
275  if (empty($alias)) {
276  dol_print_error('', 'Bad value for field with key='.$code.'. Try to redefine export.');
277  }
278 
279  $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
280  $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
281 
282  // Translation newvalue
283  if (preg_match('/^\((.*)\)$/i', $newvalue, $reg)) {
284  $newvalue = $outputlangs->transnoentities($reg[1]);
285  }
286 
287  // Clean data and add encloser if required (depending on value of USE_STRICT_CSV_RULES)
288  $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
289 
290  if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
291  $array = jsonOrUnserialize($typefield);
292  if (is_array($array) && !empty($newvalue)) {
293  $array = $array['options'];
294  $selectlabelvalues[$code."_label"] = $array[$newvalue];
295  } else {
296  $selectlabelvalues[$code."_label"] = "";
297  }
298  }
299 
300  fwrite($this->handle, $newvalue.$this->separator);
301  $this->col++;
302  }
303  foreach ($selectlabelvalues as $key => $value) {
304  fwrite($this->handle, $value.$this->separator);
305  $this->col++;
306  }
307 
308  fwrite($this->handle, "\n");
309  return 0;
310  }
311 
312  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
319  public function write_footer($outputlangs)
320  {
321  // phpcs:enable
322  return 0;
323  }
324 
325  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
331  public function close_file()
332  {
333  // phpcs:enable
334  fclose($this->handle);
335  return 0;
336  }
337 
338 
348  public function csvClean($newvalue, $charset)
349  {
350  global $conf;
351  $addquote = 0;
352 
353  // Rule Dolibarr: No HTML
354  //print $charset.' '.$newvalue."\n";
355  //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
356  $newvalue = dol_htmlcleanlastbr($newvalue);
357  //print $charset.' '.$newvalue."\n";
358 
359  // Rule 1 CSV: No CR, LF in cells (except if USE_STRICT_CSV_RULES is 1, we can keep record as it is but we must add quotes)
360  $oldvalue = $newvalue;
361  $newvalue = str_replace("\r", '', $newvalue);
362  $newvalue = str_replace("\n", '\n', $newvalue);
363  if (!empty($conf->global->USE_STRICT_CSV_RULES) && $oldvalue != $newvalue) {
364  // If we must use enclusure on text with CR/LF)
365  if ($conf->global->USE_STRICT_CSV_RULES == 1) {
366  // If we use strict CSV rules (original value must remain but we add quote)
367  $newvalue = $oldvalue;
368  }
369  $addquote = 1;
370  }
371 
372  // Rule 2 CSV: If value contains ", we must escape with ", and add "
373  if (preg_match('/"/', $newvalue)) {
374  $addquote = 1;
375  $newvalue = str_replace('"', '""', $newvalue);
376  }
377 
378  // Rule 3 CSV: If value contains separator, we must add "
379  if (preg_match('/'.$this->separator.'/', $newvalue)) {
380  $addquote = 1;
381  }
382 
383  return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
384  }
385 }
ExportCsv\getLibLabel
getLibLabel()
getLabelLabel
Definition: export_csv.modules.php:145
db
$conf db
API class for accounts.
Definition: inc.php:41
ExportCsv\write_record
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
Definition: export_csv.modules.php:254
ExportCsv\write_header
write_header($outputlangs)
Output header into file.
Definition: export_csv.modules.php:196
ExportCsv
Class to build export files with format CSV.
Definition: export_csv.modules.php:32
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5031
ExportCsv\open_file
open_file($file, $outputlangs)
Open output file.
Definition: export_csv.modules.php:169
ExportCsv\getDriverId
getDriverId()
getDriverId
Definition: export_csv.modules.php:95
ExportCsv\getLibVersion
getLibVersion()
getLibVersion
Definition: export_csv.modules.php:155
jsonOrUnserialize
jsonOrUnserialize($stringtodecode)
Decode an encode string.
Definition: functions.lib.php:11693
ExportCsv\write_footer
write_footer($outputlangs)
Output footer into file.
Definition: export_csv.modules.php:319
ExportCsv\csvClean
csvClean($newvalue, $charset)
Clean a cell to respect rules of CSV file cells Note: It uses $this->separator Note: We keep this fun...
Definition: export_csv.modules.php:348
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1669
ExportCsv\getDriverLabel
getDriverLabel()
getDriverLabel
Definition: export_csv.modules.php:105
ExportCsv\getDriverVersion
getDriverVersion()
getDriverVersion
Definition: export_csv.modules.php:135
dol_htmlcleanlastbr
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
Definition: functions.lib.php:7418
ExportCsv\__construct
__construct($db)
Constructor.
Definition: export_csv.modules.php:66
ExportCsv\getDriverDesc
getDriverDesc()
getDriverDesc
Definition: export_csv.modules.php:115
ModeleExports
Parent class for export modules.
Definition: modules_export.php:31
ExportCsv\close_file
close_file()
Close file handle.
Definition: export_csv.modules.php:331
ExportCsv\write_title
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
Definition: export_csv.modules.php:213
ExportCsv\getDriverExtension
getDriverExtension()
getDriverExtension
Definition: export_csv.modules.php:125