Completed
Push — master ( d6577e...9678d0 )
by Davide
04:54
created

MenuItem::createTree()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
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;
0 ignored issues
show
introduced by
The trait Dimsav\Translatable\Translatable requires some properties which are not provided by DavideCasiraghi\LaravelQuickMenus\Models\MenuItem: $translations, $useTranslationFallback, $translationModel, $localeKey, $translationForeignKey
Loading history...
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()) {
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

139
                    if ($user->/** @scrutinizer ignore-call */ isSuperAdmin() || $user->isAdmin()) {
Loading history...
Bug introduced by
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 ignore-call  annotation

139
                    if ($user->isSuperAdmin() || $user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
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