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 array |
22
|
|
|
*/ |
23
|
|
|
protected $options = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $optionList = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $exclude = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $sortable = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Text constructor. |
42
|
|
|
* |
43
|
|
|
* @param $name |
44
|
|
|
* @param $label |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function __construct($name, $label = null, $options = []) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
parent::__construct($name, $label); |
49
|
|
|
|
50
|
|
|
if (is_array($options)) { |
51
|
|
|
$this->setOptions($options); |
52
|
|
|
} elseif (($options instanceof Model) || is_string($options)) { |
53
|
|
|
$this->setModelForOptions($options); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool $sortable |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setSortable($sortable) |
63
|
|
|
{ |
64
|
|
|
$this->sortable = (bool) $sortable; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function isSortable() |
73
|
|
|
{ |
74
|
|
|
return $this->sortable; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function getOptions() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
83
|
|
|
$this->setOptions( |
84
|
|
|
$this->loadOptions() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$options = array_except($this->options, $this->exclude); |
89
|
|
|
if ($this->isSortable()) { |
90
|
|
|
asort($options); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $options; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function mutateOptions() |
100
|
|
|
{ |
101
|
|
|
$options = []; |
102
|
|
|
|
103
|
|
|
$this->optionList = $this->getOptions(); |
104
|
|
|
|
105
|
|
|
foreach ($this->optionList as $key => $value) { |
106
|
|
|
$options[] = ['value' => $key, 'text' => $value]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $options; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $key |
114
|
|
|
* @return mixed|null |
115
|
|
|
*/ |
116
|
|
|
public function getOptionName($value) |
117
|
|
|
{ |
118
|
|
|
if (isset($value)) { |
119
|
|
|
if (isset($this->optionList[$value])) { |
120
|
|
|
return $this->optionList[$value]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setOptions(array $options) |
133
|
|
|
{ |
134
|
|
|
$this->options = $options; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $values |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setEnum(array $values) |
145
|
|
|
{ |
146
|
|
|
return $this->setOptions(array_combine($values, $values)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function toArray() |
153
|
|
|
{ |
154
|
|
|
return parent::toArray() + [ |
155
|
|
|
'options' => $this->mutateOptions(), |
156
|
|
|
'optionName' => $this->getOptionName($this->getModelValue()), |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Request $request |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
public function save(Request $request) |
166
|
|
|
{ |
167
|
|
|
$form = new FormDefault([ |
168
|
|
|
new \SleepingOwl\Admin\Form\Element\Select( |
169
|
|
|
$this->getName() |
170
|
|
|
), |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
$model = $this->getModel(); |
174
|
|
|
|
175
|
|
|
$request->offsetSet($this->getName(), $request->input('value', null)); |
176
|
|
|
|
177
|
|
|
$form->setModelClass(get_class($model)); |
178
|
|
|
$form->initialize(); |
179
|
|
|
$form->setId($model->getKey()); |
180
|
|
|
|
181
|
|
|
$form->saveForm($request); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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.