Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion library/Zend/Navigation/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,20 @@ public function hasPage(Page\AbstractPage $page, $recursive = false)
/**
* Returns true if container contains any pages
*
* @param bool $onlyVisible whether to check only visible pages
* @return bool whether container has any pages
*/
public function hasPages()
public function hasPages($onlyVisible = false)
{
if ($onlyVisible) {
foreach ($this->pages as $page) {
if ($page->isVisible()) {
return true;
}
}
// no visible pages found
return false;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need else, just return count($this->index) > 0; directly after closing bracket because already return early.

return count($this->index) > 0;
}

Expand Down
6 changes: 3 additions & 3 deletions library/Zend/View/Helper/Navigation/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ protected function renderDeepestMenu(

// special case if active page is one below minDepth
if ($active['depth'] < $minDepth) {
if (!$active['page']->hasPages()) {
if (!$active['page']->hasPages(!$this->renderInvisible)) {
return '';
}
} elseif (!$active['page']->hasPages()) {
} elseif (!$active['page']->hasPages(!$this->renderInvisible)) {
// found pages has no children; render siblings
$active['page'] = $active['page']->getParent();
} elseif (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
Expand Down Expand Up @@ -280,7 +280,7 @@ protected function renderNormalMenu(
$accept = true;
} elseif ($foundPage->getParent()->hasPage($page)) {
// page is a sibling of the active page...
if (!$foundPage->hasPages() ||
if (!$foundPage->hasPages(!$this->renderInvisible) ||
is_int($maxDepth) && $foundDepth + 1 > $maxDepth) {
// accept if active page has no children, or the
// children are too deep to be rendered
Expand Down
20 changes: 18 additions & 2 deletions tests/ZendTest/Navigation/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,19 +682,35 @@ public function testHasPages()
{
$nav1 = new Navigation\Navigation();
$nav2 = new Navigation\Navigation();
$nav3 = new Navigation\Navigation();
$nav4 = new Navigation\Navigation();
$nav2->addPage(array(
'label' => 'Page 1',
'uri' => '#'
));
$nav3->addPage(array(
'label' => 'Page 1',
'uri' => '#',
'visible' => true
));
$nav4->addPage(array(
'label' => 'Page 1',
'uri' => '#',
'visible' => false
));

$expected = array(
'empty' => false,
'notempty' => true
'notempty' => true,
'visible' => true,
'notvisible' => false
);

$actual = array(
'empty' => $nav1->hasPages(),
'notempty' => $nav2->hasPages()
'notempty' => $nav2->hasPages(),
'visible' => $nav3->hasPages(false),
'notvisible' => $nav4->hasPages(true)
);

$this->assertEquals($expected, $actual);
Expand Down