From 241bc2a4459ff4bcec643995144d8bee6e35fa4e Mon Sep 17 00:00:00 2001 From: slri Date: Wed, 8 Mar 2023 05:57:48 -0700 Subject: [PATCH] fix: configure csv writer's escape character to be an empty string by default (#375) * fix: configure csv writer's escape character to be an empty string by default * feat: csv escape character should be configurable --------- Co-authored-by: Irina Balaban --- .../Form/Type/ExportProvider/CsvProviderType.php | 3 ++- src/DataDefinitionsBundle/Provider/CsvProvider.php | 6 ++++++ .../Resources/public/pimcore/js/export_provider/csv.js | 7 +++++++ .../Resources/translations/admin.en.yml | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/DataDefinitionsBundle/Form/Type/ExportProvider/CsvProviderType.php b/src/DataDefinitionsBundle/Form/Type/ExportProvider/CsvProviderType.php index 8b39f37a..2ecd4cf3 100644 --- a/src/DataDefinitionsBundle/Form/Type/ExportProvider/CsvProviderType.php +++ b/src/DataDefinitionsBundle/Form/Type/ExportProvider/CsvProviderType.php @@ -26,6 +26,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('delimiter', TextType::class) - ->add('enclosure', TextType::class); + ->add('enclosure', TextType::class) + ->add('escape', TextType::class, ['empty_data' => '']); } } diff --git a/src/DataDefinitionsBundle/Provider/CsvProvider.php b/src/DataDefinitionsBundle/Provider/CsvProvider.php index 49a20805..57b38d68 100644 --- a/src/DataDefinitionsBundle/Provider/CsvProvider.php +++ b/src/DataDefinitionsBundle/Provider/CsvProvider.php @@ -139,6 +139,9 @@ public function exportData(array $configuration, ExportDefinitionInterface $defi $writer = Writer::createFromPath($file, 'w+'); $writer->setDelimiter($configuration['delimiter']); $writer->setEnclosure($configuration['enclosure']); + if (isset($configuration['escape'])) { + $writer->setEscape($configuration['escape']); + } $writer->insertOne($headers); $writer->insertAll($this->exportData); } @@ -161,6 +164,9 @@ public function provideArtifactStream(array $configuration, ExportDefinitionInte $writer = Writer::createFromStream($stream); $writer->setDelimiter($configuration['delimiter']); $writer->setEnclosure($configuration['enclosure']); + if (isset($configuration['escape'])) { + $writer->setEscape($configuration['escape']); + } $writer->insertOne($headers); $writer->insertAll($this->exportData); diff --git a/src/DataDefinitionsBundle/Resources/public/pimcore/js/export_provider/csv.js b/src/DataDefinitionsBundle/Resources/public/pimcore/js/export_provider/csv.js index 37cc4a9c..a594cafa 100644 --- a/src/DataDefinitionsBundle/Resources/public/pimcore/js/export_provider/csv.js +++ b/src/DataDefinitionsBundle/Resources/public/pimcore/js/export_provider/csv.js @@ -27,6 +27,13 @@ pimcore.plugin.datadefinitions.export_provider.csv = Class.create(pimcore.plugin fieldLabel: t('data_definitions_csv_enclosure'), anchor: '100%', value: this.data['enclosure'] ? this.data.enclosure : '"' + }, { + xtype: 'textfield', + name: 'escape', + fieldLabel: t('data_definitions_csv_escape'), + anchor: '100%', + maxLength: 1, + value: this.data['escape'] || '' === this.data['escape'] ? this.data.escape : '\\' }]; } }); diff --git a/src/DataDefinitionsBundle/Resources/translations/admin.en.yml b/src/DataDefinitionsBundle/Resources/translations/admin.en.yml index 9dedb5b1..f784385e 100644 --- a/src/DataDefinitionsBundle/Resources/translations/admin.en.yml +++ b/src/DataDefinitionsBundle/Resources/translations/admin.en.yml @@ -5,6 +5,7 @@ data_definitions_provider_settings: 'Provider Settings' data_definitions_loader: 'Loader' data_definitions_csv_example: 'CSV Example' data_definitions_csv_enclosure: 'Enclosure' +data_definitions_csv_escape: 'Escape' data_definitions_csv_delimiter: 'Delimiter' data_definitions_excel_headers: 'Excel headers' data_definitions_excel_file: 'Excel example file'