Completed
Pull Request — master (#2374)
by Limon
03:00
created

Actions::display()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 17
rs 9.7
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
     * Disable view action.
61
     *
62
     * @return $this
63
     */
64
    public function disableView()
65
    {
66
        array_delete($this->actions, 'view');
67
68
        return $this;
69
    }
70
71
    /**
72
     * Disable delete.
73
     *
74
     * @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...
75
     */
76
    public function disableDelete()
77
    {
78
        array_delete($this->actions, 'delete');
79
80
        return $this;
81
    }
82
83
    /**
84
     * Disable edit.
85
     *
86
     * @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...
87
     */
88
    public function disableEdit()
89
    {
90
        array_delete($this->actions, 'edit');
91
92
        return $this;
93
    }
94
95
    /**
96
     * Set resource of current resource.
97
     *
98
     * @param $resource
99
     *
100
     * @return $this
101
     */
102
    public function setResource($resource)
103
    {
104
        $this->resource = $resource;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Get resource of current resource.
111
     *
112
     * @return string
113
     */
114
    public function getResource()
115
    {
116
        return $this->resource ?: parent::getResource();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function display($callback = null)
123
    {
124
        if ($callback instanceof \Closure) {
125
            $callback->call($this, $this);
126
        }
127
128
        $actions = $this->prepends;
129
130
        foreach ($this->actions as $action) {
131
            $method = 'render'.ucfirst($action);
132
            array_push($actions, $this->{$method}());
133
        }
134
135
        $actions = array_merge($actions, $this->appends);
136
137
        return implode('', $actions);
138
    }
139
140
    /**
141
     * Render view action.
142
     *
143
     * @return string
144
     */
145
    protected function renderView()
146
    {
147
        return <<<EOT
148
<a href="{$this->getResource()}/{$this->getKey()}">
149
    <i class="fa fa-eye"></i>
150
</a>
151
EOT;
152
    }
153
154
    /**
155
     * Render edit action.
156
     *
157
     * @return string
158
     */
159
    protected function renderEdit()
160
    {
161
        return <<<EOT
162
<a href="{$this->getResource()}/{$this->getKey()}/edit">
163
    <i class="fa fa-edit"></i>
164
</a>
165
EOT;
166
    }
167
168
    /**
169
     * Render delete action.
170
     *
171
     * @return string
172
     */
173
    protected function renderDelete()
174
    {
175
        $deleteConfirm = trans('admin.delete_confirm');
176
        $confirm = trans('admin.confirm');
177
        $cancel = trans('admin.cancel');
178
179
        $script = <<<SCRIPT
180
181
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
182
183
    var id = $(this).data('id');
184
185
    swal({
186
        title: "$deleteConfirm",
187
        type: "warning",
188
        showCancelButton: true,
189
        confirmButtonColor: "#DD6B55",
190
        confirmButtonText: "$confirm",
191
        showLoaderOnConfirm: true,
192
        cancelButtonText: "$cancel",
193
        preConfirm: function() {
194
            return new Promise(function(resolve) {
195
                $.ajax({
196
                    method: 'post',
197
                    url: '{$this->getResource()}/' + id,
198
                    data: {
199
                        _method:'delete',
200
                        _token:LA.token,
201
                    },
202
                    success: function (data) {
203
                        $.pjax.reload('#pjax-container');
204
205
                        resolve(data);
206
                    }
207
                });
208
            });
209
        }
210
    }).then(function(result) {
211
        var data = result.value;
212
        if (typeof data === 'object') {
213
            if (data.status) {
214
                swal(data.message, '', 'success');
215
            } else {
216
                swal(data.message, '', 'error');
217
            }
218
        }
219
    });
220
});
221
222
SCRIPT;
223
224
        Admin::script($script);
225
226
        return <<<EOT
227
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete">
228
    <i class="fa fa-trash"></i>
229
</a>
230
EOT;
231
    }
232
}
233