Completed
Pull Request — master (#2254)
by
unknown
12:23 queued 09:50
created

Actions::editAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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