Alert::init()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 8
nop 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace common\widgets;
9
10
use Yii;
11
use yii\bootstrap\Widget;
12
use Exception;
13
use yii\bootstrap\Alert as BootstrapAlert;
14
15
/**
16
 * Alert widget renders a message from session flash. All flash messages are displayed
17
 * in the sequence they were assigned using setFlash. You can set message as following:
18
 *
19
 * ```php
20
 * Yii::$app->session->setFlash('error', 'This is the message');
21
 * Yii::$app->session->setFlash('success', 'This is the message');
22
 * Yii::$app->session->setFlash('info', 'This is the message');
23
 * ```
24
 *
25
 * Multiple messages could be set as follows:
26
 *
27
 * ```php
28
 * Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']);
29
 * ```
30
 *
31
 * @author Kartik Visweswaran <[email protected]>
32
 * @author Alexander Makarov <[email protected]>
33
 */
34
class Alert extends Widget
35
{
36
    /**
37
     * @var array the alert types configuration for the flash messages.
38
     * This array is setup as $key => $value, where:
39
     * - $key is the name of the session flash variable
40
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
41
     */
42
    public $alertTypes = [
43
        'error' => 'alert-danger',
44
        'danger' => 'alert-danger',
45
        'success' => 'alert-success',
46
        'info' => 'alert-info',
47
        'warning' => 'alert-warning'
48
    ];
49
    /**
50
     * @var array the options for rendering the close button tag.
51
     */
52
    public $closeButton = [];
53
54
55
    public function init()
56
    {
57
        parent::init();
58
        /** @var yii\web\Session $session */
59
        $session = Yii::$app->session;
60
        $flashes = $session->getAllFlashes();
61
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
62
        foreach ($flashes as $type => $data) {
63
            if (isset($this->alertTypes[$type])) {
64
                $data = (array)$data;
65
                try {
66
                    $this->processTypes($this->alertTypes[$type], $data, $appendCss);
67
                } catch (Exception $e) {
68
                    // Save to log
69
                }
70
                $session->removeFlash($type);
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param string $type
77
     * @param array $data
78
     * @param string $appendCss
79
     * @throws Exception
80
     */
81
    protected function processTypes($type, $data, $appendCss)
82
    {
83
        foreach ($data as $i => $message) {
84
            /* initialize css class for each alert box */
85
            $this->options['class'] = $type . $appendCss;
86
87
            /* assign unique id to each alert box */
88
            $this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
89
            $this->renderFlash($message);
90
        }
91
    }
92
93
    /**
94
     * @param array $message
95
     * @throws Exception
96
     */
97
    protected function renderFlash($message)
98
    {
99
        echo BootstrapAlert::widget([
100
            'body' => $message,
101
            'closeButton' => $this->closeButton,
102
            'options' => $this->options
103
        ]);
104
    }
105
}
106