Loheide-eu-mitarbeiterplatt.../app/Support/Calendar.php
Friederich Loheide a36752e09a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m8s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m12s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m5s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m6s
CI / Build-Artefakte aktuell (pull_request) Failing after 43s
Neuigkeiten und Termine (vorgezogen)
- Neuigkeiten mit Kategorien, Titelbild (WebP-Varianten), Lesezeit, geplanter
  Veröffentlichung und Hervorhebung; Text wird beim Speichern bereinigt
- Termine mit Ort, Online-Link, ganztägigen Terminen und .ics-Export
- Startseite nach Intranet-Vorbild: Slider, Neuigkeiten, Termine, Aktuelles
  aus den Bereichen; mobil in sinnvoller Reihenfolge
- Admin: Beiträge, Kategorien und Termine
- Bildverarbeitung für Shared Hosting: nur eine Vollbild-Kopie im Speicher,
  Speicherlimit bei Bedarf auf 256 MB angehoben (wie WordPress)
- Demo-Inhalte für die Entwicklung

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 05:16:27 +00:00

86 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support;
use App\Models\Event;
use Illuminate\Support\Str;
/**
* iCalendar-Export (RFC 5545) für einzelne Termine – zum Übernehmen in Outlook & Co.
*/
class Calendar
{
public function toIcs(Event $event, string $organizer): string
{
$end = $event->ends_at ?? ($event->all_day ? $event->starts_at : $event->starts_at->copy()->addHour());
$lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//'.$this->escape($organizer).'//Inhaus//DE',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'BEGIN:VEVENT',
'UID:event-'.$event->id.'@'.(parse_url(config('app.url'), PHP_URL_HOST) ?: 'inhaus'),
'DTSTAMP:'.now()->utc()->format('Ymd\THis\Z'),
];
if ($event->all_day) {
$lines[] = 'DTSTART;VALUE=DATE:'.$event->starts_at->format('Ymd');
// DTEND ist bei ganztägigen Terminen exklusiv: der Tag nach dem letzten Tag.
$lines[] = 'DTEND;VALUE=DATE:'.$end->copy()->startOfDay()->addDay()->format('Ymd');
} else {
$lines[] = 'DTSTART:'.$event->starts_at->copy()->utc()->format('Ymd\THis\Z');
$lines[] = 'DTEND:'.$end->copy()->utc()->format('Ymd\THis\Z');
}
$lines[] = 'SUMMARY:'.$this->escape($event->title);
if ($event->location) {
$lines[] = 'LOCATION:'.$this->escape($event->location);
}
$description = trim(html_entity_decode(strip_tags(str_replace(['</p>', '<br>'], "\n", (string) $event->description_html))));
if ($description !== '') {
$lines[] = 'DESCRIPTION:'.$this->escape(Str::limit($description, 2000));
}
if ($event->online_url) {
$lines[] = 'URL:'.$this->escape($event->online_url);
}
$lines[] = 'END:VEVENT';
$lines[] = 'END:VCALENDAR';
return implode("\r\n", array_map($this->fold(...), $lines))."\r\n";
}
private function escape(string $value): string
{
return str_replace(['\\', ';', ',', "\r\n", "\n"], ['\\\\', '\;', '\\,', '\\n', '\\n'], $value);
}
/**
* Zeilen nach 75 Oktetts umbrechen (Folgezeilen beginnen mit einem Leerzeichen,
* das mitzählt), ohne UTF-8-Zeichen zu zerschneiden.
*/
private function fold(string $line): string
{
$folded = '';
$current = '';
$limit = 75;
foreach (mb_str_split($line) as $char) {
if (strlen($current.$char) > $limit) {
$folded .= $current."\r\n ";
$current = '';
$limit = 74;
}
$current .= $char;
}
return $folded.$current;
}
}