Skip to content

Commit ffbf8e6

Browse files
author
Alexey Yakimovich
committed
MAGETWO-64838: Unable to create order from store front if customer address custom attribute is required.
- Fixed an issue witn incorrent work of file/image custom attributes on checkout;
1 parent 94383aa commit ffbf8e6

File tree

11 files changed

+712
-7
lines changed

11 files changed

+712
-7
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Address\File;
9+
10+
use Magento\Framework\App\Action\Action;
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\Api\CustomAttributesDataInterface;
14+
use Magento\Customer\Api\AddressMetadataInterface;
15+
use Magento\Customer\Model\FileUploader;
16+
use Magento\Customer\Model\FileUploaderFactory;
17+
use Magento\Framework\Controller\ResultFactory;
18+
use Magento\Framework\Exception\LocalizedException;
19+
use Psr\Log\LoggerInterface;
20+
use Magento\Customer\Model\FileProcessorFactory;
21+
22+
/**
23+
* Class for upload files for customer custom address attributes
24+
*/
25+
class Upload extends Action implements HttpPostActionInterface
26+
{
27+
/**
28+
* @var FileUploaderFactory
29+
*/
30+
private $fileUploaderFactory;
31+
32+
/**
33+
* @var AddressMetadataInterface
34+
*/
35+
private $addressMetadataService;
36+
37+
/**
38+
* @var LoggerInterface
39+
*/
40+
private $logger;
41+
42+
/**
43+
* @var FileProcessorFactory
44+
*/
45+
private $fileProcessorFactory;
46+
47+
/**
48+
* @param Context $context
49+
* @param FileUploaderFactory $fileUploaderFactory
50+
* @param AddressMetadataInterface $addressMetadataService
51+
* @param LoggerInterface $logger
52+
* @param FileProcessorFactory $fileProcessorFactory
53+
*/
54+
public function __construct(
55+
Context $context,
56+
FileUploaderFactory $fileUploaderFactory,
57+
AddressMetadataInterface $addressMetadataService,
58+
LoggerInterface $logger,
59+
FileProcessorFactory $fileProcessorFactory
60+
) {
61+
$this->fileUploaderFactory = $fileUploaderFactory;
62+
$this->addressMetadataService = $addressMetadataService;
63+
$this->logger = $logger;
64+
$this->fileProcessorFactory = $fileProcessorFactory;
65+
parent::__construct($context);
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function execute()
72+
{
73+
try {
74+
$requestedFiles = $this->getRequest()->getFiles('custom_attributes');
75+
if (empty($requestedFiles)) {
76+
$result = $this->processError(__('No files for upload.'));
77+
} else {
78+
$attributeCode = key($requestedFiles);
79+
$attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode);
80+
81+
/** @var FileUploader $fileUploader */
82+
$fileUploader = $this->fileUploaderFactory->create([
83+
'attributeMetadata' => $attributeMetadata,
84+
'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
85+
'scope' => CustomAttributesDataInterface::CUSTOM_ATTRIBUTES,
86+
]);
87+
88+
$errors = $fileUploader->validate();
89+
if (true !== $errors) {
90+
$errorMessage = implode('</br>', $errors);
91+
$result = $this->processError(($errorMessage));
92+
} else {
93+
$result = $fileUploader->upload();
94+
}
95+
}
96+
} catch (LocalizedException $e) {
97+
$result = $this->processError($e->getMessage(), $e->getCode());
98+
} catch (\Exception $e) {
99+
$this->logger->critical($e);
100+
$result = $this->processError($e->getMessage(), $e->getCode());
101+
}
102+
103+
$this->moveTmpFileToSuitableFolder($result);
104+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
105+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
106+
$resultJson->setData($result);
107+
return $resultJson;
108+
}
109+
110+
/**
111+
* Move file from temporary folder to the 'customer_address' media folder
112+
*
113+
* @param array $fileInfo
114+
* @throws LocalizedException
115+
*/
116+
private function moveTmpFileToSuitableFolder(&$fileInfo)
117+
{
118+
$fileName = $fileInfo['file'];
119+
$fileProcessor = $this->fileProcessorFactory
120+
->create(['entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS]);
121+
122+
$newFilePath = $fileProcessor->moveTemporaryFile($fileName);
123+
$fileInfo['file'] = $newFilePath;
124+
$fileInfo['url'] = $fileProcessor->getViewUrl(
125+
$newFilePath,
126+
'file'
127+
);
128+
}
129+
130+
/**
131+
* Prepare result array for errors
132+
*
133+
* @param string $message
134+
* @param int $code
135+
* @return array
136+
*/
137+
private function processError($message, $code = 0)
138+
{
139+
$result = [
140+
'error' => $message,
141+
'errorcode' => $code,
142+
];
143+
144+
return $result;
145+
}
146+
}

app/code/Magento/Customer/Model/FileProcessor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Customer\Model;
77

8+
/**
9+
* Processor class for work with uploaded files
10+
*/
811
class FileProcessor
912
{
1013
/**
@@ -232,7 +235,7 @@ public function moveTemporaryFile($fileName)
232235
);
233236
}
234237

235-
$fileName = $dispersionPath . '/' . $fileName;
238+
$fileName = $dispersionPath . '/' . $destinationFileName;
236239
return $fileName;
237240
}
238241

app/code/Magento/Ui/view/base/web/js/form/element/file-uploader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ define([
1616
'Magento_Ui/js/form/element/abstract',
1717
'mage/backend/notification',
1818
'mage/translate',
19-
'jquery/file-uploader'
19+
'jquery/file-uploader',
20+
'mage/adminhtml/tools'
2021
], function ($, _, utils, uiAlert, validator, Element, notification, $t) {
2122
'use strict';
2223

app/code/Magento/Ui/view/base/web/js/form/element/image-uploader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ define([
1111
'Magento_Ui/js/modal/alert',
1212
'Magento_Ui/js/lib/validation/validator',
1313
'Magento_Ui/js/form/element/file-uploader',
14-
'mage/adminhtml/browser',
15-
'mage/adminhtml/tools'
14+
'mage/adminhtml/browser'
1615
], function ($, _, utils, uiAlert, validator, Element, browser) {
1716
'use strict';
1817

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
8+
<div class="field-control" css="'_with-tooltip': $data.tooltip">
9+
<div class="file-uploader" data-role="drop-zone" css="_loading: isLoading">
10+
<div class="file-uploader-area">
11+
<input type="file" afterRender="onElementRender" attr="id: uid, name: inputName, multiple: isMultipleFiles"
12+
disable="disabled"/>
13+
<label class="file-uploader-button action-default" attr="for: uid" translate="'Upload'"/>
14+
15+
<span class="file-uploader-spinner"/>
16+
<render args="fallbackResetTpl" if="$data.showFallbackReset && $data.isDifferedFromDefault"/>
17+
</div>
18+
19+
<render args="tooltipTpl" if="$data.tooltip"/>
20+
21+
<div class="field-note" if="$data.notice" attr="id: noticeId">
22+
<span html="notice"/>
23+
</div>
24+
25+
<each args="data: value, as: '$file'" render="$parent.getPreviewTmpl($file)"/>
26+
27+
<div if="isMultipleFiles" class="file-uploader-summary">
28+
<label attr="for: uid"
29+
class="file-uploader-placeholder"
30+
css="'placeholder-' + placeholderType">
31+
<span class="file-uploader-placeholder-text"
32+
translate="'Click here or drag and drop to add files.'"/>
33+
</label>
34+
</div>
35+
</div>
36+
<render args="$data.service.template" if="$data.hasService()"/>
37+
</div>

app/design/frontend/Magento/luma/Magento_Checkout/web/css/source/module/checkout/_checkout.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// Variables
88
// _____________________________________________
99

10+
@import 'fields/_file-uploader.less';
11+
1012
@checkout-wrapper__margin: @indent__base;
1113
@checkout-wrapper__columns: 16;
1214

0 commit comments

Comments
 (0)