<?php
/**
 * @author Kanstantsin A Kamkou (2ka.by)
 * History:
 *  - 21.08.2012 Bootstrap 2.1.0 corrections
 */
$maxDepth = $this->navigation()->menu()->getMaxDepth();

$view = $this;

echo renderSideMenu($this->container, $this);


function renderSideMenu( $pages, $view, $level=0 ){
    $html = '';

    foreach ($pages as $page) {
        // Continue if invisible on ACL check false
        if (!$page->isVisible() || !$view->navigation()->accept($page)) continue;

        $maxDepth = $view->navigation()->menu()->getMaxDepth();
        $sub = null;
        if( $level < $maxDepth ){
            
            if($submenu = renderSideMenu( $page->pages, $view, ($level+1) )){
                $sub = '<ul>'.$submenu.'</ul>';                
            }
        }


        $hasChildren = !empty($sub);


        $liclass = '';
        $aclass = $page->getClass();
        $aattr = '';

        if( $hasChildren ) {
            $liclass .= '';
            $aclass  .= '';
            $aattr .= 'id="'.$page->ref.'"';
        }
        if($page->isActive()) $liclass .= ' active';
            

        // header
        $html .= '<li' . (!empty($liclass) ? ' class="'.$liclass.'"' : '') . '>';

        $html .= '<a '. (!empty($aclass) ? ' class="'.$aclass.'"' : '') .' '.$aattr.' href="' .  $page->getHref() . '">';
        $html .= $view->translate($page->getLabel());


        
        if( $hasChildren){
            //$html .= '<b class="caret"></b>';
            $html .= '</a>';
            $html .= $sub;
        }else{
            $html .= '</a>'; 
        }
        $html .= '</li>';
    }

    return $html; 
}