davide-casiraghi /
laravel-quick-menus
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace DavideCasiraghi\LaravelQuickMenus\Models; |
||||||
| 4 | |||||||
| 5 | use Astrotomic\Translatable\Translatable; |
||||||
| 6 | use Illuminate\Database\Eloquent\Model; |
||||||
| 7 | use Illuminate\Support\Facades\Auth; |
||||||
| 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; |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 22 | |||||||
| 23 | public $translatedAttributes = ['name', 'slug']; |
||||||
| 24 | |||||||
| 25 | // 'locale', 'menu_item_id' -> added for the db seeder |
||||||
| 26 | protected $fillable = [ |
||||||
| 27 | 'parent_item_id', |
||||||
| 28 | 'url', |
||||||
| 29 | 'font_awesome_class', |
||||||
| 30 | 'route', |
||||||
| 31 | 'route_param_name_1', |
||||||
| 32 | 'route_param_name_2', |
||||||
| 33 | 'route_param_name_3', |
||||||
| 34 | 'route_param_value_1', |
||||||
| 35 | 'route_param_value_2', |
||||||
| 36 | 'route_param_value_3', |
||||||
| 37 | 'type', |
||||||
| 38 | 'menu_id', |
||||||
| 39 | 'order', |
||||||
| 40 | 'locale', |
||||||
| 41 | 'menu_item_id', |
||||||
| 42 | ]; |
||||||
| 43 | |||||||
| 44 | /***************************************************************************/ |
||||||
| 45 | |||||||
| 46 | /** |
||||||
| 47 | * Return the items of the menu in a tree format (multidimensional array) |
||||||
| 48 | * If $menuId is 0 return all the items. |
||||||
| 49 | * https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
||||||
| 50 | * |
||||||
| 51 | * @param $menuId |
||||||
| 52 | * @return array |
||||||
| 53 | */ |
||||||
| 54 | 10 | public static function getItemsTree($menuId) |
|||||
| 55 | { |
||||||
| 56 | $menuItems = self:: |
||||||
| 57 | when($menuId, function ($query, $menuId) { |
||||||
| 58 | 9 | return $query->where('menu_id', $menuId); |
|||||
| 59 | 10 | }) |
|||||
| 60 | 10 | ->orderBy('order', 'ASC') |
|||||
| 61 | 10 | ->get(); |
|||||
| 62 | |||||||
| 63 | // Create an items multidimensional array by parent_item_id |
||||||
| 64 | 10 | $new = []; |
|||||
| 65 | 10 | foreach ($menuItems as $menuItem) { |
|||||
| 66 | 7 | $new[$menuItem['parent_item_id']][] = $menuItem; |
|||||
| 67 | } |
||||||
| 68 | |||||||
| 69 | 10 | if (! empty($new)) { |
|||||
| 70 | 7 | $ret = self::createTree($new, $new[0]); |
|||||
| 71 | } else { |
||||||
| 72 | 3 | $ret = []; |
|||||
| 73 | } |
||||||
| 74 | //dump($ret); |
||||||
| 75 | |||||||
| 76 | 10 | return $ret; |
|||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /***************************************************************************/ |
||||||
| 80 | |||||||
| 81 | /** |
||||||
| 82 | * Create array tree from array list - it support more than 1 parentid[0] element |
||||||
| 83 | * https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
||||||
| 84 | * |
||||||
| 85 | * @param array $list |
||||||
| 86 | * @param array $parent |
||||||
| 87 | * @return array |
||||||
| 88 | */ |
||||||
| 89 | 7 | public static function createTree(&$list, $parent) |
|||||
| 90 | { |
||||||
| 91 | 7 | $tree = []; |
|||||
| 92 | 7 | foreach ($parent as $k=>$l) { |
|||||
| 93 | 7 | if (isset($list[$l['id']])) { |
|||||
| 94 | 2 | $l['children'] = self::createTree($list, $list[$l['id']]); |
|||||
| 95 | } |
||||||
| 96 | 7 | $tree[] = $l; |
|||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | 7 | return $tree; |
|||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | /***************************************************************************/ |
||||||
| 103 | |||||||
| 104 | /** |
||||||
| 105 | * Return the access level name |
||||||
| 106 | * https://stackoverflow.com/questions/4196157/create-array-tree-from-array-list. |
||||||
| 107 | * |
||||||
| 108 | * @param int $accessId |
||||||
| 109 | * @return string |
||||||
| 110 | */ |
||||||
| 111 | public static function getAccessName($accessId) |
||||||
| 112 | { |
||||||
| 113 | $accessLevels = [ |
||||||
| 114 | '1' => 'Public', |
||||||
| 115 | '2' => 'Guest', |
||||||
| 116 | '3' => 'Manager', |
||||||
| 117 | '4' => 'Administrator', |
||||||
| 118 | '5' => 'Super Administrator', |
||||||
| 119 | ]; |
||||||
| 120 | |||||||
| 121 | $ret = $accessLevels[$accessId]; |
||||||
| 122 | |||||||
| 123 | return $ret; |
||||||
| 124 | } |
||||||
| 125 | |||||||
| 126 | /***************************************************************************/ |
||||||
| 127 | |||||||
| 128 | /** |
||||||
| 129 | * Check if the user group with the access level and return true if the user is authorized to see the menu item. |
||||||
| 130 | * |
||||||
| 131 | * @return bool |
||||||
| 132 | */ |
||||||
| 133 | public function authorized() |
||||||
| 134 | { |
||||||
| 135 | $ret = false; |
||||||
| 136 | $user = Auth::user(); |
||||||
| 137 | |||||||
| 138 | switch ($this->access) { |
||||||
| 139 | case '1': // Public |
||||||
| 140 | $ret = true; |
||||||
| 141 | break; |
||||||
| 142 | case '2': // Guest - not authenticated user |
||||||
| 143 | if (! $user) { |
||||||
| 144 | $ret = true; |
||||||
| 145 | } |
||||||
| 146 | break; |
||||||
| 147 | case '3': // Manager |
||||||
| 148 | if ($user) { |
||||||
| 149 | $ret = true; |
||||||
| 150 | } |
||||||
| 151 | break; |
||||||
| 152 | case '4': // Admin |
||||||
| 153 | if ($user) { |
||||||
| 154 | if ($user->isSuperAdmin() || $user->isAdmin()) { |
||||||
|
0 ignored issues
–
show
The method
isAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
isSuperAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 155 | $ret = true; |
||||||
| 156 | } |
||||||
| 157 | } |
||||||
| 158 | break; |
||||||
| 159 | case '5': // Super Admin |
||||||
| 160 | if ($user) { |
||||||
| 161 | if ($user->isSuperAdmin()) { |
||||||
| 162 | $ret = true; |
||||||
| 163 | } |
||||||
| 164 | } |
||||||
| 165 | break; |
||||||
| 166 | } |
||||||
| 167 | |||||||
| 168 | return $ret; |
||||||
| 169 | } |
||||||
| 170 | } |
||||||
| 171 |