1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
|
8
|
|
|
class Checkbox extends AbstractDisplayer |
9
|
|
|
{ |
10
|
|
|
public function display($options = []) |
11
|
|
|
{ |
12
|
|
|
if ($options instanceof \Closure) { |
13
|
|
|
$options = $options->call($this, $this->row); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
$radios = ''; |
17
|
|
|
$name = $this->column->getName(); |
18
|
|
|
|
19
|
|
|
if (is_string($this->value)) { |
20
|
|
|
$this->value = explode(',', $this->value); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($this->value instanceof Arrayable) { |
24
|
|
|
$this->value = $this->value->toArray(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
foreach ($options as $value => $label) { |
|
|
|
|
28
|
|
|
$checked = in_array($value, $this->value) ? 'checked' : ''; |
29
|
|
|
$radios .= <<<EOT |
30
|
|
|
<div class="checkbox"> |
31
|
|
|
<label> |
32
|
|
|
<input type="checkbox" name="grid-checkbox-{$name}[]" value="{$value}" $checked />{$label} |
33
|
|
|
</label> |
34
|
|
|
</div> |
35
|
|
|
EOT; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
Admin::script($this->script()); |
39
|
|
|
|
40
|
|
|
return <<<EOT |
41
|
|
|
<form class="form-group grid-checkbox-$name" style="text-align:left;" data-key="{$this->getKey()}"> |
42
|
|
|
$radios |
43
|
|
|
<button type="submit" class="btn btn-info btn-xs pull-left"> |
44
|
|
|
<i class="fa fa-save"></i> {$this->trans('save')} |
45
|
|
|
</button> |
46
|
|
|
<button type="reset" class="btn btn-warning btn-xs pull-left" style="margin-left:10px;"> |
47
|
|
|
<i class="fa fa-trash"></i> {$this->trans('reset')} |
48
|
|
|
</button> |
49
|
|
|
</form> |
50
|
|
|
EOT; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function script() |
54
|
|
|
{ |
55
|
|
|
$name = $this->column->getName(); |
56
|
|
|
|
57
|
|
|
return <<<EOT |
58
|
|
|
|
59
|
|
|
$('form.grid-checkbox-$name').on('submit', function () { |
60
|
|
|
var values = $(this).find('input:checkbox:checked').map(function (_, el) { |
61
|
|
|
return $(el).val(); |
62
|
|
|
}).get(); |
63
|
|
|
|
64
|
|
|
var data = { |
65
|
|
|
$name: values, |
66
|
|
|
_token: LA.token, |
67
|
|
|
_method: 'PUT' |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$.ajax({ |
71
|
|
|
url: "{$this->getResource()}/" + $(this).data('key'), |
72
|
|
|
type: "POST", |
73
|
|
|
contentType: 'application/json;charset=utf-8', |
74
|
|
|
data: JSON.stringify(data), |
75
|
|
|
success: function (data) { |
76
|
|
|
toastr.success(data.message); |
77
|
|
|
} |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
EOT; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.