Completed
Pull Request — master (#526)
by Michael
16:45 queued 06:57
created

SessionAlert   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
ccs 0
cts 26
cp 0
wmc 11
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A quick() 0 3 1
A __construct() 0 7 1
A append() 0 10 2
A success() 0 3 1
A error() 0 3 1
A warning() 0 3 1
A getAlertBox() 0 3 1
A retrieve() 0 12 3
1
<?php
2
3
/**
4
 * Session Alerts
5
 *
6
 * Session alerts allow you to raise a message to be shown to the user on the
7
 * next page load, allowing you to still give a user a message after a
8
 * redirect. It's a lot nicer to deal with than redirecting to a message page,
9
 * or sending the user somewhere with error message parameters.
10
 *
11
 * It's advisable to use the static methods, unless you need something more
12
 * customised. The defaults should tie you over nicely.
13
 */
14
class SessionAlert
15
{
16
	private $message;
17
	private $title;
18
	private $type;
19
	private $closable;
20
	private $block;
21
22
	/**
23
	 * @param string $message
24
	 * @param string $title
25
	 * @param string $type
26
	 * @param bool $closable
27
	 * @param bool $block
28
	 */
29
	public function __construct($message, $title, $type = "alert-info", $closable = true, $block = true)
30
	{
31
		$this->message = $message;
32
		$this->title = $title;
33
		$this->type = $type;
34
		$this->closable = $closable;
35
		$this->block = $block;
36
	}
37
38
	public function getAlertBox()
39
	{
40
		return BootstrapSkin::displayAlertBox($this->message, $this->type, $this->title, $this->block, $this->closable, true);
41
	}
42
43
	/**
44
	 * Shows a quick one-liner message
45
	 * @param string $message
46
	 * @param string $type
47
	 */
48
	public static function quick($message, $type = "alert-info")
49
	{
50
		self::append(new SessionAlert($message, "", $type, true, false));
51
	}
52
53
	public static function success($message)
54
	{
55
		self::append(new SessionAlert($message, "", "alert-success", true, true));
56
	}
57
58
	public static function warning($message, $title = "Warning!")
59
	{
60
		self::append(new SessionAlert($message, $title, "alert-warning", true, true));
61
	}
62
63
	public static function error($message, $title = "Error!")
64
	{
65
		self::append(new SessionAlert($message, $title, "alert-error", true, true));
66
	}
67
68
	public static function append(SessionAlert $alert)
69
	{
70
		$data = array();
71
		if (isset($_SESSION['alerts'])) {
72
			$data = $_SESSION['alerts'];
73
		}
74
75
		$data[] = serialize($alert);
76
77
		$_SESSION['alerts'] = $data;
78
	}
79
80
	public static function retrieve()
81
	{
82
		$block = array();
83
		if (isset($_SESSION['alerts'])) {
84
			foreach ($_SESSION['alerts'] as $a) {
85
				$block[] = unserialize($a);
86
			}
87
		}
88
89
		$_SESSION['alerts'] = array();
90
91
		return $block;
92
	}
93
}
94