diff --git a/lib/classes/LayoutMessage.php b/lib/classes/LayoutMessage.php index 7072788490fa20ed7d0ecceed57bbefb9e69c5c7..d93f509c82b1e32cecffc84a532ea5c334cde97e 100644 --- a/lib/classes/LayoutMessage.php +++ b/lib/classes/LayoutMessage.php @@ -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. diff --git a/lib/classes/MessageBox.php b/lib/classes/MessageBox.php index 3573018d2e6d6ceffc1a20a7fcffe138697a630c..8c203a5843f0f54ebe1bfa37b0dc5dbc4226906b 100644 --- a/lib/classes/MessageBox.php +++ b/lib/classes/MessageBox.php @@ -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(), + ]; + } } diff --git a/lib/classes/PageLayout.php b/lib/classes/PageLayout.php index 4d4ef3ad0426f5b7859e8c0347d52873ad239d93..ed5e1aefdff89282a38a4513edcbc7aa694ece46 100644 --- a/lib/classes/PageLayout.php +++ b/lib/classes/PageLayout.php @@ -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; diff --git a/templates/layouts/base.php b/templates/layouts/base.php index 1f2ac32e7116d80fc2ad5ce6b962344fb2f48ab2..077cdf4a2e2ffa3064819c5cf09aad8c1604fd9a 100644 --- a/templates/layouts/base.php +++ b/templates/layouts/base.php @@ -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 -->