1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace helikopterspark\FlashMsg; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for displaying flash messages |
7
|
|
|
*/ |
8
|
|
|
class FlashMsg implements \Anax\DI\IInjectionAware { |
|
|
|
|
9
|
|
|
|
10
|
|
|
use \Anax\DI\TInjectable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Initialize the controller. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function initialize() { |
18
|
|
|
if (!$this->session->has('flashmsgs')) { |
|
|
|
|
19
|
|
|
$this->session->set('flashmsgs', array()); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add message to session array |
25
|
|
|
* |
26
|
|
|
* @param $type string with message type |
27
|
|
|
* @param $message string with message text |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
9 |
|
public function setMessage($type, $message) { |
32
|
9 |
|
$temp = $this->session->get('flashmsgs'); |
|
|
|
|
33
|
9 |
|
$temp[] = array('type' => $type, 'content' => $message); |
34
|
9 |
|
$this->session->set('flashmsgs', $temp); |
|
|
|
|
35
|
9 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add alert message to session array |
39
|
|
|
* |
40
|
|
|
* @param $message string with message text |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
|
public function alert($message) { |
45
|
1 |
|
$this->setMessage('alert', $message); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add error message to session array |
50
|
|
|
* |
51
|
|
|
* @param $message string with message text |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
1 |
|
public function error($message) { |
56
|
1 |
|
$this->setMessage('error', $message); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add info message to session array |
61
|
|
|
* |
62
|
|
|
* @param $message string with message text |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
1 |
|
public function info($message) { |
67
|
1 |
|
$this->setMessage('info', $message); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add notice message to session array |
72
|
|
|
* |
73
|
|
|
* @param $message string with message text |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
3 |
|
public function notice($message) { |
78
|
3 |
|
$this->setMessage('notice', $message); |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add success message to session array |
83
|
|
|
* |
84
|
|
|
* @param $message string with message text |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
3 |
|
public function success($message) { |
89
|
3 |
|
$this->setMessage('success', $message); |
90
|
3 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add warning message to session array |
94
|
|
|
* |
95
|
|
|
* @param $message string with message text |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
3 |
|
public function warning($message) { |
100
|
3 |
|
$this->setMessage('warning', $message); |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Build HTML of messages in session array |
105
|
|
|
* |
106
|
|
|
* @return $output HTML string with messages |
|
|
|
|
107
|
|
|
*/ |
108
|
1 |
|
public function outputMsgs() { |
109
|
1 |
|
$messages = $this->session->get('flashmsgs'); |
|
|
|
|
110
|
1 |
|
$output = null; |
111
|
|
|
|
112
|
1 |
|
if ($messages) { |
113
|
1 |
|
foreach ($messages as $key => $message) { |
114
|
1 |
|
$output .= '<div class="' . $message['type'] . '"><p>' . $message['content'] . '</p></div>'; |
115
|
1 |
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
return $output; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Clear session message array |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
1 |
|
public function clearMessages() { |
127
|
1 |
|
$this->session->set('flashmsgs', []); |
|
|
|
|
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|