Aanpassing planner

This commit is contained in:
2026-09-12 11:04:02 +02:00
parent 5a5456ffe0
commit 09ab527a93
7 changed files with 666 additions and 26 deletions
+151 -1
View File
@@ -747,8 +747,23 @@ try {
} catch (Throwable $e) {}
}
// 3. Batch Calendar Blocks: haal verlof en geblokkeerde vakantiedagen op voor dit venster
$blockedDates = [];
try {
$stmtBlk = $db->prepare("SELECT start_date, end_date FROM calendar_blocks WHERE user_id = ? AND start_date <= ? AND end_date >= ?");
$stmtBlk->execute([$user['id'], $rangeEndDate, $rangeStartDate]);
while ($b = $stmtBlk->fetch()) {
$bStart = strtotime($b['start_date']);
$bEnd = strtotime($b['end_date']);
for ($d = $bStart; $d <= $bEnd; $d += 86400) {
$blockedDates[date('Y-m-d', $d)] = true;
}
}
} catch (Throwable $e) {}
while ($checkedDays < $maxDays) {
$dayOfWeek = (int)$checkDate->format('N'); // 1 = Ma, 7 = Zo
$dateStr = $checkDate->format('Y-m-d');
// Niet-werkdag / weekend overslaan op basis van organisatie-instellingen
if (!in_array($dayOfWeek, $activeWorkdays, true)) {
@@ -756,7 +771,11 @@ try {
continue;
}
$dateStr = $checkDate->format('Y-m-d');
// Geblokkeerde dagen (bijv. vakantie, verlof) overslaan
if (!empty($blockedDates[$dateStr])) {
$checkDate->modify('+1 day');
continue;
}
$isToday = ($dateStr === $now->format('Y-m-d')) && !$startFromNextWorkday;
// Bepaal vroegste startminuut voor deze dag (op hele kwartieren)
@@ -854,6 +873,137 @@ try {
exit;
}
// 12. Kalenderblokkades ophalen (Vakanties / Verlof / Afwezigheid)
if ($action === 'get_calendar_blocks') {
$stmt = $db->prepare("
SELECT * FROM calendar_blocks
WHERE user_id = ?
ORDER BY start_date DESC
LIMIT 100
");
$stmt->execute([$user['id']]);
$blocks = $stmt->fetchAll() ?: [];
echo json_encode(['success' => true, 'blocks' => $blocks]);
exit;
}
// 13. Kalenderblokkade opslaan (Vakantie / Verlof / Dag(en) blokkeren)
if ($action === 'save_calendar_block') {
$rawInput = json_decode(file_get_contents('php://input'), true);
$data = is_array($rawInput) ? $rawInput : $_POST;
$blockId = (int)($data['id'] ?? 0);
$title = trim($data['title'] ?? '');
$blockType = trim($data['block_type'] ?? 'VACATION');
$startDate = trim($data['start_date'] ?? $today);
$endDate = trim($data['end_date'] ?? $startDate);
$notes = trim($data['notes'] ?? '');
$syncToOutlook = !empty($data['sync_to_outlook']);
if (empty($title)) {
echo json_encode(['success' => false, 'error' => 'Geef een titel op voor de blokkade (bijv. Vakantie of Vrije dag)']);
exit;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $startDate) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate)) {
echo json_encode(['success' => false, 'error' => 'Ongeldige datumnotatie']);
exit;
}
if ($endDate < $startDate) {
$temp = $startDate;
$startDate = $endDate;
$endDate = $temp;
}
$outlookEventId = null;
// Indien bewerken en er bestond al een Outlook event, haal het event ID op
if ($blockId > 0) {
$stmtEx = $db->prepare("SELECT outlook_event_id FROM calendar_blocks WHERE id = ? AND user_id = ?");
$stmtEx->execute([$blockId, $user['id']]);
$outlookEventId = $stmtEx->fetchColumn() ?: null;
}
// Optioneel synchroniseren naar Outlook 365
if ($syncToOutlook && function_exists('microsoft_create_event')) {
try {
if (!empty($outlookEventId) && function_exists('microsoft_delete_event')) {
microsoft_delete_event($user['id'], $outlookEventId);
}
$eventDesc = !empty($notes) ? $notes : "Geblokkeerd via Fociz Agenda ({$blockType})";
$msRes = microsoft_create_event($user['id'], [
'subject' => $title,
'description' => $eventDesc,
'date' => $startDate,
'end_date' => $endDate,
'is_all_day' => true,
'show_as' => ($blockType === 'FOCUS') ? 'busy' : 'oof',
]);
if (!empty($msRes['success']) && !empty($msRes['event_id'])) {
$outlookEventId = $msRes['event_id'];
}
} catch (Throwable $e) {
error_log("Fout bij sync calendar_block naar Outlook: " . $e->getMessage());
}
}
if ($blockId > 0) {
$stmt = $db->prepare("
UPDATE calendar_blocks
SET title = ?, block_type = ?, start_date = ?, end_date = ?, notes = ?, outlook_event_id = ?, updated_at = NOW()
WHERE id = ? AND user_id = ?
");
$stmt->execute([$title, $blockType, $startDate, $endDate, $notes, $outlookEventId, $blockId, $user['id']]);
} else {
$stmt = $db->prepare("
INSERT INTO calendar_blocks (user_id, start_date, end_date, title, block_type, notes, outlook_event_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
");
$stmt->execute([$user['id'], $startDate, $endDate, $title, $blockType, $notes, $outlookEventId]);
$blockId = (int)$db->lastInsertId();
}
echo json_encode([
'success' => true,
'message' => 'Agenda-blokkade succesvol opgeslagen',
'block_id' => $blockId,
'start_date'=> $startDate,
'end_date' => $endDate,
'title' => $title
]);
exit;
}
// 14. Kalenderblokkade verwijderen / opheffen
if ($action === 'delete_calendar_block') {
$rawInput = json_decode(file_get_contents('php://input'), true);
$data = is_array($rawInput) ? $rawInput : $_POST;
$blockId = (int)($data['id'] ?? 0);
if ($blockId <= 0) {
echo json_encode(['success' => false, 'error' => 'Ongeldig blok ID']);
exit;
}
$stmtEx = $db->prepare("SELECT outlook_event_id FROM calendar_blocks WHERE id = ? AND user_id = ?");
$stmtEx->execute([$blockId, $user['id']]);
$outlookEventId = $stmtEx->fetchColumn();
if (!empty($outlookEventId) && function_exists('microsoft_delete_event')) {
try {
microsoft_delete_event($user['id'], $outlookEventId);
} catch (Throwable $e) {}
}
$stmtDel = $db->prepare("DELETE FROM calendar_blocks WHERE id = ? AND user_id = ?");
$stmtDel->execute([$blockId, $user['id']]);
echo json_encode(['success' => true, 'message' => 'Blokkade succesvol opgeheven']);
exit;
}
echo json_encode(['error' => 'Onbekende actie']);
} catch (Exception $e) {
http_response_code(500);