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