Widget::scopePublished()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Entities;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Laracasts\Presenter\PresentableTrait;
9
use Yajra\Acl\Models\Permission;
10
use Yajra\Acl\Traits\HasPermission;
11
use Yajra\Auditable\AuditableTrait;
12
use Yajra\CMS\Contracts\Cacheable;
13
use Yajra\CMS\Entities\Traits\CanRequireAuthentication;
14
use Yajra\CMS\Entities\Traits\HasOrder;
15
use Yajra\CMS\Entities\Traits\HasParameters;
16
use Yajra\CMS\Presenters\WidgetPresenter;
17
18
/**
19
 * @property string title
20
 * @property string template
21
 * @property string custom_template
22
 * @property string position
23
 * @property int order
24
 * @property bool published
25
 * @property bool authenticated
26
 * @property string body
27
 * @property string parameter
28
 * @property string parameters
29
 * @property string type
30
 * @property int id
31
 * @property string authorization
32
 * @property Collection|Permission[] permissions
33
 * @property bool show_title
34
 * @property int extension_id
35
 */
36
class Widget extends Model implements Cacheable
37
{
38
    use PresentableTrait, AuditableTrait, CanRequireAuthentication;
39
    use HasParameters, HasPermission, HasOrder;
40
    const ALL_PAGES      = 0;
41
    const NO_PAGES       = 1;
42
    const SELECTED_PAGES = 2;
43
44
    /**
45
     * @var \Yajra\CMS\Presenters\WidgetPresenter
46
     */
47
    protected $presenter = WidgetPresenter::class;
48
49
    /**
50
     * @var array
51
     */
52
    protected $fillable = [
53
        'title',
54
        'position',
55
        'extension_id',
56
        'template',
57
        'custom_template',
58
        'order',
59
        'parameter',
60
        'parameters',
61
        'authenticated',
62
        'authorization',
63
        'body',
64
    ];
65
66
    /**
67
     * The "booting" method of the model.
68
     *
69
     * @return void
70
     */
71
    protected static function boot()
72
    {
73
        parent::boot();
74
75
        static::addGlobalScope('menu_assignment', function(Builder $builder) {
76
            $connection = $builder->getModel()->getConnection();
77
            $assignment = [0];
78
            if (session()->has('active_menu')) {
79
                $assignment[] = session('active_menu')->id;
80
            }
81
82
            $widgets    = $connection->table('widget_menu');
83
            $widgets    = $widgets->whereIn('menu_id', $assignment);
84
            $builder->whereIn('id', $widgets->pluck('widget_id'));
85
        });
86
    }
87
88
    /**
89
     * Query scope by widget position.
90
     *
91
     * @param \Illuminate\Database\Eloquent\Builder $query
92
     * @param string $position
93
     * @return \Illuminate\Database\Eloquent\Builder $query
94
     */
95
    public function scopePosition($query, $position)
96
    {
97
        return $query->where('position', $position)->published()->orderBy('order', 'asc');
98
    }
99
100
    /**
101
     * Query scope by widget published state.
102
     *
103
     * @param \Illuminate\Database\Eloquent\Builder $query
104
     * @return \Illuminate\Database\Eloquent\Builder $query
105
     */
106
    public function scopePublished($query)
107
    {
108
        return $query->where('published', true);
109
    }
110
111
    /**
112
     * Get list of keys used for caching.
113
     *
114
     * @return array
115
     */
116
    public function getCacheKeys()
117
    {
118
        return [
119
            'widgets.published',
120
            'widgets.all',
121
        ];
122
    }
123
124
    /**
125
     * Get associated permissions.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
128
     */
129
    public function permissions()
130
    {
131
        return $this->belongsToMany(Permission::class, 'widget_permission');
132
    }
133
134
    /**
135
     * Get menu assignment attribute.
136
     *
137
     * @return int
138
     */
139
    public function getAssignmentAttribute()
140
    {
141
        if (! $this->exists) {
142
            return static::ALL_PAGES;
143
        }
144
145
        $count = $this->menuPivot()->count();
146
        if ($count === 0) {
147
            return static::NO_PAGES;
148
        } elseif ($count > 1) {
149
            return static::SELECTED_PAGES;
150
        } elseif ($count === 1) {
151
            $pivot = $this->menuPivot()->first();
152
            if ($pivot->menu_id > 0) {
153
                return static::SELECTED_PAGES;
154
            }
155
        }
156
157
        return static::ALL_PAGES;
158
    }
159
160
    /**
161
     * Get related menus.
162
     *
163
     * @return \Illuminate\Database\Eloquent\Builder
164
     */
165
    public function menuPivot()
166
    {
167
        return $this->getConnection()->table('widget_menu')
168
                    ->where('widget_id', $this->id)
169
                    ->orWhere(function ($query) {
170
                        $query->where('menu_id', 0)
171
                              ->where('widget_id', $this->id);
172
                    });
173
    }
174
175
    /**
176
     * Sync widget menu assignment.
177
     *
178
     * @param array $menu
179
     * @param int $assignment
180
     * @return $this
181
     */
182
    public function syncMenuAssignment($menu, $assignment)
183
    {
184
        switch ($assignment) {
185
            case static::ALL_PAGES:
186
                $this->menus()->sync([static::ALL_PAGES]);
187
                break;
188
            case static::NO_PAGES:
189
                $this->menus()->detach();
190
                break;
191
            case static::SELECTED_PAGES:
192
                $this->menus()->sync($menu);
193
                break;
194
        }
195
196
        return $this;
197
    }
198
199
    /**
200
     * Get related menus.
201
     *
202
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
203
     */
204
    public function menus()
205
    {
206
        return $this->belongsToMany(Menu::class, 'widget_menu');
207
    }
208
209
    /**
210
     * Get related extension.
211
     *
212
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
213
     */
214
    public function extension()
215
    {
216
        return $this->belongsTo(Extension::class);
217
    }
218
}
219