123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Model\Config\Backend\Show;
- use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
- /**
- * Customer Show Customer Model
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Customer extends \Magento\Framework\App\Config\Value
- {
- /**
- * @var \Magento\Eav\Model\Config
- */
- protected $_eavConfig;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManager;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
- * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Eav\Model\Config $eavConfig
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Framework\App\Config\ScopeConfigInterface $config,
- \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Eav\Model\Config $eavConfig,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- $this->_eavConfig = $eavConfig;
- parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
- $this->storeManager = $storeManager;
- }
- /**
- * Retrieve attribute code
- *
- * @return string
- */
- protected function _getAttributeCode()
- {
- return str_replace('_show', '', $this->getField());
- }
- /**
- * Retrieve attribute objects
- *
- * @return AbstractAttribute[]
- */
- protected function _getAttributeObjects()
- {
- return [$this->_eavConfig->getAttribute('customer', $this->_getAttributeCode())];
- }
- /**
- * Actions after save
- *
- * @return $this
- */
- public function afterSave()
- {
- $result = parent::afterSave();
- $valueConfig = [
- '' => ['is_required' => 0, 'is_visible' => 0],
- 'opt' => ['is_required' => 0, 'is_visible' => 1],
- '1' => ['is_required' => 0, 'is_visible' => 1],
- 'req' => ['is_required' => 1, 'is_visible' => 1],
- ];
- $value = $this->getValue();
- if (isset($valueConfig[$value])) {
- $data = $valueConfig[$value];
- } else {
- $data = $valueConfig[''];
- }
- if ($this->getScope() == 'websites') {
- $website = $this->storeManager->getWebsite($this->getScopeCode());
- $dataFieldPrefix = 'scope_';
- } else {
- $website = null;
- $dataFieldPrefix = '';
- }
- foreach ($this->_getAttributeObjects() as $attributeObject) {
- if ($website) {
- $attributeObject->setWebsite($website);
- $attributeObject->load($attributeObject->getId());
- }
- $attributeObject->setData($dataFieldPrefix . 'is_required', $data['is_required']);
- $attributeObject->setData($dataFieldPrefix . 'is_visible', $data['is_visible']);
- $attributeObject->save();
- }
- return $result;
- }
- /**
- * Processing object after delete data
- *
- * @return \Magento\Framework\Model\AbstractModel
- */
- public function afterDelete()
- {
- $result = parent::afterDelete();
- if ($this->getScope() == 'websites') {
- $website = $this->storeManager->getWebsite($this->getScopeCode());
- foreach ($this->_getAttributeObjects() as $attributeObject) {
- $attributeObject->setWebsite($website);
- $attributeObject->load($attributeObject->getId());
- $attributeObject->setData('scope_is_required', null);
- $attributeObject->setData('scope_is_visible', null);
- $attributeObject->save();
- }
- }
- return $result;
- }
- }
|