1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelQuickMenus\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Dimsav\Translatable\Translatable; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class MenuItem extends Model |
10
|
|
|
{ |
11
|
|
|
/***************************************************************************/ |
12
|
|
|
/** |
13
|
|
|
* The table associated with the model. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $table = 'menu_items'; |
18
|
|
|
|
19
|
|
|
/***************************************************************************/ |
20
|
|
|
|
21
|
|
|
use Translatable; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public $translatedAttributes = ['name', 'slug']; |
24
|
|
|
|
25
|
|
|
protected $fillable = [ |
26
|
|
|
'parent_item_id', 'url', 'font_awesome_class', 'route', 'type', 'menu_id', 'order', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/***************************************************************************/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return the items of the menu in a tree format (multidimensional array) |
33
|
|
|
* If $menuId is 0 return all the items. |
34
|
|
|
* https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
35
|
|
|
* |
36
|
|
|
* @param $menuId |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
2 |
|
public static function getItemsTree($menuId) |
40
|
|
|
{ |
41
|
|
|
$menuItems = self:: |
42
|
|
|
when($menuId, function ($query, $menuId) { |
43
|
1 |
|
return $query->where('menu_id', $menuId); |
44
|
2 |
|
}) |
45
|
2 |
|
->orderBy('order', 'ASC') |
46
|
2 |
|
->get(); |
47
|
|
|
|
48
|
|
|
// Create an items multidimensional array by parent_item_id |
49
|
2 |
|
$new = []; |
50
|
2 |
|
foreach ($menuItems as $menuItem) { |
51
|
1 |
|
$new[$menuItem['parent_item_id']][] = $menuItem; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
if (! empty($new)) { |
55
|
1 |
|
$ret = self::createTree($new, $new[0]); |
56
|
|
|
} else { |
57
|
1 |
|
$ret = []; |
58
|
|
|
} |
59
|
|
|
//dump($ret); |
60
|
|
|
|
61
|
2 |
|
return $ret; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/***************************************************************************/ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create array tree from array list - it support more than 1 parentid[0] element |
68
|
|
|
* https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
69
|
|
|
* |
70
|
|
|
* @param array $list |
71
|
|
|
* @param array $parent |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
1 |
|
public static function createTree(&$list, $parent) |
75
|
|
|
{ |
76
|
1 |
|
$tree = []; |
77
|
1 |
|
foreach ($parent as $k=>$l) { |
78
|
1 |
|
if (isset($list[$l['id']])) { |
79
|
1 |
|
$l['children'] = self::createTree($list, $list[$l['id']]); |
80
|
|
|
} |
81
|
1 |
|
$tree[] = $l; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $tree; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/***************************************************************************/ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return the access level name |
91
|
|
|
* https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
92
|
|
|
* |
93
|
|
|
* @param int $accessId |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public static function getAccessName($accessId) |
97
|
|
|
{ |
98
|
|
|
$accessLevels = [ |
99
|
|
|
'1' => 'Public', |
100
|
|
|
'2' => 'Guest', |
101
|
|
|
'3' => 'Manager', |
102
|
|
|
'4' => 'Administrator', |
103
|
|
|
'5' => 'Super Administrator', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$ret = $accessLevels[$accessId]; |
107
|
|
|
|
108
|
|
|
return $ret; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/***************************************************************************/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if the user group with the access level and return true if the user is authorized to see the menu item. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function authorized() |
119
|
|
|
{ |
120
|
|
|
$ret = false; |
121
|
|
|
$user = Auth::user(); |
122
|
|
|
|
123
|
|
|
switch ($this->access) { |
124
|
|
|
case '1': // Public |
125
|
|
|
$ret = true; |
126
|
|
|
break; |
127
|
|
|
case '2': // Guest - not authenticated user |
128
|
|
|
if (! $user) { |
129
|
|
|
$ret = true; |
130
|
|
|
} |
131
|
|
|
break; |
132
|
|
|
case '3': // Manager |
133
|
|
|
if ($user) { |
134
|
|
|
$ret = true; |
135
|
|
|
} |
136
|
|
|
break; |
137
|
|
|
case '4': // Admin |
138
|
|
|
if ($user) { |
139
|
|
|
if ($user->isSuperAdmin() || $user->isAdmin()) { |
|
|
|
|
140
|
|
|
$ret = true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
break; |
144
|
|
|
case '5': // Super Admin |
145
|
|
|
if ($user) { |
146
|
|
|
if ($user->isSuperAdmin()) { |
147
|
|
|
$ret = true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
break; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $ret; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|