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 ;
13
11
use Magento \Customer \Model \Attribute ;
14
12
use Magento \Customer \Model \Customer ;
15
- use Magento \Customer \Model \FileProcessor ;
16
13
use Magento \Customer \Model \FileProcessorFactory ;
17
14
use Magento \Customer \Model \ResourceModel \Address \Attribute \Source \CountryWithWebsites ;
18
15
use Magento \Customer \Model \ResourceModel \Customer \Collection ;
28
25
use Magento \Ui \Component \Form \Element \Multiline ;
29
26
use Magento \Ui \Component \Form \Field ;
30
27
use Magento \Ui \DataProvider \EavValidationRules ;
28
+ use Magento \Customer \Model \FileUploaderDataResolver ;
31
29
32
30
/**
33
31
* Supplies the data for the customer UI component
34
32
*
35
33
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34
+ * @SuppressWarnings(PHPMD.TooManyFields)
36
35
*
37
- * @deprecated \Magento\Customer\Model\Address\DataProvider is used instead
36
+ * @deprecated \Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses is used instead
38
37
* @api
39
38
* @since 100.0.2
40
39
*/
@@ -112,21 +111,6 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
112
111
*/
113
112
protected $ session ;
114
113
115
- /**
116
- * @var FileProcessorFactory
117
- */
118
- private $ fileProcessorFactory ;
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
114
/**
131
115
* Customer fields that must be removed
132
116
*
@@ -150,6 +134,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
150
134
*/
151
135
private $ allowToShowHiddenAttributes ;
152
136
137
+ /**
138
+ * @var FileUploaderDataResolver
139
+ */
140
+ private $ fileUploaderDataResolver ;
141
+
153
142
/**
154
143
* @param string $name
155
144
* @param string $primaryFieldName
@@ -163,7 +152,9 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
163
152
* @param array $data
164
153
* @param ContextInterface $context
165
154
* @param bool $allowToShowHiddenAttributes
155
+ * @param FileUploaderDataResolver|null $fileUploaderDataResolver
166
156
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
157
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
167
158
*/
168
159
public function __construct (
169
160
$ name ,
@@ -177,17 +168,19 @@ public function __construct(
177
168
array $ meta = [],
178
169
array $ data = [],
179
170
ContextInterface $ context = null ,
180
- $ allowToShowHiddenAttributes = true
171
+ $ allowToShowHiddenAttributes = true ,
172
+ $ fileUploaderDataResolver = null
181
173
) {
182
174
parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data );
183
175
$ this ->eavValidationRules = $ eavValidationRules ;
184
176
$ this ->collection = $ customerCollectionFactory ->create ();
185
177
$ this ->collection ->addAttributeToSelect ('* ' );
186
178
$ this ->eavConfig = $ eavConfig ;
187
179
$ this ->filterPool = $ filterPool ;
188
- $ this ->fileProcessorFactory = $ fileProcessorFactory ?: $ this ->getFileProcessorFactory ();
189
180
$ this ->context = $ context ?: ObjectManager::getInstance ()->get (ContextInterface::class);
190
181
$ this ->allowToShowHiddenAttributes = $ allowToShowHiddenAttributes ;
182
+ $ this ->fileUploaderDataResolver = $ fileUploaderDataResolver
183
+ ?: ObjectManager::getInstance ()->get (FileUploaderDataResolver::class);
191
184
$ this ->meta ['customer ' ]['children ' ] = $ this ->getAttributesMeta (
192
185
$ this ->eavConfig ->getEntityType ('customer ' )
193
186
);
@@ -228,7 +221,7 @@ public function getData()
228
221
foreach ($ items as $ customer ) {
229
222
$ result ['customer ' ] = $ customer ->getData ();
230
223
231
- $ this ->overrideFileUploaderData ($ customer , $ result ['customer ' ]);
224
+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ customer , $ result ['customer ' ]);
232
225
233
226
$ result ['customer ' ] = array_diff_key (
234
227
$ result ['customer ' ],
@@ -243,7 +236,7 @@ public function getData()
243
236
$ result ['address ' ][$ addressId ] = $ address ->getData ();
244
237
$ this ->prepareAddressData ($ addressId , $ result ['address ' ], $ result ['customer ' ]);
245
238
246
- $ this ->overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
239
+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
247
240
}
248
241
$ this ->loadedData [$ customer ->getId ()] = $ result ;
249
242
}
@@ -258,75 +251,6 @@ public function getData()
258
251
return $ this ->loadedData ;
259
252
}
260
253
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
254
/**
331
255
* Get attributes meta
332
256
*
@@ -372,7 +296,11 @@ protected function getAttributesMeta(Type $entityType)
372
296
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['componentType ' ] = Field::NAME ;
373
297
$ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['visible ' ] = $ this ->canShowAttribute ($ attribute );
374
298
375
- $ this ->overrideFileUploaderMetadata ($ entityType , $ attribute , $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]);
299
+ $ this ->fileUploaderDataResolver ->overrideFileUploaderMetadata (
300
+ $ entityType ,
301
+ $ attribute ,
302
+ $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]
303
+ );
376
304
}
377
305
378
306
$ this ->processWebsiteMeta ($ meta );
@@ -470,97 +398,6 @@ private function processWebsiteMeta(&$meta)
470
398
}
471
399
}
472
400
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
401
/**
565
402
* Process attributes by frontend input type
566
403
*
@@ -610,19 +447,4 @@ protected function prepareAddressData($addressId, array &$addresses, array $cust
610
447
}
611
448
}
612
449
}
613
-
614
- /**
615
- * Get FileProcessorFactory instance
616
- *
617
- * @return FileProcessorFactory
618
- * @deprecated 100.1.3
619
- */
620
- private function getFileProcessorFactory ()
621
- {
622
- if ($ this ->fileProcessorFactory === null ) {
623
- $ this ->fileProcessorFactory = ObjectManager::getInstance ()
624
- ->get (\Magento \Customer \Model \FileProcessorFactory::class);
625
- }
626
- return $ this ->fileProcessorFactory ;
627
- }
628
450
}
0 commit comments