Completed
Push — master ( 631c12...4b7a32 )
by Alexey
16:00 queued 02:26
created

Alert::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace dominus77\sweetalert2;
4
5
use Yii;
6
use yii\bootstrap\Widget;
7
use yii\helpers\Json;
8
use yii\helpers\ArrayHelper;
9
use dominus77\sweetalert2\assets\SweetAlert2Asset;
10
use dominus77\sweetalert2\assets\AnimateCssAsset;
11
12
/**
13
 * Alert widget renders a message from session flash or custom messages.
14
 * @see https://sweetalert2.github.io/
15
 * @package dominus77\sweetalert2
16
 */
17
class Alert extends Widget
18
{
19
    // Modal Type
20
    const TYPE_INFO = 'info';
21
    const TYPE_ERROR = 'error';
22
    const TYPE_SUCCESS = 'success';
23
    const TYPE_WARNING = 'warning';
24
    const TYPE_QUESTION = 'question';
25
26
    // Input Type
27
    const INPUT_TYPE_TEXT = 'text';
28
    const INPUT_TYPE_EMAIL = 'email';
29
    const INPUT_TYPE_PASSWORD = 'password';
30
    const INPUT_TYPE_NUMBER = 'number';
31
    const INPUT_TYPE_RANGE = 'range';
32
    const INPUT_TYPE_TEXTAREA = 'textarea';
33
    const INPUT_TYPE_SELECT = 'select';
34
    const INPUT_TYPE_RADIO = 'radio';
35
    const INPUT_TYPE_CHECKBOX = 'checkbox';
36
    const INPUT_TYPE_FILE = 'file';
37
38
    /**
39
     * All the flash messages stored for the session are displayed and removed from the session
40
     * Defaults to false.
41
     * @var bool
42
     */
43
    public $useSessionFlash = false;
44
45
    /**
46
     * @var string alert callback
47
     */
48
    public $callback = 'function() {}';
49
50
    /**
51 4
     * @inheritdoc
52
     */
53 4
    public function init()
54 4
    {
55 4
        parent::init();
56
        SweetAlert2Asset::register($this->view);
57
    }
58
59
    /**
60 3
     * @param array $steps
61
     */
62 3
    public function initSwalQueue($steps = [])
63 2
    {
64 2
        $view = $this->getView();
65 2
        $js = "swal.queue(" . Json::encode($steps) . ");";
66 2
        $this->view->registerJs($js, $view::POS_END);
67 2
    }
68 2
69
    /**
70 2
     * @param string $options
71
     * @param string $callback
72 2
     */
73 2
    public function initSwal($options = '', $callback = '')
74 1
    {
75
        $view = $this->getView();
76 1
        $js = "swal({$options}).then({$callback}).catch(swal.noop);";
77 1
        $this->view->registerJs($js, $view::POS_END);
78 1
    }
79 1
80
    /**
81
     * @inheritdoc
82 1
     */
83 1
    public function run()
84 2
    {
85
        if ($session = $this->getSession()) {
86
            $steps = $this->processFlash($session);
87
            if (!empty($steps)) {
88 1
                if (isset($steps[0]['text']) && !is_array($steps[0]['text'])) {
89
                    $this->initSwalQueue($steps);
90 3
                } else {
91
                    $this->processFlashWidget($steps);
92
                }
93
            }
94
        } else {
95
            $this->initSwal($this->getOptions(), $this->callback);
96
        }
97 3
    }
98
99 3
    /**
100 2
     * @param $session bool|mixed|\yii\web\Session
101
     * @return array
102 3
     */
103 1
    public function processFlash($session)
104 1
    {
105 1
        $flashes = $session->getAllFlashes();
106
        $steps = [];
107 1
        foreach ($flashes as $type => $data) {
108
            $data = (array)$data;
109 2
            foreach ($data as $message) {
110
                array_push($steps, ['type' => $type, 'text' => $message]);
111
            }
112
            $session->removeFlash($type);
113
        }
114
        return $steps;
115 1
    }
116
117 1
    /**
118 1
     * @param array $steps
119 1
     */
120 1
    public function processFlashWidget($steps = [])
121
    {
122
        $params = [];
123
        if ($params['options'] = $steps[0]['text']) {
124
            $params['options']['type'] = isset($params['options']['type']) ? $params['options']['type'] : $steps[0]['type'];
125
            $params['callback'] = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback;
126 2
            $this->options = $params['options'];
127
            $this->callback = $params['callback'];
128 2
            $this->initSwal($this->getOptions(), $this->callback);
129 2
        }
130 2
    }
131 2
132
    /**
133
     * Get widget options
134
     *
135
     * @return string
136 2
     */
137
    public function getOptions()
138 2
    {
139 2
        if (isset($this->options['id']))
140
            unset($this->options['id']);
141
142
        $this->registerAnimateCss();
143
144 4
        if (ArrayHelper::isIndexed($this->options)) {
145
            $str = '';
146 4
            foreach ($this->options as $value) {
147 4
                $str .= '"' . $value . '",';
148 1
            }
149 1
            return chop($str, ' ,');
150
        }
151
        return Json::encode($this->options);
152 4
    }
153
154
    /**
155
     * Add support Animate.css
156
     * @see https://daneden.github.io/animate.css/
157 3
     */
158
    public function registerAnimateCss()
159 3
    {
160
        if (isset($this->options['animation']) && $this->options['animation'] === false) {
161
            if (isset($this->options['customClass'])) {
162
                AnimateCssAsset::register($this->view);
163
            }
164
        }
165
    }
166
167
    /**
168
     * @return bool|mixed|\yii\web\Session
169
     */
170
    private function getSession()
171
    {
172
        return $this->useSessionFlash ? Yii::$app->session : false;
173
    }
174
}
175