Completed
Push — master ( 1d3c0c...8bf0fe )
by Song
03:06
created

Dialog   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 260
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A success() 0 4 1
A error() 0 4 1
A warning() 0 4 1
A info() 0 4 1
A question() 0 4 1
A confirm() 0 4 1
A addSettings() 0 9 1
A defaultSettings() 0 15 1
A formatSettings() 0 10 2
A addScript() 0 20 1
A buildActionPromise() 0 52 2
A buildUploadFileActionPromise() 0 37 1
1
<?php
2
3
namespace Encore\Admin\Actions\Interactor;
4
5
use Encore\Admin\Admin;
6
7
class Dialog extends Interactor
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $uploadFile = false;
13
14
    /**
15
     * @var array
16
     */
17
    protected $settings;
18
19
    /**
20
     * @param string $title
21
     * @param string $text
22
     * @param array $options
23
     *
24
     * @return Dialog
25
     */
26
    public function success($title, $text = '', $options = [])
27
    {
28
        return $this->addSettings($title, __FUNCTION__, $text, $options);
29
    }
30
31
    /**
32
     * @param string $title
33
     * @param string $text
34
     * @param array $options
35
     *
36
     * @return Dialog
37
     */
38
    public function error($title, $text = '', $options = [])
39
    {
40
        return $this->addSettings($title, __FUNCTION__, $text, $options);
41
    }
42
43
    /**
44
     * @param string $title
45
     * @param string $text
46
     * @param array $options
47
     *
48
     * @return $this
49
     */
50
    public function warning($title, $text = '', $options = [])
51
    {
52
        return $this->addSettings($title, __FUNCTION__, $text, $options);
53
    }
54
55
    /**
56
     * @param string $title
57
     * @param string $text
58
     * @param array $options
59
     *
60
     * @return Dialog
61
     */
62
    public function info($title, $text = '', $options = [])
63
    {
64
        return $this->addSettings($title, __FUNCTION__, $text, $options);
65
    }
66
67
    /**
68
     * @param string $title
69
     * @param string $text
70
     * @param array $options
71
     *
72
     * @return Dialog
73
     */
74
    public function question($title, $text = '', $options = [])
75
    {
76
        return $this->addSettings($title, __FUNCTION__, $text, $options);
77
    }
78
79
    /**
80
     * @param string $title
81
     * @param string $text
82
     * @param array $options
83
     *
84
     * @return Dialog
85
     */
86
    public function confirm($title, $text = '', $options = [])
87
    {
88
        return $this->addSettings($title, 'question', $text, $options);
89
    }
90
91
    /**
92
     * @param string $title
93
     * @param string $type
94
     * @param string $text
95
     * @param array $options
96
     *
97
     * @return $this
98
     */
99
    protected function addSettings($title, $type, $text = '', $options = [])
100
    {
101
        $this->settings = array_merge(
102
            compact('title', 'text', 'type'),
103
            $options
104
        );
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    protected function defaultSettings()
113
    {
114
        $trans = [
115
            'cancel' => trans('admin.cancel'),
116
            'submit' => trans('admin.submit'),
117
        ];
118
119
        return [
120
            'type'                => 'question',
121
            'showCancelButton'    => true,
122
            'showLoaderOnConfirm' => true,
123
            'confirmButtonText'   => $trans['submit'],
124
            'cancelButtonText'    => $trans['cancel'],
125
        ];
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    protected function formatSettings()
132
    {
133
        if (empty($this->settings)) {
134
            return '';
135
        }
136
137
        $settings = array_merge($this->defaultSettings(), $this->settings);
138
139
        return trim(substr(json_encode($settings, JSON_PRETTY_PRINT), 1, -1));
140
    }
141
142
    /**
143
     * @return void
144
     */
145
    public function addScript()
146
    {
147
        $parameters = json_encode($this->action->parameters());
148
149
        $script = <<<SCRIPT
150
151
(function ($) {
152
    $('{$this->action->selector($this->action->selectorPrefix)}').off('{$this->action->event}').on('{$this->action->event}', function() {
153
        var data = $(this).data();
154
        Object.assign(data, {$parameters});
155
        {$this->action->actionScript()}
156
        {$this->buildActionPromise()}
157
        {$this->action->handleActionPromise()}
158
    });
159
})(jQuery);
160
161
SCRIPT;
162
163
        Admin::script($script);
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    protected function buildActionPromise()
170
    {
171
        call_user_func([$this->action, 'dialog']);
172
173
        $route       = $this->action->getHandleRoute();
174
        $settings    = $this->formatSettings();
175
        $calledClass = $this->action->getCalledClass();
176
177
        if ($this->uploadFile) {
178
            return $this->buildUploadFileActionPromise($settings, $calledClass, $route);
179
        }
180
181
        return <<<PROMISE
182
        var process = $.admin.swal({
183
            {$settings},
184
            preConfirm: function(input) {
185
                return new Promise(function(resolve, reject) {
186
                    Object.assign(data, {
187
                        _token: $.admin.token,
188
                        _action: '$calledClass',
189
                        _input: input,
190
                    });
191
192
                    $.ajax({
193
                        method: '{$this->action->getMethod()}',
194
                        url: '$route',
195
                        data: data,
196
                        success: function (data) {
197
                            resolve(data);
198
                        },
199
                        error:function(request){
200
                            reject(request);
201
                        }
202
                    });
203
                });
204
            }
205
        }).then(function(result) {
206
            if (typeof result.dismiss !== 'undefined') {
207
                return Promise.reject();
208
            }
209
            
210
            if (typeof result.status === "boolean") {
211
                var response = result;
212
            } else {
213
                var response = result.value;
214
            }
215
            
216
            return response;
217
            
218
        });
219
PROMISE;
220
    }
221
222
    /**
223
     * @param string $settings
224
     * @param string $calledClass
225
     * @param string $route
226
     *
227
     * @return string
228
     */
229
    protected function buildUploadFileActionPromise($settings, $calledClass, $route)
230
    {
231
        return <<<PROMISE
232
var process = $.admin.swal({
233
    {$settings}
234
}).then(function (file) {
235
    return new Promise(function (resolve) {
236
        var data = {
237
            _token: $.admin.token,
238
            _action: '$calledClass',
239
        };
240
241
        var formData = new FormData();
242
        for ( var key in data ) {
243
            formData.append(key, data[key]);
244
        }
245
246
        formData.append('_input', file.value, file.value.name);
247
248
        $.ajax({
249
            url: '{$route}',
250
            type: 'POST',
251
            data: formData,
252
            processData: false,
253
            contentType: false,
254
            enctype: 'multipart/form-data',
255
            success: function (data) {
256
                resolve(data);
257
            },
258
            error:function(request){
259
                reject(request);
260
            }
261
        });
262
    });
263
})
264
PROMISE;
265
    }
266
}