fixed cancel 2

This commit is contained in:
2026-09-18 10:13:36 +02:00
parent 7179c49865
commit c9a0cc6034
2 changed files with 14 additions and 289 deletions
+11 -284
View File
@@ -27,6 +27,7 @@ if ($token !== '') {
$state = 'form'; // form | notfound | already | toolate | done
$promotedCount = 0;
$didCancel = false;
$promoted = [];
if (!$booking) {
$state = 'notfound';
@@ -46,28 +47,34 @@ if (!$booking) {
$fresh = $bl->fetch(PDO::FETCH_ASSOC);
if ($fresh && $fresh['status'] !== 'cancelled') {
$wasOccupying = in_array($fresh['status'], ['confirmed', 'pending'], true);
$wasOccupying = in_array($fresh['status'], ['confirmed', 'pending', 'offered'], true);
$pdo->prepare("UPDATE event_bookings SET status = 'cancelled', updated_at = NOW() WHERE id = ?")
->execute([(int) $fresh['id']]);
if ($wasOccupying) {
$promoted = promoteFromWaitlist($pdo, (int) $booking['eid']);
$promotedCount = count($promoted);
// Nota: l'email di promozione (E, con riconferma) si attiva nel secondo tempo.
}
$didCancel = true;
}
$pdo->commit();
$state = 'done';
// Email di conferma disdetta all'utente (best-effort)
// Email dopo il commit (best-effort)
if ($didCancel) {
try {
$evStmt = pdo_conn()->prepare("SELECT * FROM events WHERE id = ?");
$evStmt->execute([(int) $booking['eid']]);
$evRow = $evStmt->fetch(PDO::FETCH_ASSOC);
if ($evRow) {
yse_mail_user_cancel($booking, $evRow);
yse_mail_user_cancel($booking, $evRow); // B: conferma disdetta
foreach ($promoted as $pr) { // E: promozione con riconferma
try {
yse_mail_promotion($pr, $evRow);
} catch (Throwable $pe) {
error_log('[promo mail] ' . $pe->getMessage());
}
}
}
} catch (Throwable $mailEx) {
error_log('[cancel mail] ' . $mailEx->getMessage());
@@ -273,284 +280,4 @@ if (!function_exists('e')) {
</div>
</body>
</html><?php
require_once __DIR__ . '/bootstrap.php';
require_once dirname(__DIR__) . '/include/event_waitlist.php';
require_once dirname(__DIR__) . '/include/event_mailer.php';
/**
* Disdetta pubblica di una prenotazione tramite token (link email, senza login).
*
* GET apiwp/cancel.php?token=... -> mostra pagina di conferma (NON annulla:
* evita che gli scanner email prefetchino il link e annullino da soli)
* POST (token + confirm=1) -> annulla e, se la prenotazione occupava posti,
* promuove automaticamente la prima in lista d'attesa.
*/
$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 | toolate | done
$promotedCount = 0;
$didCancel = false;
$promoted = [];
if (!$booking) {
$state = 'notfound';
} elseif ($booking['status'] === 'cancelled') {
$state = 'already';
} elseif (strtotime($booking['ev_starts']) <= time()) {
$state = 'toolate';
} elseif ($isPost && !empty($_POST['confirm'])) {
try {
$pdo->beginTransaction();
// Lock evento per serializzare con eventuali prenotazioni concorrenti
$pdo->prepare("SELECT id FROM events WHERE id = ? FOR UPDATE")->execute([(int) $booking['eid']]);
// Rileggo lo stato aggiornato della prenotazione
$bl = $pdo->prepare("SELECT * FROM event_bookings WHERE id = ?");
$bl->execute([(int) $booking['id']]);
$fresh = $bl->fetch(PDO::FETCH_ASSOC);
if ($fresh && $fresh['status'] !== 'cancelled') {
$wasOccupying = in_array($fresh['status'], ['confirmed', 'pending', 'offered'], true);
$pdo->prepare("UPDATE event_bookings SET status = 'cancelled', updated_at = NOW() WHERE id = ?")
->execute([(int) $fresh['id']]);
if ($wasOccupying) {
$promoted = promoteFromWaitlist($pdo, (int) $booking['eid']);
$promotedCount = count($promoted);
}
$didCancel = true;
}
$pdo->commit();
$state = 'done';
// Email dopo il commit (best-effort)
if ($didCancel) {
try {
$evStmt = pdo_conn()->prepare("SELECT * FROM events WHERE id = ?");
$evStmt->execute([(int) $booking['eid']]);
$evRow = $evStmt->fetch(PDO::FETCH_ASSOC);
if ($evRow) {
yse_mail_user_cancel($booking, $evRow); // B: conferma disdetta
foreach ($promoted as $pr) { // E: promozione con riconferma
try {
yse_mail_promotion($pr, $evRow);
} catch (Throwable $pe) {
error_log('[promo mail] ' . $pe->getMessage());
}
}
}
} catch (Throwable $mailEx) {
error_log('[cancel mail] ' . $mailEx->getMessage());
}
}
} catch (Throwable $ex) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
error_log('[apiwp/cancel] ' . $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>Disdetta prenotazione — 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: #b07aa1;
--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;
}
.icn.warn {
background: #fdecea;
color: #c0392b;
}
.icn.ok {
background: #e7f7ee;
color: #1a7f52;
}
.icn.info {
background: #eef0f3;
color: #6b7280;
}
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-danger {
background: #e74c3c;
color: #fff;
}
.btn-danger:hover {
filter: brightness(.94);
}
.btn-light {
background: #f3f4f6;
color: #374151;
text-decoration: none;
display: inline-block;
}
.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>Questa prenotazione non esiste o il link è errato.</p>
<?php elseif ($state === 'already') : ?>
<div class="icn info">✓</div>
<h1>Già annullata</h1>
<p>Questa prenotazione risulta già annullata.</p>
<?php elseif ($state === 'toolate') : ?>
<div class="icn info">⏱</div>
<h1>Non più annullabile</h1>
<p>L'evento è già iniziato: non è più possibile annullare 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>Prenotazione annullata</h1>
<p>La tua prenotazione per <strong><?php echo e($booking['ev_title']); ?></strong> è stata annullata.</p>
<?php if ($promotedCount > 0) : ?>
<p class="muted">Un posto è stato assegnato alla prima persona in lista d'attesa.</p>
<?php endif; ?>
<?php else : /* form */ ?>
<div class="icn warn">?</div>
<h1>Annullare la prenotazione?</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'; ?><?php echo $booking['status'] === 'waiting' ? ' (in lista d\'attesa)' : ''; ?></div>
</div>
<p>Confermi di voler annullare la prenotazione a nome di <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-danger">Sì, annulla la prenotazione</button>
</form>
<p class="muted">Se hai cambiato idea, chiudi pure questa pagina.</p>
<?php endif; ?>
</div>
</body>
</html>
+2 -4
View File
@@ -59,11 +59,9 @@ if (!$booking) {
}
}
if (!function_exists('e')) {
function e($v): string
{
function e($v): string
{
return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
}
}
?>
<!doctype html>