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

247 lines
7.8 KiB
PHP

<?php
require_once __DIR__ . '/bootstrap.php';
require_once dirname(__DIR__) . '/include/event_mailer.php';
/**
* Riconferma di una promozione da lista d'attesa (stato 'offered' -> 'confirmed').
* GET confirm.php?token=... -> pagina di conferma (non modifica nulla)
* POST (token + confirm=1) -> conferma il posto e invia l'email di conferma.
*/
$pdo = pdo_conn();
$token = trim($_POST['token'] ?? $_GET['token'] ?? '');
$isPost = ($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST';
$booking = null;
if ($token !== '') {
$q = $pdo->prepare("SELECT b.*, e.title AS ev_title, e.starts_at AS ev_starts, e.id AS eid
FROM event_bookings b JOIN events e ON e.id = b.event_id
WHERE b.token = ?");
$q->execute([$token]);
$booking = $q->fetch(PDO::FETCH_ASSOC);
}
$state = 'form'; // form | notfound | already | invalid | toolate | done | error
if (!$booking) {
$state = 'notfound';
} elseif ($booking['status'] === 'confirmed') {
$state = 'already';
} elseif ($booking['status'] !== 'offered') {
// waiting / cancelled / ecc.: non c'è un'offerta da confermare
$state = 'invalid';
} elseif (strtotime($booking['ev_starts']) <= time()) {
$state = 'toolate';
} elseif ($isPost && !empty($_POST['confirm'])) {
try {
$upd = $pdo->prepare("UPDATE event_bookings SET status = 'confirmed', updated_at = NOW() WHERE id = ? AND status = 'offered'");
$upd->execute([(int) $booking['id']]);
if ($upd->rowCount() > 0) {
$state = 'done';
// Email di conferma definitiva (best-effort)
try {
$ev = $pdo->prepare("SELECT * FROM events WHERE id = ?");
$ev->execute([(int) $booking['eid']]);
$evRow = $ev->fetch(PDO::FETCH_ASSOC);
if ($evRow) {
$booking['status'] = 'confirmed';
yse_mail_booking($booking, $evRow);
}
} catch (Throwable $me) {
error_log('[confirm mail] ' . $me->getMessage());
}
} else {
$state = 'invalid';
}
} catch (Throwable $ex) {
error_log('[apiwp/confirm] ' . $ex->getMessage());
$state = 'error';
}
}
function e($v): string
{
return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Conferma posto — YogaSoul</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--accent: #1a7f52;
--ink: #2b2622;
--muted: #8c837c;
--line: #ece7e2;
--paper: #faf8f6;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', system-ui, sans-serif;
display: flex;
min-height: 100vh;
align-items: center;
justify-content: center;
padding: 24px;
}
.card {
background: #fff;
border-radius: 18px;
box-shadow: 0 14px 40px rgba(43, 38, 34, .10);
max-width: 460px;
width: 100%;
padding: 34px 30px;
text-align: center;
}
.icn {
width: 58px;
height: 58px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 16px;
font-size: 26px;
}
.icn.ok {
background: #e7f7ee;
color: #1a7f52;
}
.icn.info {
background: #eef0f3;
color: #6b7280;
}
.icn.warn {
background: #fdecea;
color: #c0392b;
}
h1 {
font-family: 'Fraunces', serif;
font-weight: 600;
font-size: 23px;
margin: 0 0 8px;
}
p {
color: #4a433d;
font-size: 15px;
margin: 6px 0;
}
.ev {
background: var(--paper);
border-radius: 12px;
padding: 14px 16px;
margin: 18px 0;
}
.ev .t {
font-weight: 600;
}
.ev .d {
color: var(--muted);
font-size: 14px;
margin-top: 2px;
}
.btn {
border: none;
border-radius: 11px;
padding: 14px 20px;
font-size: 15px;
font-weight: 600;
font-family: inherit;
cursor: pointer;
width: 100%;
margin-top: 6px;
}
.btn-ok {
background: #1a7f52;
color: #fff;
}
.btn-ok:hover {
filter: brightness(.94);
}
.muted {
color: var(--muted);
font-size: 13px;
margin-top: 14px;
}
</style>
</head>
<body>
<div class="card">
<?php if ($state === 'notfound') : ?>
<div class="icn warn">✕</div>
<h1>Link non valido</h1>
<p>Questo link non è valido o è scaduto.</p>
<?php elseif ($state === 'already') : ?>
<div class="icn ok">✓</div>
<h1>Posto già confermato</h1>
<p>Il tuo posto per <strong><?php echo e($booking['ev_title']); ?></strong> è già confermato. Ci vediamo lì!</p>
<?php elseif ($state === 'invalid') : ?>
<div class="icn info">i</div>
<h1>Offerta non più disponibile</h1>
<p>Questa offerta non è più valida. Se pensi ci sia un errore, contattaci.</p>
<?php elseif ($state === 'toolate') : ?>
<div class="icn info">⏱</div>
<h1>Evento già iniziato</h1>
<p>Non è più possibile confermare online. Contattaci per assistenza.</p>
<?php elseif ($state === 'error') : ?>
<div class="icn warn">!</div>
<h1>Si è verificato un errore</h1>
<p>Riprova tra poco. Se il problema persiste, contattaci.</p>
<?php elseif ($state === 'done') : ?>
<div class="icn ok">✓</div>
<h1>Posto confermato!</h1>
<p>Grazie <strong><?php echo e($booking['name']); ?></strong>, il tuo posto per <strong><?php echo e($booking['ev_title']); ?></strong> è confermato. Ti abbiamo inviato l'email con tutti i dettagli.</p>
<?php else : /* form */ ?>
<div class="icn ok">🎉</div>
<h1>Si è liberato un posto!</h1>
<div class="ev">
<div class="t"><?php echo e($booking['ev_title']); ?></div>
<div class="d"><?php echo e(date('d/m/Y H:i', strtotime($booking['ev_starts']))); ?> · <?php echo (int) $booking['seats']; ?> post<?php echo (int) $booking['seats'] === 1 ? 'o' : 'i'; ?></div>
</div>
<p>Confermi la tua presenza, <strong><?php echo e($booking['name']); ?></strong>?</p>
<form method="POST">
<input type="hidden" name="token" value="<?php echo e($token); ?>">
<input type="hidden" name="confirm" value="1">
<button type="submit" class="btn btn-ok">Sì, confermo il posto</button>
</form>
<p class="muted">Se non puoi partecipare, ignora pure questa email: il posto passerà a un'altra persona.</p>
<?php endif; ?>
</div>
</body>
</html>