55 */
66namespace Magento \Customer \Model \Customer ;
77
8- use Magento \Customer \Api \AddressMetadataInterface ;
9- use Magento \Customer \Api \CustomerMetadataInterface ;
108use Magento \Customer \Api \Data \AddressInterface ;
119use Magento \Customer \Api \Data \CustomerInterface ;
1210use Magento \Customer \Model \Address ;
1311use Magento \Customer \Model \Attribute ;
1412use Magento \Customer \Model \Customer ;
15- use Magento \Customer \Model \FileProcessor ;
1613use Magento \Customer \Model \FileProcessorFactory ;
1714use Magento \Customer \Model \ResourceModel \Address \Attribute \Source \CountryWithWebsites ;
1815use Magento \Customer \Model \ResourceModel \Customer \Collection ;
2825use Magento \Ui \Component \Form \Element \Multiline ;
2926use Magento \Ui \Component \Form \Field ;
3027use Magento \Ui \DataProvider \EavValidationRules ;
28+ use Magento \Customer \Model \FileUploaderDataResolver ;
3129
3230/**
3331 * Supplies the data for the customer UI component
3432 *
3533 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34+ * @SuppressWarnings(PHPMD.TooManyFields)
3635 *
37- * @deprecated \Magento\Customer\Model\Address\DataProvider is used instead
36+ * @deprecated \Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses is used instead
3837 * @api
3938 * @since 100.0.2
4039 */
@@ -112,21 +111,6 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
112111 */
113112 protected $ session ;
114113
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-
130114 /**
131115 * Customer fields that must be removed
132116 *
@@ -150,6 +134,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
150134 */
151135 private $ allowToShowHiddenAttributes ;
152136
137+ /**
138+ * @var FileUploaderDataResolver
139+ */
140+ private $ fileUploaderDataResolver ;
141+
153142 /**
154143 * @param string $name
155144 * @param string $primaryFieldName
@@ -163,7 +152,9 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
163152 * @param array $data
164153 * @param ContextInterface $context
165154 * @param bool $allowToShowHiddenAttributes
155+ * @param FileUploaderDataResolver|null $fileUploaderDataResolver
166156 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
157+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
167158 */
168159 public function __construct (
169160 $ name ,
@@ -177,17 +168,19 @@ public function __construct(
177168 array $ meta = [],
178169 array $ data = [],
179170 ContextInterface $ context = null ,
180- $ allowToShowHiddenAttributes = true
171+ $ allowToShowHiddenAttributes = true ,
172+ $ fileUploaderDataResolver = null
181173 ) {
182174 parent ::__construct ($ name , $ primaryFieldName , $ requestFieldName , $ meta , $ data );
183175 $ this ->eavValidationRules = $ eavValidationRules ;
184176 $ this ->collection = $ customerCollectionFactory ->create ();
185177 $ this ->collection ->addAttributeToSelect ('* ' );
186178 $ this ->eavConfig = $ eavConfig ;
187179 $ this ->filterPool = $ filterPool ;
188- $ this ->fileProcessorFactory = $ fileProcessorFactory ?: $ this ->getFileProcessorFactory ();
189180 $ this ->context = $ context ?: ObjectManager::getInstance ()->get (ContextInterface::class);
190181 $ this ->allowToShowHiddenAttributes = $ allowToShowHiddenAttributes ;
182+ $ this ->fileUploaderDataResolver = $ fileUploaderDataResolver
183+ ?: ObjectManager::getInstance ()->get (FileUploaderDataResolver::class);
191184 $ this ->meta ['customer ' ]['children ' ] = $ this ->getAttributesMeta (
192185 $ this ->eavConfig ->getEntityType ('customer ' )
193186 );
@@ -228,7 +221,7 @@ public function getData()
228221 foreach ($ items as $ customer ) {
229222 $ result ['customer ' ] = $ customer ->getData ();
230223
231- $ this ->overrideFileUploaderData ($ customer , $ result ['customer ' ]);
224+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ customer , $ result ['customer ' ]);
232225
233226 $ result ['customer ' ] = array_diff_key (
234227 $ result ['customer ' ],
@@ -243,7 +236,7 @@ public function getData()
243236 $ result ['address ' ][$ addressId ] = $ address ->getData ();
244237 $ this ->prepareAddressData ($ addressId , $ result ['address ' ], $ result ['customer ' ]);
245238
246- $ this ->overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
239+ $ this ->fileUploaderDataResolver -> overrideFileUploaderData ($ address , $ result ['address ' ][$ addressId ]);
247240 }
248241 $ this ->loadedData [$ customer ->getId ()] = $ result ;
249242 }
@@ -258,75 +251,6 @@ public function getData()
258251 return $ this ->loadedData ;
259252 }
260253
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-
330254 /**
331255 * Get attributes meta
332256 *
@@ -372,7 +296,11 @@ protected function getAttributesMeta(Type $entityType)
372296 $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['componentType ' ] = Field::NAME ;
373297 $ meta [$ code ]['arguments ' ]['data ' ]['config ' ]['visible ' ] = $ this ->canShowAttribute ($ attribute );
374298
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+ );
376304 }
377305
378306 $ this ->processWebsiteMeta ($ meta );
@@ -470,97 +398,6 @@ private function processWebsiteMeta(&$meta)
470398 }
471399 }
472400
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-
564401 /**
565402 * Process attributes by frontend input type
566403 *
@@ -610,19 +447,4 @@ protected function prepareAddressData($addressId, array &$addresses, array $cust
610447 }
611448 }
612449 }
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- }
628450}
0 commit comments