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';
/*
* Form value separator between calendar key and URI.
*/
private const KEY_SEPARATOR = '::';
/*
* 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) {
if ($calendar->getKey() === $calendarId) {
if ($calendar->getKey() === $key
&& $calendar->getUri() === $uri) {
return $calendar;
}
}
@@ -252,6 +268,14 @@ class CalendarController extends Controller
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
* CALENDAR_NAME if present, otherwise the first one.
@@ -260,12 +284,12 @@ class CalendarController extends Controller
{
foreach ($calendars as $calendar) {
if ($calendar->getDisplayName() === self::CALENDAR_NAME) {
return $calendar->getKey();
return $this->calendarValue($calendar);
}
}
if (isset($calendars[0])) {
return $calendars[0]->getKey();
return $this->calendarValue($calendars[0]);
}
return '';
@@ -313,9 +337,9 @@ class CalendarController extends Controller
'error' => $error,
'l10n' => $this->l10n,
'calendars' => array_map(
static function (ICreateFromString $calendar): array {
function (ICreateFromString $calendar): array {
return [
'id' => $calendar->getKey(),
'value' => $this->calendarValue($calendar),
'displayName' => $calendar->getDisplayName() ?? '',
];
},