1 | <?php |
||
2 | |||
3 | namespace Kurious7\SimplePages\Models; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Database\Eloquent\Builder; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
9 | use Kurious7\SimplePages\Contracts\SimplePage as SimplePageContract; |
||
10 | |||
11 | class SimplePageNoAutoSlug extends Model implements SimplePageContract |
||
12 | { |
||
13 | use SoftDeletes; |
||
14 | |||
15 | protected $fillable = [ |
||
16 | 'title', |
||
17 | 'slug', |
||
18 | 'content', |
||
19 | 'show_in_menu', |
||
20 | 'public', |
||
21 | 'public_from', |
||
22 | 'public_until', |
||
23 | ]; |
||
24 | |||
25 | protected $casts = [ |
||
26 | 'public' => 'boolean', |
||
27 | 'show_in_menu' => 'boolean', |
||
28 | ]; |
||
29 | |||
30 | protected $dates = [ |
||
31 | 'public_from', |
||
32 | 'public_until', |
||
33 | 'created_at', |
||
34 | 'updated_at', |
||
35 | 'deleted_at', |
||
36 | ]; |
||
37 | |||
38 | public function __construct(array $attributes = []) |
||
39 | { |
||
40 | parent::__construct($attributes); |
||
41 | |||
42 | $this->table = config('simple-pages.table', 'pages'); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Attributes. |
||
47 | */ |
||
48 | protected function getIsPublicAttribute() |
||
49 | { |
||
50 | if ( |
||
51 | $this->public == 1 |
||
0 ignored issues
–
show
|
|||
52 | || ($this->public_from && $this->public_from < new Carbon()) |
||
0 ignored issues
–
show
|
|||
53 | || ($this->public_until && $this->public_until > new Carbon()) |
||
0 ignored issues
–
show
|
|||
54 | ) { |
||
55 | return true; |
||
56 | } |
||
57 | |||
58 | return false; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Scopes. |
||
63 | */ |
||
64 | public function scopeVisibleInMenu($query): Builder |
||
65 | { |
||
66 | return $query->where('show_in_menu', true); |
||
67 | } |
||
68 | |||
69 | public function scopePublished($query): Builder |
||
70 | { |
||
71 | return $query->where('public', true) |
||
72 | ->where(function ($query) { |
||
73 | $today = (new Carbon)->toDateString(); |
||
74 | |||
75 | $query->where(function ($query) use ($today) { |
||
76 | $query->whereDate('public_from', '<=', $today)->orWhereNull('public_from'); |
||
77 | }) |
||
78 | ->where(function ($query) use ($today) { |
||
79 | $query->whereDate('public_until', '>=', $today)->orWhereNull('public_until'); |
||
80 | }); |
||
81 | }); |
||
82 | } |
||
83 | } |
||
84 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.