engelsystem/includes/helper/message_helper.php

108 lines
2.6 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
2023-12-04 23:33:07 +01:00
* @param bool $immediatelyRaw
2017-01-03 03:22:48 +01:00
* @return string
*/
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);
}
/**
* Renders a warning message
*
* @param string $msg
* @param bool $immediately
2023-12-04 23:33:07 +01:00
* @param bool $immediatelyRaw
* @return string
*/
2023-12-04 23:33:07 +01:00
function warning($msg, $immediately = false, $immediatelyRaw = false)
{
2023-12-04 23:33:07 +01:00
return alert(NotificationType::WARNING, $msg, $immediately, $immediatelyRaw);
}
/**
* 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
*/
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);
}
/**
* 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
*/
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);
}
/**
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
* @return string
2023-12-04 23:33:07 +01:00
*
* @see \Engelsystem\Controllers\HasUserNotifications
*
*/
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
{
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>';
}
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 '';
}