Skip to content

Commit b4735f3

Browse files
committed
Merge remote-tracking branch 'panda/MAGETWO-97161' into EPAM-PR-24
2 parents 850fb73 + 5806eab commit b4735f3

File tree

2 files changed

+50
-361
lines changed

2 files changed

+50
-361
lines changed

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

Lines changed: 20 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Customer\Model\Customer;
77

8-
use Magento\Customer\Api\AddressMetadataInterface;
9-
use Magento\Customer\Api\CustomerMetadataInterface;
108
use Magento\Customer\Api\Data\AddressInterface;
119
use Magento\Customer\Api\Data\CustomerInterface;
1210
use Magento\Customer\Model\Address;
@@ -28,13 +26,15 @@
2826
use Magento\Ui\Component\Form\Element\Multiline;
2927
use Magento\Ui\Component\Form\Field;
3028
use Magento\Ui\DataProvider\EavValidationRules;
29+
use Magento\Customer\Model\FileUploaderDataResolver;
3130

3231
/**
3332
* Supplies the data for the customer UI component
3433
*
3534
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35+
* @SuppressWarnings(PHPMD.TooManyFields)
3636
*
37-
* @deprecated \Magento\Customer\Model\Address\DataProvider is used instead
37+
* @deprecated \Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses is used instead
3838
* @api
3939
* @since 100.0.2
4040
*/
@@ -117,16 +117,6 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
117117
*/
118118
private $fileProcessorFactory;
119119

120-
/**
121-
* File types allowed for file_uploader UI component
122-
*
123-
* @var array
124-
*/
125-
private $fileUploaderTypes = [
126-
'image',
127-
'file',
128-
];
129-
130120
/**
131121
* Customer fields that must be removed
132122
*
@@ -150,6 +140,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
150140
*/
151141
private $allowToShowHiddenAttributes;
152142

