Alert.php 1.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace frontend\widgets;

use yii\helpers\Html;

/**
 * Alert widget renders a message from session flash. You can set message as following:
 *
 * - \Yii::$app->getSession()->setFlash('error', 'This is the message');
 * - \Yii::$app->getSession()->setFlash('success', 'This is the message');
 * - \Yii::$app->getSession()->setFlash('info', 'This is the message');
 *
 * @author Alexander Makarov <sam@rmcerative.ru>
 */
class Alert extends \yii\bootstrap\Alert
{
Alexander Makarov committed
23
	private $_doNotRender = false;
24 25 26
	public function init()
	{
		if ($this->body = \Yii::$app->getSession()->getFlash('error')) {
Roman Revin committed
27
			Html::addCssClass($this->options, 'alert-danger');
28 29 30 31 32
		} elseif ($this->body = \Yii::$app->getSession()->getFlash('success')) {
			Html::addCssClass($this->options, 'alert-success');
		} elseif ($this->body = \Yii::$app->getSession()->getFlash('info')) {
			Html::addCssClass($this->options, 'alert-info');
		} elseif ($this->body = \Yii::$app->getSession()->getFlash('warning')) {
Roman Revin committed
33
			Html::addCssClass($this->options, 'alert-warning');
34
		} else {
Alexander Makarov committed
35
			$this->_doNotRender = true;
36 37 38 39 40
			return;
		}

		parent::init();
	}
Alexander Makarov committed
41 42 43

	public function run()
	{
Alexander Makarov committed
44
		if (!$this->_doNotRender) {
Alexander Makarov committed
45 46 47 48
			parent::run();
		}
	}
}