getConnection(); /* |-------------------------------------------------------------------------- | 1) Recupera la scuola (come nel tuo esempio) |-------------------------------------------------------------------------- */ $stmt = $pdo->prepare("SELECT id, name FROM schools WHERE owner_id = ? LIMIT 1"); $stmt->execute([$iduserlogin]); $school = $stmt->fetch(PDO::FETCH_ASSOC); if (!$school) { die("Scuola non trovata."); } $school_id = (int)$school['id']; /* |-------------------------------------------------------------------------- | 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 = ""; /* |-------------------------------------------------------------------------- | 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; ?> Impostazioni Utente - <?php echo htmlspecialchars($school['name']); ?>

Impostazioni Utente

Le notifiche sono disattivate a livello di scuola. Le preferenze qui sotto non avranno effetto finché non vengono riattivate.
Notifiche
>
Conferme, cancellazioni, promemoria.
>
Da attivare quando integri WA.
>
Da attivare quando integri app/push.

Eventi
>
>
>
>
>

Newsletter e comunicazioni
>
Novità, eventi, contenuti.
>
Sconti, pacchetti speciali, promozioni.

Preferenze
Esempio: Europe/Rome