PageRenderTime 29ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Page/Block/Html/Topmenu.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 268 lines | 139 code | 39 blank | 90 comment | 16 complexity | d6038fa18c19d3a1790783f1a1cdfc34 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Page
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Top menu block
  28. *
  29. * @category Mage
  30. * @package Mage_Page
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Page_Block_Html_Topmenu extends Mage_Core_Block_Template
  34. {
  35. /**
  36. * Top menu data tree
  37. *
  38. * @var Varien_Data_Tree_Node
  39. */
  40. protected $_menu;
  41. /**
  42. * Current entity key
  43. *
  44. * @var string|int
  45. */
  46. protected $_currentEntityKey;
  47. /**
  48. * Init top menu tree structure and cache
  49. */
  50. public function _construct()
  51. {
  52. $this->_menu = new Varien_Data_Tree_Node(array(), 'root', new Varien_Data_Tree());
  53. /*
  54. * setting cache to save the topmenu block
  55. */
  56. $this->setCacheTags(array(Mage_Catalog_Model_Category::CACHE_TAG));
  57. $this->setCacheLifetime(false);
  58. }
  59. /**
  60. * Get top menu html
  61. *
  62. * @param string $outermostClass
  63. * @param string $childrenWrapClass
  64. * @return string
  65. */
  66. public function getHtml($outermostClass = '', $childrenWrapClass = '')
  67. {
  68. Mage::dispatchEvent('page_block_html_topmenu_gethtml_before', array(
  69. 'menu' => $this->_menu,
  70. 'block' => $this
  71. ));
  72. $this->_menu->setOutermostClass($outermostClass);
  73. $this->_menu->setChildrenWrapClass($childrenWrapClass);
  74. if ($renderer = $this->getChild('catalog.topnav.renderer')) {
  75. $renderer->setMenuTree($this->_menu)->setChildrenWrapClass($childrenWrapClass);
  76. $html = $renderer->toHtml();
  77. } else {
  78. $html = $this->_getHtml($this->_menu, $childrenWrapClass);
  79. }
  80. Mage::dispatchEvent('page_block_html_topmenu_gethtml_after', array(
  81. 'menu' => $this->_menu,
  82. 'html' => $html
  83. ));
  84. return $html;
  85. }
  86. /**
  87. * Recursively generates top menu html from data that is specified in $menuTree
  88. *
  89. * @param Varien_Data_Tree_Node $menuTree
  90. * @param string $childrenWrapClass
  91. * @return string
  92. * @deprecated since 1.8.2.0 use child block catalog.topnav.renderer instead
  93. */
  94. protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass)
  95. {
  96. $html = '';
  97. $children = $menuTree->getChildren();
  98. $parentLevel = $menuTree->getLevel();
  99. $childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;
  100. $counter = 1;
  101. $childrenCount = $children->count();
  102. $parentPositionClass = $menuTree->getPositionClass();
  103. $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
  104. foreach ($children as $child) {
  105. $child->setLevel($childLevel);
  106. $child->setIsFirst($counter == 1);
  107. $child->setIsLast($counter == $childrenCount);
  108. $child->setPositionClass($itemPositionClassPrefix . $counter);
  109. $outermostClassCode = '';
  110. $outermostClass = $menuTree->getOutermostClass();
  111. if ($childLevel == 0 && $outermostClass) {
  112. $outermostClassCode = ' class="' . $outermostClass . '" ';
  113. $child->setClass($outermostClass);
  114. }
  115. $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
  116. $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
  117. . $this->escapeHtml($child->getName()) . '</span></a>';
  118. if ($child->hasChildren()) {
  119. if (!empty($childrenWrapClass)) {
  120. $html .= '<div class="' . $childrenWrapClass . '">';
  121. }
  122. $html .= '<ul class="level' . $childLevel . '">';
  123. $html .= $this->_getHtml($child, $childrenWrapClass);
  124. $html .= '</ul>';
  125. if (!empty($childrenWrapClass)) {
  126. $html .= '</div>';
  127. }
  128. }
  129. $html .= '</li>';
  130. $counter++;
  131. }
  132. return $html;
  133. }
  134. /**
  135. * Generates string with all attributes that should be present in menu item element
  136. *
  137. * @param Varien_Data_Tree_Node $item
  138. * @return string
  139. */
  140. protected function _getRenderedMenuItemAttributes(Varien_Data_Tree_Node $item)
  141. {
  142. $html = '';
  143. $attributes = $this->_getMenuItemAttributes($item);
  144. foreach ($attributes as $attributeName => $attributeValue) {
  145. $html .= ' ' . $attributeName . '="' . str_replace('"', '\"', $attributeValue) . '"';
  146. }
  147. return $html;
  148. }
  149. /**
  150. * Returns array of menu item's attributes
  151. *
  152. * @param Varien_Data_Tree_Node $item
  153. * @return array
  154. */
  155. protected function _getMenuItemAttributes(Varien_Data_Tree_Node $item)
  156. {
  157. $menuItemClasses = $this->_getMenuItemClasses($item);
  158. $attributes = array(
  159. 'class' => implode(' ', $menuItemClasses)
  160. );
  161. return $attributes;
  162. }
  163. /**
  164. * Returns array of menu item's classes
  165. *
  166. * @param Varien_Data_Tree_Node $item
  167. * @return array
  168. */
  169. protected function _getMenuItemClasses(Varien_Data_Tree_Node $item)
  170. {
  171. $classes = array();
  172. $classes[] = 'level' . $item->getLevel();
  173. $classes[] = $item->getPositionClass();
  174. if ($item->getIsFirst()) {
  175. $classes[] = 'first';
  176. }
  177. if ($item->getIsActive()) {
  178. $classes[] = 'active';
  179. }
  180. if ($item->getIsLast()) {
  181. $classes[] = 'last';
  182. }
  183. if ($item->getClass()) {
  184. $classes[] = $item->getClass();
  185. }
  186. if ($item->hasChildren()) {
  187. $classes[] = 'parent';
  188. }
  189. return $classes;
  190. }
  191. /**
  192. * Retrieve cache key data
  193. *
  194. * @return array
  195. */
  196. public function getCacheKeyInfo()
  197. {
  198. $shortCacheId = array(
  199. 'TOPMENU',
  200. Mage::app()->getStore()->getId(),
  201. Mage::getDesign()->getPackageName(),
  202. Mage::getDesign()->getTheme('template'),
  203. Mage::getSingleton('customer/session')->getCustomerGroupId(),
  204. 'template' => $this->getTemplate(),
  205. 'name' => $this->getNameInLayout(),
  206. $this->getCurrentEntityKey()
  207. );
  208. $cacheId = $shortCacheId;
  209. $shortCacheId = array_values($shortCacheId);
  210. $shortCacheId = implode('|', $shortCacheId);
  211. $shortCacheId = md5($shortCacheId);
  212. $cacheId['entity_key'] = $this->getCurrentEntityKey();
  213. $cacheId['short_cache_id'] = $shortCacheId;
  214. return $cacheId;
  215. }
  216. /**
  217. * Retrieve current entity key
  218. *
  219. * @return int|string
  220. */
  221. public function getCurrentEntityKey()
  222. {
  223. if (null === $this->_currentEntityKey) {
  224. $this->_currentEntityKey = Mage::registry('current_entity_key')
  225. ? Mage::registry('current_entity_key') : Mage::app()->getStore()->getRootCategoryId();
  226. }
  227. return $this->_currentEntityKey;
  228. }
  229. }