xref: /webtrees/app/FlashMessages.php (revision 7fb00f20f3d1ff7197d3b4f35893144badc6644f)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class FlashMessages - Flash messages allow us to generate messages
21 * in one context, and display them in another.
22 */
23class FlashMessages {
24	// Session storage key
25	const FLASH_KEY = 'flash_messages';
26
27	/**
28	 * Add a new message to the session storage.
29	 *
30	 * @param string $text
31	 * @param string $status "success", "info", "warning" or "danger"
32	 */
33	public static function addMessage($text, $status = 'info') {
34		$message         = new \stdClass;
35		$message->text   = $text;
36		$message->status = $status;
37
38		$messages = Session::get(self::FLASH_KEY, array());
39		$messages[] = $message;
40		Session::put(self::FLASH_KEY, $messages);
41	}
42
43	/**
44	 * Get the current messages, and remove them from session storage.
45	 *
46	 * @return string[]
47	 */
48	public static function getMessages() {
49		$messages = Session::get(self::FLASH_KEY, array());
50		Session::forget(self::FLASH_KEY);
51
52		return $messages;
53	}
54}
55