1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Store switcher block
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Block_Store_Switcher extends Mage_Core_Block_Template
35: {
36: protected $_groups = array();
37: protected $_stores = array();
38: protected $_loaded = false;
39:
40: public function __construct()
41: {
42: $this->_loadData();
43: $this->setStores(array());
44: $this->setLanguages(array());
45: return parent::__construct();
46: }
47:
48: protected function _loadData()
49: {
50: if ($this->_loaded) {
51: return $this;
52: }
53:
54: $websiteId = Mage::app()->getStore()->getWebsiteId();
55: $storeCollection = Mage::getModel('core/store')
56: ->getCollection()
57: ->addWebsiteFilter($websiteId);
58: $groupCollection = Mage::getModel('core/store_group')
59: ->getCollection()
60: ->addWebsiteFilter($websiteId);
61: foreach ($groupCollection as $group) {
62: $this->_groups[$group->getId()] = $group;
63: }
64: foreach ($storeCollection as $store) {
65: if (!$store->getIsActive()) {
66: continue;
67: }
68: $store->setLocaleCode(Mage::getStoreConfig('general/locale/code', $store->getId()));
69: $this->_stores[$store->getGroupId()][$store->getId()] = $store;
70: }
71:
72: $this->_loaded = true;
73:
74: return $this;
75: }
76:
77: public function getStoreCount()
78: {
79: $stores = array();
80: $localeCode = Mage::getStoreConfig('general/locale/code');
81: foreach ($this->_groups as $group) {
82: if (!isset($this->_stores[$group->getId()])) {
83: continue;
84: }
85: $useStore = false;
86: foreach ($this->_stores[$group->getId()] as $store) {
87: if ($store->getLocaleCode() == $localeCode) {
88: $useStore = true;
89: $stores[] = $store;
90: }
91: }
92: if (!$useStore && isset($this->_stores[$group->getId()][$group->getDefaultStoreId()])) {
93: $stores[] = $this->_stores[$group->getId()][$group->getDefaultStoreId()];
94: }
95: }
96:
97: $this->setStores($stores);
98: return count($this->getStores());
99: }
100:
101: public function getLanguageCount()
102: {
103: $groupId = Mage::app()->getStore()->getGroupId();
104: if (!isset($this->_stores[$groupId])) {
105: $this->setLanguages(array());
106: return 0;
107: }
108: $this->setLanguages($this->_stores[$groupId]);
109: return count($this->getLanguages());
110: }
111:
112: public function getCurrentStoreId()
113: {
114: return Mage::app()->getStore()->getId();
115: }
116:
117: public function getCurrentStoreCode()
118: {
119: return Mage::app()->getStore()->getCode();
120: }
121: }
122: