-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathGhostNav.php
57 lines (49 loc) · 1.19 KB
/
GhostNav.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
<?php
namespace webvimark\modules\UserManagement\components;
use webvimark\modules\UserManagement\models\User;
use yii\bootstrap\Nav;
/**
* Class GhostNav
*
* Show only those items in navigation menu which user can see
* If item has no "visible" key, than "visible"=>User::canRoute($item['url') will be added
*
* @package webvimark\modules\UserManagement\components
*/
class GhostNav extends Nav
{
public function init()
{
parent::init();
$this->ensureVisibility($this->items);
}
/**
* @param array $items
*
* @return bool
*/
protected function ensureVisibility(&$items)
{
$allVisible = false;
foreach ($items as &$item)
{
if ( isset( $item['url'] ) AND !isset( $item['visible'] ) AND !in_array($item['url'], ['', '#']))
{
$item['visible'] = User::canRoute($item['url']);
}
if ( isset( $item['items'] ) )
{
// If not children are visible - make invisible this node
if ( !$this->ensureVisibility($item['items']) AND !isset( $item['visible'] ) )
{
$item['visible'] = false;
}
}
if ( isset( $item['label'] ) AND ( !isset( $item['visible'] ) OR $item['visible'] === true ) )
{
$allVisible = true;
}
}
return $allVisible;
}
}