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
00035 class Mage_Catalog_Block_Navigation extends Mage_Core_Block_Template
00036 {
00037 protected $_categoryInstance = null;
00038
00039 protected function _construct()
00040 {
00041 $this->addData(array(
00042 'cache_lifetime' => false,
00043 'cache_tags' => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG),
00044 ));
00045 }
00046
00047
00048
00049
00050
00051
00052 public function getCacheKey()
00053 {
00054 return 'CATALOG_NAVIGATION_' . Mage::app()->getStore()->getId()
00055 . '_' . Mage::getDesign()->getPackageName()
00056 . '_' . Mage::getDesign()->getTheme('template')
00057 . '_' . Mage::getSingleton('customer/session')->getCustomerGroupId()
00058 . '_' . md5($this->getTemplate() . $this->getCurrenCategoryKey());
00059 }
00060
00061 public function getCurrenCategoryKey()
00062 {
00063 if ($category = Mage::registry('current_category')) {
00064 return $category->getPath();
00065 } else {
00066 return Mage::app()->getStore()->getRootCategoryId();
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075 public function getStoreCategories()
00076 {
00077 $helper = Mage::helper('catalog/category');
00078 return $helper->getStoreCategories();
00079 }
00080
00081
00082
00083
00084
00085
00086 public function getCurrentChildCategories()
00087 {
00088 $layer = Mage::getSingleton('catalog/layer');
00089 $category = $layer->getCurrentCategory();
00090
00091 $categories = $category->getChildrenCategories();
00092 $productCollection = Mage::getResourceModel('catalog/product_collection');
00093 $layer->prepareProductCollection($productCollection);
00094 $productCollection->addCountToCategories($categories);
00095 return $categories;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public function isCategoryActive($category)
00105 {
00106 if ($this->getCurrentCategory()) {
00107 return in_array($category->getId(), $this->getCurrentCategory()->getPathIds());
00108 }
00109 return false;
00110 }
00111
00112 protected function _getCategoryInstance()
00113 {
00114 if (is_null($this->_categoryInstance)) {
00115 $this->_categoryInstance = Mage::getModel('catalog/category');
00116 }
00117 return $this->_categoryInstance;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 public function getCategoryUrl($category)
00127 {
00128 if ($category instanceof Mage_Catalog_Model_Category) {
00129 $url = $category->getUrl();
00130 } else {
00131 $url = $this->_getCategoryInstance()
00132 ->setData($category->getData())
00133 ->getUrl();
00134 }
00135
00136 return $url;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 public function drawItem($category, $level=0, $last=false)
00148 {
00149 $html = '';
00150 if (!$category->getIsActive()) {
00151 return $html;
00152 }
00153 if (Mage::helper('catalog/category_flat')->isEnabled()) {
00154 $children = $category->getChildrenNodes();
00155 $childrenCount = count($children);
00156 } else {
00157 $children = $category->getChildren();
00158 $childrenCount = $children->count();
00159 }
00160 $hasChildren = $children && $childrenCount;
00161 $html.= '<li';
00162 if ($hasChildren) {
00163 $html.= ' onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)"';
00164 }
00165
00166 $html.= ' class="level'.$level;
00167 $html.= ' nav-'.str_replace('/', '-', Mage::helper('catalog/category')->getCategoryUrlPath($category->getRequestPath()));
00168 if ($this->isCategoryActive($category)) {
00169 $html.= ' active';
00170 }
00171 if ($last) {
00172 $html .= ' last';
00173 }
00174 if ($hasChildren) {
00175 $cnt = 0;
00176 foreach ($children as $child) {
00177 if ($child->getIsActive()) {
00178 $cnt++;
00179 }
00180 }
00181 if ($cnt > 0) {
00182 $html .= ' parent';
00183 }
00184 }
00185 $html.= '">'."\n";
00186 $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
00187
00188 if ($hasChildren){
00189
00190 $j = 0;
00191 $htmlChildren = '';
00192 foreach ($children as $child) {
00193 if ($child->getIsActive()) {
00194 $htmlChildren.= $this->drawItem($child, $level+1, ++$j >= $cnt);
00195 }
00196 }
00197
00198 if (!empty($htmlChildren)) {
00199 $html.= '<ul class="level' . $level . '">'."\n"
00200 .$htmlChildren
00201 .'</ul>';
00202 }
00203
00204 }
00205 $html.= '</li>'."\n";
00206 return $html;
00207 }
00208
00209
00210
00211
00212
00213
00214 public function getCurrentCategory()
00215 {
00216 if (Mage::getSingleton('catalog/layer')) {
00217 return Mage::getSingleton('catalog/layer')->getCurrentCategory();
00218 }
00219 return false;
00220 }
00221
00222
00223
00224
00225
00226
00227 public function getCurrentCategoryPath()
00228 {
00229 if ($this->getCurrentCategory()) {
00230 return explode(',', $this->getCurrentCategory()->getPathInStore());
00231 }
00232 return array();
00233 }
00234
00235
00236
00237
00238
00239
00240
00241 public function drawOpenCategoryItem($category) {
00242 $html = '';
00243 if (!$category->getIsActive()) {
00244 return $html;
00245 }
00246
00247 $html.= '<li';
00248
00249 if ($this->isCategoryActive($category)) {
00250 $html.= ' class="active"';
00251 }
00252
00253 $html.= '>'."\n";
00254 $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
00255
00256 if (in_array($category->getId(), $this->getCurrentCategoryPath())){
00257 $children = $category->getChildren();
00258 $hasChildren = $children && $children->count();
00259
00260 if ($hasChildren) {
00261 $htmlChildren = '';
00262 foreach ($children as $child) {
00263 $htmlChildren.= $this->drawOpenCategoryItem($child);
00264 }
00265
00266 if (!empty($htmlChildren)) {
00267 $html.= '<ul>'."\n"
00268 .$htmlChildren
00269 .'</ul>';
00270 }
00271 }
00272 }
00273 $html.= '</li>'."\n";
00274 return $html;
00275 }
00276
00277 }