Tpl: add get_errors() / get_messages() & purge_errors() / purge_messages() methods

This commit is contained in:
Benjamin Renard 2023-02-15 23:51:11 +01:00
parent 98f61c36eb
commit 9cc0f8581a

View file

@ -262,6 +262,44 @@ class Tpl {
$_SESSION['messages'][] = $message;
}
/**
* Get errors
* @return array<string>
*/
public static function get_errors() {
if(isset($_SESSION['errors']) && is_array($_SESSION['errors']))
return $_SESSION['errors'];
return array();
}
/**
* Get messages
* @return array<string>
*/
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<string> $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');