-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEvent.php
More file actions
124 lines (101 loc) · 3.32 KB
/
Event.php
File metadata and controls
124 lines (101 loc) · 3.32 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
<?php
namespace App\Models;
use App\Enums\EventServices;
use App\Enums\EventVisibility;
use App\Traits\HasUniqueIdentifier;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Event extends BaseModel
{
use HasUniqueIdentifier;
use SoftDeletes;
protected $table = 'events';
protected $casts = [
'is_paid' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
'active_at' => 'datetime',
'expire_at' => 'datetime',
'cancelled_at' => 'datetime',
'service_id' => 'string',
'service' => EventServices::class,
'visibility' => EventVisibility::class,
];
public function venue(): BelongsTo
{
return $this->belongsTo(Venue::class);
}
public function organization(): BelongsTo
{
return $this->belongsTo(Org::class, 'organization_id');
}
public function scopePublished(Builder $query): void
{
$query->where('visibility', EventVisibility::Published);
}
public function scopeWithActiveOrganization(Builder $query): void
{
$query->whereHas('organization', fn (Builder $q) => $q->whereNull('deleted_at'));
}
public function scopeFilterByDateRange(Builder $query, ?Carbon $startDate, ?Carbon $endDate): void
{
$query
->when($startDate, fn (Builder $q) => $q->where('active_at', '>=', $startDate->startOfDay()))
->when($endDate, fn (Builder $q) => $q->where('active_at', '<=', $endDate->endOfDay()))
->when( ! $startDate && ! $endDate, fn (Builder $q) => $q->where('active_at', '>=', now()->subDays(config('events-api.default_days'))));
}
public function scopeFuture(Builder $query): void
{
$query->where('active_at', '>=', now());
}
public function scopeOngoingAndFuture(Builder $query): void
{
$query->where('expire_at', '>=', now()->startOfDay());
}
public function url(): string
{
return $this->uri;
}
public function status(): string
{
if ($this->cancelled_at) {
return 'cancelled';
}
if ($this->active_at->isPast()) {
return 'past';
}
return 'upcoming';
}
public function displayName(): string
{
return $this->isCancelled()
? '[CANCELLED] ' . $this->event_name
: $this->event_name;
}
public function toGoogleCalendarUrl(): string
{
$starts_at = $this->active_at->format('Ymd\THis');
$ends_at = $this->expire_at->format('Ymd\THis');
$query = http_build_query(array_filter([
'text' => $this->event_name,
'dates' => "{$starts_at}/{$ends_at}",
'details' => strip_tags($this->description),
'location' => $this->venue?->fullAddress(),
'trp' => false,
]));
return 'https://www.google.com/calendar/event?action=TEMPLATE&' . $query;
}
public function doesNotExistOnEventService(): bool
{
return ! $this->organization
->getEventHandler()
->eventExistsOnService($this);
}
public function isCancelled(): bool
{
return null !== $this->cancelled_at;
}
}