1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
9
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
10
|
|
|
|
11
|
|
|
class DependentSelect extends Select implements WithRoutesInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Router $router |
16
|
|
|
*/ |
17
|
|
|
public static function registerRoutes(Router $router) |
18
|
|
|
{ |
19
|
|
|
$routeName = 'admin.form.element.dependent-select'; |
20
|
|
|
|
21
|
|
|
if (! $router->has($routeName)) { |
22
|
|
|
$router->post('{adminModel}/dependent-select/{field}/{id?}', ['as' => $routeName, function ( |
23
|
|
|
Request $request, |
24
|
|
|
ModelConfigurationInterface $model, |
25
|
|
|
$field, |
26
|
|
|
$id = null |
27
|
|
|
) { |
28
|
|
|
|
29
|
|
View Code Duplication |
if (! is_null($id)) { |
|
|
|
|
30
|
|
|
$item = $model->getRepository()->find($id); |
31
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
32
|
|
|
return new JsonResponse([ |
33
|
|
|
'message' => trans('lang.message.access_denied'), |
34
|
|
|
], 403); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$form = $model->fireEdit($id); |
38
|
|
|
} else { |
39
|
|
|
if (! $model->isCreatable()) { |
40
|
|
|
return new JsonResponse([ |
41
|
|
|
'message' => trans('lang.message.access_denied'), |
42
|
|
|
], 403); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$form = $model->fireCreate(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (is_null($element = $form->getElement($field))) { |
49
|
|
|
return new JsonResponse([ |
50
|
|
|
'message' => 'Element not found', |
51
|
|
|
], 404); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$element->setAjaxParameters( |
55
|
|
|
$request->input('depdrop_all_params', []) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$options = $element->getOptions(); |
59
|
|
|
|
60
|
|
|
if ($element->isNullable()) { |
61
|
|
|
$options = [null => trans('sleeping_owl::lang.select.nothing')] + $options; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$options = array_except($options, $element->exclude); |
65
|
|
|
|
66
|
|
|
return new JsonResponse([ |
67
|
|
|
'output' => collect($options)->map(function($value, $key) { |
68
|
|
|
return ['id' => $key, 'name' => $value]; |
69
|
|
|
}), |
70
|
|
|
'selected' => $element->getValue() |
71
|
|
|
]); |
72
|
|
|
}]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $dataUrl = ''; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
protected $dataDepends = []; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $params; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* DependentSelect constructor. |
93
|
|
|
* |
94
|
|
|
* @param string $path |
95
|
|
|
* @param null $label |
96
|
|
|
* @param array $depends |
97
|
|
|
*/ |
98
|
|
|
public function __construct($path, $label = null, array $depends = []) |
99
|
|
|
{ |
100
|
|
|
parent::__construct($path, $label, []); |
101
|
|
|
|
102
|
|
|
$this->setDataDepends($depends); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $key |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function hasDependKey($key) |
111
|
|
|
{ |
112
|
|
|
return array_has($this->params, $key); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $key |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function getDependValue($key) |
121
|
|
|
{ |
122
|
|
|
return array_get($this->params, $key, $this->getModel()->getAttribute($key)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getDataUrl() |
130
|
|
|
{ |
131
|
|
|
return $this->dataUrl ?: route('admin.form.element.dependent-select', [ |
132
|
|
|
'adminModel' => \AdminSection::getModel($this->model)->getAlias(), |
133
|
|
|
'field' => $this->getName(), |
134
|
|
|
'id' => $this->model->getKey() |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $dataUrl |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setDataUrl($dataUrl) |
144
|
|
|
{ |
145
|
|
|
$this->dataUrl = $dataUrl; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string Json |
152
|
|
|
*/ |
153
|
|
|
public function getDataDepends() |
154
|
|
|
{ |
155
|
|
|
return json_encode($this->dataDepends); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array|string $depends |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function setDataDepends($depends) |
165
|
|
|
{ |
166
|
|
|
$this->dataDepends = is_array($depends) ? $depends : func_get_args(); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function toArray() |
175
|
|
|
{ |
176
|
|
|
$attributes = [ |
177
|
|
|
'id' => $this->getName(), |
178
|
|
|
'size' => 2, |
179
|
|
|
'data-select-type' => 'single', |
180
|
|
|
'data-url' => $this->getDataUrl(), |
181
|
|
|
'data-depends' => $this->getDataDepends(), |
182
|
|
|
'class' => 'form-control input-select input-select-dependent', |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
if ($this->isReadonly()) { |
186
|
|
|
$attributes['disabled'] = 'disabled'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return [ |
190
|
|
|
'id' => $this->getName(), |
191
|
|
|
'name' => $this->getName(), |
192
|
|
|
'path' => $this->getPath(), |
193
|
|
|
'label' => $this->getLabel(), |
194
|
|
|
'readonly' => $this->isReadonly(), |
195
|
|
|
'options' => $this->getOptions(), |
196
|
|
|
'value' => $this->getValue(), |
197
|
|
|
'helpText' => $this->getHelpText(), |
198
|
|
|
'required' => in_array('required', $this->validationRules), |
199
|
|
|
'attributes' => $attributes, |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param array $params |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
protected function setAjaxParameters(array $params) |
209
|
|
|
{ |
210
|
|
|
$this->params = $params; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
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.