Completed
Push — master ( 683d01...7f40d3 )
by Alexey
13:57
created

Alert::processFlashWidget()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 1
crap 4
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
     * @param array $steps
52
     */
53 4
    public function initSwalQueue($steps = [])
54 4
    {
55 4
        $view = $this->getView();
56
        $js = "swal.queue(" . Json::encode($steps) . ");";
57
        $this->view->registerJs($js, $view::POS_END);
58
    }
59
60 3
    /**
61
     * @param string $options
62 3
     * @param string $callback
63 2
     */
64 2
    public function initSwal($options = '', $callback = '')
65 2
    {
66 2
        $view = $this->getView();
67 2
        $js = "swal({$options}).then({$callback}).catch(swal.noop);";
68 2
        $this->view->registerJs($js, $view::POS_END);
69
    }
70 2
71
    /**
72 2
     * @inheritdoc
73 2
     */
74 1
    public function run()
75
    {
76 1
        SweetAlert2Asset::register($this->view);
77 1
        if ($session = $this->getSession()) {
78 1
            $steps = $this->processFlash($session);
79 1
            if (!empty($steps)) {
80
                if (isset($steps[0]['text']) && !is_array($steps[0]['text'])) {
81
                    $this->initSwalQueue($steps);
82 1
                } else {
83 1
                    $this->processFlashWidget($steps);
84 2
                }
85
            }
86
        } else {
87
            $this->initSwal($this->getOptions(), $this->callback);
88 1
        }
89
    }
90 3
91
    /**
92
     * @param $session bool|mixed|\yii\web\Session
93
     * @return array
94
     */
95
    public function processFlash($session)
96
    {
97 3
        $flashes = $session->getAllFlashes();
98
        $steps = [];
99 3
        foreach ($flashes as $type => $data) {
100 2
            $data = (array)$data;
101
            foreach ($data as $message) {
102 3
                array_push($steps, ['type' => $type, 'text' => $message]);
103 1
            }
104 1
            $session->removeFlash($type);
105 1
        }
106
        return $steps;
107 1
    }
108
109 2
    /**
110
     * @param array $steps
111
     */
112
    public function processFlashWidget($steps = [])
113
    {
114
        $params = [];
115 1
        if ($params['options'] = $steps[0]['text']) {
116
            $params['options']['type'] = isset($params['options']['type']) ? $params['options']['type'] : $steps[0]['type'];
117 1
            $params['callback'] = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback;
118 1
            $this->options = $params['options'];
119 1
            $this->callback = $params['callback'];
120 1
            $this->initSwal($this->getOptions(), $this->callback);
121
        }
122
    }
123
124
    /**
125
     * Get widget options
126 2
     *
127
     * @return string
128 2
     */
129 2
    public function getOptions()
130 2
    {
131 2
        if (isset($this->options['id']))
132
            unset($this->options['id']);
133
134
        $this->registerAnimateCss();
135
136 2
        if (ArrayHelper::isIndexed($this->options)) {
137
            $str = '';
138 2
            foreach ($this->options as $value) {
139 2
                $str .= '"' . $value . '",';
140
            }
141
            return chop($str, ' ,');
142
        }
143
144 4
        return Json::encode($this->options);
145
    }
146 4
147 4
    /**
148 1
     * Add support Animate.css
149 1
     * @see https://daneden.github.io/animate.css/
150
     */
151
    public function registerAnimateCss()
152 4
    {
153
        if (isset($this->options['animation']) && $this->options['animation'] === false) {
154
            if (isset($this->options['customClass'])) {
155
                AnimateCssAsset::register($this->view);
156
            }
157 3
        }
158
    }
159 3
160
    /**
161
     * @return bool|mixed|\yii\web\Session
162
     */
163
    private function getSession()
164
    {
165
        return $this->useSessionFlash ? Yii::$app->session : false;
166
    }
167
}
168