Completed
Push — master ( df4ec7...ec303c )
by Song
44:32 queued 33:07
created

Tools::renderView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form;
4
5
use Encore\Admin\Facades\Admin;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Support\Collection;
9
10
class Tools implements Renderable
11
{
12
    /**
13
     * @var Builder
14
     */
15
    protected $form;
16
17
    /**
18
     * Collection of tools.
19
     *
20
     * @var array
21
     */
22
    protected $tools = ['delete', 'view', 'list'];
23
24
    /**
25
     * Tools should be appends to default tools.
26
     *
27
     * @var Collection
28
     */
29
    protected $appends;
30
31
    /**
32
     * Tools should be prepends to default tools.
33
     *
34
     * @var Collection
35
     */
36
    protected $prepends;
37
38
    /**
39
     * Create a new Tools instance.
40
     *
41
     * @param Builder $builder
42
     */
43
    public function __construct(Builder $builder)
44
    {
45
        $this->form = $builder;
46
        $this->appends = new Collection();
47
        $this->prepends = new Collection();
48
    }
49
50
    /**
51
     * Append a tools.
52
     *
53
     * @param mixed $tool
54
     *
55
     * @return $this
56
     */
57
    public function append($tool)
58
    {
59
        $this->appends->push($tool);
60
61
        return $this;
62
    }
63
64
    /**
65
     * Prepend a tool.
66
     *
67
     * @param mixed $tool
68
     *
69
     * @return $this
70
     */
71
    public function prepend($tool)
72
    {
73
        $this->prepends->push($tool);
74
75
        return $this;
76
    }
77
78
    /**
79
     * Disable `list` tool.
80
     *
81
     * @return $this
82
     */
83
    public function disableList()
84
    {
85
        array_delete($this->tools, 'list');
86
87
        return $this;
88
    }
89
90
    /**
91
     * Disable `delete` tool.
92
     *
93
     * @return $this
94
     */
95
    public function disableDelete()
96
    {
97
        array_delete($this->tools, 'delete');
98
99
        return $this;
100
    }
101
102
    /**
103
     * Disable `edit` tool.
104
     *
105
     * @return $this
106
     */
107
    public function disableView()
108
    {
109
        array_delete($this->tools, 'view');
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get request path for resource list.
116
     *
117
     * @return string
118
     */
119
    protected function getListPath()
120
    {
121
        return $this->form->getResource();
122
    }
123
124
    /**
125
     * Get request path for edit.
126
     *
127
     * @return string
128
     */
129
    protected function getDeletePath()
130
    {
131
        return $this->getViewPath();
132
    }
133
134
    /**
135
     * Get request path for delete.
136
     *
137
     * @return string
138
     */
139
    protected function getViewPath()
140
    {
141
        $key = $this->form->getResourceId();
142
143
        if ($key) {
144
            return $this->getListPath().'/'.$key;
145
        } else {
146
            return $this->getListPath();
147
        }
148
    }
149
150
    /**
151
     * Get parent form of tool.
152
     *
153
     * @return Builder
154
     */
155
    public function form()
156
    {
157
        return $this->form;
158
    }
159
160
    /**
161
     * Render list button.
162
     *
163
     * @return string
164
     */
165
    protected function renderList()
166
    {
167
        $text = trans('admin.list');
168
169
        return <<<EOT
170
<div class="btn-group pull-right" style="margin-right: 5px">
171
    <a href="{$this->getListPath()}" class="btn btn-sm btn-default" title="$text"><i class="fa fa-list"></i><span class="hidden-xs">&nbsp;$text</span></a>
172
</div>
173
EOT;
174
    }
175
176
    /**
177
     * Render list button.
178
     *
179
     * @return string
180
     */
181
    protected function renderView()
182
    {
183
        $view = trans('admin.view');
184
185
        return <<<HTML
186
<div class="btn-group pull-right" style="margin-right: 5px">
187
    <a href="{$this->getViewPath()}" class="btn btn-sm btn-primary" title="{$view}">
188
        <i class="fa fa-eye"></i><span class="hidden-xs"> {$view}</span>
189
    </a>
190
</div>
191
HTML;
192
    }
193
194
    /**
195
     * Render `delete` tool.
196
     *
197
     * @return string
198
     */
199 View Code Duplication
    protected function renderDelete()
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...
200
    {
201
        $deleteConfirm = trans('admin.delete_confirm');
202
        $confirm = trans('admin.confirm');
203
        $cancel = trans('admin.cancel');
204
205
        $class = uniqid();
206
207
        $script = <<<SCRIPT
208
209
$('.{$class}-delete').unbind('click').click(function() {
210
211
    swal({
212
        title: "$deleteConfirm",
213
        type: "warning",
214
        showCancelButton: true,
215
        confirmButtonColor: "#DD6B55",
216
        confirmButtonText: "$confirm",
217
        showLoaderOnConfirm: true,
218
        cancelButtonText: "$cancel",
219
        preConfirm: function() {
220
            return new Promise(function(resolve) {
221
                $.ajax({
222
                    method: 'post',
223
                    url: '{$this->getDeletePath()}',
224
                    data: {
225
                        _method:'delete',
226
                        _token:LA.token,
227
                    },
228
                    success: function (data) {
229
                        $.pjax({container:'#pjax-container', url: '{$this->getListPath()}' });
230
231
                        resolve(data);
232
                    }
233
                });
234
            });
235
        }
236
    }).then(function(result) {
237
        var data = result.value;
238
        if (typeof data === 'object') {
239
            if (data.status) {
240
                swal(data.message, '', 'success');
241
            } else {
242
                swal(data.message, '', 'error');
243
            }
244
        }
245
    });
246
});
247
248
SCRIPT;
249
250
        $delete = trans('admin.delete');
251
252
        Admin::script($script);
253
254
        return <<<HTML
255
<div class="btn-group pull-right" style="margin-right: 5px">
256
    <a href="javascript:void(0);" class="btn btn-sm btn-danger {$class}-delete" title="{$delete}">
257
        <i class="fa fa-trash"></i><span class="hidden-xs">  {$delete}</span>
258
    </a>
259
</div>
260
HTML;
261
    }
262
263
    /**
264
     * Add a tool.
265
     *
266
     * @param string $tool
267
     *
268
     * @return $this
269
     *
270
     * @deprecated use append instead.
271
     */
272
    public function add($tool)
273
    {
274
        return $this->append($tool);
275
    }
276
277
    /**
278
     * Disable back button.
279
     *
280
     * @return $this
281
     *
282
     * @deprecated
283
     */
284
    public function disableBackButton()
285
    {
286
    }
287
288
    /**
289
     * Disable list button.
290
     *
291
     * @return $this
292
     *
293
     * @deprecated Use disableList instead.
294
     */
295
    public function disableListButton()
296
    {
297
        return $this->disableList();
298
    }
299
300
    /**
301
     * Render custom tools.
302
     *
303
     * @param Collection $tools
304
     *
305
     * @return mixed
306
     */
307
    protected function renderCustomTools($tools)
308
    {
309
        if ($this->form->isCreating()) {
310
            $this->disableView();
311
            $this->disableDelete();
312
        }
313
314
        if (empty($tools)) {
315
            return '';
316
        }
317
318 View Code Duplication
        return $tools->map(function ($tool) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
319
            if ($tool instanceof Renderable) {
320
                return $tool->render();
321
            }
322
323
            if ($tool instanceof Htmlable) {
324
                return $tool->toHtml();
325
            }
326
327
            return (string) $tool;
328
        })->implode(' ');
329
    }
330
331
    /**
332
     * Render tools.
333
     *
334
     * @return string
335
     */
336 View Code Duplication
    public function render()
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...
337
    {
338
        $output = $this->renderCustomTools($this->prepends);
339
340
        foreach ($this->tools as $tool) {
341
            $renderMethod = 'render'.ucfirst($tool);
342
            $output .= $this->$renderMethod();
343
        }
344
345
        return $output.$this->renderCustomTools($this->appends);
346
    }
347
}
348