From 3d28325681cb6fb6ea675fe30788d164b942caca Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Fri, 22 Nov 2024 04:27:10 -0500 Subject: [PATCH] Add option to serialize csv using row header (#433) * Add option to seralize csv using row header * Use native types Co-authored-by: mcop1 <89011527+mcop1@users.noreply.github.com> --------- Co-authored-by: mcop1 <89011527+mcop1@users.noreply.github.com> --- .../Interpreter/CsvFileInterpreter.php | 25 +++++++++++++++++-- .../components/interpreter/csv.js | 6 +++++ src/Resources/translations/admin.en.yml | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/DataSource/Interpreter/CsvFileInterpreter.php b/src/DataSource/Interpreter/CsvFileInterpreter.php index 296d7633..d8eaffb8 100644 --- a/src/DataSource/Interpreter/CsvFileInterpreter.php +++ b/src/DataSource/Interpreter/CsvFileInterpreter.php @@ -27,6 +27,8 @@ class CsvFileInterpreter extends AbstractInterpreter */ protected $skipFirstRow; + protected bool $saveHeaderName; + /** * @var string */ @@ -47,12 +49,19 @@ protected function doInterpretFileAndCallProcessRow(string $path): void if (($handle = fopen($path, 'r')) !== false) { $this->skipByteOrderMark($handle); + $header = null; if ($this->skipFirstRow) { //load first row and ignore it $data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape); + if($this->saveHeaderName){ + $header = $data; + } } while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape)) !== false) { + if($header !== null){ + $data = array_combine($header, $data); + } $this->processImportRow($data); } fclose($handle); @@ -62,6 +71,7 @@ protected function doInterpretFileAndCallProcessRow(string $path): void public function setSettings(array $settings): void { $this->skipFirstRow = $settings['skipFirstRow'] ?? false; + $this->saveHeaderName = $settings['saveHeaderName'] ?? false; $this->delimiter = $settings['delimiter'] ?? ','; $this->enclosure = $settings['enclosure'] ?? '"'; $this->escape = $settings['escape'] ?? '\\'; @@ -90,6 +100,7 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo $previewData = []; $columns = []; $readRecordNumber = -1; + $header = null; if ($this->fileValid($path) && ($handle = fopen($path, 'r')) !== false) { $this->skipByteOrderMark($handle); @@ -98,13 +109,23 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo //load first row and ignore it $data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape); - foreach ($data as $index => $columnHeader) { - $columns[$index] = trim($columnHeader) . " [$index]"; + if($this->saveHeaderName){ + $header = $data; + foreach ($data as $index => $columnHeader) { + $columns[$columnHeader] = trim($columnHeader); + } + }else{ + foreach ($data as $index => $columnHeader) { + $columns[$index] = trim($columnHeader) . " [$index]"; + } } } $previousData = null; while ($readRecordNumber < $recordNumber && ($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape)) !== false) { + if($header !== null){ + $data = array_combine($header, $data); + } $previousData = $data; $readRecordNumber++; } diff --git a/src/Resources/public/js/pimcore/configuration/components/interpreter/csv.js b/src/Resources/public/js/pimcore/configuration/components/interpreter/csv.js index ed5fde45..357c6341 100644 --- a/src/Resources/public/js/pimcore/configuration/components/interpreter/csv.js +++ b/src/Resources/public/js/pimcore/configuration/components/interpreter/csv.js @@ -34,6 +34,12 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.cs name: this.dataNamePrefix + 'skipFirstRow', value: this.data.hasOwnProperty('skipFirstRow') ? this.data.skipFirstRow : false, inputValue: true + },{ + xtype: 'checkbox', + fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_csv_save_row_header'), + name: this.dataNamePrefix + 'saveHeaderName', + value: this.data.hasOwnProperty('saveHeaderName') ? this.data.saveHeaderName : false, + inputValue: true },{ xtype: 'textfield', fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_csv_delimiter'), diff --git a/src/Resources/translations/admin.en.yml b/src/Resources/translations/admin.en.yml index 918854e2..2be41434 100644 --- a/src/Resources/translations/admin.en.yml +++ b/src/Resources/translations/admin.en.yml @@ -39,6 +39,7 @@ plugin_pimcore_datahub_data_importer_configpanel_id_data_index: Data index of ID plugin_pimcore_datahub_data_importer_configpanel_cleanup.strategy_delete: Delete plugin_pimcore_datahub_data_importer_configpanel_cleanup.strategy_unpublish: Unpublish plugin_pimcore_datahub_data_importer_configpanel_csv_skip_first_row: Skip First Row +plugin_pimcore_datahub_data_importer_configpanel_csv_save_row_header: Save using Row Header plugin_pimcore_datahub_data_importer_configpanel_csv_delimiter: Delimiter plugin_pimcore_datahub_data_importer_configpanel_csv_enclosure: Enclosure plugin_pimcore_datahub_data_importer_configpanel_csv_escape: Escape