Completed
Pull Request — master (#1350)
by
unknown
03:06
created

Actions::___deleteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 54
rs 9.6716
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 App\Admin\Extensions\Grid\Displayers\TCustomActions;
6
use Encore\Admin\Admin;
7
8
class Actions extends AbstractDisplayer
9
{
10
    use TCustomActions;
11
    /**
12
     * @var array
13
     */
14
    protected $appends = [];
15
16
    /**
17
     * @var array
18
     */
19
    protected $prepends = [];
20
21
    /**
22
     * @var bool
23
     */
24
    protected $allowEdit = true;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $allowDelete = true;
30
31
    /**
32
     * @var string
33
     */
34
    protected $resource;
35
36
    /**
37
     * @var
38
     */
39
    protected $key;
40
41
    /**
42
     * Append a action.
43
     *
44
     * @param $action
45
     *
46
     * @return $this
47
     */
48
    public function append($action)
49
    {
50
        array_push($this->appends, $action);
51
52
        return $this;
53
    }
54
55
    /**
56
     * Prepend a action.
57
     *
58
     * @param $action
59
     *
60
     * @return $this
61
     */
62
    public function prepend($action)
63
    {
64
        array_unshift($this->prepends, $action);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Disable delete.
71
     *
72
     * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." 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...
73
     */
74
    public function disableDelete()
75
    {
76
        $this->allowDelete = false;
77
    }
78
79
    /**
80
     * Disable edit.
81
     *
82
     * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." 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...
83
     */
84
    public function disableEdit()
85
    {
86
        $this->allowEdit = false;
87
    }
88
89
    /**
90
     * Set resource of current resource.
91
     *
92
     * @param $resource
93
     *
94
     * @return void
95
     */
96
    public function setResource($resource)
97
    {
98
        $this->resource = $resource;
99
    }
100
101
    /**
102
     * Get resource of current resource.
103
     *
104
     * @return string
105
     */
106
    public function getResource()
107
    {
108
        return $this->resource ?: parent::getResource();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function display($callback = null)
115
    {
116
        if ($callback instanceof \Closure) {
117
            $callback->call($this, $this);
118
        }
119
120
        $actions = $this->prepends;
121
        if ($this->allowEdit) {
122
            array_push($actions, $this->editAction());
123
        }
124
125
        if ($this->allowDelete) {
126
            array_push($actions, $this->deleteAction());
127
        }
128
129
        $actions = array_merge($actions, $this->appends);
130
131
        return implode('', $actions);
132
    }
133
134
    public function setKey($key)
135
    {
136
        $this->key = $key;
137
138
        return $this;
139
    }
140
141
    public function getKey()
142
    {
143
        if ($this->key) {
144
            return $this->key;
145
        }
146
147
        return parent::getKey();
148
    }
149
150
    /**
151
     * Built edit action.
152
     *
153
     * @return string
154
     */
155
    protected function ___editAction()
156
    {
157
        return <<<EOT
158
<a href="{$this->getResource()}/{$this->getKey()}/edit">
159
    <i class="fa fa-edit"></i>
160
</a>
161
EOT;
162
    }
163
164
    /**
165
     * Built delete action.
166
     *
167
     * @return string
168
     */
169
    protected function ___deleteAction()
170
    {
171
        $deleteConfirm = trans('admin.delete_confirm');
172
        $confirm = trans('admin.confirm');
173
        $cancel = trans('admin.cancel');
174
175
        $script = <<<SCRIPT
176
177
$('.grid-row-delete').unbind('click').click(function() {
178
179
    var id = $(this).data('id');
180
181
    swal({
182
      title: "$deleteConfirm",
183
      type: "warning",
184
      showCancelButton: true,
185
      confirmButtonColor: "#DD6B55",
186
      confirmButtonText: "$confirm",
187
      closeOnConfirm: false,
188
      cancelButtonText: "$cancel"
189
    },
190
    function(){
191
        $.ajax({
192
            method: 'post',
193
            url: '{$this->getResource()}/' + id,
194
            data: {
195
                _method:'delete',
196
                _token:LA.token,
197
            },
198
            success: function (data) {
199
                $.pjax.reload('#pjax-container');
200
201
                if (typeof data === 'object') {
202
                    if (data.status) {
203
                        swal(data.message, '', 'success');
204
                    } else {
205
                        swal(data.message, '', 'error');
206
                    }
207
                }
208
            }
209
        });
210
    });
211
});
212
213
SCRIPT;
214
215
        Admin::script($script);
216
217
        return <<<EOT
218
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="grid-row-delete">
219
    <i class="fa fa-trash"></i>
220
</a>
221
EOT;
222
    }
223
224
    protected function editAction()
225
    {
226
        $_edit =  trans('admin.edit');
227
        return <<<EOT
228
<a class="btn btn-xs btn-default" href="{$this->getResource()}/{$this->getKey()}/edit">
229
    <i class="fa fa-edit"></i> $_edit
230
</a>
231
EOT;
232
233
    }
234
235
    protected function deleteAction()
236
    {
237
        $this->___deleteAction();
238
        $_delete =  trans('admin.delete');
239
        return <<<EOT
240
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="grid-row-delete btn btn-xs btn-danger">
241
    <i class="fa fa-trash"></i> $_delete
242
</a>
243
EOT;
244
    }
245
246
}
247