Completed
Push — master ( e0eabb...111cbc )
by Alexey
13:52
created

Alert::run()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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