Messages system troubleshoot

main
Yasen Pramatarov 2025-01-07 13:02:57 +02:00
parent 708a50bcf8
commit a8bf994ae5
5 changed files with 18 additions and 10 deletions

View File

@ -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 ? '<button type="button" class="btn-close' . ($small ? ' btn-close-sm' : '') . '" data-bs-dismiss="alert" aria-label="Close"></button>' : '';
$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
];
}

View File

@ -1,8 +1,7 @@
<?php
if (isset($messages) && is_array($messages)) {
foreach ($messages as $msg) {
echo Messages::render($msg['category'], $msg['key'], $msg['custom_message'] ?? null);
echo Messages::render($msg['category'], $msg['key'], $msg['custom_message'] ?? null, $msg['dismissible'] ?? false, $msg['small'] ?? false);
}
}

View File

@ -7,7 +7,9 @@ if (!empty($flash_messages)) {
return [
'category' => $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));
}

View File

@ -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 {

View File

@ -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);
}
}