From 49900a6d54b1bc49523fdc5bb83fdb3492c917db Mon Sep 17 00:00:00 2001 From: Hassan A Hashim Date: Tue, 21 Jul 2026 12:27:36 +0300 Subject: [PATCH] implement response helpers for alert and exit with error api --- data/web/inc/response_helpers.inc.php | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 data/web/inc/response_helpers.inc.php diff --git a/data/web/inc/response_helpers.inc.php b/data/web/inc/response_helpers.inc.php new file mode 100644 index 000000000..eafe1b410 --- /dev/null +++ b/data/web/inc/response_helpers.inc.php @@ -0,0 +1,58 @@ + $type, + 'msg' => $message + )); + + if ($should_exit === true) { + exit(); + } +} + +/** + * Clears the session key named `ALERT_SESSION_KEY`. + * + * Session with key named `ALERT_SESSION_KEY` is used to flash messages + * to the UI. We need to clear it at the very end of the request, therefore, + * when user refreshes the browser, won't show the same flash again. + */ +function clear_session_alerts(): void +{ + if (isset($_SESSION[ALERT_SESSION_KEY])) { + unset($_SESSION[ALERT_SESSION_KEY]); + } +} + +/** + * Appends a flash message to the session named `ALERT_SESSION_KEY`. + * + * @param string $type The alert type of bootstrap css library (e.g., 'danger', 'success', 'warning'). + * @param string $message The alert message to display in UI. + */ +function add_session_alert(string $type, string $message): void +{ + $_SESSION[ALERT_SESSION_KEY][] = [ + 'type' => $type, + 'msg' => $message + ]; +}