00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Page_Block_Switch extends Mage_Core_Block_Template
00035 {
00036 protected $_storeInUrl;
00037
00038 public function getCurrentWebsiteId()
00039 {
00040 return Mage::app()->getStore()->getWebsiteId();
00041 }
00042
00043 public function getCurrentGroupId()
00044 {
00045 return Mage::app()->getStore()->getGroupId();
00046 }
00047
00048 public function getCurrentStoreId()
00049 {
00050 return Mage::app()->getStore()->getId();
00051 }
00052
00053 public function getRawGroups()
00054 {
00055 if (!$this->hasData('raw_groups')) {
00056 $websiteGroups = Mage::app()->getWebsite()->getGroups();
00057
00058 $groups = array();
00059 foreach ($websiteGroups as $group) {
00060 $groups[$group->getId()] = $group;
00061 }
00062 $this->setData('raw_groups', $groups);
00063 }
00064 return $this->getData('raw_groups');
00065 }
00066
00067 public function getRawStores()
00068 {
00069 if (!$this->hasData('raw_stores')) {
00070 $websiteStores = Mage::app()->getWebsite()->getStores();
00071 $stores = array();
00072 foreach ($websiteStores as $store) {
00073
00074 if (!$store->getIsActive()) {
00075 continue;
00076 }
00077 $store->setLocaleCode(Mage::getStoreConfig('general/locale/code', $store->getId()));
00078
00079 $params = array(
00080 '_query' => array()
00081 );
00082 if (!$this->isStoreInUrl()) {
00083 $params['_query']['___store'] = $store->getCode();
00084 }
00085 $baseUrl = $store->getUrl('', $params);
00086
00087 $store->setHomeUrl($baseUrl);
00088 $stores[$store->getGroupId()][$store->getId()] = $store;
00089 }
00090 $this->setData('raw_stores', $stores);
00091 }
00092 return $this->getData('raw_stores');
00093 }
00094
00095 public function getGroups()
00096 {
00097 if (!$this->hasData('groups')) {
00098 $rawGroups = $this->getRawGroups();
00099 $rawStores = $this->getRawStores();
00100
00101 $groups = array();
00102 $localeCode = Mage::getStoreConfig('general/locale/code');
00103 foreach ($rawGroups as $group) {
00104 if (!isset($rawStores[$group->getId()])) {
00105 continue;
00106 }
00107 if ($group->getId() == $this->getCurrentGroupId()) {
00108 $groups[] = $group;
00109 continue;
00110 }
00111 $store = false;
00112 foreach ($rawStores[$group->getId()] as $s) {
00113 if ($s->getLocaleCode() == $localeCode) {
00114 $store = $s;
00115 break;
00116 }
00117 }
00118 if (!$store && isset($rawStores[$group->getId()][$group->getDefaultStoreId()])) {
00119 $store = $rawStores[$group->getId()][$group->getDefaultStoreId()];
00120 }
00121 if ($store) {
00122 $group->setHomeUrl($store->getHomeUrl());
00123 $groups[] = $group;
00124 }
00125 }
00126 $this->setData('groups', $groups);
00127 }
00128 return $this->getData('groups');
00129 }
00130
00131 public function getStores()
00132 {
00133 if (!$this->getData('stores')) {
00134 $rawStores = $this->getRawStores();
00135
00136 $groupId = $this->getCurrentGroupId();
00137 if (!isset($rawStores[$groupId])) {
00138 $stores = array();
00139 } else {
00140 $stores = $rawStores[$groupId];
00141 }
00142 $this->setData('stores', $stores);
00143 }
00144 return $this->getData('stores');
00145 }
00146
00147 public function getCurrentStoreCode()
00148 {
00149 return Mage::app()->getStore()->getCode();
00150 }
00151
00152 public function isStoreInUrl()
00153 {
00154 if (is_null($this->_storeInUrl)) {
00155 $this->_storeInUrl = Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL);
00156 }
00157 return $this->_storeInUrl;
00158 }
00159 }