103 lines
3.7 KiB
PHP
103 lines
3.7 KiB
PHP
<?php
|
|
// /messages/open.php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/_bootstrap.php'; // charge $pdo, $role ('praticien'|'client'), $me_id
|
|
|
|
// Accepte GET ou POST (on privilégie GET pour les liens)
|
|
$toRole = isset($_REQUEST['to']) ? strtolower(trim((string)$_REQUEST['to'])) : '';
|
|
$otherId = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
|
|
|
|
// Règles de base
|
|
if (!in_array($toRole, ['praticien','client'], true) || $otherId <= 0) {
|
|
http_response_code(400);
|
|
echo "Paramètres invalides. Exemple: /messages/open.php?to=praticien&id=123";
|
|
exit;
|
|
}
|
|
|
|
// Empêche d'ouvrir avec soi-même ou même rôle
|
|
if ($toRole === $role) {
|
|
http_response_code(400);
|
|
echo "Cible invalide : le paramètre 'to' doit être l'autre rôle.";
|
|
exit;
|
|
}
|
|
|
|
// Détermine les deux IDs (praticien / client) en fonction du rôle courant
|
|
if ($role === 'praticien') {
|
|
$praticien_id = $me_id;
|
|
$client_id = $otherId;
|
|
} else { // role === 'client'
|
|
$praticien_id = $otherId;
|
|
$client_id = $me_id;
|
|
}
|
|
|
|
// (Option recommandé) Vérifier que l'autre existe — sans FK, on contrôle à l'amiable
|
|
try {
|
|
if ($role === 'praticien') {
|
|
// On va écrire au client
|
|
$stmt = $pdo->prepare("SELECT 1 FROM client WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$client_id]);
|
|
if (!$stmt->fetchColumn()) {
|
|
http_response_code(404);
|
|
echo "Client introuvable (#$client_id).";
|
|
exit;
|
|
}
|
|
} else {
|
|
// role client -> on va écrire au praticien
|
|
$stmt = $pdo->prepare("SELECT 1 FROM praticien WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$praticien_id]);
|
|
if (!$stmt->fetchColumn()) {
|
|
http_response_code(404);
|
|
echo "Praticien introuvable (#$praticien_id).";
|
|
exit;
|
|
}
|
|
}
|
|
} catch (Throwable $e) {
|
|
// Si ces tables n'existent pas ou autre souci, on ne bloque pas l'ouverture (sécurité déjà gérée ailleurs)
|
|
// Mais on loggue minimalement si besoin (à activer plus tard)
|
|
// error_log("messages/open existence check: " . $e->getMessage());
|
|
}
|
|
|
|
// Tente de retrouver une conversation existante
|
|
$stmt = $pdo->prepare("SELECT id FROM conversations WHERE praticien_id = ? AND client_id = ? LIMIT 1");
|
|
$stmt->execute([$praticien_id, $client_id]);
|
|
$convId = (int)$stmt->fetchColumn();
|
|
|
|
if (!$convId) {
|
|
// Création atomique avec UNIQUE( praticien_id, client_id )
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
$ins = $pdo->prepare("INSERT INTO conversations (praticien_id, client_id, status, created_at, updated_at)
|
|
VALUES (?, ?, 'open', NOW(), NOW())");
|
|
$ins->execute([$praticien_id, $client_id]);
|
|
$convId = (int)$pdo->lastInsertId();
|
|
|
|
// Prépare aussi les compteurs
|
|
$pdo->prepare("INSERT INTO conversation_counters (conversation_id, p_unread, c_unread)
|
|
VALUES (?, 0, 0)")
|
|
->execute([$convId]);
|
|
|
|
$pdo->commit();
|
|
} catch (Throwable $e) {
|
|
// Conflit de concurrence (unique key déjà prise) → on relit
|
|
if ($pdo->inTransaction()) { $pdo->rollBack(); }
|
|
$stmt = $pdo->prepare("SELECT id FROM conversations WHERE praticien_id = ? AND client_id = ? LIMIT 1");
|
|
$stmt->execute([$praticien_id, $client_id]);
|
|
$convId = (int)$stmt->fetchColumn();
|
|
if (!$convId) {
|
|
http_response_code(500);
|
|
echo "Impossible de créer ou d'ouvrir la conversation.";
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Si la conversation est close, on peut choisir de la rouvrir (V1: on rouvre pour simplifier)
|
|
$pdo->prepare("UPDATE conversations SET status='open', updated_at=NOW() WHERE id = ? AND status='closed'")
|
|
->execute([$convId]);
|
|
|
|
// Redirection vers l'UI principale
|
|
header('Location: /messages/index.php?conv=' . $convId);
|
|
exit;
|