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