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: class extends Mage_Core_Block_Template
35: {
36: 37: 38: 39: 40:
41: protected ;
42:
43: 44: 45:
46: public function _construct()
47: {
48: $this->_menu = new Varien_Data_Tree_Node(array(), 'root', new Varien_Data_Tree());
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: public function getHtml($outermostClass = '', $childrenWrapClass = '')
59: {
60: Mage::dispatchEvent('page_block_html_topmenu_gethtml_before', array(
61: 'menu' => $this->_menu
62: ));
63:
64: $this->_menu->setOutermostClass($outermostClass);
65: $this->_menu->setChildrenWrapClass($childrenWrapClass);
66:
67: $html = $this->_getHtml($this->_menu, $childrenWrapClass);
68:
69: Mage::dispatchEvent('page_block_html_topmenu_gethtml_after', array(
70: 'menu' => $this->_menu,
71: 'html' => $html
72: ));
73:
74: return $html;
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass)
85: {
86: $html = '';
87:
88: $children = $menuTree->getChildren();
89: $parentLevel = $menuTree->getLevel();
90: $childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;
91:
92: $counter = 1;
93: $childrenCount = $children->count();
94:
95: $parentPositionClass = $menuTree->getPositionClass();
96: $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
97:
98: foreach ($children as $child) {
99:
100: $child->setLevel($childLevel);
101: $child->setIsFirst($counter == 1);
102: $child->setIsLast($counter == $childrenCount);
103: $child->setPositionClass($itemPositionClassPrefix . $counter);
104:
105: $outermostClassCode = '';
106: $outermostClass = $menuTree->getOutermostClass();
107:
108: if ($childLevel == 0 && $outermostClass) {
109: $outermostClassCode = ' class="' . $outermostClass . '" ';
110: $child->setClass($outermostClass);
111: }
112:
113: $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
114: $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
115: . $this->escapeHtml($child->getName()) . '</span></a>';
116:
117: if ($child->hasChildren()) {
118: if (!empty($childrenWrapClass)) {
119: $html .= '<div class="' . $childrenWrapClass . '">';
120: }
121: $html .= '<ul class="level' . $childLevel . '">';
122: $html .= $this->_getHtml($child, $childrenWrapClass);
123: $html .= '</ul>';
124:
125: if (!empty($childrenWrapClass)) {
126: $html .= '</div>';
127: }
128: }
129: $html .= '</li>';
130:
131: $counter++;
132: }
133:
134: return $html;
135: }
136:
137: 138: 139: 140: 141: 142:
143: protected function (Varien_Data_Tree_Node $item)
144: {
145: $html = '';
146: $attributes = $this->_getMenuItemAttributes($item);
147:
148: foreach ($attributes as $attributeName => $attributeValue) {
149: $html .= ' ' . $attributeName . '="' . str_replace('"', '\"', $attributeValue) . '"';
150: }
151:
152: return $html;
153: }
154:
155: 156: 157: 158: 159: 160:
161: protected function (Varien_Data_Tree_Node $item)
162: {
163: $menuItemClasses = $this->_getMenuItemClasses($item);
164: $attributes = array(
165: 'class' => implode(' ', $menuItemClasses)
166: );
167:
168: return $attributes;
169: }
170:
171: 172: 173: 174: 175: 176:
177: protected function (Varien_Data_Tree_Node $item)
178: {
179: $classes = array();
180:
181: $classes[] = 'level' . $item->getLevel();
182: $classes[] = $item->getPositionClass();
183:
184: if ($item->getIsFirst()) {
185: $classes[] = 'first';
186: }
187:
188: if ($item->getIsActive()) {
189: $classes[] = 'active';
190: }
191:
192: if ($item->getIsLast()) {
193: $classes[] = 'last';
194: }
195:
196: if ($item->getClass()) {
197: $classes[] = $item->getClass();
198: }
199:
200: if ($item->hasChildren()) {
201: $classes[] = 'parent';
202: }
203:
204: return $classes;
205: }
206: }
207: