diff --git a/db/migrations/20260814054733_add_notification_preferences_to_auth_users.php b/db/migrations/20260814054733_add_notification_preferences_to_auth_users.php new file mode 100644 index 0000000..79f5fd7 --- /dev/null +++ b/db/migrations/20260814054733_add_notification_preferences_to_auth_users.php @@ -0,0 +1,28 @@ +table('auth_users'); + + $table + ->addColumn('notify_email', 'boolean', [ + 'default' => true, + 'null' => false, + 'after' => 'announcements_last_read_at', + 'comment' => 'Preferenza notifiche via email', + ]) + ->addColumn('notify_webapp', 'boolean', [ + 'default' => true, + 'null' => false, + 'after' => 'notify_email', + 'comment' => 'Preferenza notifiche in webapp', + ]) + ->update(); + } +} diff --git a/public/userarea/include/topbar.php b/public/userarea/include/topbar.php index 735cf14..eee28a3 100644 --- a/public/userarea/include/topbar.php +++ b/public/userarea/include/topbar.php @@ -1,10 +1,10 @@
@@ -95,7 +95,7 @@
-
+ \ No newline at end of file diff --git a/public/userarea/notification_settings.php b/public/userarea/notification_settings.php new file mode 100644 index 0000000..0e8fb9e --- /dev/null +++ b/public/userarea/notification_settings.php @@ -0,0 +1,415 @@ + +getConnection(); + +$userId = (int)($iduserlogin ?? 0); + +if ($userId <= 0) { + die('Utente non valido.'); +} + +if (session_status() === PHP_SESSION_NONE) { + session_start(); +} + +if (empty($_SESSION['notif_settings_csrf'])) { + $_SESSION['notif_settings_csrf'] = bin2hex(random_bytes(32)); +} + +$csrfToken = $_SESSION['notif_settings_csrf']; + +$successMessage = ''; +$errorMessage = ''; + +// Load current user notification preferences. +$stmtUser = $pdo->prepare(" + SELECT + id, + email, + first_name, + last_name, + notify_email, + notify_webapp + FROM auth_users + WHERE id = ? + LIMIT 1 +"); +$stmtUser->execute([$userId]); +$profileUser = $stmtUser->fetch(PDO::FETCH_ASSOC); + +if (!$profileUser) { + die('Utente non trovato.'); +} + +function e($value) +{ + return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $postedToken = $_POST['csrf_token'] ?? ''; + + if (!hash_equals($csrfToken, $postedToken)) { + $errorMessage = 'Sessione non valida. Ricarica la pagina e riprova.'; + } else { + $notifyEmail = isset($_POST['notify_email']) ? 1 : 0; + $notifyWebapp = isset($_POST['notify_webapp']) ? 1 : 0; + + try { + $stmtUpdate = $pdo->prepare(" + UPDATE auth_users + SET + notify_email = :notify_email, + notify_webapp = :notify_webapp, + updated_at = NOW() + WHERE id = :id + LIMIT 1 + "); + + $stmtUpdate->execute([ + ':notify_email' => $notifyEmail, + ':notify_webapp' => $notifyWebapp, + ':id' => $userId, + ]); + + $successMessage = 'Preferenze di notifica aggiornate correttamente.'; + + // Reload updated user. + $stmtUser->execute([$userId]); + $profileUser = $stmtUser->fetch(PDO::FETCH_ASSOC); + + $_SESSION['notif_settings_csrf'] = bin2hex(random_bytes(32)); + $csrfToken = $_SESSION['notif_settings_csrf']; + } catch (Exception $e) { + $errorMessage = 'Errore durante il salvataggio delle preferenze.'; + } + } +} + +$notifyEmail = (int)($profileUser['notify_email'] ?? 0); +$notifyWebapp = (int)($profileUser['notify_webapp'] ?? 0); +?> + + + + + + + + + Notifiche <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?> + + + + + +
+ + + +
+
+ +
+

Preferenze di Notifica

+ + +
+ +
+ + + +
+ +
+ + +
+ + +
+
+
🔔
+
+

Canali di notifica

+

Scegli come vuoi ricevere gli avvisi del sistema

+
+
+ +
+ +
+
+
📧
+
+
Notifiche via Email
+

Ricevi gli avvisi all'indirizzo

+
+
+ +
+ +
+
+
💻
+
+
Notifiche in WebApp
+

Ricevi gli avvisi direttamente nell'applicazione

+
+
+ +
+ +
+ ← Torna alla dashboard + +
+ +
+
+
+
+ +
+
+ + + +
+ + + \ No newline at end of file diff --git a/public/userarea/scadenzario/cron/send_notifications.php b/public/userarea/scadenzario/cron/send_notifications.php index d3f7b38..3ed3c4c 100644 --- a/public/userarea/scadenzario/cron/send_notifications.php +++ b/public/userarea/scadenzario/cron/send_notifications.php @@ -77,6 +77,7 @@ $getRecipients = $pdo->prepare(" AND e.auth_user_id IS NOT NULL AND au.email IS NOT NULL AND au.email != '' + AND au.notify_email = 1 "); // Also get employees from assigned departments @@ -93,6 +94,7 @@ $getDeptRecipients = $pdo->prepare(" AND e.auth_user_id IS NOT NULL AND au.email IS NOT NULL AND au.email != '' + AND au.notify_email = 1 "); $checkSent = $pdo->prepare("