/
FullNavigationFactory.php
76 lines (68 loc) · 2.22 KB
/
FullNavigationFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Concrete\Core\Application\UserInterface\Dashboard\Navigation;
use Concrete\Core\Navigation\Item\PageItem;
use Concrete\Core\Page\Page;
use Concrete\Core\Permission\Checker;
class FullNavigationFactory
{
/**
* @var NavigationCache
*/
protected $cache;
public function __construct(NavigationCache $cache)
{
$this->cache = $cache;
}
protected function getPageChildren(Page $page)
{
if ($page->getCollectionPath() == '/dashboard/welcome') {
$page = Page::getByPath('/account');
}
$children = $page->getCollectionChildren();
return $children;
}
/**
* @param Page $currentPage
* @param Navigation $navigation
* @param PageItem|null $currentItem
* @return Navigation
*/
protected function populateNavigation(Page $currentPage, Navigation $navigation, PageItem $currentItem = null)
{
$permissions = new Checker($currentPage);
if ($permissions->canViewPage() && !$currentPage->getAttribute('exclude_nav')) {
$item = new PageItem($currentPage);
$children = $this->getPageChildren($currentPage);
foreach($children as $child) {
$this->populateNavigation($child, $navigation, $item);
}
if ($currentItem) {
$currentItem->addChild($item);
} else {
$navigation->add($item);
}
}
return $navigation;
}
/**
* Returns an entire dashboard navigation tree. Optionally starts at a particular section in the tree.
* Used on the Dashboard home, intelligent search, mobile menu and more.
*
* @return Navigation
*/
public function createNavigation(): Navigation
{
if (!$this->cache->has()) {
$navigation = new Navigation();
$home = Page::getByPath('/dashboard');
$children = $home->getCollectionChildren();
foreach($children as $child) {
$navigation = $this->populateNavigation($child, $navigation);
}
$this->cache->set($navigation);
} else {
$navigation = $this->cache->get();
}
return $navigation;
}
}