You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
I leverage Kartik's notification widgets.
And try to wrap the notification to own quickly static function for easy revoke. The type[info, success, warning, danger] will according title text to auto change. Also add timer and mouse_over option.
The below code block contain 3 small functions, you can put into Yourclass.
setMsg : Set title & body to flashes.
showFlashes: Will revoke showMsg function to retrieve flashes and show one by one.
showMsg: Customize Growl & Alert notification type for easy revoke.
Btw, for alert type, the delay cannot set to 0, or else it will always stay there, so, the shoMsg function will auto add 2000 milliseconds for it.
Usage 1: In your main.php to add
<?php Yourclass::showFlashes();?> or <?php Yourclass::showFlashes(['alertType'=>'Alert']);?>
right after
<div class="container">
In any controller/model, you can use
Yourclass::setMsg('title','body')
to set flash. The main will auto capture the flashes and show it one by one, each flash will add 1.5 second by sequence.
Usage 2: Or , you can directly put
Yourclass:showMsg('title','body')
in any view. Other samples:
Yourclass::showMsg('warning info','a test2 only',['delay'=>5000]);
Yourclass::showMsg('danger info','a test2 only',['from'=>'bottom']);
Code:
use kartik\widgets\Growl;
use kartik\widgets\Alert;
/**
* @param string $title
* @param string $body
*/
public static function setMsg($title = 'info', $body = 'info msg')
{
\Yii::$app->session->setFlash($title, $body);
}
/**
* @param array $additional the array will pass into showMsg function
*/
public static function showFlashes($additional = [])
{
self::showMsg('', '', array_merge($additional, ['useSessionFlash' => true]));
}
/**
* @param string $title
* @param string $body
* @param array $additional parameters which you may want to change.
*/
public static function showMsg($title = 'info', $body = 'info msg', $additional = [])
{
$additional = ArrayHelper::merge([
'linkUrl' => null,
'delay' => 0,
'showSeparator' => true,
'from' => 'top',
'align' => 'right',
'alertType' => 'Growl',
'useSessionFlash' => false,
'mouse_over'=>'pause',
'timer'=>1000,
], $additional);
$typeIndex = 'info';
$typeArray = [
'success' => ['type' => ['Growl'=>Growl::TYPE_SUCCESS,'Alert'=>Alert::TYPE_SUCCESS],
'icon' => 'glyphicon glyphicon-ok-sign'],
'info' => ['type' => ['Growl'=>Growl::TYPE_INFO,'Alert'=>Alert::TYPE_INFO],
'icon' => 'glyphicon glyphicon-info-sign'],
'warning' => ['type' => ['Growl'=>Growl::TYPE_WARNING,'Alert'=>Alert::TYPE_WARNING],
'icon' => 'glyphicon glyphicon-exclamation-sign'],
'danger' => ['type' => ['Growl'=>Growl::TYPE_DANGER,'Alert'=>Alert::TYPE_DANGER],
'icon' => 'glyphicon glyphicon-remove-sign'],
];
$delay = $additional['delay'];
if ($additional['useSessionFlash']) {
$flashes = \yii::$app->session->getAllFlashes(true);
if ($delay == 0 and $additional['alertType']=="Alert") $delay += 2000;
//self::dump(($flashes));
} else {
$flashes = [$title => $body];
}
foreach ($flashes as $title => $body) {
$typeIndex = 'info';
if (!empty($title)) {
if (stristr($title, "danger") or stristr($title, "error")) {
$typeIndex = 'danger';
} elseif (stristr($title, "warning")) {
$typeIndex = 'warning';
} elseif (stristr($title, "success")) {
$typeIndex = 'success';
} elseif (stristr($title, "info")) {
$typeIndex = 'info';
};
}
$msgArray = [
'type' => $typeArray[$typeIndex]['type'][$additional['alertType']],
'title' => $title,
'icon' => $typeArray[$typeIndex]['icon'],
'body' => $body,
'linkUrl' => $additional['linkUrl'],
'showSeparator' => $additional['showSeparator'],
'delay' => $delay,
'pluginOptions' => [
'timer'=>$additional['timer'],
'mouse_over'=>$additional['mouse_over'],
'placement' => [
'from' => $additional['from'],
'align' => $additional['align'],
]
]
];
$delay += 1500;
switch ($additional['alertType']) {
case 'Growl':
echo Growl::widget($msgArray);
break;
default:
ArrayHelper::remove($msgArray,'linkUrl');
ArrayHelper::remove($msgArray,'pluginOptions');
echo Alert::widget($msgArray);
break;
}
}
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.