yogiboook_new/public/userarea/user-settings.php
2026-01-28 20:14:49 +01:00

435 lines
21 KiB
PHP

<?php
include('include/headscript.php');
if (!isset($iduserlogin)) {
die("Errore: utente non loggato.");
}
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
/*
|--------------------------------------------------------------------------
| 1) Recupera la scuola (come nel tuo esempio)
|--------------------------------------------------------------------------
*/
// 1) Recupera school_id dalla sessione (studente) oppure fallback owner (admin/owner)
$school_id = (int)($_SESSION['school_id'] ?? 0);
if ($school_id <= 0) {
// fallback per owner/admin
$stmt = $pdo->prepare("SELECT id, name FROM schools WHERE owner_id = ? LIMIT 1");
$stmt->execute([(int)$iduserlogin]);
$school = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$school) die("Scuola non trovata.");
$school_id = (int)$school['id'];
} else {
// carica dati scuola
$stmt = $pdo->prepare("SELECT id, name FROM schools WHERE id = ? LIMIT 1");
$stmt->execute([$school_id]);
$school = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$school) die("Scuola non trovata.");
}
/*
|--------------------------------------------------------------------------
| 2) Recupera school_settings (per sapere se notifiche sono abilitate globalmente)
|--------------------------------------------------------------------------
*/
$stmt = $pdo->prepare("SELECT enable_notifications FROM school_settings WHERE school_id = ? LIMIT 1");
$stmt->execute([$school_id]);
$schoolSettings = $stmt->fetch(PDO::FETCH_ASSOC);
$school_enable_notifications = isset($schoolSettings['enable_notifications'])
? (int)$schoolSettings['enable_notifications']
: 1; // default ON se non esiste riga
/*
|--------------------------------------------------------------------------
| 3) Recupera user_settings (per questo utente in questa scuola)
|--------------------------------------------------------------------------
*/
$stmt = $pdo->prepare("SELECT * FROM user_settings WHERE school_id = ? AND user_id = ? LIMIT 1");
$stmt->execute([$school_id, (int)$iduserlogin]);
$settings = $stmt->fetch(PDO::FETCH_ASSOC);
$is_new = !$settings;
$success_message = "";
$error = "";
if (!$settings) {
$pdo->prepare("
INSERT INTO user_settings (school_id, user_id, locale, timezone, created_at, updated_at)
VALUES (?, ?, 'it', 'Europe/Rome', NOW(), NOW())
")->execute([$school_id, (int)$iduserlogin]);
header("Location: user-settings.php");
exit;
}
/*
|--------------------------------------------------------------------------
| 4) Salvataggio POST
|--------------------------------------------------------------------------
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// NOTA: se scuola disabilita notifiche globalmente, forzo tutto a 0
$notifications_allowed = ($school_enable_notifications === 1);
$notify_email = (!empty($_POST['notify_email']) && $notifications_allowed) ? 1 : 0;
$notify_whatsapp = (!empty($_POST['notify_whatsapp']) && $notifications_allowed) ? 1 : 0;
$notify_push = (!empty($_POST['notify_push']) && $notifications_allowed) ? 1 : 0;
$notify_booking_confirm = (!empty($_POST['notify_booking_confirm']) && $notifications_allowed) ? 1 : 0;
$notify_booking_cancel = (!empty($_POST['notify_booking_cancel']) && $notifications_allowed) ? 1 : 0;
$notify_session_cancel = (!empty($_POST['notify_session_cancel']) && $notifications_allowed) ? 1 : 0;
$notify_payment_receipt = (!empty($_POST['notify_payment_receipt']) && $notifications_allowed) ? 1 : 0;
$notify_expiration_reminder = (!empty($_POST['notify_expiration_reminder']) && $notifications_allowed) ? 1 : 0;
// MARKETING
$newsletter_opt_in = !empty($_POST['newsletter_opt_in']) ? 1 : 0;
$marketing_opt_in = !empty($_POST['marketing_opt_in']) ? 1 : 0;
// PREFERENZE
$locale = trim($_POST['locale'] ?? 'it');
$timezone = trim($_POST['timezone'] ?? 'Europe/Rome');
// whitelist minima (anti valori strani)
$allowed_locales = ['it', 'en', 'es'];
if (!in_array($locale, $allowed_locales, true)) $locale = 'it';
if ($timezone === '') $timezone = 'Europe/Rome';
try {
if ($is_new) {
$stmt = $pdo->prepare("
INSERT INTO user_settings
(school_id, user_id,
notify_email, notify_whatsapp, notify_push,
notify_booking_confirm, notify_booking_cancel, notify_session_cancel,
notify_payment_receipt, notify_expiration_reminder,
newsletter_opt_in, marketing_opt_in,
locale, timezone)
VALUES
(?, ?,
?, ?, ?,
?, ?, ?,
?, ?,
?, ?,
?, ?)
");
$stmt->execute([
$school_id,
(int)$iduserlogin,
$notify_email,
$notify_whatsapp,
$notify_push,
$notify_booking_confirm,
$notify_booking_cancel,
$notify_session_cancel,
$notify_payment_receipt,
$notify_expiration_reminder,
$newsletter_opt_in,
$marketing_opt_in,
$locale,
$timezone
]);
$success_message = "Impostazioni utente create con successo!";
} else {
$stmt = $pdo->prepare("
UPDATE user_settings SET
notify_email = ?, notify_whatsapp = ?, notify_push = ?,
notify_booking_confirm = ?, notify_booking_cancel = ?, notify_session_cancel = ?,
notify_payment_receipt = ?, notify_expiration_reminder = ?,
newsletter_opt_in = ?, marketing_opt_in = ?,
locale = ?, timezone = ?
WHERE school_id = ? AND user_id = ?
LIMIT 1
");
$stmt->execute([
$notify_email,
$notify_whatsapp,
$notify_push,
$notify_booking_confirm,
$notify_booking_cancel,
$notify_session_cancel,
$notify_payment_receipt,
$notify_expiration_reminder,
$newsletter_opt_in,
$marketing_opt_in,
$locale,
$timezone,
$school_id,
(int)$iduserlogin
]);
$success_message = "Impostazioni utente aggiornate con successo!";
}
// ricarica valori aggiornati
$stmt = $pdo->prepare("SELECT * FROM user_settings WHERE school_id = ? AND user_id = ? LIMIT 1");
$stmt->execute([$school_id, (int)$iduserlogin]);
$settings = $stmt->fetch(PDO::FETCH_ASSOC);
$is_new = !$settings;
} catch (Exception $e) {
$error = "Errore database: " . $e->getMessage();
}
}
/*
|--------------------------------------------------------------------------
| 5) Defaults (se non esiste ancora riga)
|--------------------------------------------------------------------------
*/
$defaults = [
'notify_email' => 1,
'notify_whatsapp' => 0,
'notify_push' => 0,
'notify_booking_confirm' => 1,
'notify_booking_cancel' => 1,
'notify_session_cancel' => 1,
'notify_payment_receipt' => 1,
'notify_expiration_reminder' => 1,
'newsletter_opt_in' => 0,
'marketing_opt_in' => 0,
'locale' => 'it',
'timezone' => 'Europe/Rome'
];
$settings = $settings ?: $defaults;
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Impostazioni Utente - <?php echo htmlspecialchars($school['name']); ?></title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
.card {
border-radius: 15px;
}
.form-check-input:checked {
background-color: #0d6efd;
border-color: #0d6efd;
}
.section-title {
font-weight: 700;
}
.small-note {
font-size: .85rem;
}
.disabled-overlay {
opacity: .55;
pointer-events: none;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="container-xl">
<div class="row">
<div class="col-12">
<div class="card shadow">
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
<h4 class="mb-0">Impostazioni Utente</h4>
<span class="badge bg-light text-dark">
<?php echo htmlspecialchars($school['name']); ?>
</span>
</div>
<div class="card-body">
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show">
<?php echo $success_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger">
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<?php if ((int)$school_enable_notifications !== 1): ?>
<div class="alert alert-warning">
Le notifiche sono disattivate a livello di scuola. Le preferenze qui sotto non avranno effetto finché non vengono riattivate.
</div>
<?php endif; ?>
<form method="POST">
<!-- NOTIFICHE -->
<h5 class="text-primary mb-3 section-title">Notifiche</h5>
<div id="notificationsBlock" class="<?php echo ((int)$school_enable_notifications !== 1) ? 'disabled-overlay' : ''; ?>">
<div class="row g-4 align-items-center mb-3">
<div class="col-md-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_email" id="notify_email"
<?php echo !empty($settings['notify_email']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_email">Email</label>
</div>
<div class="text-muted small-note">Conferme, cancellazioni, promemoria.</div>
</div>
<div class="col-md-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_whatsapp" id="notify_whatsapp"
<?php echo !empty($settings['notify_whatsapp']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_whatsapp">WhatsApp</label>
</div>
<div class="text-muted small-note">Da attivare quando integri WA.</div>
</div>
<div class="col-md-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_push" id="notify_push"
<?php echo !empty($settings['notify_push']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_push">Push</label>
</div>
<div class="text-muted small-note">Da attivare quando integri app/push.</div>
</div>
</div>
<hr class="my-4">
<h6 class="mb-3">Eventi</h6>
<div class="row g-3">
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_booking_confirm" id="notify_booking_confirm"
<?php echo !empty($settings['notify_booking_confirm']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_booking_confirm">Conferma prenotazione</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_booking_cancel" id="notify_booking_cancel"
<?php echo !empty($settings['notify_booking_cancel']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_booking_cancel">Cancellazione prenotazione</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_session_cancel" id="notify_session_cancel"
<?php echo !empty($settings['notify_session_cancel']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_session_cancel">Lezione cancellata dalla scuola</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_payment_receipt" id="notify_payment_receipt"
<?php echo !empty($settings['notify_payment_receipt']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_payment_receipt">Ricevuta / conferma pagamento</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="notify_expiration_reminder" id="notify_expiration_reminder"
<?php echo !empty($settings['notify_expiration_reminder']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notify_expiration_reminder">Promemoria scadenza abbonamento</label>
</div>
</div>
</div>
</div>
<hr class="my-5">
<!-- MARKETING -->
<h5 class="text-primary mb-3 section-title">Newsletter e comunicazioni</h5>
<div class="row g-3">
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="newsletter_opt_in" id="newsletter_opt_in"
<?php echo !empty($settings['newsletter_opt_in']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="newsletter_opt_in">Newsletter</label>
</div>
<div class="text-muted small-note">Novità, eventi, contenuti.</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="marketing_opt_in" id="marketing_opt_in"
<?php echo !empty($settings['marketing_opt_in']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="marketing_opt_in">Promo e offerte</label>
</div>
<div class="text-muted small-note">Sconti, pacchetti speciali, promozioni.</div>
</div>
</div>
<hr class="my-5">
<!-- PREFERENZE -->
<h5 class="text-primary mb-3 section-title">Preferenze</h5>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Lingua</label>
<select name="locale" class="form-select">
<option value="it" <?php echo ($settings['locale'] ?? 'it') === 'it' ? 'selected' : ''; ?>>Italiano</option>
<option value="en" <?php echo ($settings['locale'] ?? '') === 'en' ? 'selected' : ''; ?>>English</option>
<option value="es" <?php echo ($settings['locale'] ?? '') === 'es' ? 'selected' : ''; ?>>Español</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">Timezone</label>
<input type="text" name="timezone" class="form-control"
value="<?php echo htmlspecialchars($settings['timezone'] ?? 'Europe/Rome'); ?>">
<div class="text-muted small-note">Esempio: Europe/Rome</div>
</div>
</div>
<div class="text-center mt-5">
<button type="submit" class="btn btn-primary btn-lg px-5">
Salva Impostazioni
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
</body>
</html>