Completed
Push — master ( 9015cf...addbc6 )
by Song
02:23
created

Actions::getResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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