You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage View
  7. * @since 2010-03-09
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class MsgViewHelper extends ViewHelper
  12. {
  13. const SUCCESS = 'success';
  14. const ERROR = 'error';
  15. protected $get;
  16. public function msg($msg = null, $type = null)
  17. {
  18. if ($msg && $type) {
  19. if (!in_array($type, array(self::SUCCESS, self::ERROR))) {
  20. throw new Exception('Unknown message type: "' . $type . '"');
  21. }
  22. Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
  23. }
  24. return $this;
  25. }
  26. public function success($msg)
  27. {
  28. Session::set(__CLASS__, array('message' => $msg, 'type' => self::SUCCESS));
  29. }
  30. public function error($msg)
  31. {
  32. Session::set(__CLASS__, array('message' => $msg, 'type' => self::ERROR));
  33. }
  34. public function __toString()
  35. {
  36. $msg = Session::get(__CLASS__, false);
  37. if ($msg) {
  38. Session::del(__CLASS__);
  39. return '<div class="' . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
  40. }
  41. return '';
  42. }
  43. }