Auto synch outlook
This commit is contained in:
+63
-15
@@ -139,18 +139,28 @@ try {
|
||||
$externalUrl = !empty($data['external_url']) ? trim($data['external_url']) : null;
|
||||
$outlookSyncError = null;
|
||||
|
||||
// Als synchronisatie naar Outlook gewenst is (of source is OUTLOOK) en nog geen extern ID heeft
|
||||
if (($syncOutlook || $source === 'OUTLOOK') && empty($externalId)) {
|
||||
// Als er geen starttijd is ingevuld maar wel sync gewenst is, gebruik 09:00 als veilige standaard
|
||||
if (empty($timeBlockStart)) {
|
||||
$timeBlockStart = '09:00';
|
||||
// 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();
|
||||
}
|
||||
|
||||
// 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'], [
|
||||
'subject' => $title,
|
||||
'description' => "Gepland via Fociz Dagplanning ({$priority})",
|
||||
'description' => $itemDesc,
|
||||
'date' => $date,
|
||||
'start_time' => $timeBlockStart,
|
||||
'start_time' => $syncStartTime,
|
||||
'end_time' => $timeBlockEnd,
|
||||
'duration_minutes' => $durationMinutes,
|
||||
]);
|
||||
@@ -158,8 +168,8 @@ try {
|
||||
if (!empty($msResult['success']) && !empty($msResult['event_id'])) {
|
||||
$source = 'OUTLOOK';
|
||||
$externalId = $msResult['event_id'];
|
||||
$externalUrl = $msResult['web_link'] ?? 'https://outlook.office.com';
|
||||
$timeBlockStart = $msResult['start_time'] ?? $timeBlockStart;
|
||||
$externalUrl = $msResult['web_link'] ?? ($externalUrl ?: 'https://outlook.office.com');
|
||||
$timeBlockStart = $msResult['start_time'] ?? $syncStartTime;
|
||||
$timeBlockEnd = $msResult['end_time'] ?? $timeBlockEnd;
|
||||
} else {
|
||||
$outlookSyncError = $msResult['error'] ?? 'Kon afspraak niet aanmaken in Microsoft Outlook';
|
||||
@@ -257,10 +267,22 @@ try {
|
||||
$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
|
||||
if (!empty($existingTask['external_id']) && $existingTask['source'] === 'OUTLOOK') {
|
||||
if (!empty($externalId) && $source === 'OUTLOOK') {
|
||||
try {
|
||||
microsoft_update_event($user['id'], $existingTask['external_id'], [
|
||||
microsoft_update_event($user['id'], $externalId, [
|
||||
'subject' => $title,
|
||||
'date' => $planDate,
|
||||
'start_time' => $timeBlockStart ?: '09:00',
|
||||
@@ -269,6 +291,32 @@ try {
|
||||
} catch (Throwable $t) {
|
||||
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);
|
||||
@@ -276,17 +324,17 @@ try {
|
||||
if ($newPlanId) {
|
||||
$stmtUpdate = $db->prepare("
|
||||
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 = ?
|
||||
");
|
||||
$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 {
|
||||
$stmtUpdate = $db->prepare("
|
||||
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 = ?
|
||||
");
|
||||
$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([
|
||||
|
||||
Reference in New Issue
Block a user