1 | <?php |
||
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() |
|
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() |
|
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 = '') |
|
132 | |||
133 | /** |
||
134 | * Register Animate Assets |
||
135 | */ |
||
136 | 1 | protected function registerAnimate() |
|
140 | |||
141 | /** |
||
142 | * Register client assets |
||
143 | */ |
||
144 | 4 | protected function registerAssets() |
|
153 | |||
154 | /** |
||
155 | * @return bool|mixed|\yii\web\Session |
||
156 | */ |
||
157 | 3 | private function getSession() |
|
161 | } |
||
162 | |||
163 |