/** * @param \Magento\Store\Model\StoreFactory $storeFactory * @param \Magento\Store\Model\WebsiteFactory $websiteFactory * @param \Magento\Store\Model\GroupFactory $groupFactory */ public function __construct(\Magento\Store\Model\StoreFactory $storeFactory, \Magento\Store\Model\WebsiteFactory $websiteFactory, \Magento\Store\Model\GroupFactory $groupFactory) { $this->_store = $storeFactory->create(); $this->_store->setId(\Magento\Store\Model\Store::DISTRO_STORE_ID); $this->_store->setCode(\Magento\Store\Model\Store::DEFAULT_CODE); $this->_website = $websiteFactory->create(); $this->_group = $groupFactory->create(); }
/** * {@inheritdoc} */ public function get($id) { if (isset($this->entities[$id])) { return $this->entities[$id]; } $group = $this->groupFactory->create(); $group->load($id); if (null === $group->getId()) { throw new NoSuchEntityException(); } $this->entities[$id] = $group; return $group; }
/** * Retrieve list of store groups * * @return array */ protected function _getStoreGroups() { $websites = $this->_websiteFactory->create()->getCollection(); $allgroups = $this->_groupFactory->create()->getCollection(); $groups = []; foreach ($websites as $website) { $values = []; foreach ($allgroups as $group) { if ($group->getWebsiteId() == $website->getId()) { $values[] = ['label' => $group->getName(), 'value' => $group->getId()]; } } $groups[] = ['label' => $website->getName(), 'value' => $values]; } return $groups; }
/** * Get current store group name * * @return string */ public function getCurrentStoreGroupName() { if ($this->getStoreGroupId() !== null) { $group = $this->_storeGroupFactory->create(); $group->load($this->getStoreGroupId()); if ($group->getId()) { return $group->getName(); } } }
/** * @return $this */ protected function _loadData() { if ($this->_loaded) { return $this; } $websiteId = $this->_storeManager->getStore()->getWebsiteId(); $storeCollection = $this->_storeFactory->create()->getCollection()->addWebsiteFilter($websiteId); $groupCollection = $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($websiteId); foreach ($groupCollection as $group) { $this->_groups[$group->getId()] = $group; } foreach ($storeCollection as $store) { if (!$store->getIsActive()) { continue; } $store->setLocaleCode($this->_scopeConfig->getValue(Data::XML_PATH_DEFAULT_LOCALE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store->getId())); $this->_stores[$store->getGroupId()][$store->getId()] = $store; } $this->_loaded = true; return $this; }
protected function loadMocks() { $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getLocaleCode', 'isActive', 'getId', 'getGroupId', 'getCollection'])->getMock(); $groupMock = $this->getMockBuilder('Magento\\Store\\Model\\Group')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */ $storeCollectionMock = $this->getMockBuilder('Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection')->disableOriginalConstructor()->setMethods(['addWebsiteFilter', 'load'])->getMockForAbstractClass(); /** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */ $groupCollectionMock = $this->getMockBuilder('Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection')->disableOriginalConstructor()->setMethods(['addWebsiteFilter', 'load'])->getMockForAbstractClass(); $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock); $this->storeFactoryMock->expects($this->any())->method('create')->willReturn($storeMock); $this->storeGroupFactoryMock->expects($this->any())->method('create')->willReturn($groupMock); $storeMock->expects($this->any())->method('getCollection')->willReturn($storeCollectionMock); $groupMock->expects($this->any())->method('getCollection')->willReturn($groupCollectionMock); $groupMock->expects($this->any())->method('getId')->willReturn(1); $storeMock->expects($this->any())->method('isActive')->willReturn(true); $storeMock->expects($this->atLeastOnce())->method('getLocaleCode')->willReturn('en_US'); $storeMock->expects($this->any())->method('getGroupId')->willReturn(1); $storeMock->expects($this->any())->method('setLocaleCode'); $storeMock->expects($this->any())->method('getId')->willReturn(1); $storeCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$storeMock]); $groupCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$groupMock]); }
/** * Prepare website specific fieldset * * @param \Magento\Framework\Data\Form $form * @return void */ protected function _prepareStoreFieldset(\Magento\Framework\Data\Form $form) { $websiteModel = $this->_coreRegistry->registry('store_data'); $postData = $this->_coreRegistry->registry('store_post_data'); if ($postData) { $websiteModel->setData($postData['website']); } $fieldset = $form->addFieldset('website_fieldset', array('legend' => __('Web Site Information'))); /* @var $fieldset \Magento\Framework\Data\Form */ $fieldset->addField('website_name', 'text', array('name' => 'website[name]', 'label' => __('Name'), 'value' => $websiteModel->getName(), 'required' => true, 'disabled' => $websiteModel->isReadOnly())); $fieldset->addField('website_code', 'text', array('name' => 'website[code]', 'label' => __('Code'), 'value' => $websiteModel->getCode(), 'required' => true, 'disabled' => $websiteModel->isReadOnly())); $fieldset->addField('website_sort_order', 'text', array('name' => 'website[sort_order]', 'label' => __('Sort Order'), 'value' => $websiteModel->getSortOrder(), 'required' => false, 'disabled' => $websiteModel->isReadOnly())); if ($this->_coreRegistry->registry('store_action') == 'edit') { $groups = $this->_groupFactory->create()->getCollection()->addWebsiteFilter($websiteModel->getId())->setWithoutStoreViewFilter()->toOptionArray(); $fieldset->addField('website_default_group_id', 'select', array('name' => 'website[default_group_id]', 'label' => __('Default Store'), 'value' => $websiteModel->getDefaultGroupId(), 'values' => $groups, 'required' => false, 'disabled' => $websiteModel->isReadOnly())); } if (!$websiteModel->getIsDefault() && $websiteModel->getStoresCount()) { $fieldset->addField('is_default', 'checkbox', array('name' => 'website[is_default]', 'label' => __('Set as Default'), 'value' => 1, 'disabled' => $websiteModel->isReadOnly())); } else { $fieldset->addField('is_default', 'hidden', array('name' => 'website[is_default]', 'value' => $websiteModel->getIsDefault())); } $fieldset->addField('website_website_id', 'hidden', array('name' => 'website[website_id]', 'value' => $websiteModel->getId())); }
/** * @param array $data * @param bool $processStores * @param bool $processCategory * @return \Magento\Store\Model\Group */ public function createGroup(array $data, $processStores = true, $processCategory = true) { // Prepare & Validate Data $data = array_merge($this->defaultGroupData, $data); $this->validateNewGroupData($data); // Create Model $group = $this->groupFactory->create(); /** @var \Magento\Store\Model\Group $group */ $group->addData($data)->setRootCategoryId($this->getDefaultRootCategoryId())->save(); // Create/Update Stores if ($processStores && isset($data['_stores']) && is_array($data['_stores'])) { foreach ($data['_stores'] as $storeData) { $storeData['group_id'] = $group->getId(); $storeData['website_id'] = $group->getWebsiteId(); try { $this->createStore($storeData); } catch (AlreadyExistsException $e) { $this->updateStore($storeData['code'], $storeData); } } } // Default Group if (isset($data['_is_default']) && $data['_is_default']) { $group->getWebsite()->setDefaultGroupId($group->getId())->save(); } // Root Category if ($processCategory && isset($data['_root_category'])) { try { $rootCategory = $this->createRootCategory($data['_root_category']); } catch (AlreadyExistsException $e) { $rootCategory = $this->getRootCategoryByName($data['_root_category']['name']); } $group->setRootCategoryId($rootCategory->getId())->save(); } return $group; }
/** * 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 application store group object * * @param null|Group|string $groupId * @return Group * @throws \Magento\Store\Model\Exception */ public function getGroup($groupId = null) { if (is_null($groupId)) { $groupId = $this->getStore()->getGroupId(); } elseif ($groupId instanceof Group) { return $groupId; } if (empty($this->_groups[$groupId])) { $group = $this->_groupFactory->create(); if (is_numeric($groupId)) { $group->load($groupId); if (!$group->hasGroupId()) { throw new \Magento\Store\Model\Exception('Invalid store group id requested.'); } } $this->_groups[$group->getGroupId()] = $group; } return $this->_groups[$groupId]; }