@@ -37,10 +37,7 @@ public function fetchAllForCalendarHome(string $principalUri): array {
3737 return [];
3838 }
3939
40- $ configService = $ this ->configService ;
41- $ boards = array_values (array_filter ($ this ->backend ->getBoards (), function ($ board ) use ($ configService ) {
42- return $ configService ->isCalendarEnabled ($ board ->getId ());
43- }));
40+ $ boards = array_values ($ this ->getEnabledBoardsById ());
4441
4542 if ($ this ->configService ->getCalDavListMode () === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR ) {
4643 $ calendars = [];
@@ -61,38 +58,53 @@ public function hasCalendarInCalendarHome(string $principalUri, string $calendar
6158 if (!$ this ->calendarIntegrationEnabled ) {
6259 return false ;
6360 }
64- $ normalizedCalendarUri = $ this ->normalizeCalendarUri ($ calendarUri );
6561
66- if ($ this ->configService ->getCalDavListMode () === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR ) {
67- foreach ($ this ->backend ->getBoards () as $ board ) {
68- foreach ($ this ->backend ->getStacks ($ board ->getId ()) as $ stack ) {
69- if ($ normalizedCalendarUri === 'stack- ' . $ stack ->getId ()) {
70- return true ;
71- }
72- }
73- }
74- return false ;
75- }
76-
77- $ boards = array_map (static fn (Board $ board ): string => 'board- ' . $ board ->getId (), $ this ->backend ->getBoards ());
78- return in_array ($ normalizedCalendarUri , $ boards , true );
62+ return $ this ->resolveCalendar ($ principalUri , $ calendarUri ) !== null ;
7963 }
8064
8165 public function getCalendarInCalendarHome (string $ principalUri , string $ calendarUri ): ?ExternalCalendar {
8266 if (!$ this ->calendarIntegrationEnabled ) {
8367 return null ;
8468 }
69+
70+ return $ this ->resolveCalendar ($ principalUri , $ calendarUri );
71+ }
72+
73+ /**
74+ * @return array<int, Board>
75+ */
76+ private function getEnabledBoardsById (): array {
77+ $ boards = [];
78+ foreach ($ this ->backend ->getBoards () as $ board ) {
79+ if ($ this ->configService ->isCalendarEnabled ($ board ->getId ())) {
80+ $ boards [$ board ->getId ()] = $ board ;
81+ }
82+ }
83+
84+ return $ boards ;
85+ }
86+
87+ private function resolveCalendar (string $ principalUri , string $ calendarUri ): ?ExternalCalendar {
8588 $ normalizedCalendarUri = $ this ->normalizeCalendarUri ($ calendarUri );
89+ $ enabledBoardsById = $ this ->getEnabledBoardsById ();
90+ $ perListMode = $ this ->configService ->getCalDavListMode () === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR ;
8691
8792 try {
88- if (str_starts_with ($ normalizedCalendarUri , 'stack- ' )) {
93+ if ($ perListMode && str_starts_with ($ normalizedCalendarUri , 'stack- ' )) {
8994 $ stack = $ this ->backend ->getStack ((int )str_replace ('stack- ' , '' , $ normalizedCalendarUri ));
90- $ board = $ this ->backend ->getBoard ($ stack ->getBoardId ());
95+ $ board = $ enabledBoardsById [$ stack ->getBoardId ()] ?? null ;
96+ if ($ board === null ) {
97+ return null ;
98+ }
9199 return new Calendar ($ principalUri , $ normalizedCalendarUri , $ board , $ this ->backend , $ stack );
92100 }
93101
94- if (str_starts_with ($ normalizedCalendarUri , 'board- ' )) {
95- $ board = $ this ->backend ->getBoard ((int )str_replace ('board- ' , '' , $ normalizedCalendarUri ));
102+ if (!$ perListMode && str_starts_with ($ normalizedCalendarUri , 'board- ' )) {
103+ $ boardId = (int )str_replace ('board- ' , '' , $ normalizedCalendarUri );
104+ $ board = $ enabledBoardsById [$ boardId ] ?? null ;
105+ if ($ board === null ) {
106+ return null ;
107+ }
96108 return new Calendar ($ principalUri , $ normalizedCalendarUri , $ board , $ this ->backend );
97109 }
98110 } catch (NotFound $ e ) {
0 commit comments