1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Block_Navigation extends Mage_Core_Block_Template
36: {
37: protected $_categoryInstance = null;
38:
39: 40: 41: 42: 43:
44: protected $_currentCategoryKey;
45:
46: 47: 48: 49: 50:
51: protected $_itemLevelPositions = array();
52:
53: protected function _construct()
54: {
55: $this->addData(array(
56: 'cache_lifetime' => false,
57: 'cache_tags' => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG),
58: ));
59: }
60:
61: 62: 63: 64: 65:
66: public function getCacheKeyInfo()
67: {
68: $shortCacheId = array(
69: 'CATALOG_NAVIGATION',
70: Mage::app()->getStore()->getId(),
71: Mage::getDesign()->getPackageName(),
72: Mage::getDesign()->getTheme('template'),
73: Mage::getSingleton('customer/session')->getCustomerGroupId(),
74: 'template' => $this->getTemplate(),
75: 'name' => $this->getNameInLayout(),
76: $this->getCurrenCategoryKey()
77: );
78: $cacheId = $shortCacheId;
79:
80: $shortCacheId = array_values($shortCacheId);
81: $shortCacheId = implode('|', $shortCacheId);
82: $shortCacheId = md5($shortCacheId);
83:
84: $cacheId['category_path'] = $this->getCurrenCategoryKey();
85: $cacheId['short_cache_id'] = $shortCacheId;
86:
87: return $cacheId;
88: }
89:
90: 91: 92: 93: 94:
95: public function getCurrenCategoryKey()
96: {
97: if (!$this->_currentCategoryKey) {
98: $category = Mage::registry('current_category');
99: if ($category) {
100: $this->_currentCategoryKey = $category->getPath();
101: } else {
102: $this->_currentCategoryKey = Mage::app()->getStore()->getRootCategoryId();
103: }
104: }
105:
106: return $this->_currentCategoryKey;
107: }
108:
109: 110: 111: 112: 113:
114: public function getStoreCategories()
115: {
116: $helper = Mage::helper('catalog/category');
117: return $helper->getStoreCategories();
118: }
119:
120: 121: 122: 123: 124:
125: public function getCurrentChildCategories()
126: {
127: $layer = Mage::getSingleton('catalog/layer');
128: $category = $layer->getCurrentCategory();
129:
130: $categories = $category->getChildrenCategories();
131: $productCollection = Mage::getResourceModel('catalog/product_collection');
132: $layer->prepareProductCollection($productCollection);
133: $productCollection->addCountToCategories($categories);
134: return $categories;
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function isCategoryActive($category)
144: {
145: if ($this->getCurrentCategory()) {
146: return in_array($category->getId(), $this->getCurrentCategory()->getPathIds());
147: }
148: return false;
149: }
150:
151: protected function _getCategoryInstance()
152: {
153: if (is_null($this->_categoryInstance)) {
154: $this->_categoryInstance = Mage::getModel('catalog/category');
155: }
156: return $this->_categoryInstance;
157: }
158:
159: 160: 161: 162: 163: 164:
165: public function getCategoryUrl($category)
166: {
167: if ($category instanceof Mage_Catalog_Model_Category) {
168: $url = $category->getUrl();
169: } else {
170: $url = $this->_getCategoryInstance()
171: ->setData($category->getData())
172: ->getUrl();
173: }
174:
175: return $url;
176: }
177:
178: 179: 180: 181: 182: 183:
184: protected function _getItemPosition($level)
185: {
186: if ($level == 0) {
187: $zeroLevelPosition = isset($this->_itemLevelPositions[$level]) ? $this->_itemLevelPositions[$level] + 1 : 1;
188: $this->_itemLevelPositions = array();
189: $this->_itemLevelPositions[$level] = $zeroLevelPosition;
190: } elseif (isset($this->_itemLevelPositions[$level])) {
191: $this->_itemLevelPositions[$level]++;
192: } else {
193: $this->_itemLevelPositions[$level] = 1;
194: }
195:
196: $position = array();
197: for($i = 0; $i <= $level; $i++) {
198: if (isset($this->_itemLevelPositions[$i])) {
199: $position[] = $this->_itemLevelPositions[$i];
200: }
201: }
202: return implode('-', $position);
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
218: protected function ($category, $level = 0, $isLast = false, $isFirst = false,
219: $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
220: {
221: if (!$category->getIsActive()) {
222: return '';
223: }
224: $html = array();
225:
226:
227: if (Mage::helper('catalog/category_flat')->isEnabled()) {
228: $children = (array)$category->getChildrenNodes();
229: $childrenCount = count($children);
230: } else {
231: $children = $category->getChildren();
232: $childrenCount = $children->count();
233: }
234: $hasChildren = ($children && $childrenCount);
235:
236:
237: $activeChildren = array();
238: foreach ($children as $child) {
239: if ($child->getIsActive()) {
240: $activeChildren[] = $child;
241: }
242: }
243: $activeChildrenCount = count($activeChildren);
244: $hasActiveChildren = ($activeChildrenCount > 0);
245:
246:
247: $classes = array();
248: $classes[] = 'level' . $level;
249: $classes[] = 'nav-' . $this->_getItemPosition($level);
250: if ($this->isCategoryActive($category)) {
251: $classes[] = 'active';
252: }
253: $linkClass = '';
254: if ($isOutermost && $outermostItemClass) {
255: $classes[] = $outermostItemClass;
256: $linkClass = ' class="'.$outermostItemClass.'"';
257: }
258: if ($isFirst) {
259: $classes[] = 'first';
260: }
261: if ($isLast) {
262: $classes[] = 'last';
263: }
264: if ($hasActiveChildren) {
265: $classes[] = 'parent';
266: }
267:
268:
269: $attributes = array();
270: if (count($classes) > 0) {
271: $attributes['class'] = implode(' ', $classes);
272: }
273: if ($hasActiveChildren && !$noEventAttributes) {
274: $attributes['onmouseover'] = 'toggleMenu(this,1)';
275: $attributes['onmouseout'] = 'toggleMenu(this,0)';
276: }
277:
278:
279: $htmlLi = '<li';
280: foreach ($attributes as $attrName => $attrValue) {
281: $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
282: }
283: $htmlLi .= '>';
284: $html[] = $htmlLi;
285:
286: $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
287: $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
288: $html[] = '</a>';
289:
290:
291: $htmlChildren = '';
292: $j = 0;
293: foreach ($activeChildren as $child) {
294: $htmlChildren .= $this->_renderCategoryMenuItemHtml(
295: $child,
296: ($level + 1),
297: ($j == $activeChildrenCount - 1),
298: ($j == 0),
299: false,
300: $outermostItemClass,
301: $childrenWrapClass,
302: $noEventAttributes
303: );
304: $j++;
305: }
306: if (!empty($htmlChildren)) {
307: if ($childrenWrapClass) {
308: $html[] = '<div class="' . $childrenWrapClass . '">';
309: }
310: $html[] = '<ul class="level' . $level . '">';
311: $html[] = $htmlChildren;
312: $html[] = '</ul>';
313: if ($childrenWrapClass) {
314: $html[] = '</div>';
315: }
316: }
317:
318: $html[] = '</li>';
319:
320: $html = implode("\n", $html);
321: return $html;
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331: 332:
333: public function drawItem($category, $level = 0, $last = false)
334: {
335: return $this->_renderCategoryMenuItemHtml($category, $level, $last);
336: }
337:
338: 339: 340: 341: 342:
343: public function getCurrentCategory()
344: {
345: if (Mage::getSingleton('catalog/layer')) {
346: return Mage::getSingleton('catalog/layer')->getCurrentCategory();
347: }
348: return false;
349: }
350:
351: 352: 353: 354: 355:
356: public function getCurrentCategoryPath()
357: {
358: if ($this->getCurrentCategory()) {
359: return explode(',', $this->getCurrentCategory()->getPathInStore());
360: }
361: return array();
362: }
363:
364: 365: 366: 367: 368: 369:
370: public function drawOpenCategoryItem($category) {
371: $html = '';
372: if (!$category->getIsActive()) {
373: return $html;
374: }
375:
376: $html.= '<li';
377:
378: if ($this->isCategoryActive($category)) {
379: $html.= ' class="active"';
380: }
381:
382: $html.= '>'."\n";
383: $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
384:
385: if (in_array($category->getId(), $this->getCurrentCategoryPath())){
386: $children = $category->getChildren();
387: $hasChildren = $children && $children->count();
388:
389: if ($hasChildren) {
390: $htmlChildren = '';
391: foreach ($children as $child) {
392: $htmlChildren.= $this->drawOpenCategoryItem($child);
393: }
394:
395: if (!empty($htmlChildren)) {
396: $html.= '<ul>'."\n"
397: .$htmlChildren
398: .'</ul>';
399: }
400: }
401: }
402: $html.= '</li>'."\n";
403: return $html;
404: }
405:
406: 407: 408: 409: 410: 411: 412: 413:
414: public function ($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
415: {
416: $activeCategories = array();
417: foreach ($this->getStoreCategories() as $child) {
418: if ($child->getIsActive()) {
419: $activeCategories[] = $child;
420: }
421: }
422: $activeCategoriesCount = count($activeCategories);
423: $hasActiveCategoriesCount = ($activeCategoriesCount > 0);
424:
425: if (!$hasActiveCategoriesCount) {
426: return '';
427: }
428:
429: $html = '';
430: $j = 0;
431: foreach ($activeCategories as $category) {
432: $html .= $this->_renderCategoryMenuItemHtml(
433: $category,
434: $level,
435: ($j == $activeCategoriesCount - 1),
436: ($j == 0),
437: true,
438: $outermostItemClass,
439: $childrenWrapClass,
440: true
441: );
442: $j++;
443: }
444:
445: return $html;
446: }
447:
448: }
449: