-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasPageAttributesTrait.php
145 lines (130 loc) · 4.57 KB
/
HasPageAttributesTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasPageAttributes;
/**
* @mixin HasPageAttributes
*/
trait HasPageAttributesTrait
{
public function initializeHasPageAttributesTrait(): void
{
$this->mergeFillable(['title', 'publishing_begins_at', 'publishing_ends_at']);
//set casts of attributes:
$this->mergeCasts([
'publishing_begins_at' => 'datetime',
'publishing_ends_at' => 'datetime',
]);
}
/**
* {@inheritDoc}
*/
public function isPublished(): bool
{
$now = Carbon::now();
if ($this->publishing_begins_at && $this->publishing_ends_at) {
return $now->between($this->publishing_begins_at, $this->publishing_ends_at);
} elseif ($this->publishing_begins_at) {
return $now->greaterThan($this->publishing_begins_at);
} elseif ($this->publishing_ends_at) {
return $now->lessThan($this->publishing_ends_at);
} else {
return true;
}
}
public function isPublishedForDates(?Carbon $publishingBeginsAt, ?Carbon $publishingEndsAt): bool
{
$now = Carbon::now();
if ($publishingBeginsAt && $publishingEndsAt) {
return $now->between($publishingBeginsAt, $publishingEndsAt);
} elseif ($publishingBeginsAt) {
return $now->greaterThan($publishingBeginsAt);
} elseif ($publishingEndsAt) {
return $now->lessThan($publishingEndsAt);
} else {
return true;
}
}
/**
* {@inheritDoc}
*/
public function willBecomePublished(): bool
{
return $this->publishing_begins_at && $this->publishing_begins_at->isFuture();
}
/**
* {@inheritDoc}
*/
public function willBecomeUnpublished(): bool
{
return $this->publishing_ends_at && $this->publishing_ends_at->isFuture();
}
/**
* {@inheritDoc}
*/
public function wasUnpublished(): bool
{
return $this->publishing_ends_at && $this->publishing_ends_at->isPast();
}
/**
* {@inheritDoc}
*/
public function scopePublished(Builder $query): Builder
{
//we need to cover each situation where publishing_begins_at and publishing_ends_at are null:
return $query->where(function (Builder $publishedQuery) {
$this->createPublishedSubquery($publishedQuery);
});
}
/**
* {@inheritDoc}
*/
public function scopeUnpublished(Builder $query): Builder
{
//we need to cover each situation where publishing_begins_at and publishing_ends_at are null:
return $query->whereNot(function (Builder $publishedQuery) {
$this->createPublishedSubquery($publishedQuery);
});
}
private function createPublishedSubquery(Builder &$publishedQuery): Builder
{
$publishedQuery->orWhere(function (Builder $option1) {
$option1->whereNull('publishing_begins_at')
->whereNotNull('publishing_ends_at')
->whereRaw('publishing_ends_at > now()');
})->orWhere(function (Builder $option2) {
$option2->whereNotNull('publishing_begins_at')
->whereNotNull('publishing_ends_at')
->whereRaw('now() between `publishing_begins_at` and `publishing_ends_at`');
})->orWhere(function (Builder $option3) {
$option3->whereNotNull('publishing_begins_at')
->whereNull('publishing_ends_at')
->whereRaw('publishing_begins_at < now()');
})->orWhere(function (Builder $option4) {
$option4->whereNull('publishing_begins_at')
->whereNull('publishing_ends_at');
});
return $publishedQuery;
}
/**
* {@inheritDoc}
*/
public function publishingBeginsAtFormatted(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) => optional($this->publishing_begins_at)->format(FilamentFlexibleBlocksConfig::getPublishingDateFormatting()),
);
}
/**
* {@inheritDoc}
*/
public function publishingEndsAtFormatted(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) => optional($this->publishing_ends_at)->format(FilamentFlexibleBlocksConfig::getPublishingDateFormatting()),
);
}
}