|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Controller\Adminhtml\File\Address; |
| 7 | + |
| 8 | +use Magento\Backend\App\Action; |
| 9 | +use Magento\Backend\App\Action\Context; |
| 10 | +use Magento\Customer\Api\AddressMetadataInterface; |
| 11 | +use Magento\Customer\Model\FileUploader; |
| 12 | +use Magento\Customer\Model\FileUploaderFactory; |
| 13 | +use Magento\Framework\Controller\ResultFactory; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +class Upload extends Action |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Authorization level of a basic admin session |
| 21 | + * |
| 22 | + * @see _isAllowed() |
| 23 | + */ |
| 24 | + const ADMIN_RESOURCE = 'Magento_Customer::manage'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var FileUploaderFactory |
| 28 | + */ |
| 29 | + private $fileUploaderFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var AddressMetadataInterface |
| 33 | + */ |
| 34 | + private $addressMetadataService; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var LoggerInterface |
| 38 | + */ |
| 39 | + private $logger; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param Context $context |
| 43 | + * @param FileUploaderFactory $fileUploaderFactory |
| 44 | + * @param AddressMetadataInterface $addressMetadataService |
| 45 | + * @param LoggerInterface $logger |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + Context $context, |
| 49 | + FileUploaderFactory $fileUploaderFactory, |
| 50 | + AddressMetadataInterface $addressMetadataService, |
| 51 | + LoggerInterface $logger |
| 52 | + ) { |
| 53 | + $this->fileUploaderFactory = $fileUploaderFactory; |
| 54 | + $this->addressMetadataService = $addressMetadataService; |
| 55 | + $this->logger = $logger; |
| 56 | + parent::__construct($context); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritDoc |
| 61 | + */ |
| 62 | + public function execute() |
| 63 | + { |
| 64 | + try { |
| 65 | + if (empty($_FILES)) { |
| 66 | + throw new \Exception('$_FILES array is empty.'); |
| 67 | + } |
| 68 | + |
| 69 | + // Must be executed before any operations with $_FILES! |
| 70 | + $this->convertFilesArray(); |
| 71 | + |
| 72 | + $attributeCode = key($_FILES['address']['name']); |
| 73 | + $attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode); |
| 74 | + |
| 75 | + /** @var FileUploader $fileUploader */ |
| 76 | + $fileUploader = $this->fileUploaderFactory->create([ |
| 77 | + 'attributeMetadata' => $attributeMetadata, |
| 78 | + 'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS, |
| 79 | + 'scope' => 'address', |
| 80 | + ]); |
| 81 | + |
| 82 | + $errors = $fileUploader->validate(); |
| 83 | + if (true !== $errors) { |
| 84 | + $errorMessage = implode('</br>', $errors); |
| 85 | + throw new LocalizedException(__($errorMessage)); |
| 86 | + } |
| 87 | + |
| 88 | + $result = $fileUploader->upload(); |
| 89 | + } catch (LocalizedException $e) { |
| 90 | + $result = [ |
| 91 | + 'error' => $e->getMessage(), |
| 92 | + 'errorcode' => $e->getCode(), |
| 93 | + ]; |
| 94 | + } catch (\Exception $e) { |
| 95 | + $this->logger->critical($e); |
| 96 | + $result = [ |
| 97 | + 'error' => __('Something went wrong while saving file.'), |
| 98 | + 'errorcode' => $e->getCode(), |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + /** @var \Magento\Framework\Controller\Result\Json $resultJson */ |
| 103 | + $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); |
| 104 | + $resultJson->setData($result); |
| 105 | + return $resultJson; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Update global $_FILES array. Convert data to standard form |
| 110 | + * |
| 111 | + * NOTE: This conversion is required to use \Magento\Framework\File\Uploader::_setUploadFileId($fileId) method. |
| 112 | + * |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function convertFilesArray() |
| 116 | + { |
| 117 | + foreach($_FILES['address'] as $itemKey => $item) { |
| 118 | + foreach ($item as $value) { |
| 119 | + if (is_array($value)) { |
| 120 | + $_FILES['address'][$itemKey] = [ |
| 121 | + key($value) => current($value), |
| 122 | + ]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments