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_Page
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 and language switcher block
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Page_Block_Switch extends Mage_Core_Block_Template
35: {
36: protected $_storeInUrl;
37:
38: public function getCurrentWebsiteId()
39: {
40: return Mage::app()->getStore()->getWebsiteId();
41: }
42:
43: public function getCurrentGroupId()
44: {
45: return Mage::app()->getStore()->getGroupId();
46: }
47:
48: public function getCurrentStoreId()
49: {
50: return Mage::app()->getStore()->getId();
51: }
52:
53: public function getRawGroups()
54: {
55: if (!$this->hasData('raw_groups')) {
56: $websiteGroups = Mage::app()->getWebsite()->getGroups();
57:
58: $groups = array();
59: foreach ($websiteGroups as $group) {
60: $groups[$group->getId()] = $group;
61: }
62: $this->setData('raw_groups', $groups);
63: }
64: return $this->getData('raw_groups');
65: }
66:
67: public function getRawStores()
68: {
69: if (!$this->hasData('raw_stores')) {
70: $websiteStores = Mage::app()->getWebsite()->getStores();
71: $stores = array();
72: foreach ($websiteStores as $store) {
73: /* @var $store Mage_Core_Model_Store */
74: if (!$store->getIsActive()) {
75: continue;
76: }
77: $store->setLocaleCode(Mage::getStoreConfig('general/locale/code', $store->getId()));
78:
79: $params = array(
80: '_query' => array()
81: );
82: if (!$this->isStoreInUrl()) {
83: $params['_query']['___store'] = $store->getCode();
84: }
85: $baseUrl = $store->getUrl('', $params);
86:
87: $store->setHomeUrl($baseUrl);
88: $stores[$store->getGroupId()][$store->getId()] = $store;
89: }
90: $this->setData('raw_stores', $stores);
91: }
92: return $this->getData('raw_stores');
93: }
94:
95: /**
96: * Retrieve list of store groups with default urls set
97: *
98: * @return array
99: */
100: public function getGroups()
101: {
102: if (!$this->hasData('groups')) {
103: $rawGroups = $this->getRawGroups();
104: $rawStores = $this->getRawStores();
105:
106: $groups = array();
107: $localeCode = Mage::getStoreConfig('general/locale/code');
108: foreach ($rawGroups as $group) {
109: /* @var $group Mage_Core_Model_Store_Group */
110: if (!isset($rawStores[$group->getId()])) {
111: continue;
112: }
113: if ($group->getId() == $this->getCurrentGroupId()) {
114: $groups[] = $group;
115: continue;
116: }
117:
118: $store = $group->getDefaultStoreByLocale($localeCode);
119:
120: if ($store) {
121: $group->setHomeUrl($store->getHomeUrl());
122: $groups[] = $group;
123: }
124: }
125: $this->setData('groups', $groups);
126: }
127: return $this->getData('groups');
128: }
129:
130: public function getStores()
131: {
132: if (!$this->getData('stores')) {
133: $rawStores = $this->getRawStores();
134:
135: $groupId = $this->getCurrentGroupId();
136: if (!isset($rawStores[$groupId])) {
137: $stores = array();
138: } else {
139: $stores = $rawStores[$groupId];
140: }
141: $this->setData('stores', $stores);
142: }
143: return $this->getData('stores');
144: }
145:
146: public function getCurrentStoreCode()
147: {
148: return Mage::app()->getStore()->getCode();
149: }
150:
151: public function isStoreInUrl()
152: {
153: if (is_null($this->_storeInUrl)) {
154: $this->_storeInUrl = Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL);
155: }
156: return $this->_storeInUrl;
157: }
158: }
159: