Skip to content

Commit

Permalink
Merge pull request #3986 from magento-epam/EPAM-PR-46
Browse files Browse the repository at this point in the history
- fixed MAGETWO-64838 Unable to create order from store front if customer address custom attribute is required.
  • Loading branch information
irenelagno authored Apr 4, 2019
2 parents 60e3a73 + 86fb020 commit b186139
Show file tree
Hide file tree
Showing 10 changed files with 749 additions and 7 deletions.
146 changes: 146 additions & 0 deletions app/code/Magento/Customer/Controller/Address/File/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Controller\Address\File;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Model\FileUploader;
use Magento\Customer\Model\FileUploaderFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Customer\Model\FileProcessorFactory;

/**
* Class for upload files for customer custom address attributes
*/
class Upload extends Action implements HttpPostActionInterface
{
/**
* @var FileUploaderFactory
*/
private $fileUploaderFactory;

/**
* @var AddressMetadataInterface
*/
private $addressMetadataService;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var FileProcessorFactory
*/
private $fileProcessorFactory;

/**
* @param Context $context
* @param FileUploaderFactory $fileUploaderFactory
* @param AddressMetadataInterface $addressMetadataService
* @param LoggerInterface $logger
* @param FileProcessorFactory $fileProcessorFactory
*/
public function __construct(
Context $context,
FileUploaderFactory $fileUploaderFactory,
AddressMetadataInterface $addressMetadataService,
LoggerInterface $logger,
FileProcessorFactory $fileProcessorFactory
) {
$this->fileUploaderFactory = $fileUploaderFactory;
$this->addressMetadataService = $addressMetadataService;
$this->logger = $logger;
$this->fileProcessorFactory = $fileProcessorFactory;
parent::__construct($context);
}

/**
* @inheritDoc
*/
public function execute()
{
try {
$requestedFiles = $this->getRequest()->getFiles('custom_attributes');
if (empty($requestedFiles)) {
$result = $this->processError(__('No files for upload.'));
} else {
$attributeCode = key($requestedFiles);
$attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode);

/** @var FileUploader $fileUploader */
$fileUploader = $this->fileUploaderFactory->create([
'attributeMetadata' => $attributeMetadata,
'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
'scope' => CustomAttributesDataInterface::CUSTOM_ATTRIBUTES,
]);

$errors = $fileUploader->validate();
if (true !== $errors) {
$errorMessage = implode('</br>', $errors);
$result = $this->processError(($errorMessage));
} else {
$result = $fileUploader->upload();
$this->moveTmpFileToSuitableFolder($result);
}
}
} catch (LocalizedException $e) {
$result = $this->processError($e->getMessage(), $e->getCode());
} catch (\Exception $e) {
$this->logger->critical($e);
$result = $this->processError($e->getMessage(), $e->getCode());
}

/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;
}

/**
* Move file from temporary folder to the 'customer_address' media folder
*
* @param array $fileInfo
* @throws LocalizedException
*/
private function moveTmpFileToSuitableFolder(&$fileInfo)
{
$fileName = $fileInfo['file'];
$fileProcessor = $this->fileProcessorFactory
->create(['entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS]);

$newFilePath = $fileProcessor->moveTemporaryFile($fileName);
$fileInfo['file'] = $newFilePath;
$fileInfo['url'] = $fileProcessor->getViewUrl(
$newFilePath,
'file'
);
}

/**
* Prepare result array for errors
*
* @param string $message
* @param int $code
* @return array
*/
private function processError($message, $code = 0)
{
$result = [
'error' => $message,
'errorcode' => $code,
];

return $result;
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/Customer/Model/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Model;

/**
* Processor class for work with uploaded files
*/
class FileProcessor
{
/**
Expand Down Expand Up @@ -232,7 +237,7 @@ public function moveTemporaryFile($fileName)
);
}

$fileName = $dispersionPath . '/' . $fileName;
$fileName = $dispersionPath . '/' . $destinationFileName;
return $fileName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ define([
'Magento_Ui/js/modal/alert',
'Magento_Ui/js/lib/validation/validator',
'Magento_Ui/js/form/element/file-uploader',
'mage/adminhtml/browser',
'mage/adminhtml/tools'
'mage/adminhtml/browser'
], function ($, _, utils, uiAlert, validator, Element, browser) {
'use strict';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<div class="field-control" css="'_with-tooltip': $data.tooltip">
<div class="file-uploader" data-role="drop-zone" css="_loading: isLoading">
<div class="file-uploader-area">
<input type="file" afterRender="onElementRender" attr="id: uid, name: inputName, multiple: isMultipleFiles"
disable="disabled"/>
<label class="file-uploader-button action-default" attr="for: uid" translate="'Upload'"/>

<span class="file-uploader-spinner"/>
<render args="fallbackResetTpl" if="$data.showFallbackReset && $data.isDifferedFromDefault"/>
</div>

<render args="tooltipTpl" if="$data.tooltip"/>

<div class="field-note" if="$data.notice" attr="id: noticeId">
<span html="notice"/>
</div>

<each args="data: value, as: '$file'" render="$parent.getPreviewTmpl($file)"/>

<div if="isMultipleFiles" class="file-uploader-summary">
<label attr="for: uid"
class="file-uploader-placeholder"
css="'placeholder-' + placeholderType">
<span class="file-uploader-placeholder-text"
translate="'Click here or drag and drop to add files.'"/>
</label>
</div>
</div>
<render args="$data.service.template" if="$data.hasService()"/>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Variables
// _____________________________________________

@import 'fields/_file-uploader.less';

@checkout-wrapper__margin: @indent__base;
@checkout-wrapper__columns: 16;

Expand Down
Loading

0 comments on commit b186139

Please sign in to comment.