84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
require_once __DIR__ . '/../public/includes/init_praticien.php';
|
|
|
|
use Google\Client;
|
|
use Google\Service\Calendar;
|
|
use Google\Service\Calendar\Event;
|
|
|
|
function envoyerVersGoogleCalendar($praticien_id, $summary, $description, $start_iso, $end_iso = null, $duree_minutes = 60) {
|
|
global $pdo;
|
|
|
|
$config = require 'config_google.php';
|
|
|
|
// 1. Récupérer le token Google du praticien
|
|
$stmt = $pdo->prepare("SELECT * FROM google_tokens WHERE praticien_id = ?");
|
|
$stmt->execute([$praticien_id]);
|
|
$row = $stmt->fetch();
|
|
|
|
if (!$row) {
|
|
error_log("❌ Aucun token trouvé pour praticien ID $praticien_id");
|
|
return false;
|
|
}
|
|
|
|
// 2. Préparer le client Google
|
|
$client = new Client();
|
|
$client->setClientId($config['client_id']);
|
|
$client->setClientSecret($config['client_secret']);
|
|
$client->setAccessToken([
|
|
'access_token' => $row['access_token'],
|
|
'expires_in' => 3600,
|
|
'refresh_token' => $row['refresh_token'],
|
|
'created' => $row['expires_at'] - 3600
|
|
]);
|
|
|
|
// 3. Rafraîchir le token si expiré
|
|
if ($client->isAccessTokenExpired()) {
|
|
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
|
|
|
|
$new_token = $client->getAccessToken();
|
|
$stmt = $pdo->prepare("UPDATE google_tokens SET access_token = ?, expires_at = ? WHERE praticien_id = ?");
|
|
$stmt->execute([
|
|
$new_token['access_token'],
|
|
time() + $new_token['expires_in'],
|
|
$praticien_id
|
|
]);
|
|
}
|
|
|
|
// 4. Calculer l'heure de fin si elle n'est pas transmise
|
|
if (!$end_iso) {
|
|
$start = new DateTime($start_iso);
|
|
$start->modify("+{$duree_minutes} minutes");
|
|
$end_iso = $start->format(DateTime::ATOM);
|
|
}
|
|
|
|
// 🔍 Log pour debug (facultatif)
|
|
error_log("🟢 Google Sync - START: $start_iso");
|
|
error_log("🟢 Google Sync - END : $end_iso");
|
|
|
|
// 5. Préparer le service et l'événement
|
|
$calendar = new Calendar($client);
|
|
|
|
$event = new Event([
|
|
'summary' => $summary,
|
|
'description' => $description,
|
|
'start' => [
|
|
'dateTime' => $start_iso,
|
|
'timeZone' => 'Europe/Paris'
|
|
],
|
|
'end' => [
|
|
'dateTime' => $end_iso,
|
|
'timeZone' => 'Europe/Paris'
|
|
]
|
|
]);
|
|
|
|
try {
|
|
$calendar->events->insert('primary', $event);
|
|
return true;
|
|
} catch (Exception $e) {
|
|
error_log("❌ Erreur Google Calendar : " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|