75 lines
1.6 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. const INFO = 'info';
  16. const WARNING = 'warning';
  17. protected $css_prefix = '';
  18. public function msg($msg = null, $type = null)
  19. {
  20. if ($msg && $type) {
  21. if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) {
  22. throw new GeneralException('Unknown message type: "' . $type . '"');
  23. }
  24. $this->set($msg, $type);
  25. }
  26. return $this;
  27. }
  28. public function success($msg)
  29. {
  30. $this->set($msg, self::SUCCESS);
  31. }
  32. public function error($msg)
  33. {
  34. $this->set($msg, self::ERROR);
  35. }
  36. public function info($msg)
  37. {
  38. $this->set($msg, self::INFO);
  39. }
  40. public function warning($msg)
  41. {
  42. $this->set($msg, self::WARNING);
  43. }
  44. protected function set($msg, $type)
  45. {
  46. Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
  47. }
  48. public function withPrefix($css_prefix)
  49. {
  50. $this->css_prefix = $css_prefix;
  51. return $this;
  52. }
  53. public function __toString()
  54. {
  55. $msg = Session::get(__CLASS__, false);
  56. if ($msg) {
  57. Session::del(__CLASS__);
  58. return '<div class="' . $this->css_prefix . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
  59. }
  60. return '';
  61. }
  62. }