engelsystem/includes/helper/message_helper.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* Returns messages from session and removes them from the stack
2022-12-08 17:40:24 +01:00
* @param bool $includeMessagesFromNewProcedure
* If set, the messages from the new procedure are also included.
* The output will be similar to how it would be with messages.twig.
* @see \Engelsystem\Controllers\HasUserNotifications
2017-01-03 03:22:48 +01:00
* @return string
*/
2022-12-08 17:40:24 +01:00
function msg(bool $includeMessagesFromNewProcedure = false)
2017-01-02 03:57:23 +01:00
{
2017-08-30 19:57:01 +02:00
$session = session();
$message = $session->get('msg', '');
$session->set('msg', '');
2022-12-08 17:40:24 +01:00
if ($includeMessagesFromNewProcedure) {
foreach (session()->get('errors', []) as $msg) {
$message .= error(__($msg), true);
}
foreach (session()->get('warnings', []) as $msg) {
$message .= warning(__($msg), true);
}
foreach (session()->get('information', []) as $msg) {
$message .= info(__($msg), true);
}
foreach (session()->get('messages', []) as $msg) {
$message .= success(__($msg), true);
}
foreach (['errors', 'warnings', 'information', 'messages'] as $type) {
session()->remove($type);
}
}
2017-08-30 19:57:01 +02:00
return $message;
}
/**
* Renders an information message
2017-01-03 03:22:48 +01:00
*
* @param string $msg
* @param bool $immediately
* @return string
*/
2017-01-03 03:22:48 +01:00
function info($msg, $immediately = false)
2017-01-02 03:57:23 +01:00
{
2017-01-03 03:22:48 +01:00
return alert('info', $msg, $immediately);
}
/**
* Renders a warning message
*
* @param string $msg
* @param bool $immediately
* @return string
*/
function warning($msg, $immediately = false)
{
return alert('warning', $msg, $immediately);
}
/**
* Renders an error message
2017-01-03 03:22:48 +01:00
*
* @param string $msg
* @param bool $immediately
* @return string
*/
2017-01-03 03:22:48 +01:00
function error($msg, $immediately = false)
2017-01-02 03:57:23 +01:00
{
2017-01-03 03:22:48 +01:00
return alert('danger', $msg, $immediately);
}
/**
* Renders a success message
2017-01-03 03:22:48 +01:00
*
* @param string $msg
* @param bool $immediately
* @return string
*/
2017-01-03 03:22:48 +01:00
function success($msg, $immediately = false)
2017-01-02 03:57:23 +01:00
{
2017-01-03 03:22:48 +01:00
return alert('success', $msg, $immediately);
}
/**
* Renders an alert message with the given alert-* class.
2017-01-03 03:22:48 +01:00
*
* @param string $class
* @param string $msg
* @param bool $immediately
* @return string
*/
2017-01-03 03:22:48 +01:00
function alert($class, $msg, $immediately = false)
2017-01-02 03:57:23 +01:00
{
if (empty($msg)) {
return '';
}
2017-01-03 03:22:48 +01:00
if ($immediately) {
2017-01-02 03:57:23 +01:00
return '<div class="alert alert-' . $class . '">' . $msg . '</div>';
}
2017-01-02 15:43:36 +01:00
2018-08-11 23:46:28 +02:00
$session = session();
2017-08-30 19:57:01 +02:00
$message = $session->get('msg', '');
$message .= alert($class, $msg, true);
$session->set('msg', $message);
2017-01-03 03:22:48 +01:00
return '';
}