1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace terabytesoft\widgets; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Alert widget renders a message from session flash. All flash messages are displayed |
7
|
|
|
* in the sequence they were assigned using setFlash. You can set message as following:. |
8
|
|
|
* |
9
|
|
|
* @author Kartik Visweswaran <[email protected]> |
10
|
|
|
* @author Alexander Makarov <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Alert extends \yii\bootstrap4\Widget |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array the alert types configuration for the flash messages. |
17
|
|
|
* This array is setup as $key => $value, where: |
18
|
|
|
* - $key is the name of the session flash variable |
19
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
20
|
|
|
**/ |
21
|
|
|
public $alertTypes = [ |
22
|
|
|
'danger' => 'alert-danger', |
23
|
|
|
'dark' => 'alert-dark', |
24
|
|
|
'info' => 'alert-info', |
25
|
|
|
'light' => 'alert-light', |
26
|
|
|
'primary' => 'alert-primary', |
27
|
|
|
'secondary' => 'alert-secondary', |
28
|
|
|
'success' => 'alert-success', |
29
|
|
|
'warning' => 'alert-warning', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array the options for rendering the close button tag. |
34
|
|
|
**/ |
35
|
|
|
public $closeButton = []; |
36
|
|
|
|
37
|
8 |
|
public function run() |
38
|
|
|
{ |
39
|
8 |
|
$session = \Yii::$app->getSession(); |
40
|
8 |
|
$flashes = $session->getAllFlashes(); |
41
|
|
|
|
42
|
8 |
|
foreach ($flashes as $type => $data) { |
43
|
8 |
|
if (isset($this->alertTypes[$type])) { |
44
|
8 |
|
$data = (array) $data; |
45
|
8 |
|
foreach ($data as $message) { |
46
|
|
|
/* initialize css class for each alert box */ |
47
|
8 |
|
$this->options['class'] = $this->alertTypes[$type]; |
48
|
|
|
/* assign unique id to each alert box */ |
49
|
8 |
|
$this->options['id'] = $this->getId() . '-' . $type; |
50
|
8 |
|
echo \yii\bootstrap4\Alert::widget([ |
51
|
8 |
|
'body' => $message, |
52
|
8 |
|
'closeButton' => $this->closeButton, |
53
|
8 |
|
'options' => $this->options, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
8 |
|
$session->removeFlash($type); |
57
|
|
|
} |
58
|
|
|
} |
59
|
8 |
|
} |
60
|
|
|
} |
61
|
|
|
|