Visuelle iCal-Regelengine mit Originaldaten und individuellen Exportfeldern integrieren
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m37s

This commit is contained in:
Lars committed 2026-10-27 19:00:25 +01:00
1 parent c4ebccbbea
commit e9db98701b
30 files changed
+1269 -15

No files matched your search

+35 -4
View File
@@ -12,6 +12,12 @@ class ICalParser
{
/** @return list<ExternalPeriod> */
public function parse(string $ics): array
{
return array_values(array_filter(array_column($this->parseDocument($ics)['events'], 'period')));
}
/** @return array{events: list<array>, calendar: array} */
public function parseDocument(string $ics): array
{
if (str_starts_with($ics, "\xEF\xBB\xBF")) {
$ics = substr($ics, 3);
@@ -25,11 +31,17 @@ public function parse(string $ics): array
$calendar = [];
$event = [];
$periods = [];
$properties = [];
$eventLines = [];
$identities = [];
$propertyIdentities = [];
$started = false;
$closed = false;
foreach ($lines as $line) {
$line = rtrim($line, "\r");
if (in_array('VEVENT', $stack, true)) {
$eventLines[] = $line;
}
if ($line === '' && $stack === []) {
continue;
}
@@ -57,6 +69,8 @@ public function parse(string $ics): array
}
if ($component === 'VEVENT') {
$event = [];
$properties = [];
$eventLines = [$line];
}
$stack[] = $component;
@@ -76,8 +90,16 @@ public function parse(string $ics): array
if (isset($identities[$uid]) && $identities[$uid] !== $identity) {
throw new ChannelSyncException('Eine UID enthält widersprüchliche Ereignisse; bitte den Portalkalender prüfen.');
}
if (! isset($identities[$uid]) && $period !== null) {
$periods[] = $period;
$normalized = $properties;
ksort($normalized);
if (isset($propertyIdentities[$uid]) && $propertyIdentities[$uid] !== $normalized) {
throw new ChannelSyncException('Eine UID enthält widersprüchliche Originalangaben; bitte den Kalender prüfen.');
}
$propertyIdentities[$uid] = $normalized;
if (! isset($identities[$uid])) {
$raw = implode("\r\n", $eventLines)."\r\n";
$enriched = $period === null ? null : new ExternalPeriod($period->uid, $period->startsOn, $period->endsOn, $period->summary, $properties, $raw);
$periods[] = ['uid' => $uid, 'properties' => $properties, 'raw' => $raw, 'period' => $enriched];
}
$identities[$uid] = $identity;
}
@@ -103,6 +125,10 @@ public function parse(string $ics): array
}
$event[$name] = $property;
}
if (in_array('VEVENT', $stack, true)) {
$prefix = count($stack) > 2 ? implode('.', array_slice($stack, 2)).'.' : '';
$properties[$prefix.$name][] = $property;
}
}
if (! $closed || $stack !== [] || ($calendar['VERSION']['value'] ?? '') !== '2.0' || blank($calendar['PRODID']['value'] ?? null)) {
throw new ChannelSyncException('Kalender ist unvollständig; VERSION, PRODID und geschlossene Komponenten sind erforderlich.');
@@ -111,7 +137,12 @@ public function parse(string $ics): array
throw new ChannelSyncException('Erwartet wird ein vollständiger Kalenderstand, keine einzelne Kalendernachricht.');
}
return $periods;
return ['events' => $periods, 'calendar' => $calendar];
}
public static function text(string $value): string
{
return preg_replace_callback('/\\\\([nN,;\\\\])/', fn (array $match): string => in_array($match[1], ['n', 'N'], true) ? "\n" : $match[1], $value);
}
/** @param array<string, array{value: string, params: list<string>}> $event */
@@ -152,7 +183,7 @@ private function toPeriod(array $event): ?ExternalPeriod
}
$summary = $event['SUMMARY']['value'] ?? null;
return new ExternalPeriod($event['UID']['value'], $start, $end, $summary === null ? null : str_replace(['\\n', '\\N', '\\,', '\\;', '\\\\'], ["\n", "\n", ',', ';', '\\'], $summary));
return new ExternalPeriod($event['UID']['value'], $start, $end, $summary === null ? null : self::text($summary));
}
/** @param array{value: string, params: list<string>} $property */