123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Adminhtml\File\Customer;
- use Magento\Backend\App\Action;
- use Magento\Backend\App\Action\Context;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Customer\Model\FileUploader;
- use Magento\Customer\Model\FileUploaderFactory;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Framework\Exception\LocalizedException;
- use Psr\Log\LoggerInterface;
- class Upload extends Action
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- const ADMIN_RESOURCE = 'Magento_Customer::manage';
- /**
- * @var FileUploaderFactory
- */
- private $fileUploaderFactory;
- /**
- * @var CustomerMetadataInterface
- */
- private $customerMetadataService;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @param Context $context
- * @param FileUploaderFactory $fileUploaderFactory
- * @param CustomerMetadataInterface $customerMetadataService
- * @param LoggerInterface $logger
- */
- public function __construct(
- Context $context,
- FileUploaderFactory $fileUploaderFactory,
- CustomerMetadataInterface $customerMetadataService,
- LoggerInterface $logger
- ) {
- $this->fileUploaderFactory = $fileUploaderFactory;
- $this->customerMetadataService = $customerMetadataService;
- $this->logger = $logger;
- parent::__construct($context);
- }
- /**
- * @inheritDoc
- */
- public function execute()
- {
- try {
- if (empty($_FILES)) {
- throw new \Exception('$_FILES array is empty.');
- }
- $attributeCode = key($_FILES['customer']['name']);
- $attributeMetadata = $this->customerMetadataService->getAttributeMetadata($attributeCode);
- /** @var FileUploader $fileUploader */
- $fileUploader = $this->fileUploaderFactory->create([
- 'attributeMetadata' => $attributeMetadata,
- 'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'scope' => 'customer',
- ]);
- $errors = $fileUploader->validate();
- if (true !== $errors) {
- $errorMessage = implode('</br>', $errors);
- throw new LocalizedException(__($errorMessage));
- }
- $result = $fileUploader->upload();
- } catch (LocalizedException $e) {
- $result = [
- 'error' => $e->getMessage(),
- 'errorcode' => $e->getCode(),
- ];
- } catch (\Exception $e) {
- $this->logger->critical($e);
- $result = [
- 'error' => __('Something went wrong while saving file.'),
- 'errorcode' => $e->getCode(),
- ];
- }
- /** @var \Magento\Framework\Controller\Result\Json $resultJson */
- $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
- $resultJson->setData($result);
- return $resultJson;
- }
- }
|