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.

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