Large refactor

This commit is contained in:
2026-09-11 21:43:36 +02:00
parent 7fe7c0f51a
commit deba8463b8
11 changed files with 871 additions and 113 deletions
+52 -30
View File
@@ -704,6 +704,45 @@ try {
$maxDays = 14;
$checkedDays = 0;
// Bepaal het totale zoekvenster voor batch-ophalen (O(1) database & Graph API schaalbaarheid)
$rangeStartDate = $checkDate->format('Y-m-d');
$rangeEndObj = clone $checkDate;
$rangeEndObj->modify('+21 days'); // ruim venster incl. weekenden
$rangeEndDate = $rangeEndObj->format('Y-m-d');
// 1. Batch SQL: haal alle bestaande taken van de gebruiker in 1 geoptimaliseerde query op
$tasksByDate = [];
$stmtTasks = $db->prepare("
SELECT t.id, dp.plan_date, t.time_block_start, t.time_block_end, t.duration_minutes
FROM tasks t
INNER JOIN daily_plans dp ON t.daily_plan_id = dp.id
WHERE t.user_id = ?
AND dp.plan_date BETWEEN ? AND ?
AND t.time_block_start IS NOT NULL
AND t.time_block_start != ''
AND t.status != 'CANCELLED'
");
$stmtTasks->execute([$user['id'], $rangeStartDate, $rangeEndDate]);
while ($t = $stmtTasks->fetch()) {
if ($excludeTaskId > 0 && (int)$t['id'] === $excludeTaskId) {
continue;
}
$tasksByDate[$t['plan_date']][] = $t;
}
// 2. Batch Microsoft Graph: haal alle Outlook events in 1 API call op voor het gehele venster
$outlookByDate = [];
if (function_exists('microsoft_fetch_events_range')) {
try {
$allOutlookEvts = microsoft_fetch_events_range($user['id'], $rangeStartDate, $rangeEndDate, false);
foreach ($allOutlookEvts as $oe) {
if (!empty($oe['date'])) {
$outlookByDate[$oe['date']][] = $oe;
}
}
} catch (Throwable $e) {}
}
while ($checkedDays < $maxDays) {
$dayOfWeek = (int)$checkDate->format('N'); // 1 = Ma, 7 = Zo
@@ -738,22 +777,9 @@ try {
$busyIntervals[] = [$lunchStartMin, $lunchEndMin];
}
// Bestaande taken van de gebruiker op deze dag
$stmtTasks = $db->prepare("
SELECT t.id, t.time_block_start, t.time_block_end, t.duration_minutes
FROM tasks t
INNER JOIN daily_plans dp ON t.daily_plan_id = dp.id
WHERE t.user_id = ?
AND dp.plan_date = ?
AND t.time_block_start IS NOT NULL
AND t.time_block_start != ''
AND t.status != 'CANCELLED'
");
$stmtTasks->execute([$user['id'], $dateStr]);
while ($t = $stmtTasks->fetch()) {
if ($excludeTaskId > 0 && (int)$t['id'] === $excludeTaskId) {
continue;
}
// Bestaande taken voor deze datum uit het geheugen
$dayTasks = $tasksByDate[$dateStr] ?? [];
foreach ($dayTasks as $t) {
$tStart = $toMins($t['time_block_start']);
$tDur = (int)($t['duration_minutes'] ?: 30);
$tEnd = !empty($t['time_block_end']) ? $toMins($t['time_block_end']) : ($tStart + $tDur);
@@ -762,20 +788,16 @@ try {
}
}
// Outlook afspraken
if (function_exists('microsoft_fetch_events_range')) {
try {
$outlookEvts = microsoft_fetch_events_range($user['id'], $dateStr, $dateStr, false);
foreach ($outlookEvts as $oe) {
if (!empty($oe['is_all_day'])) continue;
if (empty($oe['start']) || empty($oe['end'])) continue;
$oStart = $toMins($oe['start']);
$oEnd = $toMins($oe['end']);
if ($oEnd > $oStart) {
$busyIntervals[] = [$oStart, $oEnd];
}
}
} catch (Throwable $e) {}
// Outlook afspraken voor deze datum uit het geheugen
$dayOutlook = $outlookByDate[$dateStr] ?? [];
foreach ($dayOutlook as $oe) {
if (!empty($oe['is_all_day'])) continue;
if (empty($oe['start']) || empty($oe['end'])) continue;
$oStart = $toMins($oe['start']);
$oEnd = $toMins($oe['end']);
if ($oEnd > $oStart) {
$busyIntervals[] = [$oStart, $oEnd];
}
}
// Doorzoek kwartierslots