123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Store group model
- */
- namespace Magento\Store\Model;
- /**
- * Class Group
- *
- * @api
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Group extends \Magento\Framework\Model\AbstractExtensibleModel implements
- \Magento\Framework\DataObject\IdentityInterface,
- \Magento\Store\Api\Data\GroupInterface,
- \Magento\Framework\App\ScopeInterface
- {
- const ENTITY = 'store_group';
- const CACHE_TAG = 'store_group';
- /**
- * @var bool
- */
- protected $_cacheTag = true;
- /**
- * @var string
- */
- protected $_eventPrefix = 'store_group';
- /**
- * @var string
- */
- protected $_eventObject = 'store_group';
- /**
- * Group Store collection array
- *
- * @var \Magento\Store\Model\ResourceModel\Store\Collection[]
- */
- protected $_stores;
- /**
- * Group store ids array
- *
- * @var int[]
- */
- protected $_storeIds = [];
- /**
- * Group store codes array
- *
- * @var string[]
- */
- protected $_storeCodes = [];
- /**
- * The number of stores in a group
- *
- * @var int
- */
- protected $_storesCount = 0;
- /**
- * Group default store
- *
- * @var \Magento\Store\Model\Store
- */
- protected $_defaultStore;
- /**
- * @var bool
- */
- private $_isReadOnly = false;
- /**
- * @var \Magento\Config\Model\ResourceModel\Config\Data
- */
- protected $_configDataResource;
- /**
- * @var \Magento\Store\Model\Store
- */
- protected $_storeListFactory;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Framework\Event\ManagerInterface
- */
- private $eventManager;
- /**
- * @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\Store\Model\Store $store
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @param \Magento\Framework\Event\ManagerInterface|null $eventManager
- * @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\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = [],
- \Magento\Framework\Event\ManagerInterface $eventManager = null
- ) {
- $this->_configDataResource = $configDataResource;
- $this->_storeListFactory = $storeListFactory;
- $this->_storeManager = $storeManager;
- $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Framework\Event\ManagerInterface::class);
- parent::__construct(
- $context,
- $registry,
- $extensionFactory,
- $customAttributeFactory,
- $resource,
- $resourceCollection,
- $data
- );
- }
- /**
- * Init model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init(\Magento\Store\Model\ResourceModel\Group::class);
- }
- /**
- * 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->getDefaultStoreId() == $store->getId()) {
- $this->_defaultStore = $store;
- }
- $this->_storesCount++;
- }
- }
- /**
- * Set website stores
- *
- * @param \Magento\Store\Model\Store[] $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->getDefaultStoreId() == $store->getId()) {
- $this->_defaultStore = $store;
- }
- $this->_storesCount++;
- }
- }
- /**
- * Retrieve new (not loaded) Store collection object with group filter
- *
- * @return \Magento\Store\Model\ResourceModel\Store\Collection
- */
- public function getStoreCollection()
- {
- return $this->_storeListFactory->create()->addGroupFilter($this->getId());
- }
- /**
- * Retrieve website store objects
- *
- * @return \Magento\Store\Model\ResourceModel\Store\Collection[]
- */
- public function getStores()
- {
- if ($this->_stores === null) {
- $this->_loadStores();
- }
- return $this->_stores;
- }
- /**
- * Retrieve website store ids
- *
- * @return int[]
- */
- 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;
- }
- /**
- * @return int
- */
- public function getStoresCount()
- {
- if ($this->_stores === null) {
- $this->_loadStores();
- }
- return $this->_storesCount;
- }
- /**
- * Retrieve default store model
- *
- * @return \Magento\Store\Model\Store
- */
- public function getDefaultStore()
- {
- if (!$this->hasDefaultStoreId()) {
- return false;
- }
- if ($this->_stores === null) {
- $this->_loadStores();
- }
- return $this->_defaultStore;
- }
- /**
- * Get most suitable store by locale
- * If no store with given locale is found - default store is returned
- * If group has no stores - null is returned
- *
- * @param string $locale
- * @return \Magento\Store\Model\Store|null
- */
- public function getDefaultStoreByLocale($locale)
- {
- if ($this->getDefaultStore() && $this->getDefaultStore()->getLocaleCode() == $locale) {
- return $this->getDefaultStore();
- } else {
- $stores = $this->getStoresByLocale($locale);
- if (count($stores)) {
- return $stores[0];
- } else {
- return $this->getDefaultStore() ? $this->getDefaultStore() : null;
- }
- }
- }
- /**
- * Retrieve list of stores with given locale
- *
- * @param string $locale
- * @return \Magento\Store\Model\Store[]
- */
- public function getStoresByLocale($locale)
- {
- $stores = [];
- foreach ($this->getStores() as $store) {
- /* @var $store \Magento\Store\Model\Store */
- if ($store->getLocaleCode() == $locale) {
- $stores[] = $store;
- }
- }
- return $stores;
- }
- /**
- * Set relation to the website
- *
- * @param Website $website
- * @return void
- */
- public function setWebsite(Website $website)
- {
- $this->setWebsiteId($website->getId());
- }
- /**
- * Retrieve website model
- *
- * @return Website|bool
- */
- public function getWebsite()
- {
- if ($this->getWebsiteId() === null) {
- return false;
- }
- return $this->_storeManager->getWebsite($this->getWebsiteId());
- }
- /**
- * Is can delete group
- *
- * @return bool
- */
- public function isCanDelete()
- {
- if (!$this->getId()) {
- return false;
- }
- return $this->getWebsite()->getGroupsCount() > 1;
- }
- /**
- * @return mixed
- */
- public function getDefaultStoreId()
- {
- return $this->_getData('default_store_id');
- }
- /**
- * @inheritdoc
- */
- public function setDefaultStoreId($defaultStoreId)
- {
- return $this->setData('default_store_id', $defaultStoreId);
- }
- /**
- * @return mixed
- */
- public function getRootCategoryId()
- {
- return $this->_getData('root_category_id');
- }
- /**
- * @inheritdoc
- */
- public function setRootCategoryId($rootCategoryId)
- {
- return $this->setData('root_category_id', $rootCategoryId);
- }
- /**
- * @return mixed
- */
- public function getWebsiteId()
- {
- return $this->_getData('website_id');
- }
- /**
- * @inheritdoc
- */
- public function setWebsiteId($websiteId)
- {
- return $this->setData('website_id', $websiteId);
- }
- /**
- * @return $this
- */
- public function beforeDelete()
- {
- $this->_configDataResource->clearScopeData(
- \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
- $this->getStoreIds()
- );
- return parent::beforeDelete();
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function afterDelete()
- {
- $group = $this;
- $this->getResource()->addCommitCallback(function () use ($group) {
- $this->_storeManager->reinitStores();
- $this->eventManager->dispatch($this->_eventPrefix . '_delete', ['group' => $group]);
- });
- $result = parent::afterDelete();
- if ($this->getId() === $this->getWebsite()->getDefaultGroupId()) {
- $ids = $this->getWebsite()->getGroupIds();
- if (!empty($ids) && count($ids) > 1) {
- unset($ids[$this->getId()]);
- $defaultId = current($ids);
- } else {
- $defaultId = null;
- }
- $this->getWebsite()->setDefaultGroupId($defaultId);
- $this->getWebsite()->save();
- }
- return $result;
- }
- /**
- * @inheritdoc
- * @since 100.2.5
- */
- public function afterSave()
- {
- $group = $this;
- $this->getResource()->addCommitCallback(function () use ($group) {
- $this->_storeManager->reinitStores();
- $this->eventManager->dispatch($this->_eventPrefix . '_save', ['group' => $group]);
- });
- return parent::afterSave();
- }
- /**
- * 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}
- */
- public function getName()
- {
- return $this->getData('name');
- }
- /**
- * @inheritdoc
- */
- public function setName($name)
- {
- return $this->setData('name', $name);
- }
- /**
- * @inheritdoc
- * @since 100.1.0
- */
- public function getCode()
- {
- return $this->getData('code');
- }
- /**
- * @inheritdoc
- * @since 100.2.0
- */
- public function setCode($code)
- {
- return $this->setData('code', $code);
- }
- /**
- * {@inheritdoc}
- */
- public function getExtensionAttributes()
- {
- return $this->_getExtensionAttributes();
- }
- /**
- * {@inheritdoc}
- */
- public function setExtensionAttributes(
- \Magento\Store\Api\Data\GroupExtensionInterface $extensionAttributes
- ) {
- return $this->_setExtensionAttributes($extensionAttributes);
- }
- /**
- * {@inheritdoc}
- * @since 100.1.0
- */
- public function getScopeType()
- {
- return ScopeInterface::SCOPE_GROUP;
- }
- /**
- * {@inheritdoc}
- * @since 100.1.0
- */
- public function getScopeTypeName()
- {
- return 'Store';
- }
- }
|