|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
|
|
7
|
|
|
class Select extends AbstractDisplayer |
|
8
|
|
|
{ |
|
9
|
|
|
protected function addScript() |
|
10
|
|
|
{ |
|
11
|
|
|
$name = $this->column->getName(); |
|
12
|
|
|
|
|
13
|
|
|
$class = "grid-select-{$name}"; |
|
14
|
|
|
|
|
15
|
|
|
$script = <<<EOT |
|
16
|
|
|
|
|
17
|
|
|
$('.$class .dropdown-menu li a').click(function () { |
|
18
|
|
|
var target = $(this); |
|
19
|
|
|
|
|
20
|
|
|
var text = target.text(); |
|
21
|
|
|
var value = target.data('value'); |
|
22
|
|
|
var key = target.parents('.$class').attr('key'); |
|
23
|
|
|
|
|
24
|
|
|
$.ajax({ |
|
25
|
|
|
url: "{$this->grid->resource()}/" + key, |
|
26
|
|
|
type: "POST", |
|
27
|
|
|
data: { |
|
28
|
|
|
$name: value, |
|
29
|
|
|
_token: LA.token, |
|
30
|
|
|
_method: 'PUT' |
|
31
|
|
|
}, |
|
32
|
|
|
success: function (data) { |
|
33
|
|
|
target.parents('.$class').find('.select-text').text(text); |
|
34
|
|
|
|
|
35
|
|
|
target.parents('.dropdown-menu').find('li a').each(function () { |
|
36
|
|
|
if (value == $(this).data('value')) { |
|
37
|
|
|
$(this).find('i').removeClass('invisible') |
|
38
|
|
|
} else { |
|
39
|
|
|
$(this).find('i').addClass('invisible') |
|
40
|
|
|
} |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
toastr.success(data.message); |
|
44
|
|
|
} |
|
45
|
|
|
}); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
EOT; |
|
49
|
|
|
|
|
50
|
|
|
Admin::script($script); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function addStyle() |
|
54
|
|
|
{ |
|
55
|
|
|
$style = <<<STYLE |
|
56
|
|
|
.grid-select-{$this->column->getName()} .dropdown-menu { |
|
57
|
|
|
min-width: 0px; |
|
58
|
|
|
} |
|
59
|
|
|
STYLE; |
|
60
|
|
|
|
|
61
|
|
|
Admin::style($style); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function display($options = []) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->addScript(); |
|
67
|
|
|
$this->addStyle(); |
|
68
|
|
|
|
|
69
|
|
|
$name = $this->column->getName(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if ($options instanceof \Closure) { |
|
72
|
|
|
$options = $options->call($this, $this->row); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$optionsHtml = ''; |
|
76
|
|
|
|
|
77
|
|
|
foreach ($options as $option => $text) { |
|
78
|
|
|
$invisible = $option == $this->value ? '' : 'invisible'; |
|
79
|
|
|
$optionsHtml .= "<li><a href='javascript:void(0);' data-value='{$option}'><i class=\"fa fa-check $invisible\"></i>{$text}</a></li>"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return <<<HTML |
|
83
|
|
|
<div class="dropdown grid-select-{$this->column->getName()}" key="{$this->getKey()}"> |
|
84
|
|
|
<a href="javascript:void(0);" class="dropdown-toggle" data-toggle="dropdown"> |
|
85
|
|
|
<span class="select-text text-muted">{$options[$this->value]}</span> |
|
86
|
|
|
<span class="caret"></span> |
|
87
|
|
|
</a> |
|
88
|
|
|
<ul class="dropdown-menu"> |
|
89
|
|
|
{$optionsHtml} |
|
90
|
|
|
</ul> |
|
91
|
|
|
</div> |
|
92
|
|
|
HTML; |
|
93
|
|
|
EOT; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.