Calendar fixes
This commit is contained in:
@@ -116,29 +116,33 @@ class CalendarService
|
||||
*
|
||||
* @param CalendarSlot $maintenanceSlot
|
||||
* @param string $todayIso YYYY-MM-DD
|
||||
* @param int $pastDays Maximum number of overdue days to include
|
||||
* @return CalendarEvent[] Sorted: overdue first, then today, then by title
|
||||
*/
|
||||
public static function openMaintenanceTasks(CalendarSlot $maintenanceSlot, string $todayIso): array
|
||||
public static function openMaintenanceTasks(CalendarSlot $maintenanceSlot, string $todayIso, int $pastDays = 30): array
|
||||
{
|
||||
if (!$maintenanceSlot->isEnabled()) return [];
|
||||
if (!ChronoID::isIsoDate($todayIso)) return [];
|
||||
|
||||
$pastDays = max(0, $pastDays);
|
||||
|
||||
$file = $maintenanceSlot->getFile();
|
||||
if ($file === '' || !is_file($file) || !is_readable($file)) return [];
|
||||
|
||||
$cacheKey = $maintenanceSlot->getKey() . '|tasks|' . $todayIso;
|
||||
$cacheKey = $maintenanceSlot->getKey() . '|tasks|' . $todayIso . '|' . $pastDays;
|
||||
if (isset(self::$taskCache[$cacheKey])) {
|
||||
return self::$taskCache[$cacheKey];
|
||||
}
|
||||
|
||||
$tasks = self::parseAllTasksFromFile($file, $maintenanceSlot->getKey(), $todayIso);
|
||||
$oldestOpenDateIso = self::oldestOpenTaskDate($todayIso, $pastDays);
|
||||
|
||||
// Filter: only non-completed, due today or earlier
|
||||
// Filter: only non-completed, due today or earlier, and not older than the configured overdue window.
|
||||
$open = [];
|
||||
foreach ($tasks as $task) {
|
||||
if ($task->isCompleted()) continue;
|
||||
// dateIso is the date the task falls on
|
||||
if ($task->dateIso > $todayIso) continue;
|
||||
if ($task->dateIso < $oldestOpenDateIso) continue;
|
||||
$open[] = $task;
|
||||
}
|
||||
|
||||
@@ -162,6 +166,26 @@ class CalendarService
|
||||
return $open;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $todayIso
|
||||
* @param int $pastDays
|
||||
* @return string
|
||||
*/
|
||||
protected static function oldestOpenTaskDate(string $todayIso, int $pastDays): string
|
||||
{
|
||||
try {
|
||||
$today = new DateTimeImmutable($todayIso . ' 00:00:00', new DateTimeZone('UTC'));
|
||||
} catch (Throwable $e) {
|
||||
return $todayIso;
|
||||
}
|
||||
|
||||
if ($pastDays === 0) {
|
||||
return $today->format('Y-m-d');
|
||||
}
|
||||
|
||||
return $today->sub(new DateInterval('P' . $pastDays . 'D'))->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get slot-level day indicator data for a whole month.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user