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 @@ ...@@ -7,7 +7,7 @@
* @license GPL2 or any later version * @license GPL2 or any later version
* @since Stud.IP 4.2 * @since Stud.IP 4.2
*/ */
interface LayoutMessage interface LayoutMessage extends Stringable
{ {
/** /**
* Renders the message as html. * Renders the message as html.
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
* echo MessageBox::success('Nachricht', ['optional details'], true); * echo MessageBox::success('Nachricht', ['optional details'], true);
* *
*/ */
class MessageBox implements LayoutMessage class MessageBox implements LayoutMessage, JsonSerializable
{ {
/** /**
* type and contents of the message box * type and contents of the message box
...@@ -174,4 +174,14 @@ class MessageBox implements LayoutMessage ...@@ -174,4 +174,14 @@ class MessageBox implements LayoutMessage
'counter' => self::$counter++, '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 ...@@ -600,17 +600,10 @@ class PageLayout
$_SESSION['messages'] = []; $_SESSION['messages'] = [];
} }
$structure = [
'type' => $message->class,
'message' => $message->message,
'details' => $message->details,
'closeable' => $message instanceof MessageBox ? $message->isCloseable() : false,
];
if ($id === null) { if ($id === null) {
$_SESSION['messages'][] = $structure; $_SESSION['messages'][] = $message;
} else { } else {
$_SESSION['messages'][$id] = $structure; $_SESSION['messages'][$id] = $message;
} }
} }
...@@ -713,13 +706,15 @@ class PageLayout ...@@ -713,13 +706,15 @@ class PageLayout
* *
* @return array list of MessageBox objects * @return array list of MessageBox objects
*/ */
public static function getMessages() public static function getMessages(string $type = LayoutMessage::class)
{ {
$messages = []; $messages = [];
if (isset($_SESSION['messages'])) { foreach ($_SESSION['messages'] ?? [] as $index => $message) {
$messages = $_SESSION['messages']; if (is_a($message, $type)) {
self::clearMessages(); $messages[$index] = $message;
unset($_SESSION['messages'][$index]);
}
} }
return $messages; return $messages;
......
...@@ -92,11 +92,12 @@ $lang_attr = str_replace('_', '-', $_SESSION['_language']); ...@@ -92,11 +92,12 @@ $lang_attr = str_replace('_', '-', $_SESSION['_language']);
<?= Icon::create('zoom-out2')->asImg(24) ?> <?= Icon::create('zoom-out2')->asImg(24) ?>
</button> </button>
<? endif; ?> <? endif; ?>
<?= implode(PageLayout::getMessages(QuestionBox::class)) ?>
<?= $content_for_layout ?> <?= $content_for_layout ?>
</div> </div>
<system-notification-manager <system-notification-manager
id="system-notifications" 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> placement="<?= User::findCurrent()?->getConfiguration()->SYSTEM_NOTIFICATIONS_PLACEMENT ?? 'topcenter' ?>"></system-notification-manager>
</main> </main>
<!-- End main content --> <!-- 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