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.

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