@ -8,35 +8,46 @@
namespace frontend\widgets;
namespace frontend\widgets;
use yii\helpers\Html;
use yii\helpers\Html;
use yii\bootstrap\Widget;
use yii\bootstrap\Alert as BsAlert;
/**
/**
* Alert widget renders a message from session flash. You can set message as following:
* Alert widget renders a message from session flash. All flash messages are displayed
* in the sequence they were assigned using setFlash. You can set message as following:
*
*
* - \Yii::$app->getSession()->setFlash('error', 'This is the message');
* - \Yii::$app->getSession()->setFlash('error', 'This is the message');
* - \Yii::$app->getSession()->setFlash('success', 'This is the message');
* - \Yii::$app->getSession()->setFlash('success', 'This is the message');
* - \Yii::$app->getSession()->setFlash('info', 'This is the message');
* - \Yii::$app->getSession()->setFlash('info', 'This is the message');
*
*
* @author Alexander Makarov < sam @ rmcerative . ru >
* @author Alexander Makarov < sam @ rmcerative . ru >
* @author Kartik Visweswaran < kartikv2 @ gmail . com >
*/
*/
class Alert extends \yii\bootstrap\Aler t
class Alert extends Widge t
{
{
private $_doNotRender = false;
private $_doNotRender = true;
public $allowedTypes = ['error', 'danger', 'success', 'info', 'warning'];
public function init()
public function init()
{
{
if ($this->body = \Yii::$app->getSession()->getFlash('error', null, true)) {
$this->_doNotRender = true;
Html::addCssClass($this->options, 'alert-danger');
$session = \Yii::$app->getSession();
} elseif ($this->body = \Yii::$app->getSession()->getFlash('success', null, true)) {
$flashes = $session->getAllFlashes();
Html::addCssClass($this->options, 'alert-success');
foreach ($flashes as $type => $message) {
} elseif ($this->body = \Yii::$app->getSession()->getFlash('info', null, true)) {
if (in_array($type, $this->allowedTypes)) {
Html::addCssClass($this->options, 'alert-info');
$class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type;
} elseif ($this->body = \Yii::$app->getSession()->getFlash('warning', null, true)) {
Html::addCssClass($this->options, $class);
Html::addCssClass($this->options, 'alert-warning');
echo BsAlert::widget(array(
} else {
'body' => $message,
$this->_doNotRender = true;
'options' => $this->options
return;
));
$session->removeFlash($type);
$this->_doNotRender = false;
}
}
}
parent::init();
if (!$this->_doNotRender) {
parent::init();
}
}
}
public function run()
public function run()