-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCalendarFeedController.php
More file actions
89 lines (80 loc) · 3.53 KB
/
CalendarFeedController.php
File metadata and controls
89 lines (80 loc) · 3.53 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
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CalendarFeedRequest;
use App\Models\Event;
use App\Models\Org;
use Illuminate\Contracts\Database\Query\Builder;
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event as CalendarEvent;
use Spatie\IcalendarGenerator\Enums\Classification;
use Spatie\IcalendarGenerator\Enums\EventStatus;
class CalendarFeedController extends Controller
{
public function index(CalendarFeedRequest $request)
{
return view('calendar-feed.index', [
'organizations' => Org::query()
->with('category')
->active()
->when(
value: $request->validOrganizations()->isNotEmpty(),
callback: function (Builder $query) use ($request) {
$query->whereIn('id', $request->validOrganizations()->pluck('id')->toArray())
->orderByFieldSequence('id', $request->validOrganizations()->pluck('id')->toArray())
->orderBy('title');
},
default: fn (Builder $query) => $query->orderBy('title')
)
->get()
->map(fn (Org $org) => [
'id' => $org->id,
'title' => $org->title,
'checked' => $request->validOrganizations()->contains('id', $org->id),
]),
]);
}
public function show(CalendarFeedRequest $request)
{
$events = Event::query()
->with('organization', 'venue')
->ongoingAndFuture()
->oldest('active_at')
->when($request->validOrganizations()->isNotEmpty(), fn ($query) => $query->whereIn('organization_id', $request->validOrganizations()->pluck('id')))
->get()
->mapWithKeys(fn (Event $event, $i) => [
$i => CalendarEvent::create($event->displayName())
->uniqueIdentifier(sha1($event->id))
->startsAt($event->active_at)
->endsAt($event->expire_at)
->status(EventStatus::confirmed())
->address(
address: $event->venue?->fullAddress() ?? 'Virtual Event',
name: $event->venue?->name ?? 'Virtual'
)
->classification(Classification::public())
->description("Check out latest event details at {$event->url()}")
->url($event->url()),
])
->toArray();
// Calendar
$calendar = Calendar::create()
->timezone(['America/New_York']);
if ($request->validOrganizations()->count() === 1) {
$organization = $request->validOrganizations()->first();
$calendar
->productIdentifier($organization->title . ' Event Calendar')
->name($organization->title . ' Event Calendar')
->description($organization->description ?? '')
->source(route('orgs.show', $organization));
} else {
$calendar
->productIdentifier('HackGreenville.com Event Calendar')
->name('HackGreenville Event Calendar')
->description('Tech Events in Greenville SC!')
->source(route('calendar.index'));
}
$data = $calendar->event($events)->get();
return response($data)
->header('Content-Type', 'text/calendar; charset=utf-8');
}
}