DataProvider.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Model\Address;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
  10. use Magento\Eav\Model\Config;
  11. use Magento\Eav\Model\Entity\Type;
  12. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  13. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  14. use Magento\Customer\Model\Address;
  15. use Magento\Customer\Model\FileUploaderDataResolver;
  16. use Magento\Customer\Model\AttributeMetadataResolver;
  17. use Magento\Ui\Component\Form\Element\Multiline;
  18. /**
  19. * Dataprovider of customer addresses for customer address grid.
  20. * @property \Magento\Customer\Model\ResourceModel\Address\Collection $collection
  21. */
  22. class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
  23. {
  24. /**
  25. * @var CustomerRepositoryInterface
  26. */
  27. private $customerRepository;
  28. /**
  29. * @var array
  30. */
  31. private $loadedData;
  32. /**
  33. * Allow to manage attributes, even they are hidden on storefront
  34. *
  35. * @var bool
  36. */
  37. private $allowToShowHiddenAttributes;
  38. /*
  39. * @var ContextInterface
  40. */
  41. private $context;
  42. /**
  43. * @var array
  44. */
  45. private $bannedInputTypes = ['media_image'];
  46. /**
  47. * @var array
  48. */
  49. private static $attributesToEliminate = [
  50. 'region',
  51. 'vat_is_valid',
  52. 'vat_request_date',
  53. 'vat_request_id',
  54. 'vat_request_success'
  55. ];
  56. /**
  57. * @var FileUploaderDataResolver
  58. */
  59. private $fileUploaderDataResolver;
  60. /**
  61. * @var AttributeMetadataResolver
  62. */
  63. private $attributeMetadataResolver;
  64. /**
  65. * DataProvider constructor.
  66. * @param string $name
  67. * @param string $primaryFieldName
  68. * @param string $requestFieldName
  69. * @param CollectionFactory $addressCollectionFactory
  70. * @param CustomerRepositoryInterface $customerRepository
  71. * @param Config $eavConfig
  72. * @param ContextInterface $context
  73. * @param FileUploaderDataResolver $fileUploaderDataResolver
  74. * @param AttributeMetadataResolver $attributeMetadataResolver
  75. * @param array $meta
  76. * @param array $data
  77. * @param bool $allowToShowHiddenAttributes
  78. * @throws \Magento\Framework\Exception\LocalizedException
  79. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  80. */
  81. public function __construct(
  82. $name,
  83. $primaryFieldName,
  84. $requestFieldName,
  85. CollectionFactory $addressCollectionFactory,
  86. CustomerRepositoryInterface $customerRepository,
  87. Config $eavConfig,
  88. ContextInterface $context,
  89. FileUploaderDataResolver $fileUploaderDataResolver,
  90. AttributeMetadataResolver $attributeMetadataResolver,
  91. array $meta = [],
  92. array $data = [],
  93. $allowToShowHiddenAttributes = true
  94. ) {
  95. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  96. $this->collection = $addressCollectionFactory->create();
  97. $this->collection->addAttributeToSelect('*');
  98. $this->customerRepository = $customerRepository;
  99. $this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes;
  100. $this->context = $context;
  101. $this->fileUploaderDataResolver = $fileUploaderDataResolver;
  102. $this->attributeMetadataResolver = $attributeMetadataResolver;
  103. $this->meta['general']['children'] = $this->getAttributesMeta(
  104. $eavConfig->getEntityType('customer_address')
  105. );
  106. }
  107. /**
  108. * Get Addresses data and process customer default billing & shipping addresses
  109. *
  110. * @return array
  111. * @throws \Magento\Framework\Exception\LocalizedException
  112. * @throws \Magento\Framework\Exception\NoSuchEntityException
  113. */
  114. public function getData(): array
  115. {
  116. if (null !== $this->loadedData) {
  117. return $this->loadedData;
  118. }
  119. $items = $this->collection->getItems();
  120. /** @var Address $item */
  121. foreach ($items as $item) {
  122. $addressId = $item->getEntityId();
  123. $item->load($addressId);
  124. $this->loadedData[$addressId] = $item->getData();
  125. $customerId = $this->loadedData[$addressId]['parent_id'];
  126. /** @var \Magento\Customer\Model\Customer $customer */
  127. $customer = $this->customerRepository->getById($customerId);
  128. $defaultBilling = $customer->getDefaultBilling();
  129. $defaultShipping = $customer->getDefaultShipping();
  130. $this->prepareAddressData($addressId, $this->loadedData, $defaultBilling, $defaultShipping);
  131. $this->fileUploaderDataResolver->overrideFileUploaderData($item, $this->loadedData[$addressId]);
  132. }
  133. if (null === $this->loadedData) {
  134. $this->loadedData[''] = $this->getDefaultData();
  135. }
  136. return $this->loadedData;
  137. }
  138. /**
  139. * Prepare address data
  140. *
  141. * @param int $addressId
  142. * @param array $addresses
  143. * @param string|null $defaultBilling
  144. * @param string|null $defaultShipping
  145. * @return void
  146. */
  147. private function prepareAddressData($addressId, array &$addresses, $defaultBilling, $defaultShipping): void
  148. {
  149. if (null !== $defaultBilling && $addressId === $defaultBilling) {
  150. $addresses[$addressId]['default_billing'] = '1';
  151. }
  152. if (null !== $defaultShipping && $addressId === $defaultShipping) {
  153. $addresses[$addressId]['default_shipping'] = '1';
  154. }
  155. foreach ($this->meta['general']['children'] as $attributeName => $attributeMeta) {
  156. if ($attributeMeta['arguments']['data']['config']['dataType'] === Multiline::NAME
  157. && isset($this->loadedData[$addressId][$attributeName])
  158. && !\is_array($this->loadedData[$addressId][$attributeName])
  159. ) {
  160. $this->loadedData[$addressId][$attributeName] = explode(
  161. "\n",
  162. $this->loadedData[$addressId][$attributeName]
  163. );
  164. }
  165. }
  166. }
  167. /**
  168. * Get default customer data for adding new address
  169. *
  170. * @throws \Magento\Framework\Exception\LocalizedException
  171. * @throws \Magento\Framework\Exception\NoSuchEntityException
  172. * @return array
  173. */
  174. private function getDefaultData(): array
  175. {
  176. $parentId = $this->context->getRequestParam('parent_id');
  177. $customer = $this->customerRepository->getById($parentId);
  178. $data = [
  179. 'parent_id' => $parentId,
  180. 'firstname' => $customer->getFirstname(),
  181. 'lastname' => $customer->getLastname()
  182. ];
  183. return $data;
  184. }
  185. /**
  186. * Get attributes meta
  187. *
  188. * @param Type $entityType
  189. * @return array
  190. * @throws \Magento\Framework\Exception\LocalizedException
  191. */
  192. private function getAttributesMeta(Type $entityType): array
  193. {
  194. $meta = [];
  195. $attributes = $entityType->getAttributeCollection();
  196. /* @var AbstractAttribute $attribute */
  197. foreach ($attributes as $attribute) {
  198. if (\in_array($attribute->getFrontendInput(), $this->bannedInputTypes, true)) {
  199. continue;
  200. }
  201. if (\in_array($attribute->getAttributeCode(), self::$attributesToEliminate, true)) {
  202. continue;
  203. }
  204. $meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta(
  205. $attribute,
  206. $entityType,
  207. $this->allowToShowHiddenAttributes,
  208. $this->getRequestFieldName()
  209. );
  210. }
  211. $this->attributeMetadataResolver->processWebsiteMeta($meta);
  212. return $meta;
  213. }
  214. }