engelsystem/includes/helper/message_helper.php

101 lines
2.2 KiB
PHP
Raw Permalink Normal View History

<?php
2023-02-02 22:53:51 +01:00
use Engelsystem\Controllers\NotificationType;
/**
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
*/
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');
}
/**
* 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
{
2023-02-02 22:53:51 +01:00
return alert(NotificationType::INFORMATION, $msg, $immediately);
}
/**
* Renders a warning message
*
* @param string $msg
* @param bool $immediately
* @return string
*/
function warning($msg, $immediately = false)
{
2023-02-02 22:53:51 +01:00
return alert(NotificationType::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
{
2023-02-02 22:53:51 +01:00
return alert(NotificationType::ERROR, $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
{
2023-02-02 22:53:51 +01:00
return alert(NotificationType::MESSAGE, $msg, $immediately);
}
/**
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
* @see \Engelsystem\Controllers\HasUserNotifications
*
* @param NotificationType $type
* @param string $msg
* @param bool $immediately
* @return string
*/
2023-02-02 22:53:51 +01:00
function alert(NotificationType $type, $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) {
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
);
return '<div class="alert alert-' . $type . '" role="alert">' . $msg . '</div>';
}
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
return '';
}