Document.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Ui\Component\DataProvider;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\Customer\Model\AccountManagement;
  9. use Magento\Framework\Api\AttributeValueFactory;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Customer\Api\GroupRepositoryInterface;
  13. use Magento\Framework\App\ObjectManager;
  14. use Magento\Store\Model\ScopeInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Class Document
  18. */
  19. class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
  20. {
  21. /**
  22. * @var string
  23. */
  24. private static $genderAttributeCode = 'gender';
  25. /**
  26. * @var string
  27. */
  28. private static $groupAttributeCode = 'group_id';
  29. /**
  30. * @var string
  31. */
  32. private static $websiteAttributeCode = 'website_id';
  33. /**
  34. * @var string
  35. */
  36. private static $websiteIdAttributeCode = 'original_website_id';
  37. /**
  38. * @var string
  39. */
  40. private static $confirmationAttributeCode = 'confirmation';
  41. /**
  42. * @var string
  43. */
  44. private static $accountLockAttributeCode = 'lock_expires';
  45. /**
  46. * @var CustomerMetadataInterface
  47. */
  48. private $customerMetadata;
  49. /**
  50. * @var GroupRepositoryInterface
  51. */
  52. private $groupRepository;
  53. /**
  54. * @var StoreManagerInterface
  55. */
  56. private $storeManager;
  57. /**
  58. * @var ScopeConfigInterface
  59. */
  60. private $scopeConfig;
  61. /**
  62. * Document constructor.
  63. *
  64. * @param AttributeValueFactory $attributeValueFactory
  65. * @param GroupRepositoryInterface $groupRepository
  66. * @param CustomerMetadataInterface $customerMetadata
  67. * @param StoreManagerInterface $storeManager
  68. * @param ScopeConfigInterface $scopeConfig
  69. */
  70. public function __construct(
  71. AttributeValueFactory $attributeValueFactory,
  72. GroupRepositoryInterface $groupRepository,
  73. CustomerMetadataInterface $customerMetadata,
  74. StoreManagerInterface $storeManager,
  75. ScopeConfigInterface $scopeConfig = null
  76. ) {
  77. parent::__construct($attributeValueFactory);
  78. $this->customerMetadata = $customerMetadata;
  79. $this->groupRepository = $groupRepository;
  80. $this->storeManager = $storeManager;
  81. $this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->create(ScopeConfigInterface::class);
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function getCustomAttribute($attributeCode)
  87. {
  88. switch ($attributeCode) {
  89. case self::$genderAttributeCode:
  90. $this->setGenderValue();
  91. break;
  92. case self::$groupAttributeCode:
  93. $this->setCustomerGroupValue();
  94. break;
  95. case self::$websiteAttributeCode:
  96. $this->setWebsiteValue();
  97. break;
  98. case self::$confirmationAttributeCode:
  99. $this->setConfirmationValue();
  100. break;
  101. case self::$accountLockAttributeCode:
  102. $this->setAccountLockValue();
  103. break;
  104. }
  105. return parent::getCustomAttribute($attributeCode);
  106. }
  107. /**
  108. * Update customer gender value. Method set gender label instead of id value
  109. *
  110. * @return void
  111. * @throws \Magento\Framework\Exception\LocalizedException
  112. */
  113. private function setGenderValue()
  114. {
  115. $value = $this->getData(self::$genderAttributeCode);
  116. if (!$value) {
  117. $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
  118. return;
  119. }
  120. try {
  121. $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
  122. $option = $attributeMetadata->getOptions()[$value];
  123. $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
  124. } catch (NoSuchEntityException $e) {
  125. $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
  126. }
  127. }
  128. /**
  129. * Update customer group value. Method set group code instead id value
  130. *
  131. * @return void
  132. * @throws \Magento\Framework\Exception\LocalizedException
  133. */
  134. private function setCustomerGroupValue()
  135. {
  136. $value = $this->getData(self::$groupAttributeCode);
  137. try {
  138. $group = $this->groupRepository->getById($value);
  139. $this->setCustomAttribute(self::$groupAttributeCode, $group->getCode());
  140. } catch (NoSuchEntityException $e) {
  141. $this->setCustomAttribute(self::$groupAttributeCode, 'N/A');
  142. }
  143. }
  144. /**
  145. * Update website value. Method set website name instead id value
  146. *
  147. * @return void
  148. */
  149. private function setWebsiteValue()
  150. {
  151. $value = $this->getData(self::$websiteAttributeCode);
  152. $list = $this->storeManager->getWebsites();
  153. $this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
  154. $this->setCustomAttribute(self::$websiteIdAttributeCode, $value);
  155. }
  156. /**
  157. * Update confirmation value. Method set confirmation text value to match what is shown in grid
  158. *
  159. * @return void
  160. */
  161. private function setConfirmationValue()
  162. {
  163. $value = $this->getData(self::$confirmationAttributeCode);
  164. $websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode);
  165. $isConfirmRequired = $this->scopeConfig->isSetFlag(
  166. AccountManagement::XML_PATH_IS_CONFIRM,
  167. ScopeInterface::SCOPE_WEBSITES,
  168. $websiteId
  169. );
  170. $valueText = __('Confirmation Not Required');
  171. if ($isConfirmRequired) {
  172. $valueText = $value === null ? __('Confirmed') : __('Confirmation Required');
  173. }
  174. $this->setCustomAttribute(self::$confirmationAttributeCode, $valueText);
  175. }
  176. /**
  177. * Update lock expires value. Method set account lock text value to match what is shown in grid
  178. *
  179. * @return void
  180. */
  181. private function setAccountLockValue()
  182. {
  183. $value = $this->getDataByPath(self::$accountLockAttributeCode);
  184. $valueText = __('Unlocked');
  185. if ($value !== null) {
  186. $lockExpires = new \DateTime($value);
  187. if ($lockExpires > new \DateTime()) {
  188. $valueText = __('Locked');
  189. }
  190. }
  191. $this->setCustomAttribute(self::$accountLockAttributeCode, $valueText);
  192. }
  193. }