1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use AdminSection; |
|
|
|
|
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
9
|
|
|
|
10
|
|
|
class DependentSelect extends Select implements WithRoutesInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
protected $defaultValue = 0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $language; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $initializable = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Router $router |
29
|
|
|
*/ |
30
|
|
|
public static function registerRoutes(Router $router) |
31
|
|
|
{ |
32
|
|
|
$routeName = 'admin.form.element.dependent-select'; |
33
|
|
|
|
34
|
|
|
if (! $router->has($routeName)) { |
35
|
|
|
$router->post('{adminModel}/dependent-select/{field}/{id?}', [ |
36
|
|
|
'as' => $routeName, |
37
|
|
|
'uses' => '\SleepingOwl\Admin\Http\Controllers\FormElementController@dependentSelect', |
|
|
|
|
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $dataUrl = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $dataDepends = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $params; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $view = 'form.element.dependentselect'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* DependentSelect constructor. |
64
|
|
|
* @param $path |
65
|
|
|
* @param null $label |
|
|
|
|
66
|
|
|
* @param array $depends |
67
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
68
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
69
|
|
|
*/ |
70
|
|
|
public function __construct($path, $label = null, array $depends = []) |
71
|
|
|
{ |
72
|
|
|
parent::__construct($path, $label, []); |
73
|
|
|
|
74
|
|
|
$this->setLanguage(config('app.locale')); |
75
|
|
|
$this->setDataDepends($depends); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
|
|
public function getLanguage() |
82
|
|
|
{ |
83
|
|
|
return $this->language; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $language |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setLanguage($language) |
91
|
|
|
{ |
92
|
|
|
$this->language = $language; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function isInitializable() |
101
|
|
|
{ |
102
|
|
|
return $this->initializable; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param bool $initializable |
107
|
|
|
*/ |
108
|
|
|
public function setInitializable($initializable) |
109
|
|
|
{ |
110
|
|
|
$this->initializable = $initializable; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $key |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function hasDependKey($key) |
121
|
|
|
{ |
122
|
|
|
return Arr::has($this->params, $key); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $key |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function getDependValue($key) |
131
|
|
|
{ |
132
|
|
|
return Arr::get($this->params, $key, $this->getModel()->getAttribute($key)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getDataUrl() |
139
|
|
|
{ |
140
|
|
|
return $this->dataUrl ?: route('admin.form.element.dependent-select', [ |
141
|
|
|
'adminModel' => AdminSection::getModel($this->model)->getAlias(), |
142
|
|
|
'field' => $this->getName(), |
143
|
|
|
'id' => $this->model->getKey(), |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $dataUrl |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function setDataUrl($dataUrl) |
153
|
|
|
{ |
154
|
|
|
$this->dataUrl = $dataUrl; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string Json |
161
|
|
|
*/ |
162
|
|
|
public function getDataDepends() |
163
|
|
|
{ |
164
|
|
|
return json_encode($this->dataDepends); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param array|string $depends |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
public function setDataDepends($depends) |
173
|
|
|
{ |
174
|
|
|
$this->dataDepends = is_array($depends) ? $depends : func_get_args(); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $params |
181
|
|
|
* |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function setAjaxParameters(array $params) |
185
|
|
|
{ |
186
|
|
|
$this->params = $params; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function toArray() |
195
|
|
|
{ |
196
|
|
|
$this->setHtmlAttributes([ |
197
|
|
|
'id' => $this->getId(), |
198
|
|
|
'data-select-type' => 'single', |
199
|
|
|
'data-url' => $this->getDataUrl(), |
200
|
|
|
'data-depends' => $this->getDataDepends(), |
201
|
|
|
'data-language' => $this->getLanguage(), |
202
|
|
|
'data-initialize' => $this->isInitializable() ? 'true' : 'false', |
203
|
|
|
'class' => 'form-control input-select input-select-dependent', |
204
|
|
|
]); |
205
|
|
|
|
206
|
|
|
if ($this->isReadonly()) { |
207
|
|
|
$this->setHtmlAttribute('disabled', 'disabled'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return [ |
211
|
|
|
'id' => $this->getId(), |
212
|
|
|
'name' => $this->getName(), |
213
|
|
|
'path' => $this->getPath(), |
214
|
|
|
'label' => $this->getLabel(), |
215
|
|
|
'readonly' => $this->isReadonly(), |
216
|
|
|
'visibled' => $this->isVisible(), |
217
|
|
|
'options' => $this->getOptions(), |
218
|
|
|
'value' => $this->getValueFromModel(), |
219
|
|
|
'helpText' => $this->getHelpText(), |
220
|
|
|
'required' => in_array('required', $this->validationRules), |
221
|
|
|
'attributes' => $this->getHtmlAttributes(), |
222
|
|
|
]; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths