Completed
Push — master ( 90bb37...96e874 )
by Alexey
02:36
created

Alert::initSwalQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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