Completed
Pull Request — master (#3079)
by
unknown
02:44
created

Actions::getRouteKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
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
     * 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
        $deleteConfirm = trans('admin.delete_confirm');
209
        $confirm = trans('admin.confirm');
210
        $cancel = trans('admin.cancel');
211
212
        $script = <<<SCRIPT
213
214
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
215
216
    var id = $(this).data('id');
217
218
    swal({
219
        title: "$deleteConfirm",
220
        type: "warning",
221
        showCancelButton: true,
222
        confirmButtonColor: "#DD6B55",
223
        confirmButtonText: "$confirm",
224
        showLoaderOnConfirm: true,
225
        cancelButtonText: "$cancel",
226
        preConfirm: function() {
227
            return new Promise(function(resolve) {
228
                $.ajax({
229
                    method: 'post',
230
                    url: '{$this->getResource()}/' + id,
231
                    data: {
232
                        _method:'delete',
233
                        _token:LA.token,
234
                    },
235
                    success: function (data) {
236
                        $.pjax.reload('#pjax-container');
237
238
                        resolve(data);
239
                    }
240
                });
241
            });
242
        }
243
    }).then(function(result) {
244
        var data = result.value;
245
        if (typeof data === 'object') {
246
            if (data.status) {
247
                swal(data.message, '', 'success');
248
            } else {
249
                swal(data.message, '', 'error');
250
            }
251
        }
252
    });
253
});
254
255
SCRIPT;
256
257
        Admin::script($script);
258
    }
259
}
260