Advertisement
Guest User

Untitled

a guest
May 31st, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.41 KB | None | 0 0
  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@magentocommerce.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.magentocommerce.com for more information.
  20.  *
  21.  * @category    Mage
  22.  * @package     Mage_Catalog
  23.  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  25.  */
  26.  
  27.  
  28. /**
  29.  * Catalog navigation
  30.  *
  31.  * @category   Mage
  32.  * @package    Mage_Catalog
  33.  * @author      Magento Core Team <core@magentocommerce.com>
  34.  */
  35. class Mage_Catalog_Block_Navigation extends Mage_Core_Block_Template
  36. {
  37.     protected $_categoryInstance = null;
  38.  
  39.     /**
  40.      * Current category key
  41.      *
  42.      * @var string
  43.      */
  44.     protected $_currentCategoryKey;
  45.  
  46.     /**
  47.      * Array of level position counters
  48.      *
  49.      * @var array
  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.      * Get Key pieces for caching block content
  63.      *
  64.      * @return array
  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.      * Get current category key
  92.      *
  93.      * @return mixed
  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.      * Get catagories of current store
  111.      *
  112.      * @return Varien_Data_Tree_Node_Collection
  113.      */
  114.     public function getStoreCategories()
  115.     {
  116.         $helper = Mage::helper('catalog/category');
  117.         return $helper->getStoreCategories();
  118.     }
  119.  
  120.     /**
  121.      * Retrieve child categories of current category
  122.      *
  123.      * @return Varien_Data_Tree_Node_Collection
  124.      */
  125.     public function getCurrentChildCategories()
  126.     {
  127.         $layer = Mage::getSingleton('catalog/layer');
  128.         $category   = $layer->getCurrentCategory();
  129.         /* @var $category Mage_Catalog_Model_Category */
  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.      * Checkin activity of category
  139.      *
  140.      * @param   Varien_Object $category
  141.      * @return  bool
  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.      * Get url for category data
  161.      *
  162.      * @param Mage_Catalog_Model_Category $category
  163.      * @return string
  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.      * Return item position representation in menu tree
  180.      *
  181.      * @param int $level
  182.      * @return string
  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.      * Render category to html
  207.      *
  208.      * @param Mage_Catalog_Model_Category $category
  209.      * @param int Nesting level number
  210.      * @param boolean Whether ot not this item is last, affects list item class
  211.      * @param boolean Whether ot not this item is first, affects list item class
  212.      * @param boolean Whether ot not this item is outermost, affects list item class
  213.      * @param string Extra class of outermost list items
  214.      * @param string If specified wraps children list in div with this class
  215.      * @param boolean Whether ot not to add on* attributes to list item
  216.      * @return string
  217.      */
  218.     protected function _renderCategoryMenuItemHtml($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.         // get all children
  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.         $children = $this->toLinearArray($children);
  237.         usort($children, array($this, '_sortCategoriesByName'));
  238.        
  239.         // select active children
  240.         $activeChildren = array();
  241.         foreach ($children as $child) {
  242.             if ($child->getIsActive()) {
  243.                 $activeChildren[] = $child;
  244.             }
  245.         }
  246.         $activeChildrenCount = count($activeChildren);
  247.         $hasActiveChildren = ($activeChildrenCount > 0);
  248.  
  249.         // prepare list item html classes
  250.         $classes = array();
  251.         $classes[] = 'level' . $level;
  252.         $classes[] = 'nav-' . $this->_getItemPosition($level);
  253.         if ($this->isCategoryActive($category)) {
  254.             $classes[] = 'active';
  255.         }
  256.         $linkClass = '';
  257.         if ($isOutermost && $outermostItemClass) {
  258.             $classes[] = $outermostItemClass;
  259.             $linkClass = ' class="'.$outermostItemClass.'"';
  260.         }
  261.         if ($isFirst) {
  262.             $classes[] = 'first';
  263.         }
  264.         if ($isLast) {
  265.             $classes[] = 'last';
  266.         }
  267.         if ($hasActiveChildren) {
  268.             $classes[] = 'parent';
  269.         }
  270.  
  271.         // prepare list item attributes
  272.         $attributes = array();
  273.         if (count($classes) > 0) {
  274.             $attributes['class'] = implode(' ', $classes);
  275.         }
  276.         if ($hasActiveChildren && !$noEventAttributes) {
  277.              $attributes['onmouseover'] = 'toggleMenu(this,1)';
  278.              $attributes['onmouseout'] = 'toggleMenu(this,0)';
  279.         }
  280.  
  281.         // assemble list item with attributes
  282.         $htmlLi = '<li';
  283.         foreach ($attributes as $attrName => $attrValue) {
  284.             $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
  285.         }
  286.         $htmlLi .= '>';
  287.         $html[] = $htmlLi;
  288.  
  289.         $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
  290.         $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
  291.         $html[] = '</a>';
  292.  
  293.         // render children
  294.         $htmlChildren = '';
  295.         $j = 0;
  296.         foreach ($activeChildren as $child) {
  297.             $htmlChildren .= $this->_renderCategoryMenuItemHtml(
  298.                 $child,
  299.                 ($level + 1),
  300.                 ($j == $activeChildrenCount - 1),
  301.                 ($j == 0),
  302.                 false,
  303.                 $outermostItemClass,
  304.                 $childrenWrapClass,
  305.                 $noEventAttributes
  306.             );
  307.             $j++;
  308.         }
  309.         if (!empty($htmlChildren)) {
  310.             if ($childrenWrapClass) {
  311.                 $html[] = '<div class="' . $childrenWrapClass . '">';
  312.             }
  313.             $html[] = '<ul class="level' . $level . '">';
  314.             $html[] = $htmlChildren;
  315.             $html[] = '</ul>';
  316.             if ($childrenWrapClass) {
  317.                 $html[] = '</div>';
  318.             }
  319.         }
  320.  
  321.         $html[] = '</li>';
  322.  
  323.         $html = implode("\n", $html);
  324.         return $html;
  325.     }
  326.  
  327.     /**
  328.      * Render category to html
  329.      *
  330.      * @deprecated deprecated after 1.4
  331.      * @param Mage_Catalog_Model_Category $category
  332.      * @param int Nesting level number
  333.      * @param boolean Whether ot not this item is last, affects list item class
  334.      * @return string
  335.      */
  336.     public function drawItem($category, $level = 0, $last = false)
  337.     {
  338.         return $this->_renderCategoryMenuItemHtml($category, $level, $last);
  339.     }
  340.  
  341.     /**
  342.      * Enter description here...
  343.      *
  344.      * @return Mage_Catalog_Model_Category
  345.      */
  346.     public function getCurrentCategory()
  347.     {
  348.         if (Mage::getSingleton('catalog/layer')) {
  349.             return Mage::getSingleton('catalog/layer')->getCurrentCategory();
  350.         }
  351.         return false;
  352.     }
  353.  
  354.     /**
  355.      * Enter description here...
  356.      *
  357.      * @return string
  358.      */
  359.     public function getCurrentCategoryPath()
  360.     {
  361.         if ($this->getCurrentCategory()) {
  362.             return explode(',', $this->getCurrentCategory()->getPathInStore());
  363.         }
  364.         return array();
  365.     }
  366.  
  367.     /**
  368.      * Enter description here...
  369.      *
  370.      * @param Mage_Catalog_Model_Category $category
  371.      * @return string
  372.      */
  373.     public function drawOpenCategoryItem($category) {
  374.         $html = '';
  375.         if (!$category->getIsActive()) {
  376.             return $html;
  377.         }
  378.  
  379.         $html.= '<li';
  380.  
  381.         if ($this->isCategoryActive($category)) {
  382.             $html.= ' class="active"';
  383.         }
  384.  
  385.         $html.= '>'."\n";
  386.         $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
  387.  
  388.         if (in_array($category->getId(), $this->getCurrentCategoryPath())){
  389.             $children = $category->getChildren();
  390.             $hasChildren = $children && $children->count();
  391.  
  392.             if ($hasChildren) {
  393.                 $htmlChildren = '';
  394.                 foreach ($children as $child) {
  395.                     $htmlChildren.= $this->drawOpenCategoryItem($child);
  396.                 }
  397.  
  398.                 if (!empty($htmlChildren)) {
  399.                     $html.= '<ul>'."\n"
  400.                             .$htmlChildren
  401.                             .'</ul>';
  402.                 }
  403.             }
  404.         }
  405.         $html.= '</li>'."\n";
  406.         return $html;
  407.     }
  408.  
  409.     /**
  410.      * Render categories menu in HTML
  411.      *
  412.      * @param int Level number for list item class to start from
  413.      * @param string Extra class of outermost list items
  414.      * @param string If specified wraps children list in div with this class
  415.      * @return string
  416.      */
  417.     public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
  418.     {
  419.         $activeCategories = array();
  420.         foreach ($this->getStoreCategories() as $child) {
  421.             if ($child->getIsActive()) {
  422.                 $activeCategories[] = $child;
  423.             }
  424.         }
  425.         $activeCategoriesCount = count($activeCategories);
  426.         $hasActiveCategoriesCount = ($activeCategoriesCount > 0);
  427.  
  428.         if (!$hasActiveCategoriesCount) {
  429.             return '';
  430.         }
  431.  
  432.         $html = '';
  433.         $j = 0;
  434.         foreach ($activeCategories as $category) {
  435.             $html .= $this->_renderCategoryMenuItemHtml(
  436.                 $category,
  437.                 $level,
  438.                 ($j == $activeCategoriesCount - 1),
  439.                 ($j == 0),
  440.                 true,
  441.                 $outermostItemClass,
  442.                 $childrenWrapClass,
  443.                 true
  444.             );
  445.             $j++;
  446.         }
  447.  
  448.         return $html;
  449.     }
  450.    
  451.     public function toLinearArray($collection)
  452.     {
  453.         $array = array();
  454.         foreach ($collection as $item) $array[] = $item;
  455.         return $array;
  456.     }
  457.  
  458.     /**
  459.     * Sorts the array.
  460.     *
  461.     * @param Mage_Catalog_Model_Category $cat1
  462.     * @param Mage_Catalog_Model_Category $cat2
  463.     * @return int
  464.     * @deprecated
  465.     */
  466.     protected function _sortCategoriesByName($cat1, $cat2)
  467.     {
  468.         return strcoll($cat1->getName(), $cat2->getName());
  469.     }
  470.  
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement