5
5
*/
6
6
namespace Magento \Customer \Model \Customer ;
7
7
8
- use Magento \Customer \Api \AddressMetadataInterface ;
9
- use Magento \Customer \Api \CustomerMetadataInterface ;
10
8
use Magento \Customer \Api \Data \AddressInterface ;
11
9
use Magento \Customer \Api \Data \CustomerInterface ;
12
10
use Magento \Customer \Model \Address ;
28
26
use Magento \Ui \Component \Form \Element \Multiline ;
29
27
use Magento \Ui \Component \Form \Field ;
30
28
use Magento \Ui \DataProvider \EavValidationRules ;
29
+ use Magento \Customer \Model \FileUploaderDataResolver ;
31
30
32
31
/**
33
32
* Supplies the data for the customer UI component
34
33
*
35
34
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35
+ * @SuppressWarnings(PHPMD.TooManyFields)
36
36
*
37
- * @deprecated \Magento\Customer\Model\Address\DataProvider is used instead
37
+ * @deprecated \Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses is used instead
38
38
* @api
39
39
* @since 100.0.2
40
40
*/
@@ -117,16 +117,6 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
117
117
*/
118
118
private $ fileProcessorFactory ;
119
119
120
- /**
121
- * File types allowed for file_uploader UI component
122
- *
123
- * @var array
124
- */
125
- private $ fileUploaderTypes = [
126
- 'image ' ,
127
- 'file ' ,
128
- ];
129
-
130
120
/**
131
121
* Customer fields that must be removed
132
122
*
@@ -150,6 +140,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
150
140
*/
151
141
private $ allowToShowHiddenAttributes ;
152
142
143
+ /**
144
+ * @var FileUploaderDataResolver
145
+ */
146
+ private $ fileUploaderDataResolver ;
147
+
153
148
/**
154
149
* @param string $name
155
150
* @param string $primaryFieldName
@@ -163,6 +158,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
163
158
* @param array $data
164
159
* @param ContextInterface $context
165
160
* @param bool $allowToShowHiddenAttributes
161
+ * @param FileUploaderDataResolver|null $fileUploaderDataResolver
166
162
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
167
163
*/
168
164
public function __construct (
@@ -177,7 +173,8 @@ public function __construct(
177
173
array $ meta = [],
178
174
array $ data = [],
179
175
ContextInterface $ context = null ,
180
- $ allowToShowHiddenAttributes = true
176
+ $ allowToShowHiddenAttributes = true ,
177
+ $ fileUploaderDataResolver = null
181
178
) {
182
179
parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data );
183
180
$ this ->eavValidationRules = $ eavValidationRules ;
@@ -188,6 +185,8 @@ public function __construct(
188
185
$ this ->fileProcessorFactory = $ fileProcessorFactory ?: $ this ->getFileProcessorFactory ();
189
186
$ this ->context = $ context ?: ObjectManager::getInstance ()->get (ContextInterface::class);
190
187
$ this ->allowToShowHiddenAttributes = $ allowToShowHiddenAttributes ;
188
+ $ this ->fileUploaderDataResolver = $ fileUploaderDataResolver
189
+ ?: ObjectManager::getInstance ()->get (FileUploaderDataResolver::class);
191
190
$ this ->meta ['customer ' ]['children ' ] = $ this ->getAttributesMeta (
192
191
$ this ->eavConfig ->getEntityType ('customer ' )
193
192
);
@@ -228,7 +227,7 @@ public function getData()
228
227
foreach ($ items as $ customer ) {
229
228
$ result ['customer ' ] = $ customer ->getData ();
230
229
231
- $ this ->overrideFileUploaderData ($ customer , $ result ['customer ' ]);
230
+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ customer , $ result ['customer ' ]);
232
231
233
232
$ result ['customer ' ] = array_diff_key (
234
233
$ result ['customer ' ],
@@ -243,7 +242,7 @@ public function getData()
243
242
$ result ['address ' ][$ addressId ] = $ address ->getData ();
244
243
$ this ->prepareAddressData ($ addressId , $ result ['address ' ], $ result ['customer ' ]);
245
244
246
- $ this ->overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
245
+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
247
246
}
248
247
$ this ->loadedData [$ customer ->getId ()] = $ result ;
249
248
}
@@ -258,75 +257,6 @@ public function getData()
258
257
return $ this ->loadedData ;
259
258
}
260
259
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
-
330
260
/**
331
261
* Get attributes meta
332
262
*
@@ -372,7 +302,11 @@ protected function getAttributesMeta(Type $entityType)
372
302
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['componentType ' ] = Field::NAME ;
373
303
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['visible ' ] = $ this ->canShowAttribute ($ attribute );
374
304
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
+ );
376
310
}
377
311
378
312
$ this ->processWebsiteMeta ($ meta );
@@ -470,97 +404,6 @@ private function processWebsiteMeta(&$meta)
470
404
}
471
405
}
472
406
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
-
564
407
/**
565
408
* Process attributes by frontend input type
566
409
*
0 commit comments