24 require_once DOL_DOCUMENT_ROOT.
'/core/modules/export/modules_export.php';
50 public $version =
'dolibarr';
71 $this->separator =
',';
72 if (!empty($conf->global->EXPORT_CSV_SEPARATOR_TO_USE)) {
73 $this->separator = $conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
76 $this->enclosure =
'"';
80 $this->desc = $langs->trans(
"CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
81 $this->extension =
'csv';
82 $this->picto =
'mime/other';
83 $this->version =
'1.32';
86 $this->label_lib =
'Dolibarr';
87 $this->version_lib = DOL_VERSION;
127 return $this->extension;
137 return $this->version;
147 return $this->label_lib;
157 return $this->version_lib;
174 dol_syslog(
"ExportCsv::open_file file=".$file);
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);
213 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
218 if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) {
219 $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
221 $outputlangs->charset_output =
'ISO-8859-1';
224 foreach ($array_selected_sorted as $code => $value) {
225 $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]);
226 $newvalue = $this->
csvClean($newvalue, $outputlangs->charset_output);
228 fwrite($this->handle, $newvalue.$this->separator);
230 fwrite($this->handle,
"\n");
245 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
250 if (!empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) {
251 $outputlangs->charset_output = $conf->global->EXPORT_CSV_FORCE_CHARSET;
253 $outputlangs->charset_output =
'ISO-8859-1';
260 foreach ($array_selected_sorted as $code => $value) {
261 if (strpos($code,
' as ') == 0) {
262 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $code);
264 $alias = substr($code, strpos($code,
' as ') + 4);
267 dol_print_error(
'',
'Bad value for field with key='.$code.
'. Try to redefine export.');
270 $newvalue = $outputlangs->convToOutputCharset($objp->$alias);
271 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
274 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg)) {
275 $newvalue = $outputlangs->transnoentities($reg[1]);
278 $newvalue = $this->
csvClean($newvalue, $outputlangs->charset_output);
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];
286 fwrite($this->handle, $newvalue.$this->separator);
290 fwrite($this->handle,
"\n");
316 fclose($this->handle);
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) {
347 $newvalue = $oldvalue;
352 if (preg_match(
'/"/', $newvalue)) {
354 $newvalue = str_replace(
'"',
'""', $newvalue);
358 if (preg_match(
'/'.$this->separator.
'/', $newvalue)) {
362 return ($addquote ?
'"' :
'').$newvalue.($addquote ?
'"' :
'');