1
|
|
|
<?php namespace Arcanesoft\Sidebar\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
4
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use JsonSerializable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Item |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Sidebar\Entitites |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Item implements Arrayable, Jsonable, JsonSerializable |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The item name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The item title. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $title; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The item url. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $url; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The item icon. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $icon; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The item active state. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $active = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The item roles. |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $roles = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The item permissions. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $permissions = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The item children (sub-items). |
72
|
|
|
* |
73
|
|
|
* @var \Arcanesoft\Sidebar\Entities\ItemCollection |
74
|
|
|
*/ |
75
|
|
|
protected $children; |
76
|
|
|
|
77
|
|
|
/* ----------------------------------------------------------------- |
78
|
|
|
| Constructor |
79
|
|
|
| ----------------------------------------------------------------- |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Item constructor. |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
* @param string $title |
87
|
|
|
* @param string $url |
88
|
|
|
* @param string|null $icon |
89
|
|
|
*/ |
90
|
36 |
|
public function __construct($name, $title, $url, $icon = null) |
91
|
|
|
{ |
92
|
36 |
|
$this->name = $name; |
93
|
36 |
|
$this->title = $title; |
94
|
36 |
|
$this->url = $url; |
95
|
36 |
|
$this->icon = $icon; |
96
|
36 |
|
$this->active = false; |
97
|
36 |
|
$this->children = new ItemCollection; |
98
|
36 |
|
} |
99
|
|
|
|
100
|
|
|
/* ----------------------------------------------------------------- |
101
|
|
|
| Getters & Setters |
102
|
|
|
| ----------------------------------------------------------------- |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the item name. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
18 |
|
public function name() |
111
|
|
|
{ |
112
|
18 |
|
return $this->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the item title. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
24 |
|
public function title() |
121
|
|
|
{ |
122
|
|
|
/** @var \Illuminate\Translation\Translator $trans */ |
123
|
24 |
|
$trans = trans(); |
124
|
|
|
|
125
|
24 |
|
return $trans->has($this->title) ? $trans->get($this->title) : $this->title; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the item url. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
18 |
|
public function url() |
134
|
|
|
{ |
135
|
18 |
|
return $this->url; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the item icon. |
140
|
|
|
* |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
21 |
|
public function icon() |
144
|
|
|
{ |
145
|
21 |
|
return $this->icon; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the current name. |
150
|
|
|
* |
151
|
|
|
* @param string $name |
152
|
|
|
* |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
6 |
|
public function setCurrent($name) |
156
|
|
|
{ |
157
|
6 |
|
$this->children->setCurrent($name); |
158
|
6 |
|
$this->active = ($this->name === $name || $this->children->hasActiveItem()); |
159
|
|
|
|
160
|
6 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the roles. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
12 |
|
public function getRoles() |
169
|
|
|
{ |
170
|
12 |
|
return $this->roles; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the roles. |
175
|
|
|
* |
176
|
|
|
* @param array $roles |
177
|
|
|
* |
178
|
|
|
* @return self |
179
|
|
|
*/ |
180
|
27 |
|
public function setRoles(array $roles) |
181
|
|
|
{ |
182
|
27 |
|
$this->roles = $roles; |
183
|
|
|
|
184
|
27 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the permissions. |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
12 |
|
public function getPermissions() |
193
|
|
|
{ |
194
|
12 |
|
return $this->permissions; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set the permissions. |
199
|
|
|
* |
200
|
|
|
* @param array $permissions |
201
|
|
|
* |
202
|
|
|
* @return self |
203
|
|
|
*/ |
204
|
27 |
|
public function setPermissions(array $permissions) |
205
|
|
|
{ |
206
|
27 |
|
$this->permissions = $permissions; |
207
|
|
|
|
208
|
27 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the sub-items. |
213
|
|
|
* |
214
|
|
|
* @return \Arcanesoft\Sidebar\Entities\ItemCollection |
215
|
|
|
*/ |
216
|
12 |
|
public function children() |
217
|
|
|
{ |
218
|
12 |
|
return $this->children; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the active class. |
223
|
|
|
* |
224
|
|
|
* @param string $class |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
3 |
|
public function activeClass($class = 'active') |
229
|
|
|
{ |
230
|
3 |
|
return $this->isActive() ? $class : ''; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the sub-items class. |
235
|
|
|
* |
236
|
|
|
* @param string $class |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
3 |
|
public function childrenClass($class = 'treeview') |
241
|
|
|
{ |
242
|
3 |
|
return $this->hasChildren() ? $class : ''; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/* ----------------------------------------------------------------- |
246
|
|
|
| Main Methods |
247
|
|
|
| ----------------------------------------------------------------- |
248
|
|
|
*/ |
249
|
|
|
/** |
250
|
|
|
* Make the item. |
251
|
|
|
* |
252
|
|
|
* @param string $name |
253
|
|
|
* @param string $title |
254
|
|
|
* @param string $url |
255
|
|
|
* @param string|null $icon |
256
|
|
|
* |
257
|
|
|
* @return self |
258
|
|
|
*/ |
259
|
21 |
|
public static function make($name, $title, $url, $icon = null) |
260
|
|
|
{ |
261
|
21 |
|
return new self($name, $title, $url, $icon); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Make a Sidebar item from array. |
266
|
|
|
* |
267
|
|
|
* @param array $array |
268
|
|
|
* |
269
|
|
|
* @return self |
270
|
|
|
*/ |
271
|
12 |
|
public static function makeFromArray(array $array) |
272
|
|
|
{ |
273
|
12 |
|
return tap( |
274
|
12 |
|
self::make($array['name'], $array['title'], self::getUrlFromArray($array), Arr::get($array, 'icon', null)), |
275
|
|
|
function (Item $item) use ($array) { |
276
|
12 |
|
$item->setRoles(Arr::get($array, 'roles', [])); |
277
|
12 |
|
$item->setPermissions(Arr::get($array, 'permissions', [])); |
278
|
12 |
|
$item->addChildren(Arr::get($array, 'children', [])); |
279
|
12 |
|
} |
280
|
4 |
|
); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get url from array. |
285
|
|
|
* |
286
|
|
|
* @param array $array |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
12 |
|
private static function getUrlFromArray(array $array) |
291
|
|
|
{ |
292
|
12 |
|
return Arr::has($array, 'route') |
293
|
6 |
|
? route(Arr::get($array, 'route')) |
294
|
12 |
|
: Arr::get($array, 'url', '#'); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Add children to the parent. |
299
|
|
|
* |
300
|
|
|
* @param array $children |
301
|
|
|
* |
302
|
|
|
* @return self |
303
|
|
|
*/ |
304
|
12 |
|
public function addChildren(array $children) |
305
|
|
|
{ |
306
|
12 |
|
foreach ($children as $child) { |
307
|
6 |
|
$this->addChild($child); |
308
|
4 |
|
} |
309
|
|
|
|
310
|
12 |
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Add a sub-item to the parent. |
315
|
|
|
* |
316
|
|
|
* @param array $child |
317
|
|
|
* |
318
|
|
|
* @return self |
319
|
|
|
*/ |
320
|
6 |
|
public function addChild(array $child) |
321
|
|
|
{ |
322
|
6 |
|
$item = self::makeFromArray($child); |
323
|
|
|
|
324
|
6 |
|
if ($item->allowed()) |
325
|
6 |
|
$this->children->push($item); |
326
|
|
|
|
327
|
6 |
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/* ----------------------------------------------------------------- |
331
|
|
|
| Check Methods |
332
|
|
|
| ----------------------------------------------------------------- |
333
|
|
|
*/ |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Check if the item is active one. |
337
|
|
|
* |
338
|
|
|
* @return bool |
339
|
|
|
*/ |
340
|
24 |
|
public function isActive() |
341
|
|
|
{ |
342
|
24 |
|
return $this->active; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Check if the item has children. |
347
|
|
|
* |
348
|
|
|
* @return bool |
349
|
|
|
*/ |
350
|
9 |
|
public function hasChildren() |
351
|
|
|
{ |
352
|
9 |
|
return ! $this->children->isEmpty(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Check the user is allowed to see this item. |
357
|
|
|
* |
358
|
|
|
* @return bool |
359
|
|
|
*/ |
360
|
12 |
|
public function allowed() |
361
|
|
|
{ |
362
|
|
|
/** @var \Arcanesoft\Contracts\Auth\Models\User $user */ |
363
|
12 |
|
$user = auth()->user(); |
364
|
|
|
|
365
|
12 |
|
if (is_null($user) || ( ! $this->hasRoles() && ! $this->hasPermissions())) |
366
|
12 |
|
return true; |
367
|
|
|
|
368
|
3 |
|
if ($user->isAdmin()) |
369
|
3 |
|
return true; |
370
|
|
|
|
371
|
3 |
|
foreach ($this->getRoles() as $roleSlug) { |
372
|
3 |
|
if ($user->hasRoleSlug($roleSlug)) |
373
|
3 |
|
return true; |
374
|
1 |
|
} |
375
|
|
|
|
376
|
3 |
|
foreach ($this->getPermissions() as $permissionSlug) { |
377
|
3 |
|
if ($user->may($permissionSlug)) |
378
|
3 |
|
return true; |
379
|
1 |
|
} |
380
|
|
|
|
381
|
3 |
|
return $this->children()->filter(function (Item $child) { |
382
|
3 |
|
return $child->allowed(); |
383
|
3 |
|
})->isNotEmpty(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Check if the item has roles. |
388
|
|
|
* |
389
|
|
|
* @return bool |
390
|
|
|
*/ |
391
|
9 |
|
public function hasRoles() |
392
|
|
|
{ |
393
|
9 |
|
return ! empty($this->roles); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Check if the item has permissions. |
398
|
|
|
* |
399
|
|
|
* @return bool |
400
|
|
|
*/ |
401
|
6 |
|
public function hasPermissions() |
402
|
|
|
{ |
403
|
6 |
|
return ! empty($this->permissions); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/* ----------------------------------------------------------------- |
407
|
|
|
| Other Methods |
408
|
|
|
| ----------------------------------------------------------------- |
409
|
|
|
*/ |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Get the instance as an array. |
413
|
|
|
* |
414
|
|
|
* @return array |
415
|
|
|
*/ |
416
|
12 |
|
public function toArray() |
417
|
|
|
{ |
418
|
|
|
return [ |
419
|
12 |
|
'name' => $this->name(), |
420
|
12 |
|
'title' => $this->title(), |
421
|
12 |
|
'url' => $this->url(), |
422
|
12 |
|
'icon' => $this->icon(), |
423
|
12 |
|
'active' => $this->isActive(), |
424
|
12 |
|
'roles' => $this->roles, |
425
|
12 |
|
'permissions' => $this->permissions, |
426
|
12 |
|
'children' => $this->children->toArray(), |
427
|
4 |
|
]; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Convert the object to its JSON representation. |
432
|
|
|
* |
433
|
|
|
* @param int $options |
434
|
|
|
* |
435
|
|
|
* @return string |
436
|
|
|
*/ |
437
|
3 |
|
public function toJson($options = 0) |
438
|
|
|
{ |
439
|
3 |
|
return json_encode($this->jsonSerialize(), $options); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Convert the object into something JSON serializable. |
444
|
|
|
* |
445
|
|
|
* @return array |
446
|
|
|
*/ |
447
|
6 |
|
public function jsonSerialize() |
448
|
|
|
{ |
449
|
6 |
|
return $this->toArray(); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|