diff --git a/htdocs/core/modules/export/export_csv.modules.php b/htdocs/core/modules/export/export_csv.modules.php index 4b3ede2ae14..26f20378b23 100644 --- a/htdocs/core/modules/export/export_csv.modules.php +++ b/htdocs/core/modules/export/export_csv.modules.php @@ -220,12 +220,21 @@ class ExportCsv extends ModeleExports } else { $outputlangs->charset_output = 'ISO-8859-1'; } + $selectlabel = array(); foreach ($array_selected_sorted as $code => $value) { $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output); fwrite($this->handle, $newvalue.$this->separator); + $typefield = isset($array_types[$code]) ? $array_types[$code] : ''; + + if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { + $selectlabel[$code."_label"] = $newvalue."_label"; + } + } + foreach ($selectlabel as $key => $value) { + fwrite($this->handle, $value.$this->separator); } fwrite($this->handle, "\n"); return 0; @@ -256,7 +265,7 @@ class ExportCsv extends ModeleExports $this->col = 0; $reg = array(); - + $selectlabelvalues = array(); foreach ($array_selected_sorted as $code => $value) { if (strpos($code, ' as ') == 0) { $alias = str_replace(array('.', '-', '(', ')'), '_', $code); @@ -279,14 +288,22 @@ class ExportCsv extends ModeleExports $newvalue = $this->csvClean($newvalue, $outputlangs->charset_output); if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { - $array = json_decode($typefield, true); - $array = $array['options']; - $newvalue = $array[$newvalue]; + $array = jsonOrUnserialize($typefield); + if (is_array($array) && !empty($newvalue)) { + $array = $array['options']; + $selectlabelvalues[$code."_label"] = $array[$newvalue]; + } else { + $selectlabelvalues[$code."_label"] = ""; + } } fwrite($this->handle, $newvalue.$this->separator); $this->col++; } + foreach ($selectlabelvalues as $key => $value) { + fwrite($this->handle, $value.$this->separator); + $this->col++; + } fwrite($this->handle, "\n"); return 0; diff --git a/htdocs/core/modules/export/export_excel2007.modules.php b/htdocs/core/modules/export/export_excel2007.modules.php index 54842ff8278..13967843a08 100644 --- a/htdocs/core/modules/export/export_excel2007.modules.php +++ b/htdocs/core/modules/export/export_excel2007.modules.php @@ -253,6 +253,7 @@ class ExportExcel2007 extends ModeleExports // Create a format for the column headings $this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true); $this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT); + $selectlabel = array(); $this->col = 1; if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) { @@ -264,6 +265,11 @@ class ExportExcel2007 extends ModeleExports if (empty($alias)) { dol_print_error('', 'Bad value for field with code='.$code.'. Try to redefine export.'); } + $typefield = isset($array_types[$code]) ? $array_types[$code] : ''; + + if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { + $selectlabel[$code."_label"] = $alias."_label"; + } if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) { $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($alias), $formatheader); } else { @@ -274,6 +280,17 @@ class ExportExcel2007 extends ModeleExports } $this->col++; } + foreach ($selectlabel as $key => $value) { + if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) { + $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($value), $formatheader); + } else { + $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($value)); + if (!empty($array_types[$code]) && in_array($array_types[$code], array('Date', 'Numeric', 'TextAuto'))) { // Set autowidth for some types + $this->workbook->getActiveSheet()->getColumnDimension($this->column2Letter($this->col + 1))->setAutoSize(true); + } + } + $this->col++; + } $this->row++; return 0; } @@ -300,7 +317,7 @@ class ExportExcel2007 extends ModeleExports } $reg = array(); - + $selectlabelvalues = array(); foreach ($array_selected_sorted as $code => $value) { if (strpos($code, ' as ') == 0) { $alias = str_replace(array('.', '-', '(', ')'), '_', $code); @@ -316,9 +333,13 @@ class ExportExcel2007 extends ModeleExports $typefield = isset($array_types[$code]) ? $array_types[$code] : ''; if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { - $array = json_decode($typefield, true); - $array = $array['options']; - $newvalue = $array[$newvalue]; + $array = jsonOrUnserialize($typefield); + if (is_array($array) && !empty($newvalue)) { + $array = $array['options']; + $selectlabelvalues[$code."_label"] = $array[$newvalue]; + } else { + $selectlabelvalues[$code."_label"] = ""; + } } // Traduction newvalue @@ -350,6 +371,29 @@ class ExportExcel2007 extends ModeleExports } $this->col++; } + foreach ($selectlabelvalues as $key => $newvalue) { + if (preg_match('/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) { + $newvalue = dol_stringtotime($newvalue); + $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue)); + $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate(); + $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode('yyyy-mm-dd'); + } elseif (preg_match('/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/i', $newvalue)) { + $newvalue = dol_stringtotime($newvalue); + $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue)); + $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate(); + $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode('yyyy-mm-dd h:mm:ss'); + } else { + if ($typefield == 'Text' || $typefield == 'TextAuto') { + $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (string) $newvalue); + $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate(); + $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode('@'); + $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT); + } else { + $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue); + } + } + $this->col++; + } $this->row++; return 0; } diff --git a/htdocs/core/modules/export/export_tsv.modules.php b/htdocs/core/modules/export/export_tsv.modules.php index 7718dd3e350..40ee75a1749 100644 --- a/htdocs/core/modules/export/export_tsv.modules.php +++ b/htdocs/core/modules/export/export_tsv.modules.php @@ -205,11 +205,20 @@ class ExportTsv extends ModeleExports public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types) { // phpcs:enable + $selectlabel = array(); foreach ($array_selected_sorted as $code => $value) { $newvalue = $outputlangs->transnoentities($array_export_fields_label[$code]); // newvalue is now $outputlangs->charset_output encoded $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output); fwrite($this->handle, $newvalue.$this->separator); + $typefield = isset($array_types[$code]) ? $array_types[$code] : ''; + + if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { + $selectlabel[$code."_label"] = $newvalue."_label"; + } + } + foreach ($selectlabel as $key => $value) { + fwrite($this->handle, $value.$this->separator); } fwrite($this->handle, "\n"); return 0; @@ -232,6 +241,7 @@ class ExportTsv extends ModeleExports global $conf; $this->col = 0; + $selectlabelvalues = array(); foreach ($array_selected_sorted as $code => $value) { if (strpos($code, ' as ') == 0) { $alias = str_replace(array('.', '-', '(', ')'), '_', $code); @@ -253,14 +263,22 @@ class ExportTsv extends ModeleExports $newvalue = $this->tsv_clean($newvalue, $outputlangs->charset_output); if (preg_match('/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) { - $array = json_decode($typefield, true); - $array = $array['options']; - $newvalue = $array[$newvalue]; + $array = jsonOrUnserialize($typefield); + if (is_array($array) && !empty($newvalue)) { + $array = $array['options']; + $selectlabelvalues[$code."_label"] = $array[$newvalue]; + } else { + $selectlabelvalues[$code."_label"] = ""; + } } fwrite($this->handle, $newvalue.$this->separator); $this->col++; } + foreach ($selectlabelvalues as $key => $value) { + fwrite($this->handle, $value.$this->separator); + $this->col++; + } fwrite($this->handle, "\n"); return 0; }