123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Store\Model;
- /**
- * Core Website model
- *
- * @api
- * @method string getGroupTitle()
- * @method string getStoreTitle()
- * @method int getStoreId()
- * @method int getGroupId()
- * @method int getWebsiteId()
- * @method bool hasWebsiteId()
- * @method int getSortOrder()
- * @method \Magento\Store\Model\Website setSortOrder($value)
- * @method int getIsDefault()
- * @method \Magento\Store\Model\Website setIsDefault($value)
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Website extends \Magento\Framework\Model\AbstractExtensibleModel implements
- \Magento\Framework\DataObject\IdentityInterface,
- \Magento\Framework\App\ScopeInterface,
- \Magento\Store\Api\Data\WebsiteInterface
- {
- const ENTITY = 'store_website';
- const CACHE_TAG = 'website';
- /**
- * @var bool
- */
- protected $_cacheTag = true;
- /**
- * @var string
- */
- protected $_eventPrefix = 'website';
- /**
- * @var string
- */
- protected $_eventObject = 'website';
- /**
- * Cache configuration array
- *
- * @var array
- */
- protected $_configCache = [];
- /**
- * Website Group Collection array
- *
- * @var \Magento\Store\Model\Store[]
- */
- protected $_groups;
- /**
- * Website group ids array
- *
- * @var array
- */
- protected $_groupIds = [];
- /**
- * The number of groups in a website
- *
- * @var int
- */
- protected $_groupsCount;
- /**
- * Website Store collection array
- *
- * @var array
- */
- protected $_stores;
- /**
- * Website store ids array
- *
- * @var array
- */
- protected $_storeIds = [];
- /**
- * Website store codes array
- *
- * @var array
- */
- protected $_storeCodes = [];
- /**
- * The number of stores in a website
- *
- * @var int
- */
- protected $_storesCount = 0;
- /**
- * Website default group
- *
- * @var \Magento\Store\Model\Store
- */
- protected $_defaultGroup;
- /**
- * Website default store
- *
- * @var Store
- */
- protected $_defaultStore;
- /**
- * is can delete website
- *
- * @var bool
- */
- protected $_isCanDelete;
- /**
- * @var bool
- */
- private $_isReadOnly = false;
- /**
- * @var \Magento\Config\Model\ResourceModel\Config\Data
- */
- protected $_configDataResource;
- /**
- * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
- */
- protected $storeListFactory;
- /**
- * @var \Magento\Store\Model\GroupFactory
- */
- protected $_storeGroupFactory;
- /**
- * @var WebsiteFactory
- */
- protected $_websiteFactory;
- /**
- * @var StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Directory\Model\CurrencyFactory
- */
- protected $_currencyFactory;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
- * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
- * @param \Magento\Config\Model\ResourceModel\Config\Data $configDataResource
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
- * @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory
- * @param \Magento\Store\Model\GroupFactory $storeGroupFactory
- * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
- \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
- \Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
- \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
- \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
- \Magento\Store\Model\GroupFactory $storeGroupFactory,
- \Magento\Store\Model\WebsiteFactory $websiteFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Directory\Model\CurrencyFactory $currencyFactory,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- parent::__construct(
- $context,
- $registry,
- $extensionFactory,
- $customAttributeFactory,
- $resource,
- $resourceCollection,
- $data
- );
- $this->_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);
- }
- }
|