engelsystem/includes/pages/user_ical.php

73 lines
2.2 KiB
PHP
Raw Normal View History

2011-10-11 19:47:49 +02:00
<?php
use Engelsystem\Http\Exceptions\HttpForbidden;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
use Illuminate\Support\Collection;
/**
* Controller for ical output of users own shifts or any user_shifts filter.
*/
2017-01-02 03:57:23 +01:00
function user_ical()
{
$request = request();
$user = auth()->apiUser('key');
2017-01-02 15:43:36 +01:00
if (
!$request->has('key')
2022-12-21 11:42:55 +01:00
|| !$request->input('key')
|| !$user
) {
throw new HttpForbidden('Missing or invalid key', ['content-type' => 'text/text']);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if (!auth()->can('ical')) {
throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$ical_shifts = load_ical_shifts();
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
send_ical_from_shifts($ical_shifts);
}
2011-10-11 19:47:49 +02:00
/**
2017-12-25 21:29:00 +01:00
* Renders an ical calendar from given shifts array.
*
2023-01-03 22:19:03 +01:00
* @param Shift[]|Collection $shifts Shift
*/
2017-01-02 03:57:23 +01:00
function send_ical_from_shifts($shifts)
{
2017-01-03 14:12:17 +01:00
header('Content-Type: text/calendar; charset=utf-8');
2017-01-03 15:32:12 +01:00
header('Content-Disposition: attachment; filename=shifts.ics');
$output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//" . config('app_name') . "//DE\r\nCALSCALE:GREGORIAN\r\n";
2017-01-02 03:57:23 +01:00
foreach ($shifts as $shift) {
$output .= make_ical_entry_from_shift($shift);
}
$output .= "END:VCALENDAR\r\n";
$output = trim($output, "\x0A");
2017-01-03 14:12:17 +01:00
header('Content-Length: ' . strlen($output));
2017-01-02 03:57:23 +01:00
raw_output($output);
}
/**
* Renders an ical vevent from given shift.
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function make_ical_entry_from_shift(Shift $shift)
2017-01-02 03:57:23 +01:00
{
$output = "BEGIN:VEVENT\r\n";
2023-01-03 22:19:03 +01:00
$output .= 'UID:' . md5($shift->start->timestamp . $shift->end->timestamp . $shift->shiftType->name) . "\r\n";
$output .= 'SUMMARY:' . str_replace("\n", "\\n", $shift->shiftType->name)
. ' (' . str_replace("\n", "\\n", $shift->title) . ")\r\n";
2023-01-18 13:02:11 +01:00
if (isset($shift->user_comment)) {
$output .= 'DESCRIPTION:' . str_replace("\n", "\\n", $shift->user_comment) . "\r\n";
2017-01-02 03:57:23 +01:00
}
2023-01-03 22:19:03 +01:00
$output .= 'DTSTAMP:' . $shift->start->utc()->format('Ymd\THis\Z') . "\r\n";
$output .= 'DTSTART:' . $shift->start->utc()->format('Ymd\THis\Z') . "\r\n";
$output .= 'DTEND:' . $shift->end->utc()->format('Ymd\THis\Z') . "\r\n";
$output .= 'LOCATION:' . $shift->room->name . "\r\n";
2017-01-02 03:57:23 +01:00
$output .= "END:VEVENT\r\n";
return $output;
2011-10-11 19:47:49 +02:00
}