<?php
/**
* {{rtd: Renders a horizontal navigation with drop downs}}
*/
$navigation = $this->navigation('Core/Navigation')->menu();
$html = '';

// find deepest active
$found = $navigation->findActive($container);
if ($found) {
    $foundPage  = $found['page'];
    $foundDepth = $found['depth'];
} else {
    $foundPage = null;
}
$maxDepth=100; //$navigation->getMaxDepth();
$minDepth=0;
$onlyActive=false;
// create iterator
$iterator = new \RecursiveIteratorIterator($container,
    \RecursiveIteratorIterator::SELF_FIRST);
$iterator->setMaxDepth($maxDepth);

// iterate container
$prevDepth = -1; $renderDivider=false;
foreach ($iterator as $page) {
    $depth = $iterator->getDepth();
    $isActive = $page->isActive(true);
    if ($depth < $minDepth || !$navigation->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
    
    $myIndent = "" . str_repeat('        ', $depth);

    if ($depth > $prevDepth) {
        // start new ul tag
        if ($depth ==  0) {
            $ulClass = ' class="nav nav-tabs pull-left"';
        } else {
            $ulClass = ' class="dropdown-menu"';
        }
        $html .= $myIndent . '<ul' . $ulClass . '>' . PHP_EOL;
    } elseif ($prevDepth > $depth) {
        // close li/ul tags until we're at current depth
        for ($i = $prevDepth; $i > $depth; $i--) {
            $ind = "" . str_repeat('        ', $i);
            $html .= $ind . '    </li>' . PHP_EOL;
            $html .= $ind . '</ul>' . PHP_EOL;
        }
        // close previous li tag
        $html .= $myIndent . '    </li>' . PHP_EOL;
    } else {
        // close previous li tag
        $html .= $myIndent . '    </li>' . PHP_EOL;
    }
    if ($renderDivider) {
        $html .= '<li class="divider"></li>' . PHP_EOL;
    }

    // render li tag and page
    $liClassArr=array();
    if ($isActive) {
        $liClassArr[] = "active";
    }
    if ($page->hasPages()) {
        $liClassArr[] = "dropdown";
        
    }
    $liClass=count($liClassArr) ? ' class="' . implode(' ', $liClassArr) . '"' : '';
    $link = $navigation->htmlify($page, true);
    if ($page->hasPages()) {
        
        $link = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown"', $link);
        $link = str_replace('</a>', '<b class="caret"></b></a>', $link);
    }
    $html .= $myIndent . '    <li' . $liClass . '>' . PHP_EOL
    . $myIndent . '        ' . $link . PHP_EOL;
    
    $renderDivider = (bool) $page->divider;
        

    // 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 = "" . str_repeat('        ', $i-1);
        $html .= $myIndent . '    </li>' . PHP_EOL
        . $myIndent . '</ul>' . PHP_EOL;
    }
    $html = rtrim($html, PHP_EOL);
}

echo $html;