Files
fewo-stommer/app/Channels/ICal/ICalFeed.php
T
Lars 783068501c
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
Generische iCal-Kanäle mit Bestandsklärung und sicheren Exporten bereitstellen
2026-10-27 17:14:45 +01:00

100 lines
3.7 KiB
PHP

<?php
namespace App\Channels\ICal;
use App\Enums\ChannelExportMode;
use App\Models\Booking;
use App\Models\Channel;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
/**
* The calendar a portal imports from us: every occupation of the property except the ones that
* came from this very portal (otherwise the portal would block its own bookings twice).
* Events carry no guest data.
*/
class ICalFeed
{
public function build(Channel $channel): string
{
$property = $channel->property;
$from = CarbonImmutable::today()->subDays(30);
$until = CarbonImmutable::today()->addYears(2);
$suffix = config('channels.uid_suffix');
$events = [];
$bookings = $property->bookings()->occupying()->overlapping($from, $until);
if ($channel->export_mode === ChannelExportMode::Test) {
$bookings->whereNull('import_channel_id')->whereNull('origin_channel_id')->whereIn('source', Booking::DIRECT_SOURCES);
} else {
$bookings->where(fn ($query) => $query->whereNull('import_channel_id')->orWhere('import_channel_id', '!=', $channel->id))
->where(fn ($query) => $query->whereNull('origin_channel_id')->orWhere('origin_channel_id', '!=', $channel->id));
}
foreach ($bookings->orderBy('id')->get() as $booking) {
/** @var Booking $booking */
$events[] = $this->event("booking-{$booking->id}{$suffix}", $booking->arrival, $booking->departure, $booking->updated_at);
}
$blocks = $property->blockedPeriods()
->occupying()->where('export_approved', true)
->overlapping($from, $until)
->where(fn ($query) => $query->whereNull('channel_id')->orWhere('channel_id', '!=', $channel->id))
->where(fn ($query) => $query->whereNull('origin_channel_id')->orWhere('origin_channel_id', '!=', $channel->id));
if ($channel->export_mode === ChannelExportMode::Test) {
$blocks->whereNull('channel_id')->whereNull('origin_channel_id')->where('source', 'manual');
}
foreach ($blocks->orderBy('id')->get() as $block) {
$events[] = $this->event("block-{$block->id}{$suffix}", $block->starts_on, $block->ends_on, $block->updated_at);
}
$lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//Ferienwohnung Stommer//Belegung//DE',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'X-WR-CALNAME:'.$this->escape($property->name),
...$events,
'END:VCALENDAR',
];
return implode("\r\n", array_map(fn (string $line): string => $this->fold($line), explode("\r\n", implode("\r\n", $lines))))."\r\n";
}
private function event(string $uid, CarbonInterface $start, CarbonInterface $end, ?CarbonInterface $modified): string
{
return implode("\r\n", [
'BEGIN:VEVENT',
"UID:{$uid}",
'DTSTAMP:'.($modified ?? now())->utc()->format('Ymd\THis\Z'),
'DTSTART;VALUE=DATE:'.$start->format('Ymd'),
'DTEND;VALUE=DATE:'.$end->format('Ymd'),
'SUMMARY:Belegt',
'TRANSP:OPAQUE',
'END:VEVENT',
]);
}
private function escape(string $text): string
{
return str_replace(['\\', ';', ',', "\n"], ['\\\\', '\\;', '\\,', '\\n'], str_replace(["\r\n", "\r"], "\n", $text));
}
private function fold(string $line): string
{
$parts = [];
$limit = 75;
while (strlen($line) > $limit) {
$part = mb_strcut($line, 0, $limit, 'UTF-8');
$parts[] = $part;
$line = substr($line, strlen($part));
$limit = 74;
}
$parts[] = $line;
return implode("\r\n ", $parts);
}
}