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; $_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 * Register CSS file(s) to load on next displayed page
* @param string|array<string> $args CSS files to load * @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')); self :: add_js_file(App::get('templates.included_js_files', array(), 'array'));
// Messages // Messages
self :: assign('errors', (isset($_SESSION['errors'])?$_SESSION['errors']:array())); self :: assign('errors', self :: get_errors());
self :: assign('messages', (isset($_SESSION['messages'])?$_SESSION['messages']:array())); self :: assign('messages', self :: get_messages());
// Files inclusions // Files inclusions
self :: assign('css', self :: $css_files); self :: assign('css', self :: $css_files);
@ -388,8 +426,8 @@ class Tpl {
Log :: fatal(I18n::_("An error occurred while displaying this page.")); Log :: fatal(I18n::_("An error occurred while displaying this page."));
return; return;
} }
unset($_SESSION['errors']); self :: purge_errors();
unset($_SESSION['messages']); self :: purge_messages();
Hook :: trigger('after_displaying_template'); Hook :: trigger('after_displaying_template');
$sentry_span->finish(); $sentry_span->finish();
} }
@ -408,14 +446,14 @@ class Tpl {
elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200) elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200)
http_response_code(400); http_response_code(400);
if (isset($_SESSION['messages']) && !empty($_SESSION['messages'])) { $data['messages'] = self :: get_messages();
$data['messages'] = $_SESSION['messages']; if (!$data['messages']) unset($data['messages']);
unset($_SESSION['messages']); self :: purge_messages();
}
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) { $data['errors'] = self :: get_errors();
$data['errors'] = $_SESSION['errors']; if (!$data['errors']) unset($data['errors']);
unset($_SESSION['errors']); self :: purge_errors();
}
if (self :: $_debug_ajax) if (self :: $_debug_ajax)
Log :: debug("AJAX Response : ".vardump($data)); Log :: debug("AJAX Response : ".vardump($data));
header('Content-Type: application/json'); header('Content-Type: application/json');