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