diff --git a/composer.json b/composer.json index daff1cd7..ba8028c4 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "doctrine/dbal": "^2.12 || ^3.5", "dragonmantank/cron-expression": "^3.1", "league/flysystem-sftp-v3": "^3.0", + "mtdowling/jmespath.php": "^2.8", "nesbot/carbon": "^2.27", "phpoffice/phpspreadsheet": "^1.24 || ^2.2", "pimcore/compatibility-bridge-v10": "^1.0", diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index 4b969d21..ff53c2ee 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -15,11 +15,14 @@ namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter; +use JmesPath\Env as JmesPath; use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle; use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData; class JsonFileInterpreter extends AbstractInterpreter { + protected string $path; + /** * @var array|null */ @@ -30,15 +33,23 @@ class JsonFileInterpreter extends AbstractInterpreter */ protected $cachedFilePath = null; + protected function loadDataRaw(string $path): array { + $content = file_get_contents($path); + return json_decode($this->prepareContent($content), true); + } + protected function loadData(string $path): array { - if ($this->cachedFilePath === $path && !empty($this->cachedContent)) { - $content = file_get_contents($path); - - return json_decode($this->prepareContent($content), true); + if ($this->cachedFilePath !== $path || empty($this->cachedContent)) { + $data = $this->loadDataRaw($path); } else { - return $this->cachedContent; + $data = $this->cachedContent; + } + + if (!empty($this->path)) { + return $this->getValueFromPath($data); } + return $data; } protected function doInterpretFileAndCallProcessRow(string $path): void @@ -52,7 +63,7 @@ protected function doInterpretFileAndCallProcessRow(string $path): void public function setSettings(array $settings): void { - //nothing to do + $this->path = $settings['path']; } /** @@ -86,9 +97,7 @@ public function fileValid(string $path, bool $originalFilename = false): bool } } - $content = file_get_contents($path); - - $data = json_decode($this->prepareContent($content), true); + $data = $this->loadDataRaw($path); if (json_last_error() === JSON_ERROR_NONE) { $this->cachedContent = $data; @@ -132,4 +141,12 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns); } + + /** + * Returns a value from the specified path in a nested array `$data`. + */ + private function getValueFromPath(array $data): mixed + { + return JmesPath::search($this->path, $data); + } } diff --git a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js index 63f0866a..2d7ebc7d 100644 --- a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js +++ b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js @@ -26,6 +26,12 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.js }, border: false, items: [ + { + xtype: 'textfield', + fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_json_path'), + name: this.dataNamePrefix + 'path', + value: this.data.path || '', + } ] }); } diff --git a/src/Resources/translations/admin.en.yml b/src/Resources/translations/admin.en.yml index 918854e2..a401e568 100644 --- a/src/Resources/translations/admin.en.yml +++ b/src/Resources/translations/admin.en.yml @@ -45,6 +45,7 @@ plugin_pimcore_datahub_data_importer_configpanel_csv_escape: Escape plugin_pimcore_datahub_data_importer_configpanel_xlsx_sheet: Sheet plugin_pimcore_datahub_data_importer_configpanel_xml_xpath: XPath plugin_pimcore_datahub_data_importer_configpanel_xml_schema: Schema +plugin_pimcore_datahub_data_importer_configpanel_json_path: Path plugin_pimcore_datahub_data_importer_configpanel_import_settings: Import Settings plugin_pimcore_datahub_data_importer_configpanel_import_preview: Import Preview plugin_pimcore_datahub_data_importer_configpanel_preview_data_clone: Copy preview data from data source for preview.