Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Store.php
Go to the documentation of this file.
1 <?php
7 
9 
17 {
24  protected $_websiteCollection = [];
25 
32  protected $_groupCollection = [];
33 
40  protected $_storeCollection;
41 
45  private $_isAdminScopeAllowed = true;
46 
50  protected $_storeManager;
51 
59  {
60  $this->_storeManager = $storeManager;
61  return $this->reload();
62  }
63 
69  protected function _loadWebsiteCollection()
70  {
71  $this->_websiteCollection = $this->_storeManager->getWebsites();
72  return $this;
73  }
74 
80  protected function _loadGroupCollection()
81  {
82  $this->_groupCollection = [];
83  foreach ($this->_storeManager->getWebsites() as $website) {
84  foreach ($website->getGroups() as $group) {
85  $this->_groupCollection[$group->getId()] = $group;
86  }
87  }
88  return $this;
89  }
90 
96  protected function _loadStoreCollection()
97  {
98  $this->_storeCollection = $this->_storeManager->getStores();
99  return $this;
100  }
101 
111  public function getStoreValuesForForm($empty = false, $all = false)
112  {
113  $options = [];
114  if ($empty) {
115  $options[] = ['label' => '', 'value' => ''];
116  }
117  if ($all && $this->_isAdminScopeAllowed) {
118  $options[] = ['label' => __('All Store Views'), 'value' => 0];
119  }
120 
121  $nonEscapableNbspChar = html_entity_decode('&#160;', ENT_NOQUOTES, 'UTF-8');
122 
123  foreach ($this->_websiteCollection as $website) {
124  $websiteShow = false;
125  foreach ($this->_groupCollection as $group) {
126  if ($website->getId() != $group->getWebsiteId()) {
127  continue;
128  }
129  $groupShow = false;
130  foreach ($this->_storeCollection as $store) {
131  if ($group->getId() != $store->getGroupId()) {
132  continue;
133  }
134  if (!$websiteShow) {
135  $options[] = ['label' => $website->getName(), 'value' => []];
136  $websiteShow = true;
137  }
138  if (!$groupShow) {
139  $groupShow = true;
140  $values = [];
141  }
142  $values[] = [
143  'label' => str_repeat($nonEscapableNbspChar, 4) . $store->getName(),
144  'value' => $store->getId(),
145  ];
146  }
147  if ($groupShow) {
148  $options[] = [
149  'label' => str_repeat($nonEscapableNbspChar, 4) . $group->getName(),
150  'value' => $values,
151  ];
152  }
153  }
154  }
155  return $options;
156  }
157 
169  public function getStoresStructure($isAll = false, $storeIds = [], $groupIds = [], $websiteIds = [])
170  {
171  $out = [];
172  $websites = $this->getWebsiteCollection();
173 
174  if ($isAll) {
175  $out[] = ['value' => 0, 'label' => __('All Store Views')];
176  }
177 
178  foreach ($websites as $website) {
179  $websiteId = $website->getId();
180  if ($websiteIds && !in_array($websiteId, $websiteIds)) {
181  continue;
182  }
183  $out[$websiteId] = ['value' => $websiteId, 'label' => $website->getName()];
184 
185  foreach ($website->getGroups() as $group) {
186  $groupId = $group->getId();
187  if ($groupIds && !in_array($groupId, $groupIds)) {
188  continue;
189  }
190  $out[$websiteId]['children'][$groupId] = ['value' => $groupId, 'label' => $group->getName()];
191 
192  foreach ($group->getStores() as $store) {
193  $storeId = $store->getId();
194  if ($storeIds && !in_array($storeId, $storeIds)) {
195  continue;
196  }
197  $out[$websiteId]['children'][$groupId]['children'][$storeId] = [
198  'value' => $storeId,
199  'label' => $store->getName(),
200  ];
201  }
202  if (empty($out[$websiteId]['children'][$groupId]['children'])) {
203  unset($out[$websiteId]['children'][$groupId]);
204  }
205  }
206  if (empty($out[$websiteId]['children'])) {
207  unset($out[$websiteId]);
208  }
209  }
210  return $out;
211  }
212 
220  public function getWebsiteValuesForForm($empty = false, $all = false)
221  {
222  $options = [];
223  if ($empty) {
224  $options[] = ['label' => __('-- Please Select --'), 'value' => ''];
225  }
226  if ($all && $this->_isAdminScopeAllowed) {
227  $options[] = ['label' => __('Admin'), 'value' => 0];
228  }
229 
230  foreach ($this->_websiteCollection as $website) {
231  $options[] = ['label' => $website->getName(), 'value' => $website->getId()];
232  }
233  return $options;
234  }
235 
243  public function getWebsiteOptionHash($withDefault = false, $attribute = 'name')
244  {
245  $options = [];
246  foreach ($this->_storeManager->getWebsites((bool)$withDefault && $this->_isAdminScopeAllowed) as $website) {
247  $options[$website->getId()] = $website->getDataUsingMethod($attribute);
248  }
249  return $options;
250  }
251 
259  public function getStoreOptionHash($withDefault = false, $attribute = 'name')
260  {
261  $options = [];
262  foreach ($this->_storeManager->getStores((bool)$withDefault && $this->_isAdminScopeAllowed) as $store) {
263  $options[$store->getId()] = $store->getDataUsingMethod($attribute);
264  }
265  return $options;
266  }
267 
274  public function getStoreGroupOptionHash($attribute = 'name')
275  {
276  $options = [];
277  foreach ($this->_groupCollection as $group) {
278  $options[$group->getId()] = $group->getDataUsingMethod($attribute);
279  }
280  return $options;
281  }
282 
289  public function getWebsiteName($websiteId)
290  {
291  foreach ($this->_websiteCollection as $website) {
292  if ($website->getId() == $websiteId) {
293  return $website->getName();
294  }
295  }
296  return null;
297  }
298 
305  public function getGroupName($groupId)
306  {
307  foreach ($this->_groupCollection as $group) {
308  if ($group->getId() == $groupId) {
309  return $group->getName();
310  }
311  }
312  return null;
313  }
314 
321  public function getStoreName($storeId)
322  {
323  if (isset($this->_storeCollection[$storeId])) {
324  return $this->_storeCollection[$storeId]->getName();
325  }
326  return null;
327  }
328 
335  public function getStoreData($storeId)
336  {
337  if (isset($this->_storeCollection[$storeId])) {
338  return $this->_storeCollection[$storeId];
339  }
340  return null;
341  }
342 
350  {
351  $name = '';
352  if (is_array($storeId)) {
353  $names = [];
354  foreach ($storeId as $id) {
355  $names[] = $this->getStoreNameWithWebsite($id);
356  }
357  $name = implode(', ', $names);
358  } else {
359  if (isset($this->_storeCollection[$storeId])) {
360  $data = $this->_storeCollection[$storeId];
361  $name .= $this->getWebsiteName($data->getWebsiteId());
362  $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
363  $name .= ($name ? '/' : '') . $data->getName();
364  }
365  }
366  return $name;
367  }
368 
374  public function getWebsiteCollection()
375  {
377  }
378 
384  public function getGroupCollection()
385  {
387  }
388 
394  public function getStoreCollection()
395  {
397  }
398 
406  public function reload($type = null)
407  {
408  if ($type === null) {
409  $this->_loadWebsiteCollection();
410  $this->_loadGroupCollection();
411  $this->_loadStoreCollection();
412  } else {
413  switch ($type) {
414  case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
415  $this->_loadWebsiteCollection();
416  break;
417  case \Magento\Store\Model\ScopeInterface::SCOPE_GROUP:
418  $this->_loadGroupCollection();
419  break;
420  case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
421  $this->_loadStoreCollection();
422  break;
423  default:
424  break;
425  }
426  }
427  return $this;
428  }
429 
436  public function getStoreNamePath($storeId)
437  {
438  $name = '';
439  if (is_array($storeId)) {
440  $names = [];
441  foreach ($storeId as $id) {
442  $names[] = $this->getStoreNamePath($id);
443  }
444  $name = implode(', ', $names);
445  } else {
446  if (isset($this->_storeCollection[$storeId])) {
447  $data = $this->_storeCollection[$storeId];
448  $name .= $this->getWebsiteName($data->getWebsiteId());
449  $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
450  }
451  }
452  return $name;
453  }
454 
462  {
463  $this->_isAdminScopeAllowed = (bool)$value;
464  return $this;
465  }
466 
472  public function toOptionArray()
473  {
474  return $this->getStoreValuesForForm();
475  }
476 }
getStoreValuesForForm($empty=false, $all=false)
Definition: Store.php:111
getStoreGroupOptionHash($attribute='name')
Definition: Store.php:274
getStoreOptionHash($withDefault=false, $attribute='name')
Definition: Store.php:259
$id
Definition: fieldset.phtml:14
getWebsiteValuesForForm($empty=false, $all=false)
Definition: Store.php:220
$group
Definition: sections.phtml:16
$storeManager
$values
Definition: options.phtml:88
__()
Definition: __.php:13
getWebsiteOptionHash($withDefault=false, $attribute='name')
Definition: Store.php:243
$type
Definition: item.phtml:13
getStoresStructure($isAll=false, $storeIds=[], $groupIds=[], $websiteIds=[])
Definition: Store.php:169
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
Definition: Store.php:58
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14