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
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
protected $defaultValue = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array|Model|string|null $options |
48
|
|
|
* @param string|null $title |
49
|
|
|
*/ |
50
|
|
|
public function __construct($options = null, $title = null) |
51
|
|
|
{ |
52
|
|
|
parent::__construct(); |
53
|
|
|
|
54
|
|
|
if (is_array($options)) { |
55
|
|
|
$this->setOptions($options); |
56
|
|
|
} elseif (($options instanceof Model) || is_string($options)) { |
57
|
|
|
$this->setModelForOptions($options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (! is_null($title)) { |
61
|
|
|
$this->setDisplay($title); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function multiple() |
69
|
|
|
{ |
70
|
|
|
$this->multiple = true; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function initialize() |
76
|
|
|
{ |
77
|
|
|
parent::initialize(); |
78
|
|
|
|
79
|
|
|
$this->setHtmlAttribute('class', 'form-control input-select column-filter'); |
80
|
|
|
$this->setHtmlAttribute('data-type', 'select'); |
81
|
|
|
|
82
|
|
|
if ($this->multiple) { |
83
|
|
|
$this->setHtmlAttribute('multiple', 'multiple'); |
84
|
|
|
|
85
|
|
|
if (! in_array($this->operator, ['in', 'not_in'])) { |
86
|
|
|
$this->setOperator('in'); |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
$this->setHtmlAttribute('placeholder', $this->getPlaceholder()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param bool $sortable |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setSortable($sortable) |
99
|
|
|
{ |
100
|
|
|
$this->sortable = (bool) $sortable; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function isSortable() |
109
|
|
|
{ |
110
|
|
|
return $this->sortable; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Model|string $model |
115
|
|
|
* |
116
|
|
|
* @deprecated use setModelForOptions |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setModel($model) |
120
|
|
|
{ |
121
|
|
|
return $this->setModelForOptions($model); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getOptions() |
128
|
|
|
{ |
129
|
|
|
if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
130
|
|
|
$this->setOptions( |
131
|
|
|
$this->loadOptions() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$options = $this->options; |
136
|
|
|
if ($this->isSortable()) { |
137
|
|
|
asort($options); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $options; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $options |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function setOptions(array $options) |
149
|
|
|
{ |
150
|
|
|
$this->options = $options; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function getDefault() |
159
|
|
|
{ |
160
|
|
|
return $this->defaultValue; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param mixed $value |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setDefault($value) |
169
|
|
|
{ |
170
|
|
|
$this->defaultValue = $value; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getPlaceholder() |
179
|
|
|
{ |
180
|
|
|
return $this->placeholder; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $placeholder |
185
|
|
|
* |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setPlaceholder($placeholder) |
189
|
|
|
{ |
190
|
|
|
$this->placeholder = $placeholder; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function toArray() |
199
|
|
|
{ |
200
|
|
|
return parent::toArray() + [ |
201
|
|
|
'options' => $this->getOptions(), |
202
|
|
|
'default' => $this->getDefault(), |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param mixed $selected |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function parseValue($selected) |
212
|
|
|
{ |
213
|
|
|
if (is_string($selected) && strpos($selected, ':::') !== false) { |
214
|
|
|
return explode(':::', $selected); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $selected; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|