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.

94 lines
2.2 KiB

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