Skip to content

Commit

Permalink
Add option to serialize csv using row header (#433)
Browse files Browse the repository at this point in the history
* Add option to seralize csv using row header

* Use native types

Co-authored-by: mcop1 <[email protected]>

---------

Co-authored-by: mcop1 <[email protected]>
  • Loading branch information
cancan101 and mcop1 authored Nov 22, 2024
1 parent 9037901 commit 3d28325
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/DataSource/Interpreter/CsvFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class CsvFileInterpreter extends AbstractInterpreter
*/
protected $skipFirstRow;

protected bool $saveHeaderName;

/**
* @var string
*/
Expand All @@ -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);
Expand All @@ -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'] ?? '\\';
Expand Down Expand Up @@ -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);
Expand All @@ -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++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d28325

Please sign in to comment.