Files
yogibook_aury_new/public/event_delete.php
T
2026-09-18 09:52:51 +02:00

59 lines
2.0 KiB
PHP

<?php
require_once('include/headscript.php');
require_once('include/event_mailer.php'); // stub mailer (template vero: step successivo)
/**
* Annullamento evento.
* Sceglierne uno soft (consigliato): imposta status='cancelled' e mantiene le
* prenotazioni/contatti raccolti. Se notify=1 invia la mail di annullamento a
* tutti i partecipanti attivi (recuperati PRIMA di annullare).
*
* Chiamato da events.php: event_delete.php?id=123&notify=1|0
*/
$pdo = DBHandlerSelect::getInstance()->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();
}