From 8fc77afc632beca2da8edd32ab310dee18a815f3 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 17:50:47 +0530 Subject: [PATCH 01/11] Update Alert.php to display multiple flash messages for the session. The previous alert widget within the advanced/basic app does not render multiple flash messages. The current implementation allows display of all flash messages assigned through setFlash. Also a validation is done for allowed bootstrap alert types. --- apps/advanced/frontend/widgets/Alert.php | 43 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index b68bfb0..3f8fc13 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -8,35 +8,46 @@ namespace frontend\widgets; 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('success', 'This is the message'); * - \Yii::$app->getSession()->setFlash('info', 'This is the message'); * * @author Alexander Makarov + * @author Kartik Visweswaran */ -class Alert extends \yii\bootstrap\Alert +class Alert extends Widget { - private $_doNotRender = false; + private $_doNotRender = true; + public $allowedTypes = ['error', 'danger', 'success', 'info', 'warning']; + public function init() { - if ($this->body = \Yii::$app->getSession()->getFlash('error', null, true)) { - Html::addCssClass($this->options, 'alert-danger'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('success', null, true)) { - Html::addCssClass($this->options, 'alert-success'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('info', null, true)) { - Html::addCssClass($this->options, 'alert-info'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('warning', null, true)) { - Html::addCssClass($this->options, 'alert-warning'); - } else { - $this->_doNotRender = true; - return; + $this->_doNotRender = true; + $session = \Yii::$app->getSession(); + $flashes = $session->getAllFlashes(); + foreach ($flashes as $type => $message) { + if (in_array($type, $this->allowedTypes)) { + $class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type; + Html::addCssClass($this->options, $class); + echo BsAlert::widget(array( + 'body' => $message, + 'options' => $this->options + )); + $session->removeFlash($type); + $this->_doNotRender = false; + } + } + + if (!$this->_doNotRender) { + parent::init(); } - - parent::init(); } public function run() From 1c56edd000c369190734a08517ba546cec7709d6 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 18:39:37 +0530 Subject: [PATCH 02/11] Update Alert.php --- apps/advanced/frontend/widgets/Alert.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index 3f8fc13..2e8bef5 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -36,10 +36,10 @@ class Alert extends Widget if (in_array($type, $this->allowedTypes)) { $class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type; Html::addCssClass($this->options, $class); - echo BsAlert::widget(array( + echo BsAlert::widget([ 'body' => $message, 'options' => $this->options - )); + ]); $session->removeFlash($type); $this->_doNotRender = false; } From 70f41387b63af325e1f890a698a0643b17d9a72a Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 19:39:55 +0530 Subject: [PATCH 03/11] Update Alert.php to add some refinements and clean up bugs. Added closeButton parameter, assignment of CSS class corrected, and added php doc comments. --- apps/advanced/frontend/widgets/Alert.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index 2e8bef5..a1f76df 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -24,8 +24,17 @@ use yii\bootstrap\Alert as BsAlert; */ class Alert extends Widget { - private $_doNotRender = true; + /** + * @var array the allowed bootstrap alert types. + */ public $allowedTypes = ['error', 'danger', 'success', 'info', 'warning']; + + /** + * @var array the options for rendering the close button tag. + */ + public $closeButton = []; + + private $_doNotRender = true; public function init() { @@ -35,11 +44,12 @@ class Alert extends Widget foreach ($flashes as $type => $message) { if (in_array($type, $this->allowedTypes)) { $class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type; - Html::addCssClass($this->options, $class); + $this->options['class'] = $class; echo BsAlert::widget([ 'body' => $message, + 'closeButton' => $this->closeButton, 'options' => $this->options - ]); + ]); $session->removeFlash($type); $this->_doNotRender = false; } From affa9589dcde1d69910d7108c3d47bcb14feb44a Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 19:58:25 +0530 Subject: [PATCH 04/11] CSS Class assignment corrected. Used ```Html::addCssClass``` and ```Html::removeCssClass``` more effectively. --- apps/advanced/frontend/widgets/Alert.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index a1f76df..29b2e5a 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -44,12 +44,13 @@ class Alert extends Widget foreach ($flashes as $type => $message) { if (in_array($type, $this->allowedTypes)) { $class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type; - $this->options['class'] = $class; + Html::addCssClass($this->options, $class); echo BsAlert::widget([ 'body' => $message, 'closeButton' => $this->closeButton, 'options' => $this->options ]); + Html::removeCssClass($this->options, $class); $session->removeFlash($type); $this->_doNotRender = false; } From c5506285586414c86cd2a25dfbd7579eb952fba8 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 22:35:35 +0530 Subject: [PATCH 05/11] CSS Class assignment correction # 2. Ensure previous CSS classes are not appended and reinitialized in the foreach loop. --- apps/advanced/frontend/widgets/Alert.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index 29b2e5a..dc0cc30 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -41,10 +41,11 @@ class Alert extends Widget $this->_doNotRender = true; $session = \Yii::$app->getSession(); $flashes = $session->getAllFlashes(); + $baseCssClass = isset($this->options['class']) ? $this->options['class'] : ''; + foreach ($flashes as $type => $message) { if (in_array($type, $this->allowedTypes)) { - $class = ($type === 'error') ? 'alert-danger' : 'alert-' . $type; - Html::addCssClass($this->options, $class); + $this->options['class'] = (($type === 'error') ? "alert-danger" : "alert-{$type}") . ' ' . $baseCssClass; echo BsAlert::widget([ 'body' => $message, 'closeButton' => $this->closeButton, From 8a436c4acbccb16c3229238e2a974009a29c6c60 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 22:45:49 +0530 Subject: [PATCH 06/11] Code cleanup. Removed redundant declarations and checks in the code. --- apps/advanced/frontend/widgets/Alert.php | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index dc0cc30..b174437 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -7,10 +7,6 @@ namespace frontend\widgets; -use yii\helpers\Html; -use yii\bootstrap\Widget; -use yii\bootstrap\Alert as BsAlert; - /** * 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: @@ -22,7 +18,7 @@ use yii\bootstrap\Alert as BsAlert; * @author Alexander Makarov * @author Kartik Visweswaran */ -class Alert extends Widget +class Alert extends \yii\bootstrap\Widget { /** * @var array the allowed bootstrap alert types. @@ -34,38 +30,23 @@ class Alert extends Widget */ public $closeButton = []; - private $_doNotRender = true; - public function init() { - $this->_doNotRender = true; $session = \Yii::$app->getSession(); - $flashes = $session->getAllFlashes(); - $baseCssClass = isset($this->options['class']) ? $this->options['class'] : ''; + $appendCss = isset($this->options['class']) ? $this->options['class'] : ''; foreach ($flashes as $type => $message) { if (in_array($type, $this->allowedTypes)) { - $this->options['class'] = (($type === 'error') ? "alert-danger" : "alert-{$type}") . ' ' . $baseCssClass; - echo BsAlert::widget([ + $this->options['class'] = (($type === 'error') ? 'alert-danger' : 'alert-' . $type) . ' ' . $appendCss; + echo \yii\bootstrap\Alert::widget([ 'body' => $message, 'closeButton' => $this->closeButton, 'options' => $this->options ]); - Html::removeCssClass($this->options, $class); $session->removeFlash($type); $this->_doNotRender = false; } } - - if (!$this->_doNotRender) { - parent::init(); - } - } - - public function run() - { - if (!$this->_doNotRender) { - parent::run(); - } + parent::init(); } } From 8ff4c3d46db2e4cb45bba9c5ee5dffd3447184e4 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 22:53:08 +0530 Subject: [PATCH 07/11] Code & minor bug cleanup. Cleaned up redundant code (and a typo missed earlier). --- apps/advanced/frontend/widgets/Alert.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index b174437..d2ddab0 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -33,18 +33,18 @@ class Alert extends \yii\bootstrap\Widget public function init() { $session = \Yii::$app->getSession(); - $appendCss = isset($this->options['class']) ? $this->options['class'] : ''; + $flashes = $session->getAllFlashes(); + $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; foreach ($flashes as $type => $message) { if (in_array($type, $this->allowedTypes)) { - $this->options['class'] = (($type === 'error') ? 'alert-danger' : 'alert-' . $type) . ' ' . $appendCss; + $this->options['class'] = (($type === 'error') ? 'alert-danger' : 'alert-' . $type) . $appendCss; echo \yii\bootstrap\Alert::widget([ 'body' => $message, 'closeButton' => $this->closeButton, 'options' => $this->options ]); $session->removeFlash($type); - $this->_doNotRender = false; } } parent::init(); From 2bff5f80bcfbdb03133373f7548e22c9a3667579 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 20 Nov 2013 23:15:16 +0530 Subject: [PATCH 08/11] Alert types for flash messages are now configurable. Each flash variable can be mapped to a bootstrap alert type through a configuration array passed to this widget. --- apps/advanced/frontend/widgets/Alert.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index d2ddab0..891c7e4 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -21,9 +21,18 @@ namespace frontend\widgets; class Alert extends \yii\bootstrap\Widget { /** - * @var array the allowed bootstrap alert types. + * @var array the alert types configuration for the flash messages. + * This array is setup as $key => $value, where: + * - $key is the name of the session flash variable + * - $value is the bootstrap alert type (i.e. danger, success, info, warning) */ - public $allowedTypes = ['error', 'danger', 'success', 'info', 'warning']; + public $alertTypes = [ + 'error' => 'danger', + 'danger' => 'danger', + 'success' => 'success', + 'info' => 'info', + 'warning' => 'warning' + ]; /** * @var array the options for rendering the close button tag. @@ -37,15 +46,13 @@ class Alert extends \yii\bootstrap\Widget $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; foreach ($flashes as $type => $message) { - if (in_array($type, $this->allowedTypes)) { - $this->options['class'] = (($type === 'error') ? 'alert-danger' : 'alert-' . $type) . $appendCss; - echo \yii\bootstrap\Alert::widget([ - 'body' => $message, - 'closeButton' => $this->closeButton, - 'options' => $this->options - ]); - $session->removeFlash($type); - } + $this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss; + echo \yii\bootstrap\Alert::widget([ + 'body' => $message, + 'closeButton' => $this->closeButton, + 'options' => $this->options + ]); + $session->removeFlash($type); } parent::init(); } From 62493188a243e1d25d6e0debb41548291aa47c2b Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 21 Nov 2013 15:32:07 +0530 Subject: [PATCH 09/11] 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(); } } From b979d3b0244a09b42fc246d776cb3e465f16bb8c Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 21 Nov 2013 15:36:43 +0530 Subject: [PATCH 10/11] Reverted closeButton Reverted name to ```closeButton``` to be in sync with the bootstrap ```Alert``` widget. --- apps/advanced/frontend/widgets/Alert.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index 7177fc2..d1546ef 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -37,7 +37,7 @@ class Alert extends \yii\bootstrap\Widget /** * @var array the options for rendering the close button tag. */ - public $closeButtonOptions = []; + public $closeButton = []; public function init() { @@ -56,7 +56,7 @@ class Alert extends \yii\bootstrap\Widget echo \yii\bootstrap\Alert::widget([ 'body' => $message, - 'closeButton' => $this->closeButtonOptions, + 'closeButton' => $this->closeButton, 'options' => $this->options ]); From 817cbc175a35df506ade6d5a72662b7113b563b4 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 21 Nov 2013 15:42:52 +0530 Subject: [PATCH 11/11] Comments cleanup. Cleaned up a few comments ... hope its not my OCD again... :-) --- apps/advanced/frontend/widgets/Alert.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index d1546ef..e070e1b 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -48,10 +48,10 @@ class Alert extends \yii\bootstrap\Widget $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; foreach ($flashes as $type => $message) { - /* initialize css class for each alert box in loop */ + /* initialize css class for each alert box */ $this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss; - /* assign unique id to each alert box in the loop */ + /* assign unique id to each alert box */ $this->options['id'] = $this->getId() . '-' . $type; echo \yii\bootstrap\Alert::widget([