1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mQueue\View\Helper; |
4
|
|
|
|
5
|
|
|
use Zend_Controller_Action_HelperBroker; |
6
|
|
|
use Zend_View_Helper_Abstract; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Noumenal PHP Library. |
10
|
|
|
* |
11
|
|
|
* PHP classes built on top of Zend Framework. (http://framework.zend.com/) |
12
|
|
|
* |
13
|
|
|
* Bug Reports: [email protected] |
14
|
|
|
* Questions : https://noumenal.fogbugz.com/default.asp?noumenal |
15
|
|
|
* |
16
|
|
|
* LICENSE |
17
|
|
|
* |
18
|
|
|
* This source file is subject to the new BSD license that is bundled |
19
|
|
|
* with this package in the file noumenal-new-bsd-licence.txt. |
20
|
|
|
* It is also available through the world-wide-web at this URL: |
21
|
|
|
* |
22
|
|
|
* http://noumenal.co.uk/license/new-bsd |
23
|
|
|
* |
24
|
|
|
* If you did not receive a copy of the license and are unable to |
25
|
|
|
* obtain it through the world-wide-web, please send an email |
26
|
|
|
* to [email protected] so we can send you a copy immediately. |
27
|
|
|
* |
28
|
|
|
* ATTRIBUTION |
29
|
|
|
* |
30
|
|
|
* Beyond maintaining the Copyright Notice and Licence, attribution is |
31
|
|
|
* appreciated but not required. Please attribute where appropriate by |
32
|
|
|
* linking to: |
33
|
|
|
* |
34
|
|
|
* http://noumenal.co.uk/ |
35
|
|
|
* |
36
|
|
|
* @author Carlton Gibson <[email protected]> |
37
|
|
|
* @copyright Copyright (c) 2009 Noumenal Software Ltd. (http://noumenal.co.uk/) |
38
|
|
|
* @license http://noumenal.co.uk/license/new-bsd New BSD License |
39
|
|
|
* |
40
|
|
|
* @version $Revision: 3 $ $Date: 2009-08-13 16:02:49 +0100 (Thu, 13 Aug 2009) $ modified for specific purpose |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* View Helper to Display Flash Messages. |
45
|
|
|
* |
46
|
|
|
* Checks for messages from previous requests and from the current request. |
47
|
|
|
* |
48
|
|
|
* Checks for `array($key => $value)` pairs in FlashMessenger's messages array. |
49
|
|
|
* If such a pair is found, $key is taken as the "message level", $value as the |
50
|
|
|
* message. (Simple strings are provided a default level of 'warning'.) |
51
|
|
|
* |
52
|
|
|
* NOTE: MESSAGES ARE PRESUMED TO BE SAFE HTML. IF REDISPLAYING USER |
53
|
|
|
* INPUT, ESCAPE ALL MESSAGES PRIOR TO ADDING TO FLASHMESSENGER. |
54
|
|
|
*/ |
55
|
|
|
class FlashMessenger extends Zend_View_Helper_Abstract |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var Zend_Controller_Action_Helper_FlashMessenger |
59
|
|
|
*/ |
60
|
|
|
private $_flashMessenger; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If the flashmessenger is postoned it will do nothing on first call. |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $isPostponed = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Initializing stuff. Provides fluid interface. |
71
|
|
|
*/ |
72
|
12 |
|
public function flashMessenger() |
|
|
|
|
73
|
|
|
{ |
74
|
12 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render the flash messages |
79
|
|
|
* |
80
|
|
|
* @return string flash messages formatted as div |
81
|
|
|
*/ |
82
|
12 |
|
public function __toString() |
83
|
|
|
{ |
84
|
12 |
|
if ($this->isPostponed) { |
85
|
|
|
$this->isPostponed = false; |
86
|
|
|
|
87
|
|
|
return ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
12 |
|
$flashMessenger = $this->_getFlashMessenger(); |
91
|
|
|
|
92
|
|
|
//get messages from previous requests |
93
|
12 |
|
$messages = $flashMessenger->getMessages(); |
94
|
|
|
|
95
|
|
|
//add any messages from this request |
96
|
12 |
|
if ($flashMessenger->hasCurrentMessages()) { |
97
|
|
|
$messages = array_merge( |
98
|
|
|
$messages, $flashMessenger->getCurrentMessages() |
99
|
|
|
); |
100
|
|
|
//we don't need to display them twice. |
101
|
|
|
$flashMessenger->clearCurrentMessages(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
//initialise return string |
105
|
12 |
|
$output = ''; |
106
|
|
|
|
107
|
|
|
//process messages |
108
|
12 |
|
foreach ($messages as $message) { |
109
|
|
|
$level = 'notice'; |
110
|
|
|
if (is_array($message)) { |
111
|
|
|
[$level, $message] = each($message); |
112
|
|
|
} |
113
|
|
|
$output .= '<div class="flashmessenger ' . $level . '">' . $message . '</div>'; |
114
|
|
|
} |
115
|
|
|
|
116
|
12 |
|
return $output; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Postpone the flash messages to the next next call. The next call will return empty string. |
121
|
|
|
* |
122
|
|
|
* @param $isPostponed |
123
|
|
|
*/ |
124
|
|
|
public function postpone($isPostponed = true): void |
125
|
|
|
{ |
126
|
|
|
$this->isPostponed = $isPostponed; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the flash messenger |
131
|
|
|
* |
132
|
|
|
* @return Zend_Controller_Action_Helper_FlashMessenger |
133
|
|
|
*/ |
134
|
12 |
|
protected function _getFlashMessenger() |
135
|
|
|
{ |
136
|
12 |
|
if (null === $this->_flashMessenger) { |
137
|
12 |
|
$this->_flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger'); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
12 |
|
return $this->_flashMessenger; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|