Completed
Push — master ( 77fd36...f39186 )
by Song
02:52
created

Tools::disableDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
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
    }
47
48
    /**
49
     * Append a tools.
50
     *
51
     * @param mixed $tool
52
     *
53
     * @return $this
54
     */
55
    public function append($tool)
56
    {
57
        $this->appends->push($tool);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Prepend a tool.
64
     *
65
     * @param mixed $tool
66
     *
67
     * @return $this
68
     */
69
    public function prepend($tool)
70
    {
71
        $this->prepends->push($tool);
72
73
        return $this;
74
    }
75
76
    /**
77
     * Disable `list` tool.
78
     *
79
     * @return $this
80
     */
81
    public function disableList()
82
    {
83
        array_delete($this->tools, 'list');
84
85
        return $this;
86
    }
87
88
    /**
89
     * Disable `delete` tool.
90
     *
91
     * @return $this
92
     */
93
    public function disableDelete()
94
    {
95
        array_delete($this->tools, 'delete');
96
97
        return $this;
98
    }
99
100
    /**
101
     * Disable `edit` tool.
102
     *
103
     * @return $this
104
     */
105
    public function disableView()
106
    {
107
        array_delete($this->tools, 'view');
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get request path for resource list.
114
     *
115
     * @return string
116
     */
117
    protected function getListPath()
118
    {
119
        return $this->form->getResource();
120
    }
121
122
    /**
123
     * Get request path for edit.
124
     *
125
     * @return string
126
     */
127
    protected function getDeletePath()
128
    {
129
        return $this->getViewPath();
130
    }
131
132
    /**
133
     * Get request path for delete.
134
     *
135
     * @return string
136
     */
137
    protected function getViewPath()
138
    {
139
        $key = $this->form->getResourceId();
140
141
        return $this->getListPath().'/'.$key;
142
    }
143
144
    /**
145
     * Render list button.
146
     *
147
     * @return string
148
     */
149
    protected function renderList()
150
    {
151
        $text = trans('admin.list');
152
153
        return <<<EOT
154
<div class="btn-group pull-right" style="margin-right: 5px">
155
    <a href="{$this->getListPath()}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp;$text</a>
156
</div>
157
EOT;
158
    }
159
160
    /**
161
     * Render list button.
162
     *
163
     * @return string
164
     */
165
    protected function renderView()
166
    {
167
        $view = trans('admin.view');
168
169
        return <<<HTML
170
<div class="btn-group pull-right" style="margin-right: 5px">
171
    <a href="{$this->getViewPath()}" class="btn btn-sm btn-primary">
172
        <i class="fa fa-eye"></i> {$view}
173
    </a>
174
</div>
175
HTML;
176
    }
177
178
    /**
179
     * Render `delete` tool.
180
     *
181
     * @return string
182
     */
183 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...
184
    {
185
        $deleteConfirm = trans('admin.delete_confirm');
186
        $confirm = trans('admin.confirm');
187
        $cancel = trans('admin.cancel');
188
189
        $class = uniqid();
190
191
        $script = <<<SCRIPT
192
193
$('.{$class}-delete').unbind('click').click(function() {
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->getDeletePath()}',
208
            data: {
209
                _method:'delete',
210
                _token:LA.token,
211
            },
212
            success: function (data) {
213
                $.pjax({container:'#pjax-container', url: '{$this->getListPath()}' });
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
        $delete = trans('admin.delete');
230
231
        Admin::script($script);
232
233
        return <<<HTML
234
<div class="btn-group pull-right" style="margin-right: 5px">
235
    <a href="javascript:void(0);" class="btn btn-sm btn-danger {$class}-delete">
236
        <i class="fa fa-trash"></i>  {$delete}
237
    </a>
238
</div>
239
HTML;
240
    }
241
242
    /**
243
     * Add a tool.
244
     *
245
     * @param string $tool
246
     *
247
     * @return $this
248
     *
249
     * @deprecated use append instead.
250
     */
251
    public function add($tool)
252
    {
253
        return $this->append($tool);
254
    }
255
256
    /**
257
     * Disable back button.
258
     *
259
     * @return $this
260
     *
261
     * @deprecated
262
     */
263
    public function disableBackButton()
264
    {
265
    }
266
267
    /**
268
     * Disable list button.
269
     *
270
     * @return $this
271
     *
272
     * @deprecated Use disableList instead.
273
     */
274
    public function disableListButton()
275
    {
276
        return $this->disableList();
277
    }
278
279
    /**
280
     * Render custom tools.
281
     *
282
     * @param Collection $tools
283
     * @return mixed
284
     */
285 View Code Duplication
    protected function renderCustomTools($tools)
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...
286
    {
287
        if (empty($tools)) {
288
            return '';
289
        }
290
291
        return $tools->map(function ($tool) {
292
            if ($tool instanceof Renderable) {
293
                return $tool->render();
294
            }
295
296
            if ($tool instanceof Htmlable) {
297
                return $tool->toHtml();
298
            }
299
300
            return (string) $tool;
301
        })->implode(' ');
302
    }
303
304
    /**
305
     * Render tools.
306
     *
307
     * @return string
308
     */
309 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...
310
    {
311
        $output = $this->renderCustomTools($this->prepends);
312
313
        foreach ($this->tools as $tool) {
314
            $renderMethod = 'render'.ucfirst($tool);
315
            $output .= $this->$renderMethod();
316
        }
317
318
        return $output.$this->renderCustomTools($this->appends);
319
    }
320
}
321