-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Menu.php
155 lines (127 loc) · 4.53 KB
/
Menu.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace TCG\Voyager\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use TCG\Voyager\Events\MenuDisplay;
use TCG\Voyager\Facades\Voyager;
/**
* @todo: Refactor this class by using something like MenuBuilder Helper.
*/
class Menu extends Model
{
protected $table = 'menus';
protected $guarded = [];
public static function boot()
{
parent::boot();
static::saved(function ($model) {
$model->removeMenuFromCache();
});
static::deleted(function ($model) {
$model->removeMenuFromCache();
});
}
public function items()
{
return $this->hasMany(Voyager::modelClass('MenuItem'));
}
public function parent_items()
{
return $this->hasMany(Voyager::modelClass('MenuItem'))
->whereNull('parent_id');
}
/**
* Display menu.
*
* @param string $menuName
* @param string|null $type
* @param array $options
*
* @return string
*/
public static function display($menuName, $type = null, array $options = [])
{
// GET THE MENU - sort collection in blade
$menu = \Cache::remember('voyager_menu_'.$menuName, \Carbon\Carbon::now()->addDays(30), function () use ($menuName) {
return static::where('name', '=', $menuName)
->with(['parent_items.children' => function ($q) {
$q->orderBy('order');
}])
->first();
});
// Check for Menu Existence
if (!isset($menu)) {
return false;
}
event(new MenuDisplay($menu));
// Convert options array into object
$options = (object) $options;
$items = $menu->parent_items->sortBy('order');
if ($menuName == 'admin' && $type == '_json') {
$items = static::processItems($items);
}
if ($type == 'admin') {
$type = 'voyager::menu.'.$type;
} else {
if (is_null($type)) {
$type = 'voyager::menu.default';
} elseif ($type == 'bootstrap' && !view()->exists($type)) {
$type = 'voyager::menu.bootstrap';
}
}
if (!isset($options->locale)) {
$options->locale = app()->getLocale();
}
if ($type === '_json') {
return $items;
}
return new \Illuminate\Support\HtmlString(
\Illuminate\Support\Facades\View::make($type, ['items' => $items, 'options' => $options])->render()
);
}
public function removeMenuFromCache()
{
\Cache::forget('voyager_menu_'.$this->name);
}
private static function processItems($items)
{
$items = $items->transform(function ($item) {
// Translate title
$item->title = $item->getTranslatedAttribute('title');
// Resolve URL/Route
$item->href = $item->link(true);
if ($item->href == url()->current() && $item->href != '') {
// The current URL is exactly the URL of the menu-item
$item->active = true;
} elseif (starts_with(url()->current(), Str::finish($item->href, '/'))) {
// The current URL is "below" the menu-item URL. For example "admin/posts/1/edit" => "admin/posts"
$item->active = true;
}
if (($item->href == url('') || $item->href == route('voyager.dashboard')) && $item->children->count() > 0) {
// Exclude sub-menus
$item->active = false;
} elseif ($item->href == route('voyager.dashboard') && url()->current() != route('voyager.dashboard')) {
// Exclude dashboard
$item->active = false;
}
if ($item->children->count() > 0) {
$item->setRelation('children', static::processItems($item->children));
if (!$item->children->where('active', true)->isEmpty()) {
$item->active = true;
}
}
return $item;
});
// Filter items by permission
$items = $items->filter(function ($item) {
return !$item->children->isEmpty() || app('VoyagerAuth')->user()->can('browse', $item);
})->filter(function ($item) {
// Filter out empty menu-items
if ($item->url == '' && $item->route == '' && $item->children->count() == 0) {
return false;
}
return true;
});
return $items->values();
}
}