<?php
/* @var $container Zend\Navigation\Navigation */
$container = $this->container;
?>
<ul class="<?php echo $this->navigation($container)->menu()->getUlClass(); ?>">
    <?php foreach ($container as $page): ?>
        <?php
        if (!$this->navigation()->accept($page)) {
            continue;
        }
        ?>
        <?php /* @var $page Zend\Navigation\Page\Mvc */ ?>
        <?php $hasChildren = $page->hasPages() ?>
        <?php if (!$hasChildren): ?>
            <li<?php echo ($page->isActive()) ? ' class="active"' : ''; ?>>
                <a class="nav-header" href="<?php echo $page->getHref() . ($page->get('hash') ? '#/' : '') ?>"
                    <?php if ($page->getTarget() != ""): ?>
                        target="<?php echo $page->getTarget(); ?>"
                    <?php endif; ?>
                    >
                    <?php if ($page->get("icon") !== ""): ?>
                        <span class="<?php echo $page->get("icon"); ?>"></span>
                    <?php endif; ?>
                    <?php echo $this->escapeHtml($page->getLabel()); ?>
                </a>
            </li>
        <?php else: ?>
            <?php
            //check if access is allowed at least one item
            $access = false;
            foreach ($page->getPages() as $child) {
                if ($this->navigation()->accept($child) && $child->get("separator") !== true) {
                    $access = true;
                }
            }
            if ($access) :
                ?>
                <li uib-dropdown class="dropdown<?php echo $page->isActive(true) ? ' active' : ''; ?>">
                    <a class="dropdown-toggle" uib-dropdown-toggle href="#">
                        <?php if ($page->get("icon") !== ""): ?>
                            <span class="<?php echo $page->get("icon"); ?>"></span>
                        <?php endif; ?>
                        <?php echo $this->escapeHtml($page->getLabel()); ?>
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <?php foreach ($page->getPages() as $child): ?>
                            <?php if (!$this->navigation()->accept($child)) { continue; } ?>
                            <?php if ($child->get("separator") === true): ?>
                                <li class="divider"></li>
                                <?php
                                continue;
                            endif;
                            ?>
                            <?php if ($child->get("header") === true): ?>
                                <li class="dropdown-header"><?php echo $this->escapeHtml($child->getLabel()); ?></li>
                                <?php
                                continue;
                            endif;
                            ?>
                            <li<?php echo $child->isActive() ?' class="active"' : ''; ?>>
                                <a
                                    href="<?php echo $child->getHref() . ($child->get('hash') ? '#/' : ''); ?>"
                                    <?php if ($child->getTarget() != ""): ?>
                                        target="<?php echo $child->getTarget(); ?>"
                                    <?php endif; ?> >
                                    <?php if ($child->get("icon") !== ""): ?>
                                        <span class="<?php echo $child->get("icon"); ?>"></span>
                                    <?php endif; ?>
                                    <?php echo $this->escapeHtml($child->getLabel()); ?>
                                </a>
                            </li>
                        <?php endforeach ?>
                    </ul>
                </li>
            <?php endif; ?>
        <?php endif ?>
    <?php endforeach ?>
</ul>