|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use Baum\Node; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Laracasts\Presenter\PresentableTrait; |
|
8
|
|
|
use Yajra\Acl\Models\Permission; |
|
9
|
|
|
use Yajra\Auditable\AuditableTrait; |
|
10
|
|
|
use Yajra\CMS\Entities\Traits\CanRequireAuthentication; |
|
11
|
|
|
use Yajra\CMS\Entities\Traits\HasOrder; |
|
12
|
|
|
use Yajra\CMS\Entities\Traits\HasParameters; |
|
13
|
|
|
use Yajra\CMS\Entities\Traits\PublishableTrait; |
|
14
|
|
|
use Yajra\CMS\Presenters\MenuPresenter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property int depth |
|
18
|
|
|
* @property string title |
|
19
|
|
|
* @property int id |
|
20
|
|
|
* @property mixed children |
|
21
|
|
|
* @property boolean published |
|
22
|
|
|
* @property int order |
|
23
|
|
|
* @property string url |
|
24
|
|
|
* @property int target |
|
25
|
|
|
* @property bool authenticated |
|
26
|
|
|
* @property string type |
|
27
|
|
|
* @property string parameters |
|
28
|
|
|
* @property string authorization |
|
29
|
|
|
* @property mixed permissions |
|
30
|
|
|
* @property mixed widgets |
|
31
|
|
|
* @property int navigation_id |
|
32
|
|
|
* @property int extension_id |
|
33
|
|
|
*/ |
|
34
|
|
|
class Menu extends Node |
|
35
|
|
|
{ |
|
36
|
|
|
use PresentableTrait, PublishableTrait, CanRequireAuthentication; |
|
37
|
|
|
use AuditableTrait, HasParameters, HasOrder; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $touches = ['navigation']; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \Yajra\CMS\Presenters\MenuPresenter |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $presenter = MenuPresenter::class; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $fillable = [ |
|
53
|
|
|
'title', |
|
54
|
|
|
'url', |
|
55
|
|
|
'target', |
|
56
|
|
|
'order', |
|
57
|
|
|
'published', |
|
58
|
|
|
'authenticated', |
|
59
|
|
|
'authorization', |
|
60
|
|
|
'parameters', |
|
61
|
|
|
'extension_id', |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
66
|
|
|
*/ |
|
67
|
|
|
public function navigation() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->belongsTo(Navigation::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Related menu permissions. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
76
|
|
|
*/ |
|
77
|
|
|
public function permissions() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->belongsToMany(Permission::class); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get related article. |
|
84
|
|
|
* |
|
85
|
|
|
* @return \Yajra\CMS\Entities\Article |
|
86
|
|
|
*/ |
|
87
|
|
|
public function article() |
|
88
|
|
|
{ |
|
89
|
|
|
return Article::findOrNew($this->fluentParameters()->get('article_id', 0)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get related category. |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Yajra\CMS\Entities\Article |
|
96
|
|
|
*/ |
|
97
|
|
|
public function category() |
|
98
|
|
|
{ |
|
99
|
|
|
$category = $this->fluentParameters()->get('category_id', 0); |
|
100
|
|
|
$categoryId = explode(':', $category)[0]; |
|
101
|
|
|
|
|
102
|
|
|
return Category::findOrNew($categoryId); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Menu type relation. |
|
107
|
|
|
* |
|
108
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
109
|
|
|
*/ |
|
110
|
|
|
public function menuType() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->belongsTo(Lookup::class, 'type', 'key'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get list of possible parent node. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getParentsList() |
|
121
|
|
|
{ |
|
122
|
|
|
/** @var static $root */ |
|
123
|
|
|
$root = static::root(); |
|
124
|
|
|
$items = [ |
|
125
|
|
|
['id' => '1', 'title' => 'Item Root'], |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
if ($this->exists) { |
|
129
|
|
|
$nodes = $root->descendants() |
|
130
|
|
|
->where('navigation_id', $this->navigation_id) |
|
131
|
|
|
->where('id', '<>', $this->id) |
|
132
|
|
|
->get(); |
|
133
|
|
|
} else { |
|
134
|
|
|
$nodes = $root->getDescendants(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
foreach ($nodes as $node) { |
|
138
|
|
|
$this->appendMenu($node, $items); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return array_pluck($items, 'title', 'id'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param \Baum\Node $node |
|
146
|
|
|
* @param array $items |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function appendMenu($node, &$items) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($node->isLeaf()) { |
|
152
|
|
|
$items[] = [ |
|
153
|
|
|
'title' => $node->present()->indentedTitle(), |
|
154
|
|
|
'id' => $node->id, |
|
155
|
|
|
]; |
|
156
|
|
|
} else { |
|
157
|
|
|
foreach ($node->children as $child) { |
|
158
|
|
|
$this->appendMenu($child, $items); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $items; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Check if the menu has the given widget. |
|
167
|
|
|
* |
|
168
|
|
|
* @param mixed $widget |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
|
|
public function hasWidget($widget) |
|
172
|
|
|
{ |
|
173
|
|
|
if ($widget instanceof Model) { |
|
174
|
|
|
$widget = $widget->id; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $this->widgets()->where('widget_id', $widget)->exists(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get related widgets. |
|
182
|
|
|
* |
|
183
|
|
|
* @return mixed |
|
184
|
|
|
*/ |
|
185
|
|
|
public function widgets() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->belongsToMany(Widget::class, 'widget_menu')->withoutGlobalScope('menu_assignment'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Check if the menu is currently selected/active. |
|
192
|
|
|
* |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
public function isActive() |
|
196
|
|
|
{ |
|
197
|
|
|
return request()->getRequestUri() === '/' . $this->present()->url(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get related extension. |
|
202
|
|
|
* |
|
203
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
204
|
|
|
*/ |
|
205
|
|
|
public function extension() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->belongsTo(Extension::class); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|