xref: /webtrees/app/FlashMessages.php (revision d11be7027e34e3121be11cc025421873364403f9)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22d8809d62SGreg Roachuse function is_array;
23d8809d62SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * Generate messages in one request and display them in the next.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass FlashMessages
28c1010edaSGreg Roach{
297fb00f20SGreg Roach    // Session storage key
3016d6367aSGreg Roach    private const FLASH_KEY = 'flash_messages';
317fb00f20SGreg Roach
32a25f0a04SGreg Roach    /**
3329bb8efbSGreg Roach     * Add a message to the session storage.
34a25f0a04SGreg Roach     *
35a25f0a04SGreg Roach     * @param string $text
36a25f0a04SGreg Roach     * @param string $status "success", "info", "warning" or "danger"
377e96c925SGreg Roach     *
387e96c925SGreg Roach     * @return void
39a25f0a04SGreg Roach     */
4073d58381SGreg Roach    public static function addMessage(string $text, string $status = 'info'): void
41c1010edaSGreg Roach    {
42d8809d62SGreg Roach        $messages = Session::get(self::FLASH_KEY);
43d8809d62SGreg Roach        $messages = is_array($messages) ? $messages : [];
44f70bcff5SGreg Roach
45f70bcff5SGreg Roach        $messages[] = (object) [
46f70bcff5SGreg Roach            'text'   => $text,
47f70bcff5SGreg Roach            'status' => $status,
48f70bcff5SGreg Roach        ];
49f70bcff5SGreg Roach
507fb00f20SGreg Roach        Session::put(self::FLASH_KEY, $messages);
51a25f0a04SGreg Roach    }
52a25f0a04SGreg Roach
53a25f0a04SGreg Roach    /**
54a25f0a04SGreg Roach     * Get the current messages, and remove them from session storage.
55a25f0a04SGreg Roach     *
56f70bcff5SGreg Roach     * @return array<object>
57a25f0a04SGreg Roach     */
588f53f488SRico Sonntag    public static function getMessages(): array
59c1010edaSGreg Roach    {
60d8809d62SGreg Roach        $messages = Session::pull(self::FLASH_KEY);
61d8809d62SGreg Roach
62d8809d62SGreg Roach        return is_array($messages) ? $messages : [];
63a25f0a04SGreg Roach    }
64a25f0a04SGreg Roach}
65