From 99574aa6b5e616cdbf8f876c5a138ba32a8e9d02 Mon Sep 17 00:00:00 2001 From: cgoIT Date: Thu, 11 Jul 2024 07:22:07 +0200 Subject: [PATCH] fix: the start time and end time of the "repeat exceptions" are displayed correctly in the frontend fixes #15 --- src/Classes/Utils.php | 2 +- src/Controller/Module/ModuleFullCalendar.php | 2 +- src/Controller/Module/ModuleTimeTable.php | 2 +- src/Controller/Module/ModuleYearView.php | 2 +- .../DataContainer/CalendarEventsCallbacks.php | 2 ++ .../CalendarEventsMCWCallbacks.php | 9 +++++++ .../DataContainer/ModuleCallbacks.php | 8 +++--- src/Hook/GetAllEventsHook.php | 26 +++++++++++++++---- src/Hook/ParseFrontendTemplateHook.php | 14 +++++----- src/Migration/RenameFieldsMigration.php | 3 +++ src/Models/CalendarEventsModelExt.php | 2 +- 11 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/Classes/Utils.php b/src/Classes/Utils.php index c3823cd..42107d4 100644 --- a/src/Classes/Utils.php +++ b/src/Classes/Utils.php @@ -148,7 +148,7 @@ public static function appendToArrayKey(&$arr, mixed $key, mixed $val): void * If the input array has more than 4 dates, it will only include the first 4 dates and append the count of other dates. * Otherwise, it will implode all the dates with a comma separator and append the last date with an "and" label. * - * @param array|null $arrFixedDates Array of fixed dates + * @param array|null $arrFixedDates Array of fixed dates * * @return string The imploded string of fixed dates */ diff --git a/src/Controller/Module/ModuleFullCalendar.php b/src/Controller/Module/ModuleFullCalendar.php index df85513..6de1078 100644 --- a/src/Controller/Module/ModuleFullCalendar.php +++ b/src/Controller/Module/ModuleFullCalendar.php @@ -203,7 +203,7 @@ protected function compile(): void $objTemplate->firstDay = $this->cal_startDay; if (!empty($this->businessHours)) { - $arrDays = array_map('\intval', StringUtil::deserialize($this->businessDays, true)); + $arrDays = array_map(\intval(...), StringUtil::deserialize($this->businessDays, true)); $businessHours = new \stdClass(); $businessHours->daysOfWeek = $arrDays; diff --git a/src/Controller/Module/ModuleTimeTable.php b/src/Controller/Module/ModuleTimeTable.php index a5907fd..c8d51b1 100644 --- a/src/Controller/Module/ModuleTimeTable.php +++ b/src/Controller/Module/ModuleTimeTable.php @@ -153,7 +153,7 @@ protected function compile(): void // Find the boundaries $blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode(); - $objMinMax = $this->Database->query('SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN('.implode(',', array_map('\intval', $this->cal_calendar)).')'.(!$blnShowUnpublished ? " AND published='1' AND (start='' OR start<=$time) AND (stop='' OR stop>$time)" : '')); + $objMinMax = $this->Database->query('SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN('.implode(',', array_map(\intval(...), $this->cal_calendar)).')'.(!$blnShowUnpublished ? " AND published='1' AND (start='' OR start<=$time) AND (stop='' OR stop>$time)" : '')); $dateFrom = $objMinMax->dateFrom; $dateTo = $objMinMax->dateTo; $repeatUntil = $objMinMax->repeatUntil; diff --git a/src/Controller/Module/ModuleYearView.php b/src/Controller/Module/ModuleYearView.php index efecc76..f3e9efe 100644 --- a/src/Controller/Module/ModuleYearView.php +++ b/src/Controller/Module/ModuleYearView.php @@ -132,7 +132,7 @@ protected function compile(): void // Find the boundaries $blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode(); - $objMinMax = $this->Database->query('SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN('.implode(',', array_map('\intval', $this->cal_calendar)).')'.(!$blnShowUnpublished ? " AND published='1' AND (start='' OR start<=$time) AND (stop='' OR stop>$time)" : '')); + $objMinMax = $this->Database->query('SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN('.implode(',', array_map(\intval(...), $this->cal_calendar)).')'.(!$blnShowUnpublished ? " AND published='1' AND (start='' OR start<=$time) AND (stop='' OR stop>$time)" : '')); $dateFrom = $objMinMax->dateFrom; $dateTo = $objMinMax->dateTo; $repeatUntil = $objMinMax->repeatUntil; diff --git a/src/EventListener/DataContainer/CalendarEventsCallbacks.php b/src/EventListener/DataContainer/CalendarEventsCallbacks.php index 770cde0..623268e 100644 --- a/src/EventListener/DataContainer/CalendarEventsCallbacks.php +++ b/src/EventListener/DataContainer/CalendarEventsCallbacks.php @@ -702,6 +702,8 @@ private function checkExceptionsByDate(Result|\stdClass $activeRecord, int $curr /** * Get the recurrence array. + * + * @return array */ private function getRecurrenceArray(int|false $start, int|false $end, string|null $moveReason = null): array { diff --git a/src/EventListener/DataContainer/CalendarEventsMCWCallbacks.php b/src/EventListener/DataContainer/CalendarEventsMCWCallbacks.php index 2937e2c..40af9e5 100644 --- a/src/EventListener/DataContainer/CalendarEventsMCWCallbacks.php +++ b/src/EventListener/DataContainer/CalendarEventsMCWCallbacks.php @@ -25,6 +25,15 @@ class CalendarEventsMCWCallbacks extends Backend { + /** + * __construct(). + * + * Constructor for the class. + * + * @param Connection $db The database connection object + * @param int $exceptionsMoveDays The number of days to move exceptions + * @param array $exceptionsMoveTimes The array of exception move times + */ public function __construct( private readonly Connection $db, private readonly int $exceptionsMoveDays, diff --git a/src/EventListener/DataContainer/ModuleCallbacks.php b/src/EventListener/DataContainer/ModuleCallbacks.php index ef61b48..f45346a 100644 --- a/src/EventListener/DataContainer/ModuleCallbacks.php +++ b/src/EventListener/DataContainer/ModuleCallbacks.php @@ -32,10 +32,10 @@ public function __construct( } /** - * @return array + * @return array */ #[AsCallback(table: 'tl_module', target: 'fields.filter_fields.options')] - public function getEventField() + public function getEventField(): array { // Load tl_calendar_events data $this->loadDataContainer('tl_calendar_events'); @@ -69,9 +69,9 @@ public function getEventField() } /** - * @return array|null + * @return array|null */ - public function getTimeRange() + public function getTimeRange(): array|null { return [ 'time_from' => [ diff --git a/src/Hook/GetAllEventsHook.php b/src/Hook/GetAllEventsHook.php index dd4df5d..395a51d 100644 --- a/src/Hook/GetAllEventsHook.php +++ b/src/Hook/GetAllEventsHook.php @@ -40,7 +40,12 @@ class GetAllEventsHook private int|false $intTodayEnd = false; /** + * @param array $arrEvents + * @param array $arrCalendars + * * @return array + * + * @throws \Exception */ public function __invoke(array $arrEvents, array $arrCalendars, int $timeStart, int $timeEnd, Events $objModule): array { @@ -146,9 +151,17 @@ private function handleExtendedRecurrences(array $arrEvents, array $arrCalendars } /** - * @param array $arrEvents + * Handle fixed dates with recurrences. * - * @return array + * @param array $arrEvents Array of events + * @param array $arrCalendars Array of calendar IDs + * @param int $timeStart Start time + * @param int $timeEnd End time + * @param Events $objModule Events module object + * + * @return array Array of events with fixed dates recurrences + * + * @throws \Exception */ private function handleFixedDatesRecurrences(array $arrEvents, array $arrCalendars, int $timeStart, int $timeEnd, Events $objModule): array { @@ -173,13 +186,16 @@ private function handleFixedDatesRecurrences(array $arrEvents, array $arrCalenda if (!empty($arrFixedDates) && \is_array($arrFixedDates)) { foreach ($arrFixedDates as $date) { $intStart = $date['new_repeat']; + $intEnd = $intStart; if (!empty($date['new_start']) && $objEvent->addTime) { - $objEvent->startTime = strtotime(Date::parse($objPage->dateFormat, $intStart).' '.$date['new_start']); + $objEvent->startTime = strtotime(Date::parse($objPage->dateFormat, $date['new_repeat']).' '.date('H:i', $date['new_start'])); + $intStart = $objEvent->startTime; + $intEnd = $intStart; } if (!empty($date['new_end']) && $objEvent->addTime) { - $objEvent->startTime = strtotime(Date::parse($objPage->dateFormat, $intStart).' '.$date['new_end']); + $objEvent->endTime = strtotime(Date::parse($objPage->dateFormat, $date['new_repeat']).' '.date('H:i', $date['new_end'])); + $intEnd = $objEvent->endTime; } - $intEnd = $intStart; if ($intStart >= $timeStart && $intStart <= $timeEnd) { $key = Date::parse('Ymd', $intStart); diff --git a/src/Hook/ParseFrontendTemplateHook.php b/src/Hook/ParseFrontendTemplateHook.php index 0fd699e..d2c8481 100644 --- a/src/Hook/ParseFrontendTemplateHook.php +++ b/src/Hook/ParseFrontendTemplateHook.php @@ -86,7 +86,7 @@ private function processEventTemplate(CalendarEventsModel $objEvent, FrontendTem // Replace the date an time with the correct ones from the recurring event if (Input::get('times')) { - [$intStartTime, $intEndTime] = array_map('\intval', explode(',', Input::get('times'))); + [$intStartTime, $intEndTime] = array_map(\intval(...), explode(',', Input::get('times'))); } else { // Do not show dates in the past if the event is recurring (see #923) if (!empty($objEvent->allRecurrences)) { @@ -260,13 +260,15 @@ private function processEventTemplate(CalendarEventsModel $objEvent, FrontendTem } /** - * Return the date and time strings. + * Get the date and time based on the provided parameters. * - * @param int $intStartTime - * @param int $intEndTime - * @param int $span + * @param CalendarEventsModel $objEvent the event model + * @param PageModel $objPage the page model + * @param int $intStartTime the start time + * @param int $intEndTime the end time + * @param int $span the span value * - * @return array + * @return array the array containing the date and time */ private function getDateAndTime(CalendarEventsModel $objEvent, PageModel $objPage, $intStartTime, $intEndTime, $span) { diff --git a/src/Migration/RenameFieldsMigration.php b/src/Migration/RenameFieldsMigration.php index def9b41..c24181b 100644 --- a/src/Migration/RenameFieldsMigration.php +++ b/src/Migration/RenameFieldsMigration.php @@ -21,6 +21,9 @@ class RenameFieldsMigration extends AbstractMigration { + /** + * @var array + */ private static array $arrColumns = [ 'show_holiday' => 'hide_holiday', 'cellhight' => 'cellheight', diff --git a/src/Models/CalendarEventsModelExt.php b/src/Models/CalendarEventsModelExt.php index 2577942..9cb66a7 100644 --- a/src/Models/CalendarEventsModelExt.php +++ b/src/Models/CalendarEventsModelExt.php @@ -80,7 +80,7 @@ public static function findUpcomingByPids($arrIds, $intLimit = 0, array $arrOpti $time = Date::floorToMinute(); // Get upcoming events using endTime instead of startTime (see #3917) - $arrColumns = ["$t.pid IN(".implode(',', array_map('\intval', $arrIds)).") AND $t.published=1 AND ($t.start='' OR $t.start<=$time) AND ($t.stop='' OR $t.stop>$time) AND ($t.endTime>=$time OR (($t.recurringExt=1 OR $t.recurring=1) AND ($t.recurrences=0 OR $t.repeatEnd>=$time)))"]; + $arrColumns = ["$t.pid IN(".implode(',', array_map(\intval(...), $arrIds)).") AND $t.published=1 AND ($t.start='' OR $t.start<=$time) AND ($t.stop='' OR $t.stop>$time) AND ($t.endTime>=$time OR (($t.recurringExt=1 OR $t.recurring=1) AND ($t.recurrences=0 OR $t.repeatEnd>=$time)))"]; if ($intLimit > 0) { $arrOptions['limit'] = $intLimit;