calendar key uniqueness is now more robust

This commit is contained in:
stefan.tollkuehn committed 2026-10-08 13:44:55 +02:00
1 parent f759c2c8d5
commit ce0b6ccbc7
2 files changed
+33 -9

No files matched your search

+31 -7
View File
@@ -35,6 +35,11 @@ class CalendarController extends Controller
*/ */
private const CALENDAR_APP_ID = 'calendar'; private const CALENDAR_APP_ID = 'calendar';
/*
* Form value separator between calendar key and URI.
*/
private const KEY_SEPARATOR = '::';
/* /*
* Default reminder: 15 minutes before the event starts. * Default reminder: 15 minutes before the event starts.
*/ */
@@ -239,12 +244,23 @@ class CalendarController extends Controller
} }
/** /**
* The calendar with the given key, if it is writable. * The calendar with the given form value ("key::uri"),
* if it is writable. Keys alone are not unique across
* calendar providers, so the URI is matched too.
*/ */
private function selectCalendar(array $calendars, string $calendarId): ?ICreateFromString private function selectCalendar(array $calendars, string $value): ?ICreateFromString
{ {
$parts = explode(self::KEY_SEPARATOR, $value, 2);
if (!isset($parts[1])) {
return null;
}
[$key, $uri] = $parts;
foreach ($calendars as $calendar) { foreach ($calendars as $calendar) {
if ($calendar->getKey() === $calendarId) { if ($calendar->getKey() === $key
&& $calendar->getUri() === $uri) {
return $calendar; return $calendar;
} }
} }
@@ -252,6 +268,14 @@ class CalendarController extends Controller
return null; return null;
} }
/**
* Form value identifying a calendar: "key::uri".
*/
private function calendarValue(ICreateFromString $calendar): string
{
return $calendar->getKey() . self::KEY_SEPARATOR . $calendar->getUri();
}
/** /**
* Key of the calendar to preselect: the preferred * Key of the calendar to preselect: the preferred
* CALENDAR_NAME if present, otherwise the first one. * CALENDAR_NAME if present, otherwise the first one.
@@ -260,12 +284,12 @@ class CalendarController extends Controller
{ {
foreach ($calendars as $calendar) { foreach ($calendars as $calendar) {
if ($calendar->getDisplayName() === self::CALENDAR_NAME) { if ($calendar->getDisplayName() === self::CALENDAR_NAME) {
return $calendar->getKey(); return $this->calendarValue($calendar);
} }
} }
if (isset($calendars[0])) { if (isset($calendars[0])) {
return $calendars[0]->getKey(); return $this->calendarValue($calendars[0]);
} }
return ''; return '';
@@ -313,9 +337,9 @@ class CalendarController extends Controller
'error' => $error, 'error' => $error,
'l10n' => $this->l10n, 'l10n' => $this->l10n,
'calendars' => array_map( 'calendars' => array_map(
static function (ICreateFromString $calendar): array { function (ICreateFromString $calendar): array {
return [ return [
'id' => $calendar->getKey(), 'value' => $this->calendarValue($calendar),
'displayName' => $calendar->getDisplayName() ?? '', 'displayName' => $calendar->getDisplayName() ?? '',
]; ];
}, },
+2 -2
View File
@@ -119,8 +119,8 @@ $l = $_['l10n'];
<select id="atocal-calendar" name="calendarId"> <select id="atocal-calendar" name="calendarId">
<?php foreach ($_['calendars'] as $calendar): ?> <?php foreach ($_['calendars'] as $calendar): ?>
<option <option
value="<?= $h($calendar['id']) ?>" value="<?= $h($calendar['value']) ?>"
<?= $calendar['id'] === $_['calendarId'] ? 'selected' : '' ?> <?= $calendar['value'] === $_['calendarId'] ? 'selected' : '' ?>
> >
<?= $h($calendar['displayName']) ?> <?= $h($calendar['displayName']) ?>
</option> </option>