<?php
/* @var $container Zend\Navigation\Navigation */
$container = $this->container;
?>
<ul class="nav navbar-nav navbar-right">
    <?php foreach ($container as $page): ?>
        <?php if (!$this->navigation()->accept($page)) {
            continue;
        }
        $hasPages = false;

        if ($page->hasPages()) :
            foreach ($page->getPages() as $child) :
                if ($this->navigation()->accept($child)) :
                    $hasPages = true;
                    break;
                endif;
            endforeach;
        endif;
        ?>
        <li class="navigation<?= $hasPages ? ' dropdown' : ''; ?><?= $page->isActive(true) ? ' active' : '' ?>">
            <a class="<?= $page->getClass() ?>"
               href="<?= $page->getHref() ?>"
                <?php if (null !== $page->getTarget()): ?>
                    target="<?= $page->getTarget(); ?>"
                <?php endif; ?>
                <?= $hasPages?'data-toggle="dropdown"':'' ?>
            >
                <?= $this->escapeHtml(
                        $this->translate(
                            $page->getLabel(),
                            $this->navigation($container)->getTranslatorTextDomain()
                        )
                    );
                ?>
                <?= $hasPages ? '<span class="caret"></span>' : '' ?>
            </a>
            <?php if ($hasPages) : ?>
                <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                    <?php /** @var Zend\Navigation\Page\AbstractPage $child */ ?>
                    <?php foreach ($page as $child): ?>
                        <?php if (!$this->navigation()->accept($child)) {
                            continue;
                        } ?>
                        <li<?php if ($child->isActive(true)) echo ' class="active"' ?>>
                            <a href="<?= $child->getHref() ?>"
                                <?php if (null !== $child->getTarget()): ?>
                                    target="<?= $child->getTarget(); ?>"
                                <?php endif; ?> >
                                <?= $this->escapeHtml(
                                        $this->translate(
                                            $child->getLabel(),
                                            $this->navigation($container)->getTranslatorTextDomain()
                                        )
                                    );
                                ?>
                            </a>
                        </li>
                    <?php endforeach ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach ?>
</ul>