2013-09-18 01:38:36 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
use Engelsystem\Controllers\NotificationType;
|
|
|
|
|
2013-09-18 01:38:36 +02:00
|
|
|
/**
|
2023-02-02 22:53:51 +01:00
|
|
|
* Returns messages from session and removes them from the stack by rendering the messages twig template
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2023-02-02 22:53:51 +01:00
|
|
|
* @see \Engelsystem\Controllers\HasUserNotifications
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2023-02-02 22:53:51 +01:00
|
|
|
function msg()
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2023-02-02 22:53:51 +01:00
|
|
|
return view('layouts/parts/messages.twig');
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-26 14:35:01 +01:00
|
|
|
* Renders an information message
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @param bool $immediately
|
2023-12-04 23:33:07 +01:00
|
|
|
* @param bool $immediatelyRaw
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2023-12-04 23:33:07 +01:00
|
|
|
function info($msg, $immediately = false, $immediatelyRaw = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2023-12-04 23:33:07 +01:00
|
|
|
return alert(NotificationType::INFORMATION, $msg, $immediately, $immediatelyRaw);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-26 14:35:01 +01:00
|
|
|
* Renders a warning message
|
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @param bool $immediately
|
2023-12-04 23:33:07 +01:00
|
|
|
* @param bool $immediatelyRaw
|
2017-12-26 14:35:01 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-12-04 23:33:07 +01:00
|
|
|
function warning($msg, $immediately = false, $immediatelyRaw = false)
|
2017-12-26 14:35:01 +01:00
|
|
|
{
|
2023-12-04 23:33:07 +01:00
|
|
|
return alert(NotificationType::WARNING, $msg, $immediately, $immediatelyRaw);
|
2017-12-26 14:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an error message
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @param bool $immediately
|
2023-12-04 23:33:07 +01:00
|
|
|
* @param bool $immediatelyRaw
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2023-12-04 23:33:07 +01:00
|
|
|
function error($msg, $immediately = false, $immediatelyRaw = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2023-12-04 23:33:07 +01:00
|
|
|
return alert(NotificationType::ERROR, $msg, $immediately, $immediatelyRaw);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-26 14:35:01 +01:00
|
|
|
* Renders a success message
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @param bool $immediately
|
2023-12-04 23:33:07 +01:00
|
|
|
* @param bool $immediatelyRaw
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2023-12-04 23:33:07 +01:00
|
|
|
function success($msg, $immediately = false, $immediatelyRaw = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2023-12-04 23:33:07 +01:00
|
|
|
return alert(NotificationType::MESSAGE, $msg, $immediately, $immediatelyRaw);
|
2016-09-29 09:49:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-02 22:53:51 +01:00
|
|
|
* Renders an alert message with the given alert-* class or sets it in session
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2023-02-02 22:53:51 +01:00
|
|
|
* @param NotificationType $type
|
|
|
|
* @param string $msg
|
|
|
|
* @param bool $immediately
|
2023-12-04 23:33:07 +01:00
|
|
|
* @param bool $immediatelyRaw
|
2017-08-31 12:25:06 +02:00
|
|
|
* @return string
|
2023-12-04 23:33:07 +01:00
|
|
|
*
|
|
|
|
* @see \Engelsystem\Controllers\HasUserNotifications
|
|
|
|
*
|
2016-09-29 09:49:25 +02:00
|
|
|
*/
|
2023-12-04 23:33:07 +01:00
|
|
|
function alert(NotificationType $type, $msg, $immediately = false, $immediatelyRaw = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-08-31 12:25:06 +02:00
|
|
|
if (empty($msg)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
if ($immediately) {
|
2023-02-02 22:53:51 +01:00
|
|
|
$type = str_replace(
|
|
|
|
[
|
|
|
|
NotificationType::ERROR->value,
|
|
|
|
NotificationType::WARNING->value,
|
|
|
|
NotificationType::INFORMATION->value,
|
|
|
|
NotificationType::MESSAGE->value,
|
|
|
|
],
|
|
|
|
['danger', 'warning', 'info', 'success'],
|
|
|
|
$type->value
|
|
|
|
);
|
2023-12-04 23:33:07 +01:00
|
|
|
$msg = $immediatelyRaw ? $msg : htmlspecialchars($msg);
|
2023-02-02 22:53:51 +01:00
|
|
|
return '<div class="alert alert-' . $type . '" role="alert">' . $msg . '</div>';
|
2016-09-29 09:49:25 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$type = 'messages.' . $type->value;
|
2018-08-11 23:46:28 +02:00
|
|
|
$session = session();
|
2023-02-02 22:53:51 +01:00
|
|
|
$messages = $session->get($type, []);
|
|
|
|
$messages[] = $msg;
|
|
|
|
$session->set($type, $messages);
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2017-08-31 12:25:06 +02:00
|
|
|
return '';
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|