Completed
Push — master ( 0ba9c9...c4f173 )
by jxlwqq
15s queued 11s
created

src/Actions/Interactor/Dialog.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        var target = $(this);
155
        Object.assign(data, {$parameters});
156
        {$this->action->actionScript()}
157
        {$this->buildActionPromise()}
158
        {$this->action->handleActionPromise()}
159
    });
160
})(jQuery);
161
162
SCRIPT;
163
164
        Admin::script($script);
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    protected function buildActionPromise()
171
    {
172
        call_user_func([$this->action, 'dialog']);
173
174
        $route = $this->action->getHandleRoute();
175
        $settings = $this->formatSettings();
176
        $calledClass = $this->action->getCalledClass();
177
178
        if ($this->uploadFile) {
179
            return $this->buildUploadFileActionPromise($settings, $calledClass, $route);
0 ignored issues
show
It seems like $route defined by $this->action->getHandleRoute() on line 174 can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Encore\Admin\Actions\Int...loadFileActionPromise() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
180
        }
181
182
        return <<<PROMISE
183
        var process = $.admin.swal({
184
            {$settings},
185
            preConfirm: function(input) {
186
                return new Promise(function(resolve, reject) {
187
                    Object.assign(data, {
188
                        _token: $.admin.token,
189
                        _action: '$calledClass',
190
                        _input: input,
191
                    });
192
193
                    $.ajax({
194
                        method: '{$this->action->getMethod()}',
195
                        url: '$route',
196
                        data: data,
197
                        success: function (data) {
198
                            resolve(data);
199
                        },
200
                        error:function(request){
201
                            reject(request);
202
                        }
203
                    });
204
                });
205
            }
206
        }).then(function(result) {
207
            if (typeof result.dismiss !== 'undefined') {
208
                return Promise.reject();
209
            }
210
            
211
            if (typeof result.status === "boolean") {
212
                var response = result;
213
            } else {
214
                var response = result.value;
215
            }
216
217
            return [response, target];
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([response, target]);
257
            },
258
            error:function(request){
259
                reject(request);
260
            }
261
        });
262
    });
263
})
264
PROMISE;
265
    }
266
}
267