SHARE
TWEET

Menu Block for JR CleverCMS

kiatng Nov 8th, 2012 229 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * @category   JR
  4.  * @package    JR_CleverCms
  5.  * @author     Ng Kiat Siong, Celera eShop, kiatsiong.ng@gmail.com
  6.  */
  7.  
  8. class JR_CleverCms_Block_Cms_Page_Menu extends JR_CleverCms_Block_Catalog_Navigation
  9. {
  10.    
  11.     protected function _toHtml()
  12.     {
  13.         $html = '';        
  14.         if ($identifier = $this->getIdentifier()) {          
  15.             $storeId = Mage::app()->getStore()->getId();        
  16.             if ($pageId = Mage::getResourceModel('cms/page')->checkIdentifier($identifier, $storeId)) {
  17.                 $page = Mage::getModel('cms/page')->load($pageId);
  18.                 $html = $this->_renderCmsMenuItemHtml($page);
  19.             }
  20.         }
  21.         return $html;
  22.     }  
  23.    
  24.     /**
  25.      * Render CMS Page Menu
  26.      *
  27.      * @param Mage_Cms_Model_Page $page
  28.      * @param int Nesting level number
  29.      * @param boolean Whether or not this item is last, affects list item class
  30.      * @param boolean Whether or not this item is first, affects list item class
  31.      * @param boolean Whether or not this item is outermost, affects list item class
  32.      * @param string Extra class of outermost list items
  33.      * @param string If specified wraps children list in div with this class
  34.      * @param boolean Whether or not to add on* attributes to list item
  35.      * @return string
  36.      */
  37.     protected function _renderCmsMenuItemHtml($page, $level = 1, $isLast = false, $isFirst = false,
  38.         $isOutermost = true, $outermostItemClass = 'block-account', $childrenWrapClass = 'block-content', $noEventAttributes = false)
  39.     {
  40.         if (! $this->_isAllowed($page)) {
  41.             return '';
  42.         }
  43.         $html = array();
  44.  
  45.         // get all children
  46.         $children = $page->getChildren();
  47.         if (Mage::helper('cms/page')->isPermissionsEnabled($this->getStore())) {
  48.             $children->addPermissionsFilter($this->getCustomerGroupId());
  49.         }
  50.         $childrenCount = $children->count();
  51.         $hasChildren = ($children && $childrenCount);
  52.  
  53.         // select active children
  54.         $activeChildren = array();
  55.         foreach ($children as $child) {
  56.             if ($child->getIsActive() && $child->getIncludeInMenu()) {
  57.                 $activeChildren[] = $child;
  58.             }
  59.         }
  60.         $activeChildrenCount = count($activeChildren);
  61.         $hasActiveChildren = ($activeChildrenCount > 0);
  62.  
  63.         // prepare list item html classes
  64.         $classes = array();
  65.         $classes[] = 'level'.$level;
  66.         // note: not dealing with the 'nav-' class at the moment
  67.         if ($this->isCmsPageActive($page)) {
  68.             $classes[] = 'current';
  69.         }
  70.         $linkClass = '';
  71.         if ($isOutermost && $outermostItemClass) {
  72.             $classes[] = $outermostItemClass;
  73.             $linkClass = ' class="'.$outermostItemClass.'"';
  74.         }
  75.         if ($isFirst) {
  76.             $classes[] = 'first';
  77.         }
  78.         if ($isLast) {
  79.             $classes[] = 'last';
  80.         }
  81.         if ($hasActiveChildren) {
  82.             $classes[] = 'parent';
  83.         }
  84.  
  85.         // prepare list item attributes
  86.         $attributes = array();
  87.         if (count($classes) > 0) {
  88.             $attributes['class'] = implode(' ', $classes);
  89.         }
  90.         if ($hasActiveChildren && !$noEventAttributes) {
  91.              $attributes['onmouseover'] = 'toggleMenu(this,1)';
  92.              $attributes['onmouseout'] = 'toggleMenu(this,0)';
  93.         }
  94.  
  95.         // assemble list item with attributes
  96.         if ($isOutermost) {
  97.             if ($page->getId()==$this->getCurrentCmsPage()->getId()) {
  98.                 $html[] = '<div '.$linkClass.'><div class="block-title"><strong><span>'. $this->escapeHtml($page->getTitle()) .'</span></strong></div>';
  99.             } else {
  100.                 $html[] = '<div '.$linkClass.'><div class="block-title"><strong>'.'<a href="'. $page->getUrl() . '"/><span>'. $this->escapeHtml($page->getTitle()) .'</span></a></strong></div>';
  101.             }
  102.         } else {
  103.             $htmlLi = '<li';
  104.             foreach ($attributes as $attrName => $attrValue) {
  105.                 $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
  106.             }
  107.             $htmlLi .= '>';
  108.             $html[] = $htmlLi;
  109.             if ($this->isCmsPageActive($page)) {
  110.                 $html[] .= '<strong>'. $this->escapeHtml($page->getTitle()) .'</strong>';
  111.             } else {
  112.                 $html[] .= '<a href="'. $page->getUrl() . "/" .'"'.$linkClass.'>';
  113.                 $html[] .= '<span>'. $this->escapeHtml($page->getTitle()) .'</span>';
  114.                 $html[] .= '</a>';
  115.             }
  116.         }    
  117.        
  118.         // render children
  119.         $htmlChildren = '';
  120.         $j = 0;
  121.         foreach ($activeChildren as $child) {
  122.             $htmlChildren .= $this->_renderCmsMenuItemHtml(
  123.                 $child,
  124.                 ($level + 1),
  125.                 ($j == $activeChildrenCount - 1),  // is last
  126.                 ($j == 0),                         // is first
  127.                 false,                             // is outermost
  128.                 $outermostItemClass,
  129.                 $childrenWrapClass,
  130.                 $noEventAttributes
  131.             );
  132.             $j++;
  133.         }
  134.  
  135.         if (!empty($htmlChildren)) {
  136.             if ($childrenWrapClass) {
  137.                 $html[] = '<div class="' . $childrenWrapClass . '">';
  138.             }
  139.             $html[] = '<ul class="level'. $level .'">';
  140.             $html[] = $htmlChildren;
  141.             $html[] = '</ul>';
  142.             if ($childrenWrapClass) {
  143.                 $html[] = '</div>';
  144.             }
  145.         }
  146.  
  147.         $html[] = $isOutermost ? '</div>' : '</li>';
  148.  
  149.         $html = implode("\n", $html);
  150.         return $html;
  151.     }  
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top