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_Adminhtml
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_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Adminhtml_Block_Store_Switcher extends Mage_Adminhtml_Block_Template
35: {
36: /**
37: * Key in config for store switcher hint
38: */
39: const XPATH_HINT_KEY = 'store_switcher';
40:
41: /**
42: * @var array
43: */
44: protected $_storeIds;
45:
46: /**
47: * Name of store variable
48: *
49: * @var string
50: */
51: protected $_storeVarName = 'store';
52:
53: /**
54: * Url for store switcher hint
55: *
56: * @var string
57: */
58: protected $_hintUrl;
59:
60: /**
61: * @var bool
62: */
63: protected $_hasDefaultOption = true;
64:
65: public function __construct()
66: {
67: parent::__construct();
68: $this->setTemplate('store/switcher.phtml');
69: $this->setUseConfirm(true);
70: $this->setUseAjax(true);
71: $this->setDefaultStoreName($this->__('All Store Views'));
72: }
73:
74: /**
75: * Deprecated
76: */
77: public function getWebsiteCollection()
78: {
79: $collection = Mage::getModel('core/website')->getResourceCollection();
80:
81: $websiteIds = $this->getWebsiteIds();
82: if (!is_null($websiteIds)) {
83: $collection->addIdFilter($this->getWebsiteIds());
84: }
85:
86: return $collection->load();
87: }
88:
89: /**
90: * Get websites
91: *
92: * @return array
93: */
94: public function getWebsites()
95: {
96: $websites = Mage::app()->getWebsites();
97: if ($websiteIds = $this->getWebsiteIds()) {
98: foreach ($websites as $websiteId => $website) {
99: if (!in_array($websiteId, $websiteIds)) {
100: unset($websites[$websiteId]);
101: }
102: }
103: }
104: return $websites;
105: }
106:
107: /**
108: * Deprecated
109: */
110: public function getGroupCollection($website)
111: {
112: if (!$website instanceof Mage_Core_Model_Website) {
113: $website = Mage::getModel('core/website')->load($website);
114: }
115: return $website->getGroupCollection();
116: }
117:
118: /**
119: * Get store groups for specified website
120: *
121: * @param Mage_Core_Model_Website $website
122: * @return array
123: */
124: public function getStoreGroups($website)
125: {
126: if (!$website instanceof Mage_Core_Model_Website) {
127: $website = Mage::app()->getWebsite($website);
128: }
129: return $website->getGroups();
130: }
131:
132: /**
133: * Deprecated
134: */
135: public function getStoreCollection($group)
136: {
137: if (!$group instanceof Mage_Core_Model_Store_Group) {
138: $group = Mage::getModel('core/store_group')->load($group);
139: }
140: $stores = $group->getStoreCollection();
141: $_storeIds = $this->getStoreIds();
142: if (!empty($_storeIds)) {
143: $stores->addIdFilter($_storeIds);
144: }
145: return $stores;
146: }
147:
148: /**
149: * Get store views for specified store group
150: *
151: * @param Mage_Core_Model_Store_Group $group
152: * @return array
153: */
154: public function getStores($group)
155: {
156: if (!$group instanceof Mage_Core_Model_Store_Group) {
157: $group = Mage::app()->getGroup($group);
158: }
159: $stores = $group->getStores();
160: if ($storeIds = $this->getStoreIds()) {
161: foreach ($stores as $storeId => $store) {
162: if (!in_array($storeId, $storeIds)) {
163: unset($stores[$storeId]);
164: }
165: }
166: }
167: return $stores;
168: }
169:
170: public function getSwitchUrl()
171: {
172: if ($url = $this->getData('switch_url')) {
173: return $url;
174: }
175: return $this->getUrl('*/*/*', array('_current' => true, $this->_storeVarName => null));
176: }
177:
178: public function setStoreVarName($varName)
179: {
180: $this->_storeVarName = $varName;
181: return $this;
182: }
183:
184: public function getStoreId()
185: {
186: return $this->getRequest()->getParam($this->_storeVarName);
187: }
188:
189: public function setStoreIds($storeIds)
190: {
191: $this->_storeIds = $storeIds;
192: return $this;
193: }
194:
195: public function getStoreIds()
196: {
197: return $this->_storeIds;
198: }
199:
200: public function isShow()
201: {
202: return !Mage::app()->isSingleStoreMode();
203: }
204:
205: protected function _toHtml()
206: {
207: if (!Mage::app()->isSingleStoreMode()) {
208: return parent::_toHtml();
209: }
210: return '';
211: }
212:
213: /**
214: * Set/Get whether the switcher should show default option
215: *
216: * @param bool $hasDefaultOption
217: * @return bool
218: */
219: public function hasDefaultOption($hasDefaultOption = null)
220: {
221: if (null !== $hasDefaultOption) {
222: $this->_hasDefaultOption = $hasDefaultOption;
223: }
224: return $this->_hasDefaultOption;
225: }
226:
227: /**
228: * Return url for store switcher hint
229: *
230: * @return string
231: */
232: public function getHintUrl()
233: {
234: if (null === $this->_hintUrl) {
235: $this->_hintUrl = Mage::helper('core/hint')->getHintByCode(self::XPATH_HINT_KEY);
236: }
237: return $this->_hintUrl;
238: }
239:
240: /**
241: * Return store switcher hint html
242: *
243: * @return string
244: */
245: public function getHintHtml()
246: {
247: $html = '';
248: $url = $this->getHintUrl();
249: if ($url) {
250: $html = '<a'
251: . ' href="'. $this->escapeUrl($url) . '"'
252: . ' onclick="this.target=\'_blank\'"'
253: . ' title="' . $this->__('What is this?') . '"'
254: . ' class="link-store-scope">'
255: . $this->__('What is this?')
256: . '</a>';
257: }
258: return $html;
259: }
260: }
261: