getConnection(); $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; $notify = (int) ($_GET['notify'] ?? 0) === 1; if ($id <= 0) { header('Location: events.php'); exit(); } $stmt = $pdo->prepare("SELECT * FROM events WHERE id = ?"); $stmt->execute([$id]); $event = $stmt->fetch(PDO::FETCH_ASSOC); if (!$event) { header('Location: events.php?message=error'); exit(); } // 1) Recupera i partecipanti attivi PRIMA di annullare (servono per la mail) $pStmt = $pdo->prepare("SELECT * FROM event_bookings WHERE event_id = ? AND status <> 'cancelled'"); $pStmt->execute([$id]); $participants = $pStmt->fetchAll(PDO::FETCH_ASSOC); try { // 2) Annulla l'evento (soft) e le relative prenotazioni $pdo->prepare("UPDATE events SET status = 'cancelled', updated_at = NOW() WHERE id = ?")->execute([$id]); $pdo->prepare("UPDATE event_bookings SET status = 'cancelled', updated_at = NOW() WHERE event_id = ? AND status <> 'cancelled'")->execute([$id]); // 3) Notifica (opzionale). Il template vero arriva nello step successivo. if ($notify && !empty($participants)) { foreach ($participants as $p) { try { yse_mail_event_cancel($p, $event); } catch (Throwable $mailEx) { error_log('[event_delete mail] ' . $mailEx->getMessage()); } } } header('Location: events.php?message=cancelled'); exit(); } catch (Throwable $ex) { error_log('[event_delete] ' . $ex->getMessage()); header('Location: events.php?message=error'); exit(); }