vendor/pimcore/data-importer/src/Controller/ConfigDataObjectController.php line 577

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\DataImporterBundle\Controller;
  15. use Cron\CronExpression;
  16. use League\Flysystem\FilesystemOperator;
  17. use Pimcore\Bundle\AdminBundle\Helper\QueryParams;
  18. use Pimcore\Bundle\DataHubBundle\Configuration\Dao;
  19. use Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter\InterpreterFactory;
  20. use Pimcore\Bundle\DataImporterBundle\DataSource\Loader\DataLoaderFactory;
  21. use Pimcore\Bundle\DataImporterBundle\DataSource\Loader\PushLoader;
  22. use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
  23. use Pimcore\Bundle\DataImporterBundle\Mapping\MappingConfigurationFactory;
  24. use Pimcore\Bundle\DataImporterBundle\Mapping\Type\ClassificationStoreDataTypeService;
  25. use Pimcore\Bundle\DataImporterBundle\Mapping\Type\TransformationDataTypeService;
  26. use Pimcore\Bundle\DataImporterBundle\Preview\PreviewService;
  27. use Pimcore\Bundle\DataImporterBundle\Processing\ImportPreparationService;
  28. use Pimcore\Bundle\DataImporterBundle\Processing\ImportProcessingService;
  29. use Pimcore\Bundle\DataImporterBundle\Settings\ConfigurationPreparationService;
  30. use Pimcore\Logger;
  31. use Pimcore\Model\DataObject;
  32. use Pimcore\Model\DataObject\QuantityValue\Unit;
  33. use Pimcore\Translation\Translator;
  34. use Symfony\Component\HttpFoundation\JsonResponse;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\Routing\Annotation\Route;
  37. /**
  38.  * @Route("/admin/pimcoredataimporter/dataobject/config")
  39.  */
  40. class ConfigDataObjectController extends \Pimcore\Bundle\AdminBundle\Controller\AdminController
  41. {
  42.     public const CONFIG_NAME 'plugin_datahub_config';
  43.     /**
  44.      * @var PreviewService
  45.      */
  46.     protected $previewService;
  47.     /**
  48.      * ConfigDataObjectController constructor.
  49.      *
  50.      * @param PreviewService $previewService
  51.      */
  52.     public function __construct(PreviewService $previewService)
  53.     {
  54.         $this->previewService $previewService;
  55.     }
  56.     /**
  57.      * @Route("/save")
  58.      *
  59.      * @param Request $request
  60.      *
  61.      * @return JsonResponse
  62.      *
  63.      * @throws \Exception
  64.      *
  65.      */
  66.     public function saveAction(Request $request): ?JsonResponse
  67.     {
  68.         $this->checkPermission(self::CONFIG_NAME);
  69.         try {
  70.             $data $request->get('data');
  71.             $modificationDate $request->get('modificationDate'0);
  72.             if ($modificationDate Dao::getConfigModificationDate()) {
  73.                 throw new \Exception('The configuration was modified during editing, please reload the configuration and make your changes again');
  74.             }
  75.             $dataDecoded json_decode($datatrue);
  76.             $name $dataDecoded['general']['name'];
  77.             $dataDecoded['general']['active'] = $dataDecoded['general']['active'] ?? false;
  78.             $config Dao::getByName($name);
  79.             if (!$config->isAllowed('update')) {
  80.                 throw $this->createAccessDeniedHttpException();
  81.             }
  82.             $config->setConfiguration($dataDecoded);
  83.             // @phpstan-ignore-next-line isAllowed return can changed now
  84.             if ($config->isAllowed('read') && $config->isAllowed('update')) {
  85.                 $config->save();
  86.                 return $this->json(['success' => true'modificationDate' => Dao::getConfigModificationDate()]);
  87.             } else {
  88.                 return $this->json(['success' => false'permissionError' => true]);
  89.             }
  90.         } catch (\Exception $e) {
  91.             return $this->json(['success' => false'message' => $e->getMessage()]);
  92.         }
  93.     }
  94.     protected function loadAvailableColumnHeaders(
  95.         string $configName,
  96.         array $config,
  97.         InterpreterFactory $interpreterFactory
  98.     ) {
  99.         $previewFilePath $this->previewService->getLocalPreviewFile($configName$this->getAdminUser());
  100.         if (is_file($previewFilePath)) {
  101.             try {
  102.                 $interpreter $interpreterFactory->loadInterpreter($configName$config['interpreterConfig'], $config['processingConfig']);
  103.                 $dataPreview $interpreter->previewData($previewFilePath);
  104.                 return $dataPreview->getDataColumnHeaders();
  105.             } catch (\Exception $e) {
  106.                 Logger::warning($e);
  107.             }
  108.         }
  109.         return [];
  110.     }
  111.     /**
  112.      * @Route("/get")
  113.      *
  114.      * @param Request $request
  115.      * @param ConfigurationPreparationService $configurationPreparationService
  116.      * @param InterpreterFactory $interpreterFactory
  117.      *
  118.      * @return JsonResponse
  119.      *
  120.      * @throws \Exception
  121.      */
  122.     public function getAction(Request $request,
  123.         ConfigurationPreparationService $configurationPreparationService,
  124.         InterpreterFactory $interpreterFactory
  125.     ): JsonResponse {
  126.         $this->checkPermission(self::CONFIG_NAME);
  127.         $name $request->get('name');
  128.         $config $configurationPreparationService->prepareConfiguration($name);
  129.         return new JsonResponse(
  130.             [
  131.                 'name' => $name,
  132.                 'configuration' => $config,
  133.                 'userPermissions' => $config['userPermissions'],
  134.                 'modificationDate' => Dao::getConfigModificationDate(),
  135.                 'columnHeaders' => $this->loadAvailableColumnHeaders($name$config$interpreterFactory)
  136.             ]
  137.         );
  138.     }
  139.     /**
  140.      * @Route("/upload-preview", methods={"POST"})
  141.      *
  142.      * @param Request $request
  143.      *
  144.      * @return JsonResponse
  145.      *
  146.      * @throws \Exception
  147.      */
  148.     public function uploadPreviewDataAction(Request $request)
  149.     {
  150.         try {
  151.             if (array_key_exists('Filedata'$_FILES)) {
  152.                 $filename $_FILES['Filedata']['name'];
  153.                 $sourcePath $_FILES['Filedata']['tmp_name'];
  154.             } else {
  155.                 throw new \Exception('The filename of the preview data is empty');
  156.             }
  157.             if (is_file($sourcePath) && filesize($sourcePath) < 1) {
  158.                 throw new \Exception('File is empty!');
  159.             } elseif (!is_file($sourcePath)) {
  160.                 throw new \Exception('Something went wrong, please check upload_max_filesize and post_max_size in your php.ini and write permissions of your temporary directories.');
  161.             }
  162.             if (filesize($sourcePath) > 10485760) { //10 MB
  163.                 throw new \Exception('File it too big for preview file, please create a smaller one');
  164.             }
  165.             $this->previewService->writePreviewFile($request->get('config_name'), $sourcePath$this->getAdminUser());
  166.             @unlink($sourcePath);
  167.             return new JsonResponse(['success' => true]);
  168.         } catch (\Exception $e) {
  169.             Logger::error($e);
  170.             return $this->adminJson([
  171.                 'success' => false,
  172.                 'message' => $e->getMessage(),
  173.             ]);
  174.         }
  175.     }
  176.     /**
  177.      * @Route("/copy-preview", methods={"POST"})
  178.      *
  179.      * @param Request $request
  180.      * @param ConfigurationPreparationService $configurationPreparationService
  181.      * @param DataLoaderFactory $dataLoaderFactory
  182.      *
  183.      * @return JsonResponse
  184.      *
  185.      * @throws \Exception
  186.      */
  187.     public function copyPreviewDataAction(
  188.         Request $request,
  189.         ConfigurationPreparationService $configurationPreparationService,
  190.         DataLoaderFactory $dataLoaderFactory
  191.     ) {
  192.         try {
  193.             $configName $request->get('config_name');
  194.             $currentConfig $request->get('current_config');
  195.             $config $configurationPreparationService->prepareConfiguration($configName$currentConfig);
  196.             $loader $dataLoaderFactory->loadDataLoader($config['loaderConfig']);
  197.             if ($loader instanceof PushLoader) {
  198.                 throw new \Exception('Cannot copy data from push loader for preview.');
  199.             }
  200.             $sourcePath $loader->loadData();
  201.             if (is_file($sourcePath) && filesize($sourcePath) < 1) {
  202.                 throw new \Exception('File is empty!');
  203.             } elseif (!is_file($sourcePath)) {
  204.                 throw new \Exception('Something went wrong, please check upload_max_filesize and post_max_size in your php.ini and write permissions of your temporary directories.');
  205.             }
  206.             if (filesize($sourcePath) > 10485760) { //10 MB
  207.                 throw new \Exception('File it too big for preview file, please create a smaller one');
  208.             }
  209.             $this->previewService->writePreviewFile($request->get('config_name'), $sourcePath$this->getAdminUser());
  210.             $loader->cleanup();
  211.             return new JsonResponse(['success' => true]);
  212.         } catch (\Exception $e) {
  213.             Logger::error($e);
  214.             return $this->adminJson([
  215.                 'success' => false,
  216.                 'message' => $e->getMessage(),
  217.             ]);
  218.         }
  219.     }
  220.     /**
  221.      * @Route("/load-preview-data", methods={"POST"})
  222.      *
  223.      * @param Request $request
  224.      * @param ConfigurationPreparationService $configurationPreparationService
  225.      * @param InterpreterFactory $interpreterFactory
  226.      * @param Translator $translator
  227.      *
  228.      * @return JsonResponse
  229.      *
  230.      * @throws \Exception
  231.      */
  232.     public function loadDataPreviewAction(
  233.         Request $request,
  234.         ConfigurationPreparationService $configurationPreparationService,
  235.         InterpreterFactory $interpreterFactory,
  236.         Translator $translator
  237.     ) {
  238.         $configName $request->get('config_name');
  239.         $currentConfig $request->get('current_config');
  240.         $recordNumber intval($request->get('record_number'));
  241.         $dataPreview null;
  242.         $hasData false;
  243.         $errorMessage '';
  244.         $previewFilePath $this->previewService->getLocalPreviewFile($configName$this->getAdminUser());
  245.         if (is_file($previewFilePath)) {
  246.             $config $configurationPreparationService->prepareConfiguration($configName$currentConfig);
  247.             $mappedColumns = [];
  248.             foreach (($config['mappingConfig'] ?? []) as $mapping) {
  249.                 if (isset($mapping['dataSourceIndex']) && is_array($mapping['dataSourceIndex'])) {
  250.                     $mappedColumns array_merge($mappedColumns$mapping['dataSourceIndex']);
  251.                 }
  252.             }
  253.             $mappedColumns array_unique($mappedColumns);
  254.             try {
  255.                 $interpreter $interpreterFactory->loadInterpreter($configName$config['interpreterConfig'], $config['processingConfig']);
  256.                 if ($interpreter->fileValid($previewFilePath)) {
  257.                     $dataPreview $interpreter->previewData($previewFilePath$recordNumber$mappedColumns);
  258.                     $hasData true;
  259.                 } else {
  260.                     $errorMessage $translator->trans('plugin_pimcore_datahub_data_importer_configpanel_preview_error_invalid_file', [], 'admin');
  261.                 }
  262.             } catch (\Exception $e) {
  263.                 Logger::error($e);
  264.                 $errorMessage $translator->trans('plugin_pimcore_datahub_data_importer_configpanel_preview_error_prefix', [], 'admin') . ': ' $e->getMessage();
  265.             }
  266.         }
  267.         return new JsonResponse([
  268.             'dataPreview' => $dataPreview $dataPreview->getDataPreview() : [],
  269.             'previewRecordIndex' => $dataPreview $dataPreview->getRecordNumber() : 0,
  270.             'hasData' => $hasData,
  271.             'errorMessage' => $errorMessage
  272.         ]);
  273.     }
  274.     /**
  275.      * @Route("/load-column-headers", methods={"POST"})
  276.      *
  277.      * @param Request $request
  278.      * @param ConfigurationPreparationService $configurationPreparationService
  279.      * @param InterpreterFactory $interpreterFactory
  280.      *
  281.      * @return JsonResponse
  282.      *
  283.      * @throws \Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException|\Exception
  284.      */
  285.     public function loadAvailableColumnHeadersAction(
  286.         Request $request,
  287.         ConfigurationPreparationService $configurationPreparationService,
  288.         InterpreterFactory $interpreterFactory
  289.     ) {
  290.         $configName $request->get('config_name');
  291.         $currentConfig $request->get('current_config');
  292.         $config $configurationPreparationService->prepareConfiguration($configName$currentConfig);
  293.         return new JsonResponse([
  294.             'columnHeaders' => $this->loadAvailableColumnHeaders($configName$config$interpreterFactory)
  295.         ]);
  296.     }
  297.     /**
  298.      * @Route("/load-transformation-result", methods={"POST"})
  299.      *
  300.      * @param Request $request
  301.      * @param ConfigurationPreparationService $configurationPreparationService
  302.      * @param MappingConfigurationFactory $factory
  303.      * @param InterpreterFactory $interpreterFactory
  304.      * @param ImportProcessingService $importProcessingService
  305.      *
  306.      * @return JsonResponse
  307.      *
  308.      * @throws InvalidConfigurationException|\Exception
  309.      */
  310.     public function loadTransformationResultPreviewsAction(
  311.         Request $request,
  312.         ConfigurationPreparationService $configurationPreparationService,
  313.         MappingConfigurationFactory $factory,
  314.         InterpreterFactory $interpreterFactory,
  315.         ImportProcessingService $importProcessingService
  316.     ) {
  317.         $configName $request->get('config_name');
  318.         $currentConfig $request->get('current_config');
  319.         $recordNumber intval($request->get('current_preview_record'));
  320.         $config $configurationPreparationService->prepareConfiguration($configName$currentConfig);
  321.         $previewFilePath $this->previewService->getLocalPreviewFile($configName$this->getAdminUser());
  322.         $importDataRow = [];
  323.         $transformationResults = [];
  324.         $errorMessage '';
  325.         try {
  326.             if (is_file($previewFilePath)) {
  327.                 $interpreter $interpreterFactory->loadInterpreter($configName$config['interpreterConfig'], $config['processingConfig']);
  328.                 $dataPreview $interpreter->previewData($previewFilePath$recordNumber);
  329.                 $importDataRow $dataPreview->getRawData();
  330.             }
  331.             $mapping $factory->loadMappingConfiguration($configName$config['mappingConfig'], true);
  332.             foreach ($mapping as $index => $mappingConfiguration) {
  333.                 $transformationResults[] = $importProcessingService->generateTransformationResultPreview($importDataRow$mappingConfiguration);
  334.             }
  335.         } catch (\Exception $e) {
  336.             Logger::error($e);
  337.             $errorMessage $e->getMessage();
  338.         }
  339.         return new JsonResponse([
  340.             'transformationResultPreviews' => $transformationResults,
  341.             'errorMessage' => $errorMessage
  342.         ]);
  343.     }
  344.     /**
  345.      * @Route("/calculate-transformation-result-type", methods={"POST"})
  346.      *
  347.      * @param Request $request
  348.      * @param MappingConfigurationFactory $factory
  349.      * @param ImportProcessingService $importProcessingService
  350.      *
  351.      * @return JsonResponse
  352.      */
  353.     public function calculateTransformationResultTypeAction(
  354.         Request $request,
  355.         MappingConfigurationFactory $factory,
  356.         ImportProcessingService $importProcessingService
  357.     ) {
  358.         try {
  359.             $currentConfig json_decode($request->get('current_config'), true);
  360.             $configName $request->get('config_name');
  361.             $mappingConfiguration $factory->loadMappingConfigurationItem($configName$currentConfigtrue);
  362.             return new JsonResponse($importProcessingService->evaluateTransformationResultDataType($mappingConfiguration));
  363.         } catch (InvalidConfigurationException $e) {
  364.             return new JsonResponse('ERROR: ' $e->getMessage());
  365.         }
  366.     }
  367.     /**
  368.      * @Route("/load-class-attributes", methods={"GET"})
  369.      *
  370.      * @param Request $request
  371.      * @param TransformationDataTypeService $transformationDataTypeService
  372.      *
  373.      * @return JsonResponse
  374.      *
  375.      * @throws \Exception
  376.      */
  377.     public function loadDataObjectAttributesAction(Request $requestTransformationDataTypeService $transformationDataTypeService)
  378.     {
  379.         $classId $request->get('class_id');
  380.         if (empty($classId)) {
  381.             return new JsonResponse([]);
  382.         }
  383.         $loadAdvancedRelations boolval($request->get('load_advanced_relations'false));
  384.         $includeSystemRead boolval($request->get('system_read'false));
  385.         $includeSystemWrite boolval($request->get('system_write'false));
  386.         $transformationTargetType $request->get('transformation_result_type', [TransformationDataTypeService::DEFAULT_TYPETransformationDataTypeService::NUMERIC]);
  387.         return new JsonResponse([
  388.             'attributes' => $transformationDataTypeService->getPimcoreDataTypes($classId$transformationTargetType$includeSystemRead$includeSystemWrite$loadAdvancedRelations)
  389.         ]);
  390.     }
  391.     /**
  392.      * @Route("/load-class-classificationstore-attributes", methods={"GET"})
  393.      *
  394.      * @param Request $request
  395.      * @param TransformationDataTypeService $transformationDataTypeService
  396.      *
  397.      * @return JsonResponse
  398.      */
  399.     public function loadDataObjectClassificationStoreAttributesAction(Request $requestTransformationDataTypeService $transformationDataTypeService)
  400.     {
  401.         $classId $request->get('class_id');
  402.         if (empty($classId)) {
  403.             return new JsonResponse([]);
  404.         }
  405.         return new JsonResponse([
  406.             'attributes' => $transformationDataTypeService->getClassificationStoreAttributes($classId)
  407.         ]);
  408.     }
  409.     /**
  410.      * @Route("/load-class-classificationstore-keys", methods={"GET"})
  411.      *
  412.      * @param Request $request
  413.      * @param ClassificationStoreDataTypeService $classificationStoreDataTypeService
  414.      *
  415.      * @return JsonResponse
  416.      *
  417.      * @throws \Exception
  418.      */
  419.     public function loadDataObjectClassificationStoreKeysAction(Request $requestClassificationStoreDataTypeService $classificationStoreDataTypeService)
  420.     {
  421.         $sortParams QueryParams::extractSortingSettings(['sort' => $request->get('sort')]);
  422.         $list $classificationStoreDataTypeService->listClassificationStoreKeyList(
  423.             strip_tags($request->get('class_id')),
  424.             strip_tags($request->get('field_name')),
  425.             strip_tags($request->get('transformation_result_type')),
  426.             $sortParams['orderKey'] ?? 'name',
  427.             $sortParams['order'] ?? 'ASC',
  428.             intval($request->get('start')),
  429.             intval($request->get('limit')),
  430.             strip_tags($request->get('searchfilter')),
  431.             strip_tags($request->get('filter'))
  432.         );
  433.         $data = [];
  434.         foreach ($list as $config) {
  435.             $item = [
  436.                 'keyId' => $config->getKeyId(),
  437.                 'groupId' => $config->getGroupId(),
  438.                 'keyName' => $config->getName(),
  439.                 'keyDescription' => $config->getDescription(),
  440.                 'id' => $config->getGroupId() . '-' $config->getKeyId(),
  441.                 'sorter' => $config->getSorter(),
  442.             ];
  443.             $groupConfig DataObject\Classificationstore\GroupConfig::getById($config->getGroupId());
  444.             if ($groupConfig) {
  445.                 $item['groupName'] = $groupConfig->getName();
  446.             }
  447.             $data[] = $item;
  448.         }
  449.         return new JsonResponse([
  450.             'success' => true,
  451.             'data' => $data,
  452.             'total' => $list->getTotalCount()
  453.         ]);
  454.     }
  455.     /**
  456.      * @Route("/load-class-classificationstore-key-name", methods={"GET"})
  457.      *
  458.      * @param Request $request
  459.      *
  460.      * @return JsonResponse
  461.      *
  462.      * @throws \Exception
  463.      */
  464.     public function loadDataObjectClassificationStoreKeyNameAction(Request $request)
  465.     {
  466.         $keyId $request->get('key_id');
  467.         $keyParts explode('-'$keyId);
  468.         if (count($keyParts) == 2) {
  469.             $keyGroupRelation DataObject\Classificationstore\KeyGroupRelation::getByGroupAndKeyId((int)$keyParts[0], (int)$keyParts[1]);
  470.             $group DataObject\Classificationstore\GroupConfig::getById($keyGroupRelation->getGroupId());
  471.             if ($keyGroupRelation && $group) {
  472.                 return new JsonResponse([
  473.                     'groupName' => $group->getName(),
  474.                     'keyName' => $keyGroupRelation->getName()
  475.                 ]);
  476.             }
  477.         }
  478.         return new JsonResponse([
  479.             'keyId' => $keyId
  480.         ]);
  481.     }
  482.     /**
  483.      * @Route("/start-import", methods={"PUT"})
  484.      *
  485.      * @param Request $request
  486.      * @param ImportPreparationService $importPreparationService
  487.      *
  488.      * @return JsonResponse
  489.      */
  490.     public function startBatchImportAction(Request $requestImportPreparationService $importPreparationService)
  491.     {
  492.         $configName $request->get('config_name');
  493.         $success $importPreparationService->prepareImport($configNametrue);
  494.         return new JsonResponse([
  495.             'success' => $success
  496.         ]);
  497.     }
  498.     /**
  499.      * @Route("/check-import-progress", methods={"GET"})
  500.      *
  501.      * @param Request $request
  502.      * @param ImportProcessingService $importProcessingService
  503.      *
  504.      * @return JsonResponse
  505.      */
  506.     public function checkImportProgressAction(Request $requestImportProcessingService $importProcessingService)
  507.     {
  508.         $configName $request->get('config_name');
  509.         return new JsonResponse($importProcessingService->getImportStatus($configName));
  510.     }
  511.     /**
  512.      * @Route("/check-crontab", methods={"GET"})
  513.      *
  514.      * @param Request $request
  515.      *
  516.      * @return JsonResponse
  517.      */
  518.     public function isCronExpressionValidAction(Request $request)
  519.     {
  520.         $message '';
  521.         $success true;
  522.         $cronExpression $request->get('cron_expression');
  523.         if (!empty($cronExpression)) {
  524.             try {
  525.                 new CronExpression($cronExpression);
  526.             } catch (\Exception $e) {
  527.                 $success false;
  528.                 $message $e->getMessage();
  529.             }
  530.         }
  531.         return new JsonResponse([
  532.             'success' => $success,
  533.             'message' => $message
  534.         ]);
  535.     }
  536.     /**
  537.      * @Route("/cancel-execution", methods={"PUT"})
  538.      *
  539.      * @param Request $request
  540.      * @param ImportProcessingService $importProcessingService
  541.      *
  542.      * @return JsonResponse
  543.      *
  544.      * @throws \Doctrine\DBAL\DBALException
  545.      */
  546.     public function cancelExecutionAction(Request $requestImportProcessingService $importProcessingService)
  547.     {
  548.         $configName $request->get('config_name');
  549.         $importProcessingService->cancelImportAndCleanupQueue($configName);
  550.         return new JsonResponse([
  551.             'success' => true
  552.         ]);
  553.     }
  554.     /**
  555.      * @Route("/upload-import-file", methods={"POST"})
  556.      *
  557.      * @param Request $request
  558.      * @param FilesystemOperator $pimcoreDataImporterUploadStorage
  559.      *
  560.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse|JsonResponse
  561.      *
  562.      * @throws \League\Flysystem\FilesystemException
  563.      */
  564.     public function uploadImportFileAction(Request $requestFilesystemOperator $pimcoreDataImporterUploadStorage)
  565.     {
  566.         try {
  567.             if (array_key_exists('Filedata'$_FILES)) {
  568.                 $filename $_FILES['Filedata']['name'];
  569.                 $sourcePath $_FILES['Filedata']['tmp_name'];
  570.             } else {
  571.                 throw new \Exception('The filename of the upload data is empty');
  572.             }
  573.             $target $this->getImportFilePath($request->get('config_name'));
  574.             $pimcoreDataImporterUploadStorage->write($targetfile_get_contents($sourcePath));
  575.             @unlink($sourcePath);
  576.             return new JsonResponse(['success' => true]);
  577.         } catch (\Exception $e) {
  578.             Logger::error($e);
  579.             return $this->adminJson([
  580.                 'success' => false,
  581.                 'message' => $e->getMessage(),
  582.             ]);
  583.         }
  584.     }
  585.     /**
  586.      * @param string $configName
  587.      *
  588.      * @return string
  589.      *
  590.      * @throws \Exception
  591.      */
  592.     protected function getImportFilePath(string $configName): string
  593.     {
  594.         $configuration Dao::getByName($configName);
  595.         if (!$configuration) {
  596.             throw new \Exception('Configuration ' $configName ' does not exist.');
  597.         }
  598.         $filePath $configuration->getName() . '/upload.import';
  599.         return $filePath;
  600.     }
  601.     /**
  602.      * @Route("/has-import-file-uploaded", methods={"GET"})
  603.      *
  604.      * @param Request $request
  605.      * @param Translator $translator
  606.      * @param FilesystemOperator $pimcoreDataImporterUploadStorage
  607.      *
  608.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse|JsonResponse
  609.      */
  610.     public function hasImportFileUploadedAction(Request $requestTranslator $translatorFilesystemOperator $pimcoreDataImporterUploadStorage)
  611.     {
  612.         try {
  613.             $importFile $this->getImportFilePath($request->get('config_name'));
  614.             if ($pimcoreDataImporterUploadStorage->fileExists($importFile)) {
  615.                 return new JsonResponse(['success' => true'filePath' => $importFile'message' => $translator->trans('plugin_pimcore_datahub_data_importer_configpanel_type_upload_exists', [], 'admin')]);
  616.             }
  617.             return new JsonResponse(['success' => false'message' => $translator->trans('plugin_pimcore_datahub_data_importer_configpanel_type_upload_not_exists', [], 'admin')]);
  618.         } catch (\Exception $e) {
  619.             Logger::error($e);
  620.             return $this->adminJson([
  621.                 'success' => false,
  622.                 'message' => $e->getMessage(),
  623.             ]);
  624.         }
  625.     }
  626.     /**
  627.      * @Route("/load-unit-data", methods={"GET"})
  628.      *
  629.      * @param Request $request
  630.      *
  631.      * @return JsonResponse
  632.      */
  633.     public function loadUnitDataAction(Request $request): JsonResponse
  634.     {
  635.         $unitList = new Unit\Listing();
  636.         $unitList->setOrderKey('abbreviation');
  637.         $data = [];
  638.         foreach ($unitList as $unit) {
  639.             $data[] = ['unitId' => $unit->getId(), 'abbreviation' => $unit->getAbbreviation()];
  640.         }
  641.         return new JsonResponse(['UnitList' => $data]);
  642.     }
  643. }