From 9cc0f8581adda394aa2439a4180742bdafb44158 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 15 Feb 2023 23:51:11 +0100 Subject: [PATCH] Tpl: add get_errors() / get_messages() & purge_errors() / purge_messages() methods --- src/Tpl.php | 62 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/src/Tpl.php b/src/Tpl.php index db81899..30a13e1 100644 --- a/src/Tpl.php +++ b/src/Tpl.php @@ -262,6 +262,44 @@ class Tpl { $_SESSION['messages'][] = $message; } + /** + * Get errors + * @return array + */ + public static function get_errors() { + if(isset($_SESSION['errors']) && is_array($_SESSION['errors'])) + return $_SESSION['errors']; + return array(); + } + + /** + * Get messages + * @return array + */ + public static function get_messages() { + if(isset($_SESSION['messages']) && is_array($_SESSION['messages'])) + return $_SESSION['messages']; + return array(); + } + + /** + * Purge messages + * @return void + */ + public static function purge_errors() { + if(isset($_SESSION['errors'])) + unset($_SESSION['errors']); + } + + /** + * Purge messages + * @return void + */ + public static function purge_messages() { + if(isset($_SESSION['messages'])) + unset($_SESSION['messages']); + } + /** * Register CSS file(s) to load on next displayed page * @param string|array $args CSS files to load @@ -329,8 +367,8 @@ class Tpl { self :: add_js_file(App::get('templates.included_js_files', array(), 'array')); // Messages - self :: assign('errors', (isset($_SESSION['errors'])?$_SESSION['errors']:array())); - self :: assign('messages', (isset($_SESSION['messages'])?$_SESSION['messages']:array())); + self :: assign('errors', self :: get_errors()); + self :: assign('messages', self :: get_messages()); // Files inclusions self :: assign('css', self :: $css_files); @@ -388,8 +426,8 @@ class Tpl { Log :: fatal(I18n::_("An error occurred while displaying this page.")); return; } - unset($_SESSION['errors']); - unset($_SESSION['messages']); + self :: purge_errors(); + self :: purge_messages(); Hook :: trigger('after_displaying_template'); $sentry_span->finish(); } @@ -408,14 +446,14 @@ class Tpl { elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200) http_response_code(400); - if (isset($_SESSION['messages']) && !empty($_SESSION['messages'])) { - $data['messages'] = $_SESSION['messages']; - unset($_SESSION['messages']); - } - if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) { - $data['errors'] = $_SESSION['errors']; - unset($_SESSION['errors']); - } + $data['messages'] = self :: get_messages(); + if (!$data['messages']) unset($data['messages']); + self :: purge_messages(); + + $data['errors'] = self :: get_errors(); + if (!$data['errors']) unset($data['errors']); + self :: purge_errors(); + if (self :: $_debug_ajax) Log :: debug("AJAX Response : ".vardump($data)); header('Content-Type: application/json');