Completed
Pull Request — master (#2254)
by
unknown
02:53
created

Actions::getResourceURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
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
     * 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
     * Get url of current resource.
121
     * @return string
122
     */
123
    public function getResourceURL(){
124
        return url($this->getResource());
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function display($callback = null)
131
    {
132
        if ($callback instanceof \Closure) {
133
            $callback->call($this, $this);
134
        }
135
136
        $actions = $this->prepends;
137
138
        foreach ($this->actions as $action) {
139
            $method = 'render'.ucfirst($action);
140
            array_push($actions, $this->{$method}());
141
        }
142
143
        $actions = array_merge($actions, $this->appends);
144
145
        return implode('', $actions);
146
    }
147
148
    /**
149
     * Render view action.
150
     *
151
     * @return string
152
     */
153
    protected function renderView()
154
    {
155
        return <<<EOT
156
<a href="{$this->getResource()}/{$this->getKey()}">
157
    <i class="fa fa-eye"></i>
158
</a>
159
EOT;
160
    }
161
162
    /**
163
     * Render edit action.
164
     *
165
     * @return string
166
     */
167
    protected function renderEdit()
168
    {
169
        $editText = trans("admin.edit");
170
171
        return <<<EOT
172
<a href="{$this->getResourceURL()}/{$this->getKey()}/edit">
173
    <i class="fa fa-edit btn btn-primary" title="{$editText}"></i>
174
</a>
175
EOT;
176
    }
177
178
    /**
179
     * Render delete action.
180
     *
181
     * @return string
182
     */
183
    protected function renderDelete()
184
    {
185
        $deleteConfirm = trans('admin.delete_confirm');
186
        $confirm = trans('admin.confirm');
187
        $cancel = trans('admin.cancel');
188
189
        $script = <<<SCRIPT
190
191
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
192
193
    var id = $(this).data('id');
194
195
    swal({
196
      title: "$deleteConfirm",
197
      type: "warning",
198
      showCancelButton: true,
199
      confirmButtonColor: "#DD6B55",
200
      confirmButtonText: "$confirm",
201
      closeOnConfirm: false,
202
      cancelButtonText: "$cancel"
203
    },
204
    function(){
205
        $.ajax({
206
            method: 'post',
207
            url: '{$this->getResourceURL()}/' + id,
208
            data: {
209
                _method:'delete',
210
                _token:LA.token,
211
            },
212
            success: function (data) {
213
                $.pjax.reload('#pjax-container');
214
215
                if (typeof data === 'object') {
216
                    if (data.status) {
217
                        swal(data.message, '', 'success');
218
                    } else {
219
                        swal(data.message, '', 'error');
220
                    }
221
                }
222
            }
223
        });
224
    });
225
});
226
227
SCRIPT;
228
229
        Admin::script($script);
230
231
        $deleteText = trans("admin.delete");
232
        return <<<EOT
233
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete">
234
    <i class="fa fa-trash btn btn-danger" title="{$deleteText}"></i>
235
</a>
236
EOT;
237
    }
238
}
239