yogibook_aury_new/public/cancella-prenotazione.php

140 lines
6.4 KiB
PHP

<?php
// Abilita visualizzazione errori PHP
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Connessione al database
include('include/headscript.php');
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connessione al database fallita: " . $conn->connect_error);
}
// Recupera parametri GET: idbookingclass e token
if (isset($_GET['idbookingclass']) && isset($_GET['token'])) {
$idbookingclass = $_GET['idbookingclass'];
$token = $_GET['token'];
// Verifica validità: token corrisponde, lezione futura e prima delle 12:00 del giorno
$query = "SELECT * FROM bookingclass
WHERE idbookingclass = ?
AND cancellation_token = ?
AND status = 'booked'
AND bookingstart > NOW()
AND NOW() <= DATE_ADD(DATE(bookingstart), INTERVAL 12 HOUR)";
$stmt = $conn->prepare($query);
if ($stmt) {
$stmt->bind_param("is", $idbookingclass, $token);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$bookingstart = $row['bookingstart'];
$newtimeformat = date("d-m-Y H:i", strtotime($bookingstart));
// Aggiorna status a 'cancelled' e invalida token
$updateQuery = "UPDATE bookingclass
SET status = 'cancelled', cancellation_token = NULL
WHERE idbookingclass = ?";
$updateStmt = $conn->prepare($updateQuery);
$updateStmt->bind_param("i", $idbookingclass);
$updateStmt->execute();
// Recupera dati utente e servizio
$dataQuery = "SELECT bookingclass.*, auth_users.*, service.*
FROM bookingclass
LEFT JOIN auth_users ON bookingclass.iduser = auth_users.id
LEFT JOIN service ON bookingclass.idservice = service.idservice
WHERE bookingclass.idbookingclass = ?";
$dataStmt = $conn->prepare($dataQuery);
$dataStmt->bind_param("i", $idbookingclass);
$dataStmt->execute();
$dataResult = $dataStmt->get_result();
$dataRow = $dataResult->fetch_assoc();
$emailuser = $dataRow['email'];
$firstname = $dataRow['first_name'] ?? 'Utente';
// Prepara messaggio email
$messagecancel = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 18px; line-height: 34.2px;'><strong><span style='line-height: 34.2px; font-size: 18px;'> Ciao $firstname , </span></strong></span></p>
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>La tua lezione è stata cancellata con successo! </span></p>
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Dettaglio: $newtimeformat</span></p>
<br>
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it </span></p>
<br>
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Ci vediamo sul tappetino!</span></p>
<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>Il Team Yogasoul</span></p>";
// Definisci $messageedit per il template
$messageedit = $messagecancel;
// Definisci $buttonedit (pulsante generico)
$buttonedit = "<a href='https://yogibook.yogasoul.it/' target='_blank' class='v-button v-font-size' style='box-sizing: border-box;display: inline-block;text-decoration: none;-webkit-text-size-adjust: none;text-align: center;color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px;-webkit-border-radius: 4px; -moz-border-radius: 4px; width:auto; max-width:100%; overflow-wrap: break-word; word-break: break-word; word-wrap:break-word; mso-border-alt: none;font-size: 14px;'>
<span style='display:block;padding:10px 20px;line-height:120%;'><span style='line-height: 16.8px;'>YogiBook - YogaSoul</span></span>
</a>";
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'mail.yogasoul.it';
$mail->SMTPAuth = true;
$mail->Username = 'info@yogasoul.it';
$mail->Password = '!Testolina88';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Verifica esistenza file template
if (!file_exists('mail/emailtemplate2.php')) {
throw new Exception("File emailtemplate2.php non trovato.");
}
include('mail/emailtemplate2.php');
// Verifica che $mailmessage1 esista
if (!isset($mailmessage1)) {
throw new Exception("Variabile \$mailmessage1 non definita in emailtemplate2.php.");
}
// Sostituisci placeholder (per compatibilità)
$htmlContent = str_replace('{message}', $messagecancel, $mailmessage1);
$mail->From = 'info@yogasoul.it';
$mail->FromName = 'YogiBook [YogaSoul]';
$mail->addAddress($emailuser);
$mail->Subject = "YogiBook - Lezione cancellata con successo!";
$mail->Body = $htmlContent;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
// Mostra landing di conferma
echo "<h1>Cancellazione confermata</h1>";
echo "<p>La lezione del $newtimeformat è stata cancellata con successo.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
} catch (Exception $e) {
echo "Errore invio email: " . $mail->ErrorInfo;
}
} else {
echo "Link non valido o scaduto.";
}
$stmt->close();
} else {
echo "Errore nella preparazione della query: " . $conn->error;
}
} else {
echo "Parametri mancanti.";
}
$conn->close();