-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathSwitcher.php
172 lines (153 loc) · 4.33 KB
/
Switcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Copyright 2014 Adobe
* All Rights Reserved.
*/
/**
* Store switcher block
*/
namespace Magento\Store\Block\Store;
use Magento\Directory\Helper\Data;
class Switcher extends \Magento\Framework\View\Element\Template
{
/**
* @var array
*/
protected $_groups = [];
/**
* @var array
*/
protected $_stores = [];
/**
* @var bool
*/
protected $_loaded = false;
/**
* @var \Magento\Store\Model\StoreFactory
*/
protected $_storeFactory;
/**
* @var \Magento\Store\Model\GroupFactory
*/
protected $_storeGroupFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Store\Model\GroupFactory $storeGroupFactory
* @param \Magento\Store\Model\StoreFactory $storeFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Store\Model\GroupFactory $storeGroupFactory,
\Magento\Store\Model\StoreFactory $storeFactory,
array $data = []
) {
$this->_storeGroupFactory = $storeGroupFactory;
$this->_storeFactory = $storeFactory;
parent::__construct($context, $data);
}
/**
* @inheritdoc
*/
protected function _construct()
{
$this->_loadData();
$this->setStores([]);
$this->setLanguages([]);
parent::_construct();
}
/**
* Load data
*
* @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;
}
/** @var \Magento\Store\Model\Store $store */
foreach ($storeCollection as $store) {
if (!$store->isActive()) {
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;
}
/**
* Sets stores and returns their count
*
* @return int
*/
public function getStoreCount()
{
$stores = [];
$localeCode = $this->_scopeConfig->getValue(
Data::XML_PATH_DEFAULT_LOCALE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
foreach ($this->_groups as $group) {
if (!isset($this->_stores[$group->getId()])) {
continue;
}
$useStore = false;
foreach ($this->_stores[$group->getId()] as $store) {
if ($store->getLocaleCode() == $localeCode) {
$useStore = true;
$stores[] = $store;
}
}
if (!$useStore && isset($this->_stores[$group->getId()][$group->getDefaultStoreId()])) {
$stores[] = $this->_stores[$group->getId()][$group->getDefaultStoreId()];
}
}
$this->setStores($stores);
return count($this->getStores());
}
/**
* Sets languages and returns their count
*
* @return int
*/
public function getLanguageCount()
{
$groupId = $this->_storeManager->getStore()->getGroupId();
if (!isset($this->_stores[$groupId])) {
$this->setLanguages([]);
return 0;
}
$this->setLanguages($this->_stores[$groupId]);
return count($this->getLanguages());
}
/**
* Returns current store id
*
* @return int
*/
public function getCurrentStoreId()
{
return $this->_storeManager->getStore()->getId();
}
/**
* Returns current store code
*
* @return string
*/
public function getCurrentStoreCode()
{
return $this->_storeManager->getStore()->getCode();
}
}