Skip to content
Snippets Groups Projects
Commit 1d8dfc51 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

fix handling of different LayoutMessage types, fixes #4345

Closes #4345

Merge request studip/studip!3148
parent 8fc39a2c
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
* @license GPL2 or any later version
* @since Stud.IP 4.2
*/
interface LayoutMessage
interface LayoutMessage extends Stringable
{
/**
* Renders the message as html.
......
......@@ -31,7 +31,7 @@
* echo MessageBox::success('Nachricht', ['optional details'], true);
*
*/
class MessageBox implements LayoutMessage
class MessageBox implements LayoutMessage, JsonSerializable
{
/**
* type and contents of the message box
......@@ -174,4 +174,14 @@ class MessageBox implements LayoutMessage
'counter' => self::$counter++,
]);
}
public function jsonSerialize(): mixed
{
return [
'type' => $this->class,
'message' => $this->message,
'details' => $this->details,
'closeable' => $this->isCloseable(),
];
}
}
......@@ -600,17 +600,10 @@ class PageLayout
$_SESSION['messages'] = [];
}
$structure = [
'type' => $message->class,
'message' => $message->message,
'details' => $message->details,
'closeable' => $message instanceof MessageBox ? $message->isCloseable() : false,
];
if ($id === null) {
$_SESSION['messages'][] = $structure;
$_SESSION['messages'][] = $message;
} else {
$_SESSION['messages'][$id] = $structure;
$_SESSION['messages'][$id] = $message;
}
}
......@@ -713,13 +706,15 @@ class PageLayout
*
* @return array list of MessageBox objects
*/
public static function getMessages()
public static function getMessages(string $type = LayoutMessage::class)
{
$messages = [];
if (isset($_SESSION['messages'])) {
$messages = $_SESSION['messages'];
self::clearMessages();
foreach ($_SESSION['messages'] ?? [] as $index => $message) {
if (is_a($message, $type)) {
$messages[$index] = $message;
unset($_SESSION['messages'][$index]);
}
}
return $messages;
......
......@@ -92,11 +92,12 @@ $lang_attr = str_replace('_', '-', $_SESSION['_language']);
<?= Icon::create('zoom-out2')->asImg(24) ?>
</button>
<? endif; ?>
<?= implode(PageLayout::getMessages(QuestionBox::class)) ?>
<?= $content_for_layout ?>
</div>
<system-notification-manager
id="system-notifications"
:notifications='<?= htmlReady(json_encode(PageLayout::getMessages())) ?>'
:notifications='<?= htmlReady(json_encode(PageLayout::getMessages(MessageBox::class))) ?>'
placement="<?= User::findCurrent()?->getConfiguration()->SYSTEM_NOTIFICATIONS_PLACEMENT ?? 'topcenter' ?>"></system-notification-manager>
</main>
<!-- End main content -->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment