Completed
Push — master ( 03896d...38a124 )
by Song
02:41
created

Actions::setupDeleteScript()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
7
class Actions extends AbstractDisplayer
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $appends = [];
13
14
    /**
15
     * @var array
16
     */
17
    protected $prepends = [];
18
19
    /**
20
     * Default actions.
21
     *
22
     * @var array
23
     */
24
    protected $actions = ['view', 'edit', 'delete'];
25
26
    /**
27
     * @var string
28
     */
29
    protected $resource;
30
31
    /**
32
     * Append a action.
33
     *
34
     * @param $action
35
     *
36
     * @return $this
37
     */
38
    public function append($action)
39
    {
40
        array_push($this->appends, $action);
41
42
        return $this;
43
    }
44
45
    /**
46
     * Prepend a action.
47
     *
48
     * @param $action
49
     *
50
     * @return $this
51
     */
52
    public function prepend($action)
53
    {
54
        array_unshift($this->prepends, $action);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Get route key name of current row.
61
     *
62
     * @return mixed
63
     */
64
    public function getRouteKey()
65
    {
66
        return $this->row->{$this->row->getRouteKeyName()};
67
    }
68
69
    /**
70
     * Disable view action.
71
     *
72
     * @return $this
73
     */
74 View Code Duplication
    public function disableView(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if ($disable) {
77
            array_delete($this->actions, 'view');
78
        } elseif (!in_array('view', $this->actions)) {
79
            array_push($this->actions, 'view');
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Disable delete.
87
     *
88
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
89
     */
90 View Code Duplication
    public function disableDelete(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        if ($disable) {
93
            array_delete($this->actions, 'delete');
94
        } elseif (!in_array('delete', $this->actions)) {
95
            array_push($this->actions, 'delete');
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Disable edit.
103
     *
104
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105
     */
106 View Code Duplication
    public function disableEdit(bool $disable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        if ($disable) {
109
            array_delete($this->actions, 'edit');
110
        } elseif (!in_array('edit', $this->actions)) {
111
            array_push($this->actions, 'edit');
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * Set resource of current resource.
119
     *
120
     * @param $resource
121
     *
122
     * @return $this
123
     */
124
    public function setResource($resource)
125
    {
126
        $this->resource = $resource;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Get resource of current resource.
133
     *
134
     * @return string
135
     */
136
    public function getResource()
137
    {
138
        return $this->resource ?: parent::getResource();
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function display($callback = null)
145
    {
146
        if ($callback instanceof \Closure) {
147
            $callback->call($this, $this);
148
        }
149
150
        $actions = $this->prepends;
151
152
        foreach ($this->actions as $action) {
153
            $method = 'render'.ucfirst($action);
154
            array_push($actions, $this->{$method}());
155
        }
156
157
        $actions = array_merge($actions, $this->appends);
158
159
        return implode('', $actions);
160
    }
161
162
    /**
163
     * Render view action.
164
     *
165
     * @return string
166
     */
167
    protected function renderView()
168
    {
169
        return <<<EOT
170
<a href="{$this->getResource()}/{$this->getRouteKey()}">
171
    <i class="fa fa-eye"></i>
172
</a>
173
EOT;
174
    }
175
176
    /**
177
     * Render edit action.
178
     *
179
     * @return string
180
     */
181
    protected function renderEdit()
182
    {
183
        return <<<EOT
184
<a href="{$this->getResource()}/{$this->getRouteKey()}/edit">
185
    <i class="fa fa-edit"></i>
186
</a>
187
EOT;
188
    }
189
190
    /**
191
     * Render delete action.
192
     *
193
     * @return string
194
     */
195
    protected function renderDelete()
196
    {
197
        $this->setupDeleteScript();
198
199
        return <<<EOT
200
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete">
201
    <i class="fa fa-trash"></i>
202
</a>
203
EOT;
204
    }
205
206
    protected function setupDeleteScript()
207
    {
208
        $trans = [
209
            'delete_confirm' => trans('admin.delete_confirm'),
210
            'confirm'        => trans('admin.confirm'),
211
            'cancel'         => trans('admin.cancel'),
212
        ];
213
214
        $script = <<<SCRIPT
215
216
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
217
218
    var id = $(this).data('id');
219
220
    swal({
221
        title: "{$trans['delete_confirm']}",
222
        type: "warning",
223
        showCancelButton: true,
224
        confirmButtonColor: "#DD6B55",
225
        confirmButtonText: "{$trans['confirm']}",
226
        showLoaderOnConfirm: true,
227
        cancelButtonText: "{$trans['cancel']}",
228
        preConfirm: function() {
229
            return new Promise(function(resolve) {
230
                $.ajax({
231
                    method: 'post',
232
                    url: '{$this->getResource()}/' + id,
233
                    data: {
234
                        _method:'delete',
235
                        _token:LA.token,
236
                    },
237
                    success: function (data) {
238
                        $.pjax.reload('#pjax-container');
239
240
                        resolve(data);
241
                    }
242
                });
243
            });
244
        }
245
    }).then(function(result) {
246
        var data = result.value;
247
        if (typeof data === 'object') {
248
            if (data.status) {
249
                swal(data.message, '', 'success');
250
            } else {
251
                swal(data.message, '', 'error');
252
            }
253
        }
254
    });
255
});
256
257
SCRIPT;
258
259
        Admin::script($script);
260
    }
261
}
262