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->with(['guest', 'property'])->orderBy('id')->get() as $booking) { /** @var Booking $booking */ $events[] = $this->event("booking-{$booking->id}{$suffix}", $booking->arrival, $booking->departure, $booking->updated_at, (new ICalExportFields)->properties($channel, $booking)); } $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->with(['property', 'channel', 'originChannel'])->orderBy('id')->get() as $block) { $events[] = $this->event("block-{$block->id}{$suffix}", $block->starts_on, $block->ends_on, $block->updated_at, (new ICalExportFields)->properties($channel, $block)); } $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, array $properties): 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'), ...array_map(fn (string $field, string $value): string => $field.':'.$this->escape($value), array_keys($properties), array_values($properties)), '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); } }