1 | <?php |
||
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() |
|
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 |
||
128 | |||
129 | /** |
||
130 | * Returns the flash messenger |
||
131 | * |
||
132 | * @return Zend_Controller_Action_Helper_FlashMessenger |
||
133 | */ |
||
134 | 12 | protected function _getFlashMessenger() |
|
142 | } |
||
143 |