Completed
Push — master ( 024746...0b49f1 )
by Song
04:00
created

Tools::getListPath()   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\Show;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Contracts\Support\Renderable;
7
8
class Tools implements Renderable
9
{
10
    /**
11
     * The panel that holds this tool.
12
     *
13
     * @var Panel
14
     */
15
    protected $panel;
16
17
    /**
18
     * @var string
19
     */
20
    protected $resource;
21
22
    /**
23
     * Default tools.
24
     *
25
     * @var array
26
     */
27
    protected $tools = ['delete', 'list', 'edit'];
28
29
    /**
30
     * Tools should be appends to default tools.
31
     *
32
     * @var array
33
     */
34
    protected $appends = [];
35
36
    /**
37
     * Tools should be prepends to default tools.
38
     *
39
     * @var array
40
     */
41
    protected $prepends = [];
42
43
    /**
44
     * Tools constructor.
45
     *
46
     * @param Panel $panel
47
     */
48
    public function __construct(Panel $panel)
49
    {
50
        $this->panel = $panel;
51
    }
52
53
    /**
54
     * Append a tools.
55
     *
56
     * @param mixed $tool
57
     *
58
     * @return $this
59
     */
60
    public function append($tool)
61
    {
62
        $this->appends[] = $tool;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Prepend a tool.
69
     *
70
     * @param mixed $tool
71
     *
72
     * @return $this
73
     */
74
    public function prepend($tool)
75
    {
76
        $this->prepends[] = $tool;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get resource path.
83
     *
84
     * @return string
85
     */
86
    public function getResource()
87
    {
88
        if (is_null($this->resource)) {
89
            $this->resource = $this->panel->getParent()->getResourcePath();
90
        }
91
92
        return $this->resource;
93
    }
94
95
    /**
96
     * Disable `list` tool.
97
     *
98
     * @return $this
99
     */
100
    public function disableList()
101
    {
102
        array_delete($this->tools, 'list');
103
104
        return $this;
105
    }
106
107
    /**
108
     * Disable `delete` tool.
109
     *
110
     * @return $this
111
     */
112
    public function disableDelete()
113
    {
114
        array_delete($this->tools, 'delete');
115
116
        return $this;
117
    }
118
119
    /**
120
     * Disable `edit` tool.
121
     *
122
     * @return $this
123
     */
124
    public function disableEdit()
125
    {
126
        array_delete($this->tools, 'edit');
127
128
        return $this;
129
    }
130
131
    /**
132
     * Get request path for resource list.
133
     *
134
     * @return string
135
     */
136
    protected function getListPath()
137
    {
138
        return '/' . ltrim($this->getResource(), '/');
139
    }
140
141
    /**
142
     * Get request path for edit.
143
     *
144
     * @return string
145
     */
146
    protected function getEditPath()
147
    {
148
        $key = $this->panel->getParent()->getModel()->getKey();
149
150
        return $this->getListPath() . '/' . $key . '/edit';
151
    }
152
153
    /**
154
     * Get request path for delete.
155
     *
156
     * @return string
157
     */
158
    protected function getDeletePath()
159
    {
160
        $key = $this->panel->getParent()->getModel()->getKey();
161
162
        return $this->getListPath() . '/' . $key;
163
    }
164
165
    /**
166
     * Render `list` tool.
167
     *
168
     * @return string
169
     */
170
    protected function renderList()
171
    {
172
        return <<<HTML
173
<div class="btn-group pull-right" style="margin-right: 5px">
174
    <a href="{$this->getListPath()}" class="btn btn-sm btn-default">
175
        <i class="fa fa-list"></i>
176
    </a>
177
</div>
178
HTML;
179
    }
180
181
    /**
182
     * Render `edit` tool.
183
     *
184
     * @return string
185
     */
186
    protected function renderEdit()
187
    {
188
        return <<<HTML
189
<div class="btn-group pull-right" style="margin-right: 5px">
190
    <a href="{$this->getEditPath()}" class="btn btn-sm btn-primary">
191
        <i class="fa fa-edit"></i>
192
    </a>
193
</div>
194
HTML;
195
    }
196
197
    /**
198
     * Render `delete` tool.
199
     *
200
     * @return string
201
     */
202
    protected function renderDelete()
203
    {
204
        $deleteConfirm = trans('admin.delete_confirm');
205
        $confirm       = trans('admin.confirm');
206
        $cancel        = trans('admin.cancel');
207
208
        $class = uniqid();
209
210
        $script = <<<SCRIPT
211
212
$('.{$class}-delete').unbind('click').click(function() {
213
214
    swal({
215
      title: "$deleteConfirm",
216
      type: "warning",
217
      showCancelButton: true,
218
      confirmButtonColor: "#DD6B55",
219
      confirmButtonText: "$confirm",
220
      closeOnConfirm: false,
221
      cancelButtonText: "$cancel"
222
    },
223
    function(){
224
        $.ajax({
225
            method: 'post',
226
            url: '{$this->getDeletePath()}',
227
            data: {
228
                _method:'delete',
229
                _token:LA.token,
230
            },
231
            success: function (data) {
232
                $.pjax.reload('#pjax-container');
233
234
                if (typeof data === 'object') {
235
                    if (data.status) {
236
                        swal(data.message, '', 'success');
237
                    } else {
238
                        swal(data.message, '', 'error');
239
                    }
240
                }
241
            }
242
        });
243
    });
244
});
245
246
SCRIPT;
247
248
        Admin::script($script);
249
250
        return <<<HTML
251
<div class="btn-group pull-right" style="margin-right: 5px">
252
    <a href="javascript:void(0);" class="btn btn-sm btn-danger {$class}-delete">
253
        <i class="fa fa-trash"></i>
254
    </a>
255
</div>
256
HTML;
257
    }
258
259
    /**
260
     * Render tools.
261
     *
262
     * @return string
263
     */
264
    public function render()
265
    {
266
        $output = implode('', $this->prepends);
267
268
        foreach ($this->tools as $tool) {
269
            $renderMethod = 'render' . ucfirst($tool);
270
            $output .= $this->$renderMethod();
271
        }
272
273
        return $output . implode('', $this->appends);
274
    }
275
}
276