_configDataResource = $configDataResource; $this->_coreConfig = $coreConfig; $this->storeListFactory = $storeListFactory; $this->_storeGroupFactory = $storeGroupFactory; $this->_websiteFactory = $websiteFactory; $this->_storeManager = $storeManager; $this->_currencyFactory = $currencyFactory; } /** * init model * * @return void */ protected function _construct() { $this->_init(\Magento\Store\Model\ResourceModel\Website::class); } /** * Custom load * * @param int|string $id * @param string $field * @return $this */ public function load($id, $field = null) { if (!is_numeric($id) && $field === null) { $this->_getResource()->load($this, $id, 'code'); return $this; } return parent::load($id, $field); } /** * Get website config data * * @param string $path * @return mixed */ public function getConfig($path) { if (!isset($this->_configCache[$path])) { $config = $this->_coreConfig->getValue( $path, \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $this->getCode() ); if (!$config) { return false; } $this->_configCache[$path] = $config; } return $this->_configCache[$path]; } /** * Load group collection and set internal data * * @return void */ protected function _loadGroups() { $this->_groups = []; $this->_groupsCount = 0; foreach ($this->getGroupCollection() as $group) { $this->_groups[$group->getId()] = $group; $this->_groupIds[$group->getId()] = $group->getId(); if ($this->getDefaultGroupId() == $group->getId()) { $this->_defaultGroup = $group; } $this->_groupsCount++; } } /** * Set website groups * * @param array $groups * @return $this */ public function setGroups($groups) { $this->_groups = []; $this->_groupsCount = 0; foreach ($groups as $group) { $this->_groups[$group->getId()] = $group; $this->_groupIds[$group->getId()] = $group->getId(); if ($this->getDefaultGroupId() == $group->getId()) { $this->_defaultGroup = $group; } $this->_groupsCount++; } return $this; } /** * Retrieve new (not loaded) Group collection object with website filter * * @return \Magento\Store\Model\ResourceModel\Group\Collection */ public function getGroupCollection() { return $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($this->getId()) ->setLoadDefault(true); } /** * Retrieve website groups * * @return \Magento\Store\Model\Store[] */ public function getGroups() { if ($this->_groups === null) { $this->_loadGroups(); } return $this->_groups; } /** * Retrieve website group ids * * @return array */ public function getGroupIds() { if ($this->_groups === null) { $this->_loadGroups(); } return $this->_groupIds; } /** * Retrieve number groups in a website * * @return int */ public function getGroupsCount() { if ($this->_groups === null) { $this->_loadGroups(); } return $this->_groupsCount; } /** * Retrieve default group model * * @return \Magento\Store\Model\Store */ public function getDefaultGroup() { if (!$this->hasDefaultGroupId()) { return false; } if ($this->_groups === null) { $this->_loadGroups(); } return $this->_defaultGroup; } /** * Load store collection and set internal data * * @return void */ protected function _loadStores() { $this->_stores = []; $this->_storesCount = 0; foreach ($this->getStoreCollection() as $store) { $this->_stores[$store->getId()] = $store; $this->_storeIds[$store->getId()] = $store->getId(); $this->_storeCodes[$store->getId()] = $store->getCode(); if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) { $this->_defaultStore = $store; } $this->_storesCount++; } } /** * Set website stores * * @param array $stores * @return void */ public function setStores($stores) { $this->_stores = []; $this->_storesCount = 0; foreach ($stores as $store) { $this->_stores[$store->getId()] = $store; $this->_storeIds[$store->getId()] = $store->getId(); $this->_storeCodes[$store->getId()] = $store->getCode(); if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) { $this->_defaultStore = $store; } $this->_storesCount++; } } /** * Retrieve new (not loaded) Store collection object with website filter * * @return \Magento\Store\Model\ResourceModel\Store\Collection */ public function getStoreCollection() { return $this->storeListFactory->create()->addWebsiteFilter($this->getId())->setLoadDefault(true); } /** * Retrieve website store objects * * @return array */ public function getStores() { if ($this->_stores === null) { $this->_loadStores(); } return $this->_stores; } /** * Retrieve website store ids * * @return array */ public function getStoreIds() { if ($this->_stores === null) { $this->_loadStores(); } return $this->_storeIds; } /** * Retrieve website store codes * * @return array */ public function getStoreCodes() { if ($this->_stores === null) { $this->_loadStores(); } return $this->_storeCodes; } /** * Retrieve number stores in a website * * @return int */ public function getStoresCount() { if ($this->_stores === null) { $this->_loadStores(); } return $this->_storesCount; } /** * Can delete website * * @return bool */ public function isCanDelete() { if ($this->_isReadOnly || !$this->getId()) { return false; } if ($this->_isCanDelete === null) { $this->_isCanDelete = $this->_websiteFactory->create()->getCollection()->getSize() > 1 && !$this->getIsDefault(); } return $this->_isCanDelete; } /** * Retrieve unique website-group-store key for collection with groups and stores * * @return string */ public function getWebsiteGroupStore() { return implode('-', [$this->getWebsiteId(), $this->getGroupId(), $this->getStoreId()]); } /** * @return mixed */ public function getDefaultGroupId() { return $this->_getData('default_group_id'); } /** * @inheritdoc */ public function setDefaultGroupId($defaultGroupId) { return $this->setData('default_group_id', $defaultGroupId); } /** * @return mixed */ public function getCode() { return $this->_getData('code'); } /** * @inheritdoc */ public function setCode($code) { return $this->setData('code', $code); } /** * @inheritdoc */ public function getName() { return $this->_getData('name'); } /** * @inheritdoc */ public function setName($name) { return $this->setData('name', $name); } /** * @return $this */ public function beforeDelete() { $this->_configDataResource->clearScopeData( \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES, $this->getId() ); $this->_configDataResource->clearScopeData( \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $this->getStoreIds() ); return parent::beforeDelete(); } /** * Rewrite in order to clear configuration cache * * @return $this */ public function afterDelete() { $this->_storeManager->reinitStores(); parent::afterDelete(); return $this; } /** * Clear configuration cache after creation website * * @return $this * @since 100.2.0 */ public function afterSave() { if ($this->isObjectNew()) { $this->_storeManager->reinitStores(); } return parent::afterSave(); } /** * Retrieve website base currency code * * @return string */ public function getBaseCurrencyCode() { if ($this->getConfig( \Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE ) == \Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL ) { $currencyCode = $this->_coreConfig->getValue( \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default' ); } else { $currencyCode = $this->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE); } return $currencyCode; } /** * Retrieve website base currency * * @return \Magento\Directory\Model\Currency */ public function getBaseCurrency() { $currency = $this->getData('base_currency'); if ($currency === null) { $currency = $this->_currencyFactory->create()->load($this->getBaseCurrencyCode()); $this->setData('base_currency', $currency); } return $currency; } /** * Retrieve Default Website Store or null * * @return Store */ public function getDefaultStore() { // init stores if not loaded $this->getStores(); return $this->_defaultStore; } /** * Retrieve default stores select object * Select fields website_id, store_id * * @param bool $withDefault include/exclude default admin website * @return \Magento\Framework\DB\Select */ public function getDefaultStoresSelect($withDefault = false) { return $this->getResource()->getDefaultStoresSelect($withDefault); } /** * Get/Set isReadOnly flag * * @param bool $value * @return bool */ public function isReadOnly($value = null) { if (null !== $value) { $this->_isReadOnly = (bool)$value; } return $this->_isReadOnly; } /** * Get identities * * @return array */ public function getIdentities() { return [self::CACHE_TAG]; } /** * {@inheritdoc} * @since 100.1.0 */ public function getScopeType() { return \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE; } /** * {@inheritdoc} * @since 100.1.0 */ public function getScopeTypeName() { return 'Website'; } /** * {@inheritdoc} */ public function getExtensionAttributes() { return $this->_getExtensionAttributes(); } /** * {@inheritdoc} */ public function setExtensionAttributes( \Magento\Store\Api\Data\WebsiteExtensionInterface $extensionAttributes ) { return $this->_setExtensionAttributes($extensionAttributes); } }