dolibarr  16.0.5
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 
224  foreach ($array_selected_sorted as $code => $value) {
225  $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded
226  $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
227 
228  fwrite($this->handle, $newvalue.$this->separator);
229  }
230  fwrite($this->handle, "\n");
231  return 0;
232  }
233 
234 
235  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245  public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
246  {
247  // phpcs:enable
248  global $conf;
249 
250  if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) {
251  $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
252  } else {
253  $outputlangs->charset_output = 'ISO-8859-1';
254  }
255 
256  $this->col = 0;
257 
258  $reg = array();
259 
260  foreach ($array_selected_sorted as $code => $value) {
261  if (strpos($code, ' as ') == 0) {
262  $alias = str_replace(array('.', '-', '(', ')'), '_', $code);
263  } else {
264  $alias = substr($code, strpos($code, ' as ') + 4);
265  }
266  if (empty($alias)) {
267  dol_print_error('', 'Bad value for field with key='.$code.'. Try to redefine export.');
268  }
269 
270  $newvalue = $outputlangs->convToOutputCharset($objp->$alias); // objp->$alias must be utf8 encoded as any var in memory // newvalue is now $outputlangs->charset_output encoded
271  $typefield = isset($array_types[$code]) ? $array_types[$code] : '';
272 
273  // Translation newvalue
274  if (preg_match('/^\((.*)\)$/i', $newvalue, $reg)) {
275  $newvalue = $outputlangs->transnoentities($reg[1]);
276  }
277 
278  $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output);
279 
280  if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
281  $array = json_decode($typefield, true);
282  $array = $array['options'];
283  $newvalue = $array[$newvalue];
284  }
285 
286  fwrite($this->handle, $newvalue.$this->separator);
287  $this->col++;
288  }
289 
290  fwrite($this->handle, "\n");
291  return 0;
292  }
293 
294  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
301  public function write_footer($outputlangs)
302  {
303  // phpcs:enable
304  return 0;
305  }
306 
307  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
313  public function close_file()
314  {
315  // phpcs:enable
316  fclose($this->handle);
317  return 0;
318  }
319 
320 
330  public function csvClean($newvalue, $charset)
331  {
332  global $conf;
333  $addquote = 0;
334 
335  // Rule Dolibarr: No HTML
336  //print $charset.' '.$newvalue."\n";
337  //$newvalue=dol_string_nohtmltag($newvalue,0,$charset);
338  $newvalue = dol_htmlcleanlastbr($newvalue);
339  //print $charset.' '.$newvalue."\n";
340 
341  // 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)
342  $oldvalue = $newvalue;
343  $newvalue = str_replace("\r", '', $newvalue);
344  $newvalue = str_replace("\n", '\n', $newvalue);
345  if (!empty($conf->global->USE_STRICT_CSV_RULES) && $oldvalue != $newvalue) {
346  // If strict use of CSV rules, we just add quote
347  $newvalue = $oldvalue;
348  $addquote = 1;
349  }
350 
351  // Rule 2 CSV: If value contains ", we must escape with ", and add "
352  if (preg_match('/"/', $newvalue)) {
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  $addquote = 1;
360  }
361 
362  return ($addquote ? '"' : '').$newvalue.($addquote ? '"' : '');
363  }
364 }
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:245
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:4844
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
ExportCsv\write_footer
write_footer($outputlangs)
Output footer into file.
Definition: export_csv.modules.php:301
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:330
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
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:7036
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:313
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