From 62493188a243e1d25d6e0debb41548291aa47c2b Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 21 Nov 2013 15:32:07 +0530 Subject: [PATCH] Nomenclature and code realignment. 1. Renamed closeButton to closeButtonOptions. 2. Generate unique id for each alert box. 3. Moved parent init to the top. --- apps/advanced/frontend/widgets/Alert.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index 891c7e4..7177fc2 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -15,8 +15,8 @@ namespace frontend\widgets; * - \Yii::$app->getSession()->setFlash('success', 'This is the message'); * - \Yii::$app->getSession()->setFlash('info', 'This is the message'); * - * @author Alexander Makarov * @author Kartik Visweswaran + * @author Alexander Makarov */ class Alert extends \yii\bootstrap\Widget { @@ -37,23 +37,30 @@ class Alert extends \yii\bootstrap\Widget /** * @var array the options for rendering the close button tag. */ - public $closeButton = []; + public $closeButtonOptions = []; public function init() { + parent::init(); + $session = \Yii::$app->getSession(); $flashes = $session->getAllFlashes(); $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; - + foreach ($flashes as $type => $message) { - $this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss; + /* initialize css class for each alert box in loop */ + $this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss; + + /* assign unique id to each alert box in the loop */ + $this->options['id'] = $this->getId() . '-' . $type; + echo \yii\bootstrap\Alert::widget([ 'body' => $message, - 'closeButton' => $this->closeButton, + 'closeButton' => $this->closeButtonOptions, 'options' => $this->options ]); + $session->removeFlash($type); } - parent::init(); } }