From a8bf994ae59bc66d7ac8a7a1d666eaefb78404b6 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 7 Jan 2025 13:02:57 +0200 Subject: [PATCH] Messages system troubleshoot --- app/classes/messages.php | 15 +++++++++++---- app/includes/messages-show.php | 3 +-- app/includes/messages.php | 4 +++- app/pages/login.php | 2 +- public_html/index.php | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/classes/messages.php b/app/classes/messages.php index 99da945..2e8c0f1 100644 --- a/app/classes/messages.php +++ b/app/classes/messages.php @@ -124,12 +124,13 @@ class Messages { /** * Render message HTML */ - public static function render($category, $key, $customMessage = null, $dismissible = false, $small = false) { + // Usage: echo Messages::render('LOGIN', 'LOGIN_SUCCESS', 'custom message [or null]', true [for dismissible; or null], true [for small; or omit]); + public static function render($category, $key, $customMessage = null, $dismissible = null, $small = false) { $config = self::get($category, $key); if (!$config) return ''; $message = $customMessage ?? $config['message']; - $isDismissible = $dismissible ?? $config['dismissible']; + $isDismissible = $dismissible ?? $config['dismissible'] ?? false; $dismissClass = $isDismissible ? ' alert-dismissible fade show' : ''; $dismissButton = $isDismissible ? '' : ''; $smallClass = $small ? ' alert-sm' : ''; @@ -147,15 +148,21 @@ class Messages { /** * Store message in session for display after redirect */ - public static function flash($category, $key, $customMessage = null, $dismissible = false, $small = false) { + // Usage: Messages::flash('LOGIN', 'LOGIN_SUCCESS', 'custom message [or null]', true [for dismissible; or null], true [for small; or omit]); + public static function flash($category, $key, $customMessage = null, $dismissible = null, $small = false) { if (!isset($_SESSION['flash_messages'])) { $_SESSION['flash_messages'] = []; } + + // Get the message configuration + $config = self::get($category, $key); + $isDismissible = $dismissible ?? $config['dismissible'] ?? false; + $_SESSION['flash_messages'][] = [ 'category' => $category, 'key' => $key, 'custom_message' => $customMessage, - 'dismissible' => $dismissible, + 'dismissible' => $isDismissible, 'small' => $small ]; } diff --git a/app/includes/messages-show.php b/app/includes/messages-show.php index 9cc4ddf..7ff8f6f 100644 --- a/app/includes/messages-show.php +++ b/app/includes/messages-show.php @@ -1,8 +1,7 @@ $flash['category'], 'key' => $flash['key'], - 'custom_message' => $flash['custom_message'] + 'custom_message' => $flash['custom_message'] ?? null, + 'dismissible' => $flash['dismissible'] ?? false, + 'small' => $flash['small'] ?? false ]; }, $flash_messages)); } diff --git a/app/pages/login.php b/app/pages/login.php index 6adedc2..3e08791 100644 --- a/app/pages/login.php +++ b/app/pages/login.php @@ -74,7 +74,7 @@ try { $logObject->insertLog($user_id, "Login: User \"$username\" logged in. IP: $user_IP", 'user'); // Set success message and redirect - Messages::flash('LOGIN', 'LOGIN_SUCCESS'); + Messages::flash('LOGIN', 'LOGIN_SUCCESS', null, true); header('Location: ' . htmlspecialchars($app_root)); exit(); } else { diff --git a/public_html/index.php b/public_html/index.php index 767fd36..b3e4e92 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -22,7 +22,7 @@ require '../app/includes/sanitize.php'; require_once '../app/classes/messages.php'; $messages = []; -include '../app/includes/messages.php'; +//include '../app/includes/messages.php'; require '../app/includes/errors.php'; @@ -189,7 +189,7 @@ if ($page == 'logout') { $server_endpoint = '/health'; $server_status = $serverObject->getServerStatus($server_host, $server_port, $server_endpoint); if (!$server_status) { - echo Messages::render('ERROR', 'DEFAULT', 'The Jilo Server is not running. Some data may be old and incorrect.', false, true); + Messages::flash('ERROR', 'DEFAULT', 'The Jilo Server is not running. Some data may be old and incorrect.', false, true); } }