123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Store\Model\System;
- use Magento\Framework\Data\OptionSourceInterface;
- /**
- * Core System Store Model
- *
- * @api
- * @since 100.0.2
- */
- class Store extends \Magento\Framework\DataObject implements OptionSourceInterface
- {
- /**
- * Website collection
- * websiteId => \Magento\Store\Model\Website
- *
- * @var array
- */
- protected $_websiteCollection = [];
- /**
- * Group collection
- * groupId => \Magento\Store\Model\Group
- *
- * @var array
- */
- protected $_groupCollection = [];
- /**
- * Store collection
- * storeId => \Magento\Store\Model\Store
- *
- * @var array
- */
- protected $_storeCollection;
- /**
- * @var bool
- */
- private $_isAdminScopeAllowed = true;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * Init model
- * Load Website, Group and Store collections
- *
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- */
- public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
- {
- $this->_storeManager = $storeManager;
- return $this->reload();
- }
- /**
- * Load/Reload Website collection
- *
- * @return $this
- */
- protected function _loadWebsiteCollection()
- {
- $this->_websiteCollection = $this->_storeManager->getWebsites();
- return $this;
- }
- /**
- * Load/Reload Group collection
- *
- * @return $this
- */
- protected function _loadGroupCollection()
- {
- $this->_groupCollection = [];
- foreach ($this->_storeManager->getWebsites() as $website) {
- foreach ($website->getGroups() as $group) {
- $this->_groupCollection[$group->getId()] = $group;
- }
- }
- return $this;
- }
- /**
- * Load/Reload Store collection
- *
- * @return $this
- */
- protected function _loadStoreCollection()
- {
- $this->_storeCollection = $this->_storeManager->getStores();
- return $this;
- }
- /**
- * Retrieve store values for form
- *
- * @param bool $empty
- * @param bool $all
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function getStoreValuesForForm($empty = false, $all = false)
- {
- $options = [];
- if ($empty) {
- $options[] = ['label' => '', 'value' => ''];
- }
- if ($all && $this->_isAdminScopeAllowed) {
- $options[] = ['label' => __('All Store Views'), 'value' => 0];
- }
- $nonEscapableNbspChar = html_entity_decode(' ', ENT_NOQUOTES, 'UTF-8');
- foreach ($this->_websiteCollection as $website) {
- $websiteShow = false;
- foreach ($this->_groupCollection as $group) {
- if ($website->getId() != $group->getWebsiteId()) {
- continue;
- }
- $groupShow = false;
- foreach ($this->_storeCollection as $store) {
- if ($group->getId() != $store->getGroupId()) {
- continue;
- }
- if (!$websiteShow) {
- $options[] = ['label' => $website->getName(), 'value' => []];
- $websiteShow = true;
- }
- if (!$groupShow) {
- $groupShow = true;
- $values = [];
- }
- $values[] = [
- 'label' => str_repeat($nonEscapableNbspChar, 4) . $store->getName(),
- 'value' => $store->getId(),
- ];
- }
- if ($groupShow) {
- $options[] = [
- 'label' => str_repeat($nonEscapableNbspChar, 4) . $group->getName(),
- 'value' => $values,
- ];
- }
- }
- }
- return $options;
- }
- /**
- * Retrieve stores structure
- *
- * @param bool $isAll
- * @param array $storeIds
- * @param array $groupIds
- * @param array $websiteIds
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function getStoresStructure($isAll = false, $storeIds = [], $groupIds = [], $websiteIds = [])
- {
- $out = [];
- $websites = $this->getWebsiteCollection();
- if ($isAll) {
- $out[] = ['value' => 0, 'label' => __('All Store Views')];
- }
- foreach ($websites as $website) {
- $websiteId = $website->getId();
- if ($websiteIds && !in_array($websiteId, $websiteIds)) {
- continue;
- }
- $out[$websiteId] = ['value' => $websiteId, 'label' => $website->getName()];
- foreach ($website->getGroups() as $group) {
- $groupId = $group->getId();
- if ($groupIds && !in_array($groupId, $groupIds)) {
- continue;
- }
- $out[$websiteId]['children'][$groupId] = ['value' => $groupId, 'label' => $group->getName()];
- foreach ($group->getStores() as $store) {
- $storeId = $store->getId();
- if ($storeIds && !in_array($storeId, $storeIds)) {
- continue;
- }
- $out[$websiteId]['children'][$groupId]['children'][$storeId] = [
- 'value' => $storeId,
- 'label' => $store->getName(),
- ];
- }
- if (empty($out[$websiteId]['children'][$groupId]['children'])) {
- unset($out[$websiteId]['children'][$groupId]);
- }
- }
- if (empty($out[$websiteId]['children'])) {
- unset($out[$websiteId]);
- }
- }
- return $out;
- }
- /**
- * Website label/value array getter, compatible with form dropdown options
- *
- * @param bool $empty
- * @param bool $all
- * @return array
- */
- public function getWebsiteValuesForForm($empty = false, $all = false)
- {
- $options = [];
- if ($empty) {
- $options[] = ['label' => __('-- Please Select --'), 'value' => ''];
- }
- if ($all && $this->_isAdminScopeAllowed) {
- $options[] = ['label' => __('Admin'), 'value' => 0];
- }
- foreach ($this->_websiteCollection as $website) {
- $options[] = ['label' => $website->getName(), 'value' => $website->getId()];
- }
- return $options;
- }
- /**
- * Get websites as id => name associative array
- *
- * @param bool $withDefault
- * @param string $attribute
- * @return array
- */
- public function getWebsiteOptionHash($withDefault = false, $attribute = 'name')
- {
- $options = [];
- foreach ($this->_storeManager->getWebsites((bool)$withDefault && $this->_isAdminScopeAllowed) as $website) {
- $options[$website->getId()] = $website->getDataUsingMethod($attribute);
- }
- return $options;
- }
- /**
- * Get store views as id => name associative array
- *
- * @param bool $withDefault
- * @param string $attribute
- * @return array
- */
- public function getStoreOptionHash($withDefault = false, $attribute = 'name')
- {
- $options = [];
- foreach ($this->_storeManager->getStores((bool)$withDefault && $this->_isAdminScopeAllowed) as $store) {
- $options[$store->getId()] = $store->getDataUsingMethod($attribute);
- }
- return $options;
- }
- /**
- * Get store groups as id => name associative array
- *
- * @param string $attribute
- * @return array
- */
- public function getStoreGroupOptionHash($attribute = 'name')
- {
- $options = [];
- foreach ($this->_groupCollection as $group) {
- $options[$group->getId()] = $group->getDataUsingMethod($attribute);
- }
- return $options;
- }
- /**
- * Retrieve Website name by Id
- *
- * @param int $websiteId
- * @return string|null
- */
- public function getWebsiteName($websiteId)
- {
- foreach ($this->_websiteCollection as $website) {
- if ($website->getId() == $websiteId) {
- return $website->getName();
- }
- }
- return null;
- }
- /**
- * Retrieve Group name by Id
- *
- * @param int $groupId
- * @return string|null
- */
- public function getGroupName($groupId)
- {
- foreach ($this->_groupCollection as $group) {
- if ($group->getId() == $groupId) {
- return $group->getName();
- }
- }
- return null;
- }
- /**
- * Retrieve Store name by Id
- *
- * @param int $storeId
- * @return string|null
- */
- public function getStoreName($storeId)
- {
- if (isset($this->_storeCollection[$storeId])) {
- return $this->_storeCollection[$storeId]->getName();
- }
- return null;
- }
- /**
- * Retrieve store name with website and website store
- *
- * @param int $storeId
- * @return \Magento\Store\Model\Store|null
- */
- public function getStoreData($storeId)
- {
- if (isset($this->_storeCollection[$storeId])) {
- return $this->_storeCollection[$storeId];
- }
- return null;
- }
- /**
- * Retrieve store name with website and website store
- *
- * @param int $storeId
- * @return string
- */
- public function getStoreNameWithWebsite($storeId)
- {
- $name = '';
- if (is_array($storeId)) {
- $names = [];
- foreach ($storeId as $id) {
- $names[] = $this->getStoreNameWithWebsite($id);
- }
- $name = implode(', ', $names);
- } else {
- if (isset($this->_storeCollection[$storeId])) {
- $data = $this->_storeCollection[$storeId];
- $name .= $this->getWebsiteName($data->getWebsiteId());
- $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
- $name .= ($name ? '/' : '') . $data->getName();
- }
- }
- return $name;
- }
- /**
- * Retrieve Website collection as array
- *
- * @return array
- */
- public function getWebsiteCollection()
- {
- return $this->_websiteCollection;
- }
- /**
- * Retrieve Group collection as array
- *
- * @return array
- */
- public function getGroupCollection()
- {
- return $this->_groupCollection;
- }
- /**
- * Retrieve Store collection as array
- *
- * @return array
- */
- public function getStoreCollection()
- {
- return $this->_storeCollection;
- }
- /**
- * Load/Reload collection(s) by type
- * Allowed types: website, group, store or null for all
- *
- * @param string $type
- * @return $this
- */
- public function reload($type = null)
- {
- if ($type === null) {
- $this->_loadWebsiteCollection();
- $this->_loadGroupCollection();
- $this->_loadStoreCollection();
- } else {
- switch ($type) {
- case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
- $this->_loadWebsiteCollection();
- break;
- case \Magento\Store\Model\ScopeInterface::SCOPE_GROUP:
- $this->_loadGroupCollection();
- break;
- case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
- $this->_loadStoreCollection();
- break;
- default:
- break;
- }
- }
- return $this;
- }
- /**
- * Retrieve store path with website and website store
- *
- * @param int $storeId
- * @return string
- */
- public function getStoreNamePath($storeId)
- {
- $name = '';
- if (is_array($storeId)) {
- $names = [];
- foreach ($storeId as $id) {
- $names[] = $this->getStoreNamePath($id);
- }
- $name = implode(', ', $names);
- } else {
- if (isset($this->_storeCollection[$storeId])) {
- $data = $this->_storeCollection[$storeId];
- $name .= $this->getWebsiteName($data->getWebsiteId());
- $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
- }
- }
- return $name;
- }
- /**
- * Specify whether to show admin-scope options
- *
- * @param bool $value
- * @return $this
- */
- public function setIsAdminScopeAllowed($value)
- {
- $this->_isAdminScopeAllowed = (bool)$value;
- return $this;
- }
- /**
- * Return array of options as value-label pairs
- *
- * @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
- */
- public function toOptionArray()
- {
- return $this->getStoreValuesForForm();
- }
- }
|