143+
/**
144+
* @var FileUploaderDataResolver
145+
*/
146+
private $fileUploaderDataResolver;
147+
153148
/**
154149
* @param string $name
155150
* @param string $primaryFieldName
@@ -163,6 +158,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
163158
* @param array $data
164159
* @param ContextInterface $context
165160
* @param bool $allowToShowHiddenAttributes
161+
* @param FileUploaderDataResolver|null $fileUploaderDataResolver
166162
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
167163
*/
168164
public function __construct(
@@ -177,7 +173,8 @@ public function __construct(
177173
array $meta = [],
178174
array $data = [],
179175
ContextInterface $context = null,
180-
$allowToShowHiddenAttributes = true
176+
$allowToShowHiddenAttributes = true,
177+
$fileUploaderDataResolver = null
181178
) {
182179
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
183180
$this->eavValidationRules = $eavValidationRules;
@@ -188,6 +185,8 @@ public function __construct(
188185
$this->fileProcessorFactory = $fileProcessorFactory ?: $this->getFileProcessorFactory();
189186
$this->context = $context ?: ObjectManager::getInstance()->get(ContextInterface::class);
190187
$this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes;
188+
$this->fileUploaderDataResolver = $fileUploaderDataResolver
189+
?: ObjectManager::getInstance()->get(FileUploaderDataResolver::class);
191190
$this->meta['customer']['children'] = $this->getAttributesMeta(
192191
$this->eavConfig->getEntityType('customer')
193192
);
@@ -228,7 +227,7 @@ public function getData()
228227
foreach ($items as $customer) {
229228
$result['customer'] = $customer->getData();
230229

231-
$this->overrideFileUploaderData($customer, $result['customer']);
230+
$this->fileUploaderDataResolver->overrideFileUploaderData($customer, $result['customer']);
232231

233232
$result['customer'] = array_diff_key(
234233
$result['customer'],
@@ -243,7 +242,7 @@ public function getData()
243242
$result['address'][$addressId] = $address->getData();
244243
$this->prepareAddressData($addressId, $result['address'], $result['customer']);
245244

246-
$this->overrideFileUploaderData($address, $result['address'][$addressId]);
245+
$this->fileUploaderDataResolver->overrideFileUploaderData($address, $result['address'][$addressId]);
247246
}
248247
$this->loadedData[$customer->getId()] = $result;
249248
}
@@ -258,75 +257,6 @@ public function getData()
258257
return $this->loadedData;
259258
}
260259

261-
/**
262-
* Override file uploader UI component data
263-
*
264-
* Overrides data for attributes with frontend_input equal to 'image' or 'file'.
265-
*
266-
* @param Customer|Address $entity
267-
* @param array $entityData
268-
* @return void
269-
*/
270-
private function overrideFileUploaderData($entity, array &$entityData)
271-
{
272-
$attributes = $entity->getAttributes();
273-
foreach ($attributes as $attribute) {
274-
/** @var Attribute $attribute */
275-
if (in_array($attribute->getFrontendInput(), $this->fileUploaderTypes)) {
276-
$entityData[$attribute->getAttributeCode()] = $this->getFileUploaderData(
277-
$entity->getEntityType(),
278-
$attribute,
279-
$entityData
280-
);
281-
}
282-
}
283-
}
284-
285-
/**
286-
* Retrieve array of values required by file uploader UI component
287-
*
288-
* @param Type $entityType
289-
* @param Attribute $attribute
290-
* @param array $customerData
291-
* @return array
292-
* @SuppressWarnings(PHPMD.NPathComplexity)
293-
*/
294-
private function getFileUploaderData(
295-
Type $entityType,
296-
Attribute $attribute,
297-
array $customerData
298-
) {
299-
$attributeCode = $attribute->getAttributeCode();
300-
301-
$file = isset($customerData[$attributeCode])
302-
? $customerData[$attributeCode]
303-
: '';
304-
305-
/** @var FileProcessor $fileProcessor */
306-
$fileProcessor = $this->getFileProcessorFactory()->create([
307-
'entityTypeCode' => $entityType->getEntityTypeCode(),
308-
]);
309-
310-
if (!empty($file)
311-
&& $fileProcessor->isExist($file)
312-
) {
313-
$stat = $fileProcessor->getStat($file);
314-
$viewUrl = $fileProcessor->getViewUrl($file, $attribute->getFrontendInput());
315-
316-
return [
317-
[
318-
'file' => $file,
319-
'size' => isset($stat) ? $stat['size'] : 0,
320-
'url' => isset($viewUrl) ? $viewUrl : '',
321-
'name' => basename($file),
322-
'type' => $fileProcessor->getMimeType($file),
323-
],
324-
];
325-
}
326-
327-
return [];
328-
}
329-
330260
/**
331261
* Get attributes meta
332262
*
@@ -372,7 +302,11 @@ protected function getAttributesMeta(Type $entityType)
372302
$meta[$code]['arguments']['data']['config']['componentType'] = Field::NAME;
373303
$meta[$code]['arguments']['data']['config']['visible'] = $this->canShowAttribute($attribute);
374304

375-
$this->overrideFileUploaderMetadata($entityType, $attribute, $meta[$code]['arguments']['data']['config']);
305+
$this->fileUploaderDataResolver->overrideFileUploaderMetadata(
306+
$entityType,
307+
$attribute,
308+
$meta[$code]['arguments']['data']['config']
309+
);
376310
}
377311

378312
$this->processWebsiteMeta($meta);
@@ -470,97 +404,6 @@ private function processWebsiteMeta(&$meta)
470404
}
471405
}
472406

473-
/**
474-
* Override file uploader UI component metadata
475-
*
476-
* Overrides metadata for attributes with frontend_input equal to 'image' or 'file'.
477-
*
478-
* @param Type $entityType
479-
* @param AbstractAttribute $attribute
480-
* @param array $config
481-
* @return void
482-
*/
483-
private function overrideFileUploaderMetadata(
484-
Type $entityType,
485-
AbstractAttribute $attribute,
486-
array &$config
487-
) {
488-
if (in_array($attribute->getFrontendInput(), $this->fileUploaderTypes)) {
489-
$maxFileSize = self::MAX_FILE_SIZE;
490-
491-
if (isset($config['validation']['max_file_size'])) {
492-
$maxFileSize = (int)$config['validation']['max_file_size'];
493-
}
494-
495-
$allowedExtensions = [];
496-
497-
if (isset($config['validation']['file_extensions'])) {
498-
$allowedExtensions = explode(',', $config['validation']['file_extensions']);
499-
array_walk($allowedExtensions, function (&$value) {
500-
$value = strtolower(trim($value));
501-
});
502-
}
503-
504-
$allowedExtensions = implode(' ', $allowedExtensions);
505-
506-
$entityTypeCode = $entityType->getEntityTypeCode();
507-
$url = $this->getFileUploadUrl($entityTypeCode);
508-
509-
$config = [
510-
'formElement' => 'fileUploader',
511-
'componentType' => 'fileUploader',
512-
'maxFileSize' => $maxFileSize,
513-
'allowedExtensions' => $allowedExtensions,
514-
'uploaderConfig' => [
515-
'url' => $url,
516-
],
517-
'label' => $this->getMetadataValue($config, 'label'),
518-
'sortOrder' => $this->getMetadataValue($config, 'sortOrder'),
519-
'required' => $this->getMetadataValue($config, 'required'),
520-
'visible' => $this->getMetadataValue($config, 'visible'),
521-
'validation' => $this->getMetadataValue($config, 'validation'),
522-
];
523-
}
524-
}
525-
526-
/**
527-
* Retrieve metadata value
528-
*
529-
* @param array $config
530-
* @param string $name
531-
* @param mixed $default
532-
* @return mixed
533-
*/
534-
private function getMetadataValue($config, $name, $default = null)
535-
{
536-
$value = isset($config[$name]) ? $config[$name] : $default;
537-
return $value;
538-
}
539-
540-
/**
541-
* Retrieve URL to file upload
542-
*
543-
* @param string $entityTypeCode
544-
* @return string
545-
*/
546-
private function getFileUploadUrl($entityTypeCode)
547-
{
548-
switch ($entityTypeCode) {
549-
case CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER:
550-
$url = 'customer/file/customer_upload';
551-
break;
552-
553-
case AddressMetadataInterface::ENTITY_TYPE_ADDRESS:
554-
$url = 'customer/file/address_upload';
555-
break;
556-
557-
default:
558-
$url = '';
559-
break;
560-
}
561-
return $url;
562-
}
563-
564407
/**
565408
* Process attributes by frontend input type
566409
*

0 commit comments

Comments
 (0)