Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
c26ebed1
Commit
c26ebed1
authored
Nov 21, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1250 from kartik-v/patch-1
Update Alert.php to display multiple flash messages for the session.
parents
58bac36f
817cbc17
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
23 deletions
+41
-23
Alert.php
apps/advanced/frontend/widgets/Alert.php
+41
-23
No files found.
apps/advanced/frontend/widgets/Alert.php
View file @
c26ebed1
...
@@ -7,42 +7,60 @@
...
@@ -7,42 +7,60 @@
namespace
frontend\widgets
;
namespace
frontend\widgets
;
use
yii\helpers\Html
;
/**
/**
* 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 Kartik Visweswaran <kartikv2@gmail.com>
* @author Alexander Makarov <sam@rmcerative.ru>
* @author Alexander Makarov <sam@rmcerative.ru>
*/
*/
class
Alert
extends
\yii\bootstrap\
Aler
t
class
Alert
extends
\yii\bootstrap\
Widge
t
{
{
private
$_doNotRender
=
false
;
/**
* @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
$alertTypes
=
[
'error'
=>
'danger'
,
'danger'
=>
'danger'
,
'success'
=>
'success'
,
'info'
=>
'info'
,
'warning'
=>
'warning'
];
/**
* @var array the options for rendering the close button tag.
*/
public
$closeButton
=
[];
public
function
init
()
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
;
}
parent
::
init
();
parent
::
init
();
}
public
function
run
()
$session
=
\Yii
::
$app
->
getSession
();
{
$flashes
=
$session
->
getAllFlashes
();
if
(
!
$this
->
_doNotRender
)
{
$appendCss
=
isset
(
$this
->
options
[
'class'
])
?
' '
.
$this
->
options
[
'class'
]
:
''
;
parent
::
run
();
foreach
(
$flashes
as
$type
=>
$message
)
{
/* initialize css class for each alert box */
$this
->
options
[
'class'
]
=
'alert-'
.
$this
->
alertTypes
[
$type
]
.
$appendCss
;
/* assign unique id to each alert box */
$this
->
options
[
'id'
]
=
$this
->
getId
()
.
'-'
.
$type
;
echo
\yii\bootstrap\Alert
::
widget
([
'body'
=>
$message
,
'closeButton'
=>
$this
->
closeButton
,
'options'
=>
$this
->
options
]);
$session
->
removeFlash
(
$type
);
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment