Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to serialize csv using row header #433

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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){
mcop1 marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading