131 lines
5.3 KiB
PHP
131 lines
5.3 KiB
PHP
<?php
|
|
// Buffer per scartare eventuale HTML degli include: risposta JSON pulita
|
|
ob_start();
|
|
require_once('include/headscript.php');
|
|
require_once __DIR__ . '/class/mailer.php';
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
/**
|
|
* Conferma una prenotazione in attesa: pending -> booked.
|
|
* Se è una riprogrammazione (prevbookingstart valorizzato) imposta anche
|
|
* is_reprogrammed = 'Y', come acceptbookingclass.php.
|
|
* Dopo la conferma avvisa l'allieva via email (se l'invio fallisce la
|
|
* conferma resta valida e l'errore va nel log).
|
|
*/
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
function jsonOut(array $payload): void
|
|
{
|
|
if (ob_get_length() !== false) {
|
|
ob_end_clean();
|
|
}
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$idbookingclass = isset($_POST['idbookingclass']) ? (int) $_POST['idbookingclass'] : 0;
|
|
|
|
if ($idbookingclass <= 0) {
|
|
jsonOut(['success' => false, 'error' => 'ID prenotazione non valido.']);
|
|
}
|
|
|
|
try {
|
|
// Dati della prenotazione e dell'allieva
|
|
$stmt = $pdo->prepare(
|
|
"SELECT bc.status, bc.prevbookingstart, bc.bookingstart,
|
|
u.first_name, u.email, s.servicename
|
|
FROM bookingclass bc
|
|
LEFT JOIN auth_users u ON u.id = bc.iduser
|
|
LEFT JOIN service s ON s.idservice = bc.idservice
|
|
WHERE bc.idbookingclass = :id"
|
|
);
|
|
$stmt->execute([':id' => $idbookingclass]);
|
|
$bk = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$bk || $bk['status'] !== 'pending') {
|
|
jsonOut(['success' => false, 'error' => 'La prenotazione non è più in attesa.']);
|
|
}
|
|
|
|
$isReprogram = !empty($bk['prevbookingstart']);
|
|
|
|
// Conferma solo se effettivamente in pending (evita conferme accidentali)
|
|
$sql = "UPDATE bookingclass SET status = 'booked'"
|
|
. ($isReprogram ? ", is_reprogrammed = 'Y'" : "")
|
|
. " WHERE idbookingclass = :id AND status = 'pending'";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([':id' => $idbookingclass]);
|
|
|
|
if ($stmt->rowCount() !== 1) {
|
|
jsonOut(['success' => false, 'error' => 'La prenotazione non è più in attesa.']);
|
|
}
|
|
} catch (Throwable $ex) {
|
|
error_log('conferma_pending error: ' . $ex->getMessage());
|
|
jsonOut(['success' => false, 'error' => 'Errore durante la conferma.']);
|
|
}
|
|
|
|
// ---- Email all'allieva (dopo l'aggiornamento) ----
|
|
$mailSent = false;
|
|
|
|
if (!empty($bk['email'])) {
|
|
$nome = htmlspecialchars((string) $bk['first_name'], ENT_QUOTES, 'UTF-8');
|
|
$classe = htmlspecialchars((string) ($bk['servicename'] ?? ''), ENT_QUOTES, 'UTF-8');
|
|
$quando = date('d-m-Y H:i', strtotime($bk['bookingstart']));
|
|
|
|
if ($isReprogram) {
|
|
$subject = 'YogiBook - Lezione riprogrammata con successo!';
|
|
$intro = 'La tua richiesta di riprogrammazione è stata accettata!';
|
|
} else {
|
|
$subject = 'YogiBook - Prenotazione confermata!';
|
|
$intro = 'La tua prenotazione è stata confermata!';
|
|
}
|
|
|
|
$p = "<p style='font-size: 14px; line-height: 190%;'><span style='font-size: 16px; line-height: 30.4px;'>";
|
|
$pc = "</span></p>";
|
|
|
|
// Variabili usate da mail/emailtemplate2.php
|
|
$messageedit = "<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 $nome , </span></strong></span></p>"
|
|
. $p . $intro . $pc
|
|
. $p . "Lezione: $classe del $quando" . $pc
|
|
. "<br>"
|
|
. $p . "Per vedere e gestire le tue lezioni clicca qui: https://yogibook.yogasoul.it " . $pc
|
|
. "<br>"
|
|
. $p . "Ci vediamo sul tappetino!" . $pc
|
|
. $p . "Il Team Yogasoul" . $pc;
|
|
|
|
$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>";
|
|
|
|
try {
|
|
include('mail/emailtemplate2.php'); // crea $mailmessage1
|
|
|
|
$mail = new PHPMailer(true);
|
|
$mail->isSMTP();
|
|
$mail->Host = $_ENV['MAIL_HOST'];
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = $_ENV['MAIL_USERNAME'];
|
|
$mail->Password = $_ENV['MAIL_PASSWORD'];
|
|
$mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION'];
|
|
$mail->Port = (int) $_ENV['MAIL_PORT'];
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->From = $_ENV['MAIL_FROM_ADDRESS'];
|
|
$mail->FromName = $_ENV['MAIL_FROM_NAME'];
|
|
$mail->addAddress($bk['email']);
|
|
|
|
$mail->isHTML(true);
|
|
$mail->Subject = $subject;
|
|
$mail->Body = $mailmessage1;
|
|
$mail->AltBody = $intro . ' Lezione: ' . ($bk['servicename'] ?? '') . ' del ' . $quando;
|
|
|
|
$mail->send();
|
|
$mailSent = true;
|
|
} catch (Throwable $ex) {
|
|
error_log('conferma_pending mail (booking ' . $idbookingclass . '): ' . $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
jsonOut(['success' => true, 'mail' => $mailSent]);
|