-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathICalendarController.php
More file actions
41 lines (34 loc) · 1.28 KB
/
ICalendarController.php
File metadata and controls
41 lines (34 loc) · 1.28 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
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\CalendarToken;
use Illuminate\Http\Request;
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event;
class ICalendarController extends Controller
{
public function show(
Request $request,
string $token
): \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory {
$calendar_token = CalendarToken::getToken($token);
$cal = Calendar::create($calendar_token->name);
$calendar_token
->choreInstances()
->with('chore')
->each(function ($chore_instance) use ($cal) {
$cal->event(
Event::create($chore_instance->chore->title)
->startsAt($chore_instance->due_date)
->endsAt($chore_instance->due_date)
->fullDay()
);
});
$filename = preg_replace("/[^a-z0-9\.]/", '', strtolower($calendar_token->name));
return response($cal->get(), 200, [
'Content-Type' => 'text/calendar',
'Content-Disposition' => "attachment; filename=\"{$filename}.ics\"",
'charset' => 'utf-8',
]);
}
}