|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class Select extends BaseColumnFilter |
|
8
|
|
|
{ |
|
9
|
|
|
use \SleepingOwl\Admin\Traits\SelectOptionsFromModel; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $view = 'column.filter.select'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Model |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $model; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $options = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $placeholder; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $multiple = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $sortable = true; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array|Model|string|null $options |
|
43
|
|
|
* @param string|null $title |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($options = null, $title = null) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct(); |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
if (is_array($options)) { |
|
|
|
|
|
|
50
|
|
|
$this->setOptions($options); |
|
51
|
|
|
} elseif (($options instanceof Model) or is_string($options)) { |
|
|
|
|
|
|
52
|
|
|
$this->setModelForOptions($options); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (! is_null($title)) { |
|
56
|
|
|
$this->setDisplay($title); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function multiple() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->multiple = true; |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function initialize() |
|
71
|
|
|
{ |
|
72
|
|
|
parent::initialize(); |
|
73
|
|
|
|
|
74
|
|
|
$this->setHtmlAttribute('class', 'form-control input-select column-filter'); |
|
75
|
|
|
$this->setHtmlAttribute('data-type', 'select'); |
|
76
|
|
|
|
|
77
|
|
|
if ($this->multiple) { |
|
78
|
|
|
$this->setHtmlAttribute('multiple', 'multiple'); |
|
79
|
|
|
|
|
80
|
|
|
if (! in_array($this->operator, ['in', 'not_in'])) { |
|
81
|
|
|
$this->setOperator('in'); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->setHtmlAttribute('placeholder', $this->getPlaceholder()); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param bool $sortable |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setSortable($sortable) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->sortable = (bool) $sortable; |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function isSortable() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->sortable; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param Model|string $model |
|
110
|
|
|
* |
|
111
|
|
|
* @deprecated use setModelForOptions |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setModel($model) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->setModelForOptions($model); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function getOptions() |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
if (! is_null($this->getModelForOptions()) and ! is_null($this->getDisplay())) { |
|
|
|
|
|
|
125
|
|
|
$this->setOptions( |
|
126
|
|
|
$this->loadOptions() |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$options = $this->options; |
|
131
|
|
|
if ($this->isSortable()) { |
|
132
|
|
|
asort($options); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $options; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param array $options |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setOptions(array $options) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->options = $options; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getPlaceholder() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->placeholder; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $placeholder |
|
160
|
|
|
* |
|
161
|
|
|
* @return $this |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setPlaceholder($placeholder) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->placeholder = $placeholder; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
public function toArray() |
|
174
|
|
|
{ |
|
175
|
|
|
return parent::toArray() + [ |
|
176
|
|
|
'options' => $this->getOptions(), |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param mixed $selected |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
|
|
public function parseValue($selected) |
|
186
|
|
|
{ |
|
187
|
|
|
if (is_string($selected) && strpos($selected, ',') !== false) { |
|
188
|
|
|
return explode(',', $selected); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $selected; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
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.