Grouped FlashMessenger View Helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 | <?php /** * FlashMessages view helper * application/modules/admin/views/helpers/FlashMessages.php * * This helper creates an easy method to return groupings of * flash messages by status. * * @author Aaron Bach <bachya1208[at]googlemail.com * @license Free to use - no strings. */ class Zend_View_Helper_FlashMessages { /** * flashMessages function. * * Takes a specially formatted array of flash messages and prepares them * for output. * * SAMPLE INPUT (in, say, a controller): * $this->_flashMessenger->addMessage(array('message' => 'Success message #1', 'status' => 'success')); * $this->_flashMessenger->addMessage(array('message' => 'Error message #1', 'status' => 'error')); * $this->_flashMessenger->addMessage(array('message' => 'Warning message #1', 'status' => 'warning')); * $this->_flashMessenger->addMessage(array('message' => 'Success message #2', 'status' => 'success')); * * SAMPLE OUTPUT (in a view): * <div class="success"> * <ul> * <li>Success message #1</li> * <li>Success message #2</li> * </ul> * </div> * <div class="error">Error message #1</div> * <div class="warning">Warning message #2</div> * * @access public * @param $translator An optional instance of Zend_Translate * @return string HTML of output messages */ public function flashMessages($translator = NULL) { // Set up some variables, including the retrieval of all flash messages. $messages = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->getMessages(); $statMessages = array(); $output = ''; // If there are no messages, don't bother with this whole process. if (count($messages) > 0) { // This chunk of code takes the messages (formatted as in the above sample // input) and puts them into an array of the form: // Array( // [status1] => Array( // [0] => "Message 1" // [1] => "Message 2" // ), // [status2] => Array( // [0] => "Message 1" // [1] => "Message 2" // ) // .... // ) foreach ($messages as $message) { if (!array_key_exists($message['status'], $statMessages)) $statMessages[$message['status']] = array(); if ($translator != NULL && $translator instanceof Zend_Translate) array_push($statMessages[$message['status']], $translator->_($message['message'])); else array_push($statMessages[$message['status']], $message['message']); } // This chunk of code formats messages for HTML output (per // the example in the class comments). foreach ($statMessages as $status => $messages) { $output .= '<div class="' . $status . '">'; // If there is only one message to look at, we don't need to deal with // ul or li - just output the message into the div. if (count($messages) == 1) $output .= $messages[0]; // If there are more than one message, format it in the fashion of the // sample output above. else { $output .= '<ul>'; foreach ($messages as $message) $output .= '<li>' . $message . '</li>'; $output .= '</ul>'; } $output .= '</div>'; } // Return the final HTML string to use. return $output; } } } |
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 20 | Instantiation (demonstrated in controller here): <?php public function init(){ // Enable the flash messenger helper so we can send messages. $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger'); } ... public function deleteAction() { // Tell the user that we succeeded. $this->_flashMessenger->addMessage(array('message' => 'Broker deleted.', 'status' => 'success')); // Redirect user back to edit page. $this->_helper->redirector('edit', 'broker'); } |
1 2 | Usage (in a view): <?php echo($this->flashMessages()); ?> |
Comments
Looks great! Only one comment: if I were you, I'd pass the messages trough the translate view helper. Maybe less important for english only sites, but a need-have for international sites.
I've made some improvements: http://www.zfsnippets.com/snippets/view/id/106/viewhelperflashmessages
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
Whew, so this is a little verbose, but I hope this helps somebody. I spent an entire evening hammering this guy out.
My site implements CSS-based message boxes like those seen here:
http://css.dzone.com/news/css-message-boxes-different-me
I wanted to use these with Zend's FlashMessenger, but short of rewriting the view helper, there's no way to send the status ('error', 'info', 'success', etc.) along with the message. Furthermore, it was a pain to even think of grouping FlashMessenger messages into any sort of logical order.
This view helper tries to address these two issues. Overall, this should made it easier to have flash messages grouped by status type.
The view helper itself is well commented (I hope), so take a look there at how this guy works and what it outputs. I am open to any comments/questions you have! :)
UPDATE: now has support for translation via optional Zend_Translate parameter.
Snippet details
- Created:
-
bachya
- Edited:
-
bachya
- Revision Id:
- 57
- Edit Message:
- Fixed some more documentation (a little clearer)
- Tags:
- view helper flash messages
- Comments:
- 5
- Views:
- 10868
- Points:
- 3 (3 votes)
2 years ago
Great Helper, looked for something like this a while ago, had no joy, will be using it in the future.