Customer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Config\Backend\Show;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. /**
  9. * Customer Show Customer Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Customer extends \Magento\Framework\App\Config\Value
  14. {
  15. /**
  16. * @var \Magento\Eav\Model\Config
  17. */
  18. protected $_eavConfig;
  19. /**
  20. * @var \Magento\Store\Model\StoreManagerInterface
  21. */
  22. protected $storeManager;
  23. /**
  24. * @param \Magento\Framework\Model\Context $context
  25. * @param \Magento\Framework\Registry $registry
  26. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  27. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  28. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  29. * @param \Magento\Eav\Model\Config $eavConfig
  30. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  31. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Framework\Model\Context $context,
  36. \Magento\Framework\Registry $registry,
  37. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  38. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  39. \Magento\Store\Model\StoreManagerInterface $storeManager,
  40. \Magento\Eav\Model\Config $eavConfig,
  41. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  42. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  43. array $data = []
  44. ) {
  45. $this->_eavConfig = $eavConfig;
  46. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  47. $this->storeManager = $storeManager;
  48. }
  49. /**
  50. * Retrieve attribute code
  51. *
  52. * @return string
  53. */
  54. protected function _getAttributeCode()
  55. {
  56. return str_replace('_show', '', $this->getField());
  57. }
  58. /**
  59. * Retrieve attribute objects
  60. *
  61. * @return AbstractAttribute[]
  62. */
  63. protected function _getAttributeObjects()
  64. {
  65. return [$this->_eavConfig->getAttribute('customer', $this->_getAttributeCode())];
  66. }
  67. /**
  68. * Actions after save
  69. *
  70. * @return $this
  71. */
  72. public function afterSave()
  73. {
  74. $result = parent::afterSave();
  75. $valueConfig = [
  76. '' => ['is_required' => 0, 'is_visible' => 0],
  77. 'opt' => ['is_required' => 0, 'is_visible' => 1],
  78. '1' => ['is_required' => 0, 'is_visible' => 1],
  79. 'req' => ['is_required' => 1, 'is_visible' => 1],
  80. ];
  81. $value = $this->getValue();
  82. if (isset($valueConfig[$value])) {
  83. $data = $valueConfig[$value];
  84. } else {
  85. $data = $valueConfig[''];
  86. }
  87. if ($this->getScope() == 'websites') {
  88. $website = $this->storeManager->getWebsite($this->getScopeCode());
  89. $dataFieldPrefix = 'scope_';
  90. } else {
  91. $website = null;
  92. $dataFieldPrefix = '';
  93. }
  94. foreach ($this->_getAttributeObjects() as $attributeObject) {
  95. if ($website) {
  96. $attributeObject->setWebsite($website);
  97. $attributeObject->load($attributeObject->getId());
  98. }
  99. $attributeObject->setData($dataFieldPrefix . 'is_required', $data['is_required']);
  100. $attributeObject->setData($dataFieldPrefix . 'is_visible', $data['is_visible']);
  101. $attributeObject->save();
  102. }
  103. return $result;
  104. }
  105. /**
  106. * Processing object after delete data
  107. *
  108. * @return \Magento\Framework\Model\AbstractModel
  109. */
  110. public function afterDelete()
  111. {
  112. $result = parent::afterDelete();
  113. if ($this->getScope() == 'websites') {
  114. $website = $this->storeManager->getWebsite($this->getScopeCode());
  115. foreach ($this->_getAttributeObjects() as $attributeObject) {
  116. $attributeObject->setWebsite($website);
  117. $attributeObject->load($attributeObject->getId());
  118. $attributeObject->setData('scope_is_required', null);
  119. $attributeObject->setData('scope_is_visible', null);
  120. $attributeObject->save();
  121. }
  122. }
  123. return $result;
  124. }
  125. }