getConnection(); // Verifica utente loggato if (!isset($iduserlogin)) { header("Location: login.php"); exit; } // =========================== // Helpers flash // =========================== function setFlash(string $type, string $text): void { $_SESSION['flash'] = ['type' => $type, 'text' => $text]; } function getFlash(): ?array { if (!isset($_SESSION['flash'])) return null; $f = $_SESSION['flash']; unset($_SESSION['flash']); return $f; } // =========================== // POST - Salva impostazioni utente // =========================== if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $receive_newsletter = isset($_POST['receive_newsletter']) ? 1 : 0; $receive_marketing_offers = isset($_POST['receive_marketing_offers']) ? 1 : 0; $notify_new_appointment = isset($_POST['notify_new_appointment']) ? 1 : 0; $notify_reminder = isset($_POST['notify_reminder']) ? 1 : 0; $notify_cancellation = isset($_POST['notify_cancellation']) ? 1 : 0; $notify_modification = isset($_POST['notify_modification']) ? 1 : 0; $notify_email = isset($_POST['notify_email']) ? 1 : 0; $notify_sms = isset($_POST['notify_sms']) ? 1 : 0; $notify_whatsapp = isset($_POST['notify_whatsapp']) ? 1 : 0; // Controlla esistenza riga $stmt = $pdo->prepare("SELECT id FROM user_settings WHERE user_id = ?"); $stmt->execute([$iduserlogin]); $exists = $stmt->fetchColumn() !== false; if ($exists) { $stmt = $pdo->prepare(" UPDATE user_settings SET receive_newsletter = ?, receive_marketing_offers = ?, notify_new_appointment = ?, notify_reminder = ?, notify_cancellation = ?, notify_modification = ?, notify_email = ?, notify_sms = ?, notify_whatsapp = ?, updated_at = NOW() WHERE user_id = ? "); $ok = $stmt->execute([ $receive_newsletter, $receive_marketing_offers, $notify_new_appointment, $notify_reminder, $notify_cancellation, $notify_modification, $notify_email, $notify_sms, $notify_whatsapp, $iduserlogin ]); } else { $stmt = $pdo->prepare(" INSERT INTO user_settings ( user_id, receive_newsletter, receive_marketing_offers, notify_new_appointment, notify_reminder, notify_cancellation, notify_modification, notify_email, notify_sms, notify_whatsapp ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $ok = $stmt->execute([ $iduserlogin, $receive_newsletter, $receive_marketing_offers, $notify_new_appointment, $notify_reminder, $notify_cancellation, $notify_modification, $notify_email, $notify_sms, $notify_whatsapp ]); } setFlash($ok ? 'success' : 'danger', $ok ? "Preferenze utente salvate!" : "Errore durante il salvataggio."); header("Location: user_settings.php"); exit; } catch (Throwable $e) { setFlash('danger', "Errore: " . $e->getMessage()); header("Location: user_settings.php"); exit; } } // Fetch impostazioni utente $stmt = $pdo->prepare("SELECT * FROM user_settings WHERE user_id = ?"); $stmt->execute([$iduserlogin]); $userSettings = $stmt->fetch(PDO::FETCH_ASSOC) ?: [ 'receive_newsletter' => 1, 'receive_marketing_offers' => 1, 'notify_new_appointment' => 1, 'notify_reminder' => 1, 'notify_cancellation' => 1, 'notify_modification' => 1, 'notify_email' => 1, 'notify_sms' => 0, 'notify_whatsapp' => 0 ]; $flash = getFlash(); ?>