1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Editable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use SleepingOwl\Admin\Form\FormDefault; |
8
|
|
|
use SleepingOwl\Admin\Traits\SelectOptionsFromModel; |
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
10
|
|
|
|
11
|
|
|
class Select extends EditableColumn implements ColumnEditableInterface |
12
|
|
|
{ |
13
|
|
|
use SelectOptionsFromModel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $view = 'column.editable.select'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var null |
22
|
|
|
*/ |
23
|
|
|
protected $relationKey = null; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $optionList = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $exclude = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $sortable = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null |
46
|
|
|
*/ |
47
|
|
|
protected $defaultValue = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Select constructor. |
51
|
|
|
* @param $name |
52
|
|
|
* @param null $label |
|
|
|
|
53
|
|
|
* @param array $options |
54
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
55
|
|
|
*/ |
56
|
|
|
public function __construct($name, $label = null, $options = []) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($name, $label); |
59
|
|
|
|
60
|
|
|
if (is_array($options)) { |
|
|
|
|
61
|
|
|
$this->setOptions($options); |
62
|
|
|
} elseif (($options instanceof Model) || is_string($options)) { |
63
|
|
|
$this->setModelForOptions($options); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $relationKey |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setRelationKey($relationKey) |
72
|
|
|
{ |
73
|
|
|
$this->relationKey = $relationKey; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return null |
80
|
|
|
*/ |
81
|
|
|
public function getRelationKey() |
82
|
|
|
{ |
83
|
|
|
return $this->relationKey; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $defaultValue |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setDefaultValue($defaultValue) |
91
|
|
|
{ |
92
|
|
|
$this->defaultValue = $defaultValue; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return null |
99
|
|
|
*/ |
100
|
|
|
public function getDefaultValue() |
101
|
|
|
{ |
102
|
|
|
return $this->defaultValue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param bool $sortable |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setSortable($sortable) |
111
|
|
|
{ |
112
|
|
|
$this->sortable = (bool) $sortable; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function isSortable() |
121
|
|
|
{ |
122
|
|
|
return $this->sortable; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getOptions() |
129
|
|
|
{ |
130
|
|
|
if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
131
|
|
|
$this->setOptions( |
132
|
|
|
$this->loadOptions() |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$options = array_except($this->options, $this->exclude); |
137
|
|
|
if ($this->isSortable()) { |
138
|
|
|
asort($options); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $options; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function mutateOptions() |
148
|
|
|
{ |
149
|
|
|
$options = []; |
150
|
|
|
|
151
|
|
|
$this->optionList = $this->getOptions(); |
152
|
|
|
|
153
|
|
|
foreach ($this->optionList as $key => $value) { |
154
|
|
|
$options[] = ['value' => $key, 'text' => $value]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $options; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $key |
162
|
|
|
* @return mixed|null |
163
|
|
|
*/ |
164
|
|
|
public function getOptionName($value) |
165
|
|
|
{ |
166
|
|
|
if (isset($value)) { |
167
|
|
|
if (isset($this->optionList[$value])) { |
168
|
|
|
return $this->optionList[$value]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array |
177
|
|
|
* |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
|
|
public function setOptions(array $options) |
181
|
|
|
{ |
182
|
|
|
$this->options = $options; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param array $values |
189
|
|
|
* |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
|
|
public function setEnum(array $values) |
193
|
|
|
{ |
194
|
|
|
return $this->setOptions(array_combine($values, $values)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function toArray() |
201
|
|
|
{ |
202
|
|
|
return parent::toArray() + [ |
203
|
|
|
'options' => $this->mutateOptions(), |
204
|
|
|
'optionName' => $this->getOptionName($this->getModelValue()), |
205
|
|
|
]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param Request $request |
210
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\FormException |
211
|
|
|
*/ |
212
|
|
|
public function save(Request $request) |
213
|
|
|
{ |
214
|
|
|
$model = $this->getModel(); |
215
|
|
|
|
216
|
|
|
if (strpos($this->getName(), '.') !== false) { |
217
|
|
|
if ($this->getRelationKey()) { |
|
|
|
|
218
|
|
|
$this->setName($this->getRelationKey()); |
|
|
|
|
219
|
|
|
} else { |
220
|
|
|
//@TODO Make Relation Resolver |
221
|
|
|
$relationName = explode('.', $this->getName()); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$form = new FormDefault([ |
226
|
|
|
new \SleepingOwl\Admin\Form\Element\Select( |
227
|
|
|
$this->getName() |
228
|
|
|
), |
229
|
|
|
]); |
230
|
|
|
|
231
|
|
|
$request->offsetSet($this->getName(), $request->input('value', $this->getDefaultValue())); |
|
|
|
|
232
|
|
|
|
233
|
|
|
$form->setModelClass(get_class($model)); |
234
|
|
|
$form->initialize(); |
235
|
|
|
$form->setId($model->getKey()); |
236
|
|
|
|
237
|
|
|
$form->saveForm($request); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|