Ver Mensaje Individual
  #15 (permalink)  
Antiguo 28/09/2012, 04:31
Avatar de Uncontroled_Duck
Uncontroled_Duck
Colaborador
 
Fecha de Ingreso: mayo-2011
Ubicación: Málaga [Spain]
Mensajes: 806
Antigüedad: 15 años, 4 meses
Puntos: 261
Respuesta: Zf2 Como Crear View Helper Navigation

Código PHP:
/**
 * Renders a normal menu (called from {@link renderMenu()})
 *
 * @param  AbstractContainer         $container    container to render
 * @param  string                    $ulClass      CSS class for first UL
 * @param  string                    $indent       initial indentation
 * @param  int|null                  $minDepth     minimum depth
 * @param  int|null                  $maxDepth     maximum depth
 * @param  bool                      $onlyActive   render only active branch?
 * @param  bool                      $escapeLabels Whether or not to escape the labels
 * @return string
 */
protected function renderNormalMenu(AbstractContainer $container, $ulClass, $indent, $minDepth, $maxDepth, $onlyActive, $escapeLabels)
{
    
$html = '';

    
// find deepest active
    
$found = $this->findActive($container, $minDepth, $maxDepth);
    if( 
$found )
    {
        
$foundPage = $found['page'];
        
$foundDepth = $found['depth'];
    }
    else
    {
        
$foundPage = null;
    }

    
// create iterator
    
$iterator = new RecursiveIteratorIterator($container,
                    
RecursiveIteratorIterator::SELF_FIRST);
    if( 
is_int($maxDepth) )
    {
        
$iterator->setMaxDepth($maxDepth);
    }

    
// iterate container
    
$prevDepth = -1;
    foreach( 
$iterator as $page )
    {
        
$depth = $iterator->getDepth();
        
$isActive = $page->isActive(true);
        if( 
$depth < $minDepth || !$this->accept($page) )
        {
            
// page is below minDepth or not accepted by acl/visibility
            
continue;
        }
        elseif( 
$onlyActive && !$isActive )
        {
            
// page is not active itself, but might be in the active branch
            
$accept = false;
            if( 
$foundPage )
            {
                if( 
$foundPage->hasPage($page) )
                {
                    
// accept if page is a direct child of the active page
                    
$accept = true;
                }
                elseif( 
$foundPage->getParent()->hasPage($page) )
                {
                    
// page is a sibling of the active page...
                    
if( !$foundPage->hasPages() ||
                            
is_int($maxDepth) && $foundDepth + 1 > $maxDepth )
                    {
                        
// accept if active page has no children, or the
                        // children are too deep to be rendered
                        
$accept = true;
                    }
                }
            }

            if( !
$accept )
            {
                continue;
            }
        }

        
// make sure indentation is correct
        
$depth -= $minDepth;
        
$myIndent = $indent . str_repeat('        ', $depth);

        if( 
$depth > $prevDepth )
        {
            
// start new ul tag
            
if( $ulClass && $depth == 0 )
            {
                
$ulClass = ' class="' . $ulClass . '"';
            }
            else
            {
                
// ***************** THESE ARE NEW LINES *************** //
                
$ulClass = ' class="dropdown-menu"';
                
// ***************** END OF NEW STUFF *************** //
                // render lu tag (ORGINAL LINE REMOVED)
                //$ulClass = '';
            
}
            
$html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
        }
        elseif( 
$prevDepth > $depth )
        {
            
// close li/ul tags until we're at current depth
            
for( $i = $prevDepth; $i > $depth; $i-- )
            {
                
$ind = $indent . str_repeat('        ', $i);
                
$html .= $ind . '    </li>' . self::EOL;
                
$html .= $ind . '</ul>' . self::EOL;
            }
            
// close previous li tag
            
$html .= $myIndent . '    </li>' . self::EOL;
        }
        else
        {
            
// close previous li tag
            
$html .= $myIndent . '    </li>' . self::EOL;
        }

        
// ***************** THESE ARE NEW LINES *************** //
        
$liMyClass = $page->get('liclass') ? $page->liclass : '';

        if( 
$isActive )
        {
            
$liClass = ' class="active' . (($liMyClass) ? ' ' . $liMyClass : '') . '" ';
        }
        else
        {
            
$liClass = $liMyClass ? " class=\"$liMyClass\" " : '';
        }

        
// ***************** END OF NEW STUFF *************** //
        // render li tag and page
        //$liClass = $isActive ? ' class="active"' : '';

        
$html .= $myIndent . '    <li' . $liClass . '>' . self::EOL
                
. $myIndent . '        ' . $this->htmlify($page, $escapeLabels) . self::EOL;

        
// store as previous depth for next iteration
        
$prevDepth = $depth;
    }

    if( 
$html )
    {
        
// done iterating container; close open ul/li tags
        
for( $i = $prevDepth + 1; $i > 0; $i-- )
        {
            
$myIndent = $indent . str_repeat('        ', $i - 1);
            
$html .= $myIndent . '    </li>' . self::EOL
                    
. $myIndent . '</ul>' . self::EOL;
        }
        
$html = rtrim($html, self::EOL);
    }

    return 
$html;
} 
__________________
Todos agradeceremos que pongas el código en su respectivo Highlight