Completed
Push — master ( 7c17f8...b26d37 )
by Song
05:23 queued 02:38
created

Actions::append()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * Disable all actions.
33
     *
34
     * @var bool
35
     */
36
    protected $disableAll = false;
37
38
    /**
39
     * diy translate.
40
     *
41
     * @var array
42
     */
43
    protected $trans = [];
44
45
    /**
46
     * Append a action.
47
     *
48
     * @param $action
49
     *
50
     * @return $this
51
     */
52
    public function append($action)
53
    {
54
        array_push($this->appends, $action);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Prepend a action.
61
     *
62
     * @param $action
63
     *
64
     * @return $this
65
     */
66
    public function prepend($action)
67
    {
68
        array_unshift($this->prepends, $action);
69
70
        return $this;
71
    }
72
73
    /**
74
     * Get route key name of current row.
75
     *
76
     * @return mixed
77
     */
78
    public function getRouteKey()
79
    {
80
        return $this->row->{$this->row->getRouteKeyName()};
81
    }
82
83
    /**
84
     * Disable view action.
85
     *
86
     * @return $this
87
     */
88 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...
89
    {
90
        if ($disable) {
91
            array_delete($this->actions, 'view');
92
        } elseif (!in_array('view', $this->actions)) {
93
            array_push($this->actions, 'view');
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Disable delete.
101
     *
102
     * @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...
103
     */
104 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...
105
    {
106
        if ($disable) {
107
            array_delete($this->actions, 'delete');
108
        } elseif (!in_array('delete', $this->actions)) {
109
            array_push($this->actions, 'delete');
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * Disable edit.
117
     *
118
     * @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...
119
     */
120 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...
121
    {
122
        if ($disable) {
123
            array_delete($this->actions, 'edit');
124
        } elseif (!in_array('edit', $this->actions)) {
125
            array_push($this->actions, 'edit');
126
        }
127
128
        return $this;
129
    }
130
131
    /**
132
     * Disable all actions.
133
     *
134
     * @return $this
135
     */
136
    public function disableAll()
137
    {
138
        $this->disableAll = true;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Set resource of current resource.
145
     *
146
     * @param $resource
147
     *
148
     * @return $this
149
     */
150
    public function setResource($resource)
151
    {
152
        $this->resource = $resource;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Get resource of current resource.
159
     *
160
     * @return string
161
     */
162
    public function getResource()
163
    {
164
        return $this->resource ?: parent::getResource();
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function display($callback = null)
171
    {
172
        if ($callback instanceof \Closure) {
173
            $callback->call($this, $this);
174
        }
175
176
        if ($this->disableAll) {
177
            return '';
178
        }
179
180
        $actions = $this->prepends;
181
182
        foreach ($this->actions as $action) {
183
            $method = 'render'.ucfirst($action);
184
            array_push($actions, $this->{$method}());
185
        }
186
187
        $actions = array_merge($actions, $this->appends);
188
189
        return implode('', $actions);
190
    }
191
192
    /**
193
     * Render view action.
194
     *
195
     * @return string
196
     */
197
    protected function renderView()
198
    {
199
        return <<<EOT
200
<a href="{$this->getResource()}/{$this->getRouteKey()}" class="{$this->grid->getGridRowName()}-view">
201
    <i class="fa fa-eye"></i>
202
</a>
203
EOT;
204
    }
205
206
    /**
207
     * Render edit action.
208
     *
209
     * @return string
210
     */
211
    protected function renderEdit()
212
    {
213
        return <<<EOT
214
<a href="{$this->getResource()}/{$this->getRouteKey()}/edit" class="{$this->grid->getGridRowName()}-edit">
215
    <i class="fa fa-edit"></i>
216
</a>
217
EOT;
218
    }
219
220
    /**
221
     * Render delete action.
222
     *
223
     * @return string
224
     */
225
    protected function renderDelete()
226
    {
227
        $this->setupDeleteScript();
228
229
        return <<<EOT
230
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete">
231
    <i class="fa fa-trash"></i>
232
</a>
233
EOT;
234
    }
235
236
    protected function setupDeleteScript()
237
    {
238
        $trans = [
239
            'delete_confirm' => trans('admin.delete_confirm'),
240
            'confirm'        => trans('admin.confirm'),
241
            'cancel'         => trans('admin.cancel'),
242
        ];
243
244
        $trans = array_merge($trans, $this->trans);
245
246
        $script = <<<SCRIPT
247
248
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
249
250
    var id = $(this).data('id');
251
252
    swal({
253
        title: "{$trans['delete_confirm']}",
254
        type: "warning",
255
        showCancelButton: true,
256
        confirmButtonColor: "#DD6B55",
257
        confirmButtonText: "{$trans['confirm']}",
258
        showLoaderOnConfirm: true,
259
        cancelButtonText: "{$trans['cancel']}",
260
        preConfirm: function() {
261
            return new Promise(function(resolve) {
262
                $.ajax({
263
                    method: 'post',
264
                    url: '{$this->getResource()}/' + id,
265
                    data: {
266
                        _method:'delete',
267
                        _token:LA.token,
268
                    },
269
                    success: function (data) {
270
                        $.pjax.reload('#pjax-container');
271
272
                        resolve(data);
273
                    }
274
                });
275
            });
276
        }
277
    }).then(function(result) {
278
        var data = result.value;
279
        if (typeof data === 'object') {
280
            if (data.status) {
281
                swal(data.message, '', 'success');
282
            } else {
283
                swal(data.message, '', 'error');
284
            }
285
        }
286
    });
287
});
288
289
SCRIPT;
290
291
        Admin::script($script);
292
    }
293
294
    /**
295
     * diy delete translate.
296
     *
297
     * @param $tans
298
     */
299
    public function setTrans($tans)
300
    {
301
        $this->trans = $tans;
302
    }
303
}
304