Passed
Push — master ( fa07c5...26ce7c )
by Al Amin
06:02
created

Menu::scopeProtected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace PhpCollective\MenuMaker\Storage;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Kalnoy\Nestedset\NodeTrait;
8
9
class Menu extends Model
10
{
11
    use NodeTrait;
0 ignored issues
show
Bug introduced by
The trait Kalnoy\Nestedset\NodeTrait requires the property $forceDeleting which is not provided by PhpCollective\MenuMaker\Storage\Menu.
Loading history...
12
13
    /**
14
     * The table associated with the model.
15
     *
16
     * @var string
17
     */
18
    protected $table = 'pcmm_menus';
19
20
    /**
21
     * Attributes that should be mass-assignable.
22
     *
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'parent_id',
27
        'name',
28
        'alias',
29
        'routes',
30
        'link',
31
        'icon',
32
        'class',
33
        'attr',
34
        'position',
35
        'privilege',
36
        'visible',
37
    ];
38
39
    /**
40
     * The group's default attributes.
41
     *
42
     * @var array
43
     */
44
    protected $attributes = [
45
        'parent_id' => null,
46
        'routes'    => null,
47
        'link'      => null,
48
        'icon'      => null,
49
        'class'     => null,
50
        'attr'      => null,
51
        'position'  => 0,
52
        'privilege' => self::DEFAULT_PRIVILAGE,
53
        'visible'   => true,
54
    ];
55
56
    /**
57
     * The attributes that should be cast to native types.
58
     *
59
     * @var array
60
     */
61
    protected $casts = [
62
        'visible' => 'integer',
63
        'routes'  => 'array',
64
    ];
65
66
    /**
67
     * The attributes that should be hidden for serialization.
68
     *
69
     * @var array
70
     */
71
    protected $hidden = [
72
        '_lft',
73
        '_rgt',
74
        'position'
75
    ];
76
77
    const DEFAULT_PRIVILAGE = 'PROTECTED';
78
79
    public static $privileges = [
80
        'PUBLIC'    => 'Public',
81
        'PROTECTED' => 'Protected',
82
        'PRIVATE'   => 'Private',
83
    ];
84
85
    /*
86
    |--------------------------------------------------------------------------
87
    | Booting
88
    |--------------------------------------------------------------------------
89
    */
90
    public static function boot()
91
    {
92
        parent::boot();
93
94
        static::creating(function ($menu) {
95
            $menu->position = self::position($menu->parent_id);
96
        });
97
    }
98
99
    protected static function position($parent_id)
100
    {
101
        return self::where('parent_id', $parent_id)
102
                ->max('position') + 1;
103
    }
104
105
    public static function rearrangePosition($commonStrategicObjectiveId)
106
    {
107
        (new static)->ofCommonStrategicObjective($commonStrategicObjectiveId)
108
            ->get()
109
            ->each(function ($objective, $key) {
110
                $objective->sequence = $key + 1;
111
                $objective->save();
112
            });
113
    }
114
115
    /**
116
     * Filter query result based on user search.
117
     *
118
     * @param Builder $query
119
     * @return Builder
120
     */
121
    public function scopeFilter(Builder $query)
122
    {
123
        $query->where('id', '<>', request('e_id'));
124
        if (request()->has('p_id')) {
125
            $query->where('parent_id', request('p_id'));
126
        }
127
128
        return $query;
129
    }
130
131
    public function setRoutesAttribute($value)
132
    {
133
        $this->attributes['routes'] = $value
134
            ? json_encode(array_map('trim', explode(',', $value)))
135
            : null;
136
    }
137
138
    public function getRouteListAttribute()
139
    {
140
        if (! $this->routes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->routes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
141
            return $this->routes;
142
        }
143
        return implode(', ', $this->routes);
144
    }
145
146
    public function setLinkAttribute($value)
147
    {
148
        $link = str_replace(url('/'), '', $value);
149
        $this->attributes['link'] = trim($link, '/');
150
    }
151
152
    public function scopeSections(Builder $query)
153
    {
154
        return $query->whereNull('parent_id');
155
    }
156
157
    public function scopeVisible(Builder $query)
158
    {
159
        return $query->whereVisible(true);
160
    }
161
162
    public function scopePublic(Builder $query)
163
    {
164
        return $query->where('privilege', 'PUBLIC');
165
    }
166
167
    public function scopeProtected(Builder $query)
168
    {
169
        return $query->where('privilege', 'PROTECTED');
170
    }
171
172
    public function scopePrivate(Builder $query)
173
    {
174
        return $query->where('privilege', 'PRIVATE');
175
    }
176
177
    /**
178
     * The permissions that associates with this menu.
179
     */
180
    public function permissions()
181
    {
182
        return $this->hasMany(Permission::class);
183
    }
184
185
    /**
186
     * Get the roles that related with the menu.
187
     */
188
    public function roles()
189
    {
190
        return $this->belongsToMany(Role::class, 'pcmm_menu_role');
191
    }
192
193
    public static function findParent()
194
    {
195
        $parent_ids = request('parent_id', []);
196
        $parent_id = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parent_id is dead and can be removed.
Loading history...
197
        do {
198
            $parent_id = array_pop($parent_ids);
199
        } while (is_null($parent_id) && count($parent_ids) > 0);
200
201
        return $parent_id;
202
    }
203
}
204