Skip to content

Commit

Permalink
Merge pull request Dolibarr#22361 from Hystepik/develop#4
Browse files Browse the repository at this point in the history
Close Dolibarr#20215 : export label of extrafields
  • Loading branch information
eldy authored Sep 23, 2022
2 parents 020458c + 4d0507b commit 805e5be
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
25 changes: 21 additions & 4 deletions htdocs/core/modules/export/export_csv.modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,21 @@ public function write_title($array_export_fields_label, $array_selected_sorted,
} 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;
Expand Down Expand Up @@ -256,7 +265,7 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
$this->col = 0;

$reg = array();

$selectlabelvalues = array();
foreach ($array_selected_sorted as $code => $value) {
if (strpos($code, ' as ') == 0) {
$alias = str_replace(array('.', '-', '(', ')'), '_', $code);
Expand All @@ -279,14 +288,22 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
$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;
Expand Down
52 changes: 48 additions & 4 deletions htdocs/core/modules/export/export_excel2007.modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public function write_title($array_export_fields_label, $array_selected_sorted,
// 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)) {
Expand All @@ -264,6 +265,11 @@ public function write_title($array_export_fields_label, $array_selected_sorted,
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 {
Expand All @@ -274,6 +280,17 @@ public function write_title($array_export_fields_label, $array_selected_sorted,
}
$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;
}
Expand All @@ -300,7 +317,7 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
}

$reg = array();

$selectlabelvalues = array();
foreach ($array_selected_sorted as $code => $value) {
if (strpos($code, ' as ') == 0) {
$alias = str_replace(array('.', '-', '(', ')'), '_', $code);
Expand All @@ -316,9 +333,13 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
$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
Expand Down Expand Up @@ -350,6 +371,29 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
}
$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;
}
Expand Down
24 changes: 21 additions & 3 deletions htdocs/core/modules/export/export_tsv.modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,20 @@ public function write_header($outputlangs)
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;
Expand All @@ -232,6 +241,7 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
global $conf;

$this->col = 0;
$selectlabelvalues = array();
foreach ($array_selected_sorted as $code => $value) {
if (strpos($code, ' as ') == 0) {
$alias = str_replace(array('.', '-', '(', ')'), '_', $code);
Expand All @@ -253,14 +263,22 @@ public function write_record($array_selected_sorted, $objp, $outputlangs, $array
$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;
}
Expand Down

0 comments on commit 805e5be

Please sign in to comment.