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.

95 lines
2.2 KiB

<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-09
*/
class MsgViewHelper extends ViewHelper
{
const SUCCESS = 'success';
const ERROR = 'error';
const INFO = 'info';
const WARNING = 'warning';
protected $use_as_html = false;
private static $type_to_class = array(
self::SUCCESS => 'success',
self::ERROR => 'error',
self::INFO => 'info',
self::WARNING => 'warning',
);
protected $css_prefix = '';
public function msg($msg = null, $type = null)
{
if ($msg && $type) {
if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) {
throw new \Majestic\Exception\GeneralException('Unknown message type: "' . $type . '"');
}
$this->set($msg, $type);
}
return $this;
}
public function success($msg)
{
$this->set($msg, self::SUCCESS);
}
public function error($msg)
{
$this->set($msg, self::ERROR);
}
public function info($msg)
{
$this->set($msg, self::INFO);
}
public function warning($msg)
{
$this->set($msg, self::WARNING);
}
protected function set($msg, $type)
{
\Majestic\Session\Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
}
public static function getType()
{
$msg = \Majestic\Session\Session::get(__CLASS__, false);
return $msg ? $msg['type'] : '';
}
public function withPrefix($css_prefix)
{
$this->css_prefix = $css_prefix;
return $this;
}
public function __toString()
{
$msg = \Majestic\Session\Session::get(__CLASS__, false);
if ($msg) {
\Majestic\Session\Session::del(__CLASS__);
$type_to_class = static::getTypeToClass();
return '<div class="' . $this->css_prefix . $type_to_class[$msg['type']] . '">' . (($this->use_as_html)?$msg['message']:$this->view->escape($msg['message'])) . '</div>';
}
return '';
}
protected static function getTypeToClass()
{
return self::$type_to_class;
}
}