Cleans up error/notice messages
parent
96de30f3e0
commit
c5e123ea2f
|
@ -1,5 +1,16 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Generate an error or notice message based on the environment.
|
||||
*
|
||||
* In a production environment, hides detailed error messages and returns
|
||||
* a generic message. In other environments, returns the provided message.
|
||||
*
|
||||
* @param string $message A user-friendly message to display.
|
||||
* @param string $error The detailed error message for debugging (optional).
|
||||
* @param string|null $environment The environment type ('production', 'development', etc.). If null, defaults to the configured environment.
|
||||
* @return string The appropriate message based on the environment.
|
||||
*/
|
||||
function getError($message, $error = '', $environment = null) {
|
||||
global $config;
|
||||
$environment = $config['environment'] ?? 'production';
|
||||
|
@ -11,4 +22,20 @@ function getError($message, $error = '', $environment = null) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a message if it exists, and optionally unset it after display.
|
||||
*
|
||||
* @param string $message The message to display.
|
||||
* @param string $type The type of message (e.g., 'error', 'notice').
|
||||
* @param bool $unset Whether to unset the message after display.
|
||||
*/
|
||||
function renderMessage(&$message, $type, $unset = false) {
|
||||
if (isset($message)) {
|
||||
echo "\t\t<div class=\"{$type}\">" . $message . "</div>\n";
|
||||
if ($unset) {
|
||||
$message = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -1,24 +1,18 @@
|
|||
<?php
|
||||
|
||||
// 'error' for errors
|
||||
if (isset($_SESSION['error'])) {
|
||||
echo "\t\t" . '<div class="error">' . $_SESSION['error'] . '</div>';
|
||||
// Display and clean up session messages
|
||||
foreach (['error', 'notice'] as $type) {
|
||||
if (isset($_SESSION[$type])) {
|
||||
renderMessage($_SESSION[$type], $type, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 'notice' for all non-critical messages
|
||||
if (isset($_SESSION['notice'])) {
|
||||
echo "\t\t" . '<div class="notice">' . $_SESSION['notice'] . '</div>';
|
||||
}
|
||||
|
||||
// 'error' for errors
|
||||
// Display standalone messages
|
||||
if (isset($error)) {
|
||||
echo "\t\t" . '<div class="error">' . $error . '</div>';
|
||||
renderMessage($error, 'error', true);
|
||||
}
|
||||
|
||||
// 'notice' for all non-critical messages
|
||||
if (isset($_SESSION['notice'])) {
|
||||
echo "\t\t" . '<div class="notice">' . $_SESSION['notice'] . '</div>';
|
||||
if (isset($notice)) {
|
||||
renderMessage($notice, 'notice', true);
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue