Auto synch outlook

This commit is contained in:
2026-09-14 09:21:19 +02:00
parent 31caf540a3
commit 32d27cd457
5 changed files with 152 additions and 28 deletions
+1 -1
View File
@@ -391,7 +391,7 @@ try {
$resCreate = microsoft_create_event($assigneeId, $eventPayload); $resCreate = microsoft_create_event($assigneeId, $eventPayload);
if (!empty($resCreate['success'])) { if (!empty($resCreate['success'])) {
$outlookEventId = $resCreate['id'] ?? null; $outlookEventId = $resCreate['event_id'] ?? ($resCreate['id'] ?? null);
$outlookSyncedAt = date('Y-m-d H:i:s'); $outlookSyncedAt = date('Y-m-d H:i:s');
$outlookSyncMsg = 'Taak succesvol ingepland in de Outlook agenda van de collega!'; $outlookSyncMsg = 'Taak succesvol ingepland in de Outlook agenda van de collega!';
} else { } else {
+46 -4
View File
@@ -112,10 +112,50 @@ try {
$planId = (int)$db->lastInsertId(); $planId = (int)$db->lastInsertId();
} }
// Bereken eindtijd indien starttijd bekend is
if (!empty($timeBlockStart) && empty($timeBlockEnd)) {
try {
$dtEnd = new DateTime("{$planDate} {$timeBlockStart}:00", new DateTimeZone('Europe/Amsterdam'));
$dtEnd->modify("+{$durationMinutes} minutes");
$timeBlockEnd = $dtEnd->format('H:i');
} catch (Throwable $e) {}
}
$source = 'JIRA';
$externalId = $key;
$externalUrl = $url;
$outlookSyncMsg = null;
// Automatisch synchroniseren naar Microsoft Outlook indien gekoppeld
if (function_exists('microsoft_create_event')) {
try {
$syncStartTime = !empty($timeBlockStart) ? $timeBlockStart : '09:00';
$msResult = microsoft_create_event($user['id'], [
'subject' => $title,
'description' => "Jira ticket {$key} gepland via Fociz ({$priority})\n\n{$url}",
'date' => $planDate,
'start_time' => $syncStartTime,
'end_time' => $timeBlockEnd,
'duration_minutes' => $durationMinutes,
]);
if (!empty($msResult['success']) && !empty($msResult['event_id'])) {
$source = 'OUTLOOK';
$externalId = $msResult['event_id'];
$externalUrl = $msResult['web_link'] ?? ($url ?: 'https://outlook.office.com');
$timeBlockStart = $msResult['start_time'] ?? $syncStartTime;
$timeBlockEnd = $msResult['end_time'] ?? $timeBlockEnd;
$outlookSyncMsg = 'Gesynchroniseerd naar Microsoft Outlook.';
}
} catch (Throwable $t) {
error_log("Fout bij auto-sync Jira naar Outlook: " . $t->getMessage());
}
}
// Voeg taak toe // Voeg taak toe
$stmt = $db->prepare(" $stmt = $db->prepare("
INSERT INTO tasks (user_id, daily_plan_id, title, priority, time_block_start, time_block_end, duration_minutes, status, source, external_id, external_url) INSERT INTO tasks (user_id, daily_plan_id, title, priority, time_block_start, time_block_end, duration_minutes, status, source, external_id, external_url)
VALUES (?, ?, ?, ?, ?, ?, ?, 'TODO', 'JIRA', ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, 'TODO', ?, ?, ?)
"); ");
$stmt->execute([ $stmt->execute([
$user['id'], $user['id'],
@@ -125,15 +165,17 @@ try {
$timeBlockStart, $timeBlockStart,
$timeBlockEnd, $timeBlockEnd,
$durationMinutes, $durationMinutes,
$key, $source,
$url $externalId,
$externalUrl
]); ]);
$taskId = (int)$db->lastInsertId(); $taskId = (int)$db->lastInsertId();
echo json_encode([ echo json_encode([
'success' => true, 'success' => true,
'task_id' => $taskId, 'task_id' => $taskId,
'message' => "Ticket {$key} is succesvol ingepland voor " . date('d-m-Y', strtotime($planDate)) . "!", 'outlook_synced' => ($source === 'OUTLOOK'),
'message' => "Ticket {$key} is succesvol ingepland voor " . date('d-m-Y', strtotime($planDate)) . "!" . ($outlookSyncMsg ? " ({$outlookSyncMsg})" : ""),
]); ]);
exit; exit;
} }
+37 -4
View File
@@ -164,9 +164,40 @@ try {
} }
} }
$source = 'OUTLOOK_MAIL';
$externalId = $messageId;
$externalUrl = $webLink;
$outlookSyncMsg = null;
// Automatisch synchroniseren naar Microsoft Outlook agenda
if (function_exists('microsoft_create_event')) {
try {
$syncStartTime = !empty($timeBlockStart) ? $timeBlockStart : '09:00';
$msResult = microsoft_create_event($userId, [
'subject' => $taskTitle,
'description' => "E-mail actiepunt gepland via Fociz ({$priority})\n\nAfzender: {$sender}\nWeblink: {$webLink}",
'date' => $planDate,
'start_time' => $syncStartTime,
'end_time' => $timeBlockEnd,
'duration_minutes' => $durationMinutes,
]);
if (!empty($msResult['success']) && !empty($msResult['event_id'])) {
$source = 'OUTLOOK';
$externalId = $msResult['event_id'];
$externalUrl = $msResult['web_link'] ?? ($webLink ?: 'https://outlook.office.com');
$timeBlockStart = $msResult['start_time'] ?? $syncStartTime;
$timeBlockEnd = $msResult['end_time'] ?? $timeBlockEnd;
$outlookSyncMsg = 'Gesynchroniseerd naar Microsoft Outlook.';
}
} catch (Throwable $t) {
error_log("Fout bij auto-sync Mail naar Outlook: " . $t->getMessage());
}
}
$stmtTask = $db->prepare(" $stmtTask = $db->prepare("
INSERT INTO tasks (user_id, daily_plan_id, title, priority, time_block_start, time_block_end, duration_minutes, source, external_id, external_url) INSERT INTO tasks (user_id, daily_plan_id, title, priority, time_block_start, time_block_end, duration_minutes, source, external_id, external_url)
VALUES (?, ?, ?, ?, ?, ?, ?, 'OUTLOOK_MAIL', ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"); ");
$stmtTask->execute([ $stmtTask->execute([
$userId, $userId,
@@ -176,8 +207,9 @@ try {
$timeBlockStart, $timeBlockStart,
$timeBlockEnd, $timeBlockEnd,
$durationMinutes, $durationMinutes,
$messageId, $source,
$webLink $externalId,
$externalUrl
]); ]);
$taskId = (int)$db->lastInsertId(); $taskId = (int)$db->lastInsertId();
@@ -185,7 +217,8 @@ try {
'success' => true, 'success' => true,
'task_id' => $taskId, 'task_id' => $taskId,
'plan_date' => $planDate, 'plan_date' => $planDate,
'message' => "E-mail succesvol ingepland voor {$planDate}." 'outlook_synced' => ($source === 'OUTLOOK'),
'message' => "E-mail succesvol ingepland voor {$planDate}." . ($outlookSyncMsg ? " ({$outlookSyncMsg})" : "")
]); ]);
exit; exit;
} }
+63 -15
View File
@@ -139,18 +139,28 @@ try {
$externalUrl = !empty($data['external_url']) ? trim($data['external_url']) : null; $externalUrl = !empty($data['external_url']) ? trim($data['external_url']) : null;
$outlookSyncError = null; $outlookSyncError = null;
// Als synchronisatie naar Outlook gewenst is (of source is OUTLOOK) en nog geen extern ID heeft // Controleer of de gebruiker Outlook 365 heeft gekoppeld
if (($syncOutlook || $source === 'OUTLOOK') && empty($externalId)) { $userHasOutlook = false;
// Als er geen starttijd is ingevuld maar wel sync gewenst is, gebruik 09:00 als veilige standaard if (function_exists('microsoft_create_event')) {
if (empty($timeBlockStart)) { $stmtChk = $db->prepare("SELECT id FROM integrations WHERE user_id = ? AND provider = 'OUTLOOK' AND is_enabled = 1 AND access_token IS NOT NULL LIMIT 1");
$timeBlockStart = '09:00'; $stmtChk->execute([$user['id']]);
$userHasOutlook = (bool)$stmtChk->fetchColumn();
}
// Automatisch synchroniseren naar Outlook indien gevraagd OF indien gebruiker Outlook heeft gekoppeld
if (($syncOutlook || $source === 'OUTLOOK' || $userHasOutlook) && empty($externalId)) {
$syncStartTime = !empty($timeBlockStart) ? $timeBlockStart : '09:00';
$itemDesc = !empty($data['description']) ? trim((string)$data['description']) : "Gepland via Fociz Dagplanning ({$priority})";
if ($source === 'JIRA' && !empty($data['external_url'])) {
$itemDesc .= "\n\nJira: " . $data['external_url'];
} }
$msResult = microsoft_create_event($user['id'], [ $msResult = microsoft_create_event($user['id'], [
'subject' => $title, 'subject' => $title,
'description' => "Gepland via Fociz Dagplanning ({$priority})", 'description' => $itemDesc,
'date' => $date, 'date' => $date,
'start_time' => $timeBlockStart, 'start_time' => $syncStartTime,
'end_time' => $timeBlockEnd, 'end_time' => $timeBlockEnd,
'duration_minutes' => $durationMinutes, 'duration_minutes' => $durationMinutes,
]); ]);
@@ -158,8 +168,8 @@ try {
if (!empty($msResult['success']) && !empty($msResult['event_id'])) { if (!empty($msResult['success']) && !empty($msResult['event_id'])) {
$source = 'OUTLOOK'; $source = 'OUTLOOK';
$externalId = $msResult['event_id']; $externalId = $msResult['event_id'];
$externalUrl = $msResult['web_link'] ?? 'https://outlook.office.com'; $externalUrl = $msResult['web_link'] ?? ($externalUrl ?: 'https://outlook.office.com');
$timeBlockStart = $msResult['start_time'] ?? $timeBlockStart; $timeBlockStart = $msResult['start_time'] ?? $syncStartTime;
$timeBlockEnd = $msResult['end_time'] ?? $timeBlockEnd; $timeBlockEnd = $msResult['end_time'] ?? $timeBlockEnd;
} else { } else {
$outlookSyncError = $msResult['error'] ?? 'Kon afspraak niet aanmaken in Microsoft Outlook'; $outlookSyncError = $msResult['error'] ?? 'Kon afspraak niet aanmaken in Microsoft Outlook';
@@ -257,10 +267,22 @@ try {
$planDate = $stmtPlan->fetchColumn() ?: date('Y-m-d'); $planDate = $stmtPlan->fetchColumn() ?: date('Y-m-d');
} }
// Controleer of de gebruiker Outlook 365 heeft gekoppeld
$userHasOutlook = false;
if (function_exists('microsoft_create_event')) {
$stmtChk = $db->prepare("SELECT id FROM integrations WHERE user_id = ? AND provider = 'OUTLOOK' AND is_enabled = 1 AND access_token IS NOT NULL LIMIT 1");
$stmtChk->execute([$user['id']]);
$userHasOutlook = (bool)$stmtChk->fetchColumn();
}
$source = $existingTask['source'] ?? 'FOCIZ';
$externalId = $existingTask['external_id'] ?? null;
$externalUrl = $existingTask['external_url'] ?? null;
// Indien gekoppeld aan Outlook, werk afspraak bij via Graph API // Indien gekoppeld aan Outlook, werk afspraak bij via Graph API
if (!empty($existingTask['external_id']) && $existingTask['source'] === 'OUTLOOK') { if (!empty($externalId) && $source === 'OUTLOOK') {
try { try {
microsoft_update_event($user['id'], $existingTask['external_id'], [ microsoft_update_event($user['id'], $externalId, [
'subject' => $title, 'subject' => $title,
'date' => $planDate, 'date' => $planDate,
'start_time' => $timeBlockStart ?: '09:00', 'start_time' => $timeBlockStart ?: '09:00',
@@ -269,6 +291,32 @@ try {
} catch (Throwable $t) { } catch (Throwable $t) {
error_log("Fout bij bijwerken Outlook event: " . $t->getMessage()); error_log("Fout bij bijwerken Outlook event: " . $t->getMessage());
} }
} elseif ($userHasOutlook && empty($externalId) && function_exists('microsoft_create_event')) {
// Nog niet in Outlook gesynchroniseerd, maar gebruiker heeft Outlook: maak afspraak aan
try {
$syncStartTime = !empty($timeBlockStart) ? $timeBlockStart : '09:00';
$itemDesc = !empty($existingTask['description']) ? trim((string)$existingTask['description']) : "Gepland via Fociz Dagplanning ({$priority})";
if ($source === 'JIRA' && !empty($externalUrl)) {
$itemDesc .= "\n\nJira: " . $externalUrl;
}
$msResult = microsoft_create_event($user['id'], [
'subject' => $title,
'description' => $itemDesc,
'date' => $planDate,
'start_time' => $syncStartTime,
'end_time' => $timeBlockEnd,
'duration_minutes' => $durationMinutes,
]);
if (!empty($msResult['success']) && !empty($msResult['event_id'])) {
$source = 'OUTLOOK';
$externalId = $msResult['event_id'];
$externalUrl = $msResult['web_link'] ?? ($externalUrl ?: 'https://outlook.office.com');
}
} catch (Throwable $t) {
error_log("Fout bij auto-create Outlook event in update_task: " . $t->getMessage());
}
} }
$description = isset($data['description']) ? trim((string)$data['description']) : ($existingTask['description'] ?? null); $description = isset($data['description']) ? trim((string)$data['description']) : ($existingTask['description'] ?? null);
@@ -276,17 +324,17 @@ try {
if ($newPlanId) { if ($newPlanId) {
$stmtUpdate = $db->prepare(" $stmtUpdate = $db->prepare("
UPDATE tasks UPDATE tasks
SET title = ?, description = ?, priority = ?, time_block_start = ?, time_block_end = ?, duration_minutes = ?, daily_plan_id = ? SET title = ?, description = ?, priority = ?, time_block_start = ?, time_block_end = ?, duration_minutes = ?, source = ?, external_id = ?, external_url = ?, daily_plan_id = ?
WHERE id = ? AND user_id = ? WHERE id = ? AND user_id = ?
"); ");
$stmtUpdate->execute([$title, $description, $priority, $timeBlockStart, $timeBlockEnd, $durationMinutes, $newPlanId, $taskId, $user['id']]); $stmtUpdate->execute([$title, $description, $priority, $timeBlockStart, $timeBlockEnd, $durationMinutes, $source, $externalId, $externalUrl, $newPlanId, $taskId, $user['id']]);
} else { } else {
$stmtUpdate = $db->prepare(" $stmtUpdate = $db->prepare("
UPDATE tasks UPDATE tasks
SET title = ?, description = ?, priority = ?, time_block_start = ?, time_block_end = ?, duration_minutes = ? SET title = ?, description = ?, priority = ?, time_block_start = ?, time_block_end = ?, duration_minutes = ?, source = ?, external_id = ?, external_url = ?
WHERE id = ? AND user_id = ? WHERE id = ? AND user_id = ?
"); ");
$stmtUpdate->execute([$title, $description, $priority, $timeBlockStart, $timeBlockEnd, $durationMinutes, $taskId, $user['id']]); $stmtUpdate->execute([$title, $description, $priority, $timeBlockStart, $timeBlockEnd, $durationMinutes, $source, $externalId, $externalUrl, $taskId, $user['id']]);
} }
echo json_encode([ echo json_encode([
+2 -1
View File
@@ -1100,13 +1100,14 @@ function openCardModal(cardId = null) {
if (hasSched) { if (hasSched) {
document.getElementById('cardStartDate').value = foundCard.start_date || ''; document.getElementById('cardStartDate').value = foundCard.start_date || '';
document.getElementById('cardEndDate').value = foundCard.end_date || ''; document.getElementById('cardEndDate').value = foundCard.end_date || '';
document.getElementById('cardSyncOutlook').checked = !!foundCard.outlook_event_id; document.getElementById('cardSyncOutlook').checked = foundCard.outlook_event_id ? true : true; // Standaard gesynchroniseerd
} }
} }
} else { } else {
titleEl.innerText = 'Nieuwe Taak Toevoegen'; titleEl.innerText = 'Nieuwe Taak Toevoegen';
deleteBtn.classList.add('hidden'); deleteBtn.classList.add('hidden');
document.getElementById('cardScheduleToggle').checked = false; document.getElementById('cardScheduleToggle').checked = false;
document.getElementById('cardSyncOutlook').checked = true;
toggleScheduleFields(false); toggleScheduleFields(false);
} }