Switcher.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Store switcher block
  8. */
  9. namespace Magento\Store\Block\Store;
  10. use Magento\Directory\Helper\Data;
  11. class Switcher extends \Magento\Framework\View\Element\Template
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $_groups = [];
  17. /**
  18. * @var array
  19. */
  20. protected $_stores = [];
  21. /**
  22. * @var bool
  23. */
  24. protected $_loaded = false;
  25. /**
  26. * Store factory
  27. *
  28. * @var \Magento\Store\Model\StoreFactory
  29. */
  30. protected $_storeFactory;
  31. /**
  32. * Store group factory
  33. *
  34. * @var \Magento\Store\Model\GroupFactory
  35. */
  36. protected $_storeGroupFactory;
  37. /**
  38. * @param \Magento\Framework\View\Element\Template\Context $context
  39. * @param \Magento\Store\Model\GroupFactory $storeGroupFactory
  40. * @param \Magento\Store\Model\StoreFactory $storeFactory
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\Template\Context $context,
  45. \Magento\Store\Model\GroupFactory $storeGroupFactory,
  46. \Magento\Store\Model\StoreFactory $storeFactory,
  47. array $data = []
  48. ) {
  49. $this->_storeGroupFactory = $storeGroupFactory;
  50. $this->_storeFactory = $storeFactory;
  51. parent::__construct($context, $data);
  52. }
  53. /**
  54. * @return void
  55. */
  56. protected function _construct()
  57. {
  58. $this->_loadData();
  59. $this->setStores([]);
  60. $this->setLanguages([]);
  61. return parent::_construct();
  62. }
  63. /**
  64. * @return $this
  65. */
  66. protected function _loadData()
  67. {
  68. if ($this->_loaded) {
  69. return $this;
  70. }
  71. $websiteId = $this->_storeManager->getStore()->getWebsiteId();
  72. $storeCollection = $this->_storeFactory->create()->getCollection()->addWebsiteFilter($websiteId);
  73. $groupCollection = $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($websiteId);
  74. foreach ($groupCollection as $group) {
  75. $this->_groups[$group->getId()] = $group;
  76. }
  77. /** @var \Magento\Store\Model\Store $store */
  78. foreach ($storeCollection as $store) {
  79. if (!$store->isActive()) {
  80. continue;
  81. }
  82. $store->setLocaleCode($this->_scopeConfig->getValue(
  83. Data::XML_PATH_DEFAULT_LOCALE,
  84. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  85. $store->getId()
  86. ));
  87. $this->_stores[$store->getGroupId()][$store->getId()] = $store;
  88. }
  89. $this->_loaded = true;
  90. return $this;
  91. }
  92. /**
  93. * @return int
  94. */
  95. public function getStoreCount()
  96. {
  97. $stores = [];
  98. $localeCode = $this->_scopeConfig->getValue(
  99. Data::XML_PATH_DEFAULT_LOCALE,
  100. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  101. );
  102. foreach ($this->_groups as $group) {
  103. if (!isset($this->_stores[$group->getId()])) {
  104. continue;
  105. }
  106. $useStore = false;
  107. foreach ($this->_stores[$group->getId()] as $store) {
  108. if ($store->getLocaleCode() == $localeCode) {
  109. $useStore = true;
  110. $stores[] = $store;
  111. }
  112. }
  113. if (!$useStore && isset($this->_stores[$group->getId()][$group->getDefaultStoreId()])) {
  114. $stores[] = $this->_stores[$group->getId()][$group->getDefaultStoreId()];
  115. }
  116. }
  117. $this->setStores($stores);
  118. return count($this->getStores());
  119. }
  120. /**
  121. * @return int
  122. */
  123. public function getLanguageCount()
  124. {
  125. $groupId = $this->_storeManager->getStore()->getGroupId();
  126. if (!isset($this->_stores[$groupId])) {
  127. $this->setLanguages([]);
  128. return 0;
  129. }
  130. $this->setLanguages($this->_stores[$groupId]);
  131. return count($this->getLanguages());
  132. }
  133. /**
  134. * @return int
  135. */
  136. public function getCurrentStoreId()
  137. {
  138. return $this->_storeManager->getStore()->getId();
  139. }
  140. /**
  141. * @return string
  142. */
  143. public function getCurrentStoreCode()
  144. {
  145. return $this->_storeManager->getStore()->getCode();
  146. }
  147. }