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