-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEvent.php
More file actions
173 lines (139 loc) · 5.16 KB
/
Event.php
File metadata and controls
173 lines (139 loc) · 5.16 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace TransformStudios\Events\Types;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Support\Collection;
use RRule\RRuleInterface;
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
use Statamic\Entries\Entry;
abstract class Event
{
abstract protected function rule(): RRuleInterface;
public function __construct(protected Entry $event) {}
public function __get(string $key): mixed
{
return $this->event->$key;
}
/*
Can remove this once https://github.com/statamic/cms/pull/11402 is released
*/
public function __isset(string $key): bool
{
return isset($this->event->$key);
}
public function endTime(): string
{
return $this->end_time ?? now()->endOfDay()->toTimeString();
}
public function hasEndTime(): bool
{
return boolval($this->end_time);
}
public function isAllDay(): bool
{
return boolval($this->all_day);
}
public function isMultiDay(): bool
{
return boolval(($this->multi_day || $this->recurrence?->value() === 'multi_day') && ! empty($this->days));
}
public function isRecurring(): bool
{
// this is a select field so you have to get its value
return match ($this->recurrence?->value()) {
'daily', 'weekly', 'monthly', 'yearly' => true,
default => false,
};
}
public function occurrencesBetween(string|CarbonInterface $from, string|CarbonInterface $to): Collection
{
return $this->collect($this->rule()->getOccurrencesBetween(begin: $from, end: $to));
}
public function occursOnDate(string|CarbonInterface $date): bool
{
$immutableDate = is_string($date) ? CarbonImmutable::parse($date) : $date->toImmutable();
return ! empty($this->rule()->getOccurrencesBetween(begin: $immutableDate->startOfDay(), end: $immutableDate->endOfDay()));
}
public function nextOccurrences(int $limit = 1): Collection
{
return $this->collect($this->rule()->getOccurrencesAfter(date: now(), inclusive: true, limit: $limit));
}
public function startTime(): string
{
return $this->start_time ?? now()->startOfDay()->toTimeString('second');
}
public function start(): CarbonImmutable
{
return CarbonImmutable::parse($this->start_date)
->shiftTimezone($this->timezone['timezone'])
->setTimeFromTimeString($this->startTime());
}
public function end(): CarbonImmutable
{
return CarbonImmutable::parse($this->start_date)
->shiftTimezone($this->timezone['timezone'])
->setTimeFromTimeString($this->endTime());
}
public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
{
if (! $this->occursOnDate($date)) {
return null;
}
$immutableDate = $this->toCarbonImmutable($date);
$iCalEvent = ICalendarEvent::create($this->event->title)
->withoutTimezone()
->uniqueIdentifier($this->event->id())
->startsAt($immutableDate->setTimeFromTimeString($this->startTime()))
->endsAt($immutableDate->setTimeFromTimeString($this->endTime()));
if (! is_null($address = $this->event->address ?? $this->location($this->event))) {
$iCalEvent->address($address);
}
if (! is_null($coords = $this->event->coordinates)) {
$iCalEvent->coordinates($coords['latitude'], $coords['longitude']);
}
if (! is_null($description = $this->event->description)) {
$iCalEvent->description($description);
}
if (! is_null($link = $this->event->link)) {
$iCalEvent->url($link);
}
return $iCalEvent;
}
/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
return [
$this->toICalendarEvent($this->start()),
];
}
protected function location(Entry $event): ?string
{
$collectionHandle = $event->collectionHandle();
$locationField = config("events.collections.$collectionHandle.location_field", 'location');
return $event->{$locationField};
}
protected function supplement(CarbonInterface $date): ?Entry
{
return unserialize(serialize($this->event))
->setSupplement('multi_day', false)
->setSupplement('start', $date->setTimeFromTimeString($this->startTime()))
->setSupplement('end', $date->setTimeFromTimeString($this->endTime()))
->setSupplement('has_end_time', $this->hasEndTime());
}
protected function toCarbonImmutable(string|CarbonInterface $date): CarbonImmutable
{
$carbon = is_string($date) ? Carbon::parse($date) : $date;
return $carbon->shiftTimezone($this->timezone['timezone'])->toImmutable();
}
private function collect(array $dates): Collection
{
return collect($dates)
->map(fn (DateTimeInterface $date) => $this->supplement(
date: CarbonImmutable::parse($date, $this->timezone['timezone'])
))->filter();
}
}