1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use SleepingOwl\Admin\Traits\SelectOptionsFromModel; |
8
|
|
|
|
9
|
|
|
class Select extends NamedFormElement |
10
|
|
|
{ |
11
|
|
|
use SelectOptionsFromModel; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $options = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $nullable = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $sortable = true; |
27
|
|
|
protected $sortable_flags = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $exclude = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $limit = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $view = 'form.element.select'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $view_select2 = 'form.element.select2'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $select2_mode = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Select constructor. |
56
|
|
|
* @param $path |
57
|
|
|
* @param null $label |
|
|
|
|
58
|
|
|
* @param array $options |
59
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
60
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
61
|
|
|
*/ |
62
|
|
|
public function __construct($path, $label = null, $options = []) |
63
|
|
|
{ |
64
|
|
|
parent::__construct($path, $label); |
65
|
|
|
|
66
|
|
|
if (is_array($options)) { |
|
|
|
|
67
|
|
|
$this->setOptions($options); |
68
|
|
|
} elseif (($options instanceof Model) || is_string($options)) { |
69
|
|
|
$this->setModelForOptions($options); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getOptions() |
77
|
|
|
{ |
78
|
|
|
if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
79
|
|
|
$this->setOptions( |
80
|
|
|
$this->loadOptions() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$options = Arr::except($this->options, $this->exclude); |
85
|
|
|
if ($this->isSortable()) { |
86
|
|
|
asort($options, $this->getSortableFlags()); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $options; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function mutateOptions() |
96
|
|
|
{ |
97
|
|
|
$options = []; |
98
|
|
|
$temp = $this->getOptions(); |
99
|
|
|
foreach ($temp as $key => $value) { |
100
|
|
|
$options[] = ['id' => $key, 'text' => $value]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $options; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setOptions(array $options) |
112
|
|
|
{ |
113
|
|
|
$this->options = $options; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $values |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setEnum(array $values) |
124
|
|
|
{ |
125
|
|
|
return $this->setOptions(array_combine($values, $values)); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function isNullable() |
132
|
|
|
{ |
133
|
|
|
return $this->nullable; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function nullable() |
140
|
|
|
{ |
141
|
|
|
$this->nullable = true; |
142
|
|
|
|
143
|
|
|
$this->addValidationRule('nullable'); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param bool $sortable |
150
|
|
|
* |
151
|
|
|
* @param null $sortable_flags |
|
|
|
|
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setSortable($sortable, $sortable_flags = null) |
155
|
|
|
{ |
156
|
|
|
$this->sortable = (bool) $sortable; |
157
|
|
|
$this->sortable_flags = $sortable_flags; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return null |
164
|
|
|
*/ |
165
|
|
|
protected function getSortableFlags() |
166
|
|
|
{ |
167
|
|
|
return $this->sortable_flags; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function isSortable() |
174
|
|
|
{ |
175
|
|
|
return $this->sortable; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
protected function getLimit() |
182
|
|
|
{ |
183
|
|
|
return $this->limit; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $limit |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function setLimit($limit) |
191
|
|
|
{ |
192
|
|
|
$this->limit = $limit; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function getSelect2() |
201
|
|
|
{ |
202
|
|
|
return $this->select2_mode; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param bool $mode |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function setSelect2($mode) |
211
|
|
|
{ |
212
|
|
|
$this->select2_mode = $mode; |
213
|
|
|
|
214
|
|
|
$class = 'input-select'; |
215
|
|
|
$class_escaped = strtr($class, ['-' => '\-']); |
216
|
|
|
|
217
|
|
|
if ($this->select2_mode) { |
218
|
|
|
$this->setView($this->view_select2); |
219
|
|
|
$this->setHtmlAttribute('class', $class); |
220
|
|
|
} else { |
221
|
|
|
$attrs = $this->getHtmlAttribute('class'); |
222
|
|
|
$pattern = "~(?:^{$class_escaped}$|^{$class_escaped}\s|s\{$class_escaped}$|\s{$class_escaped}\s)~s"; |
223
|
|
|
$replace = trim(preg_replace($pattern, ' ', $attrs)); |
224
|
|
|
|
225
|
|
|
$this->setView($this->view); |
226
|
|
|
$this->removeHtmlAttribute('class'); |
227
|
|
|
$this->setHtmlAttribute('class', $replace); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function getExclude() |
237
|
|
|
{ |
238
|
|
|
return $this->exclude; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param array $keys |
243
|
|
|
* |
244
|
|
|
* @return $this |
245
|
|
|
*/ |
246
|
|
|
public function setExclude($keys) |
247
|
|
|
{ |
248
|
|
|
return $this->exclude($keys); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param array $keys |
253
|
|
|
* |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
|
|
public function exclude($keys) |
257
|
|
|
{ |
258
|
|
|
if (! is_array($keys)) { |
259
|
|
|
$keys = func_get_args(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$this->exclude = array_filter($keys); |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return null|string |
269
|
|
|
*/ |
270
|
|
|
public function getForeignKey() |
271
|
|
|
{ |
272
|
|
|
if (is_null($this->foreignKey)) { |
273
|
|
|
return $this->foreignKey = $this->getModel()->getForeignKey(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $this->foreignKey; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function toArray() |
283
|
|
|
{ |
284
|
|
|
$this->setHtmlAttribute('id', ($this->getHtmlAttribute('id') ?: $this->getId())); |
285
|
|
|
$this->setHtmlAttributes([ |
286
|
|
|
'size' => 2, |
287
|
|
|
'data-select-type' => 'single', |
288
|
|
|
]); |
289
|
|
|
|
290
|
|
|
$this->setHtmlAttribute('class', 'form-control'); |
291
|
|
|
|
292
|
|
|
if ($this->isReadonly()) { |
293
|
|
|
$this->setHtmlAttribute('disabled', 'disabled'); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$options = $this->mutateOptions(); |
297
|
|
|
|
298
|
|
|
if ($this->isNullable()) { |
299
|
|
|
$this->setHtmlAttribute('data-nullable', 'true'); |
300
|
|
|
$options = collect($options)->prepend(['id' => null, 'text' => trans('sleeping_owl::lang.select.nothing')]); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$return = [ |
304
|
|
|
'attributes' => $this->htmlAttributesToString(), |
305
|
|
|
'attributes_array' => $this->getHtmlAttributes(), |
306
|
|
|
] + parent::toArray() + [ |
307
|
|
|
'options' => $options, |
308
|
|
|
'limit' => $this->getLimit(), |
309
|
|
|
'nullable' => $this->isNullable(), |
310
|
|
|
]; |
311
|
|
|
|
312
|
|
|
return $return; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param mixed $value |
317
|
|
|
* |
318
|
|
|
* @return mixed |
319
|
|
|
*/ |
320
|
|
|
public function prepareValue($value) |
321
|
|
|
{ |
322
|
|
|
if ($this->isNullable() && $value == '') { |
323
|
|
|
return; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return parent::prepareValue($value); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|