|
1
|
|
|
<?php namespace Arcanesoft\Core\Helpers\Sidebar\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
4
|
|
|
use Illuminate\Support\Arr; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Item |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanesoft\Core\Helpers\Sidebar\Entities |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Item implements Arrayable |
|
13
|
|
|
{ |
|
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
*/ |
|
18
|
|
|
/** |
|
19
|
|
|
* The item name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The item title. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
public $title; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The item url. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public $url; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The item icon. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
public $icon; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The item active state. |
|
48
|
|
|
* |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public $active = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The authenticated user. |
|
55
|
|
|
* |
|
56
|
|
|
* @var \Arcanesoft\Contracts\Auth\Models\User |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $user; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The item roles. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $roles = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The item permissions. |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $permissions = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The item children (sub-items). |
|
76
|
|
|
* |
|
77
|
|
|
* @var \Arcanesoft\Core\Helpers\Sidebar\Entities\ItemCollection |
|
78
|
|
|
*/ |
|
79
|
|
|
public $children; |
|
80
|
|
|
|
|
81
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
82
|
|
|
| Constructor |
|
83
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
84
|
|
|
*/ |
|
85
|
|
|
/** |
|
86
|
|
|
* Item constructor. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @param string $title |
|
90
|
|
|
* @param string $url |
|
91
|
|
|
* @param string|null $icon |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __construct($name, $title, $url, $icon = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->name = $name; |
|
96
|
|
|
$this->title = $title; |
|
97
|
|
|
$this->url = $url; |
|
98
|
|
|
$this->icon = $icon; |
|
99
|
|
|
$this->active = false; |
|
100
|
|
|
$this->children = new ItemCollection; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
104
|
|
|
| Getters & Setters |
|
105
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
106
|
|
|
*/ |
|
107
|
|
|
/** |
|
108
|
|
|
* Set the current name. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $currentName |
|
111
|
|
|
* |
|
112
|
|
|
* @return self |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setCurrent($currentName) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->children->setCurrent($currentName); |
|
117
|
|
|
$this->active = ($this->name === $currentName || $this->children->hasActiveItem()); |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set the authenticated user. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
126
|
|
|
* |
|
127
|
|
|
* @return self |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setUser($user) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->user = $user; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the roles. |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getRoles() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->roles; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the roles. |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $roles |
|
150
|
|
|
* |
|
151
|
|
|
* @return self |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setRoles(array $roles) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->roles = $roles; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get the permissions. |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getPermissions() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->permissions; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Set the permissions. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $permissions |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setPermissions(array $permissions) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->permissions = $permissions; |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the active class. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $class |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function activeClass($class = 'active') |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->isActive() ? $class : ''; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get the tree view class. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $class |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function treeViewClass($class = 'treeview') |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->hasChildren() ? $class : ''; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
209
|
|
|
| Main Functions |
|
210
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
211
|
|
|
*/ |
|
212
|
|
|
/** |
|
213
|
|
|
* Make the item. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $name |
|
216
|
|
|
* @param string $title |
|
217
|
|
|
* @param string $url |
|
218
|
|
|
* @param string|null $icon |
|
219
|
|
|
* |
|
220
|
|
|
* @return self |
|
221
|
|
|
*/ |
|
222
|
|
|
public static function make($name, $title, $url, $icon = null) |
|
223
|
|
|
{ |
|
224
|
|
|
return new self($name, $title, $url, $icon); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Make a Sidebar item from array. |
|
229
|
|
|
* |
|
230
|
|
|
* @param array $array |
|
231
|
|
|
* |
|
232
|
|
|
* @return self |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function makeFromArray(array $array, $user) |
|
235
|
|
|
{ |
|
236
|
|
|
$item = self::make( |
|
237
|
|
|
$array['name'], |
|
238
|
|
|
$array['title'], |
|
239
|
|
|
self::getUrlFromArray($array), |
|
240
|
|
|
Arr::get($array, 'icon', null) |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
|
|
$item->setUser($user); |
|
244
|
|
|
$item->setRoles(Arr::get($array, 'roles', [])); |
|
245
|
|
|
$item->setPermissions(Arr::get($array, 'permissions', [])); |
|
246
|
|
|
$item->addChildren(Arr::get($array, 'children', [])); |
|
247
|
|
|
|
|
248
|
|
|
return $item; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Get url from array. |
|
253
|
|
|
* |
|
254
|
|
|
* @param array $array |
|
255
|
|
|
* |
|
256
|
|
|
* @return string |
|
257
|
|
|
*/ |
|
258
|
|
|
private static function getUrlFromArray(array $array) |
|
259
|
|
|
{ |
|
260
|
|
|
return Arr::has($array, 'route') |
|
261
|
|
|
? route(Arr::get($array, 'route')) |
|
262
|
|
|
: Arr::get($array, 'url', '#'); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Add children to the parent. |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $children |
|
269
|
|
|
* |
|
270
|
|
|
* @return self |
|
271
|
|
|
*/ |
|
272
|
|
|
private function addChildren(array $children) |
|
273
|
|
|
{ |
|
274
|
|
|
foreach ($children as $child) { |
|
275
|
|
|
$item = self::makeFromArray($child, $this->user); |
|
276
|
|
|
|
|
277
|
|
|
if ($item->allowed()) |
|
278
|
|
|
$this->children->push($item); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
285
|
|
|
| Check Functions |
|
286
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
287
|
|
|
*/ |
|
288
|
|
|
/** |
|
289
|
|
|
* Check if the item is active one. |
|
290
|
|
|
* |
|
291
|
|
|
* @return bool |
|
292
|
|
|
*/ |
|
293
|
|
|
public function isActive() |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->active; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Check if the item has children. |
|
300
|
|
|
* |
|
301
|
|
|
* @return bool |
|
302
|
|
|
*/ |
|
303
|
|
|
public function hasChildren() |
|
304
|
|
|
{ |
|
305
|
|
|
return ! $this->children->isEmpty(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Check the user is allowed to see this item. |
|
310
|
|
|
* |
|
311
|
|
|
* @return bool |
|
312
|
|
|
*/ |
|
313
|
|
|
public function allowed() |
|
314
|
|
|
{ |
|
315
|
|
|
if ($this->user->isAdmin()) |
|
316
|
|
|
return true; |
|
317
|
|
|
|
|
318
|
|
|
if (empty($this->roles) && empty($this->permissions)) |
|
319
|
|
|
return true; |
|
320
|
|
|
|
|
321
|
|
|
foreach ($this->roles as $roleSlug) { |
|
322
|
|
|
if ($this->user->hasRoleSlug($roleSlug)) return true; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
foreach ($this->permissions as $permissionSlug) { |
|
326
|
|
|
if ($this->user->may($permissionSlug)) return true; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return false; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
333
|
|
|
| Other Functions |
|
334
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
335
|
|
|
*/ |
|
336
|
|
|
/** |
|
337
|
|
|
* Get the instance as an array. |
|
338
|
|
|
* |
|
339
|
|
|
* @return array |
|
340
|
|
|
*/ |
|
341
|
|
|
public function toArray() |
|
342
|
|
|
{ |
|
343
|
|
|
return [ |
|
344
|
|
|
'name' => $this->name, |
|
345
|
|
|
'title' => $this->title, |
|
346
|
|
|
'url' => $this->url, |
|
347
|
|
|
'icon' => $this->icon, |
|
348
|
|
|
'active' => $this->active, |
|
349
|
|
|
'roles' => $this->roles, |
|
350
|
|
|
'permissions' => $this->permissions, |
|
351
|
|
|
'children' => $this->children->toArray(), |
|
352
|
|
|
]; |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|