|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormInterface; |
|
7
|
|
|
use SleepingOwl\Admin\Http\Controllers\FormElementController; |
|
8
|
|
|
use SleepingOwl\Admin\Exceptions\Form\Element\SelectException; |
|
9
|
|
|
|
|
10
|
|
|
trait SelectAjaxFunctions |
|
11
|
|
|
{ |
|
12
|
|
|
protected $search_url = null; |
|
13
|
|
|
protected $search = null; |
|
14
|
|
|
protected $min_symbols = 3; |
|
15
|
|
|
protected $default_query_preparer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $params; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $dataDepends = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Closure|null|mixed |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $modelForOptionsCallback = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string|\Closure|mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $custom_name = 'custom_name'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string|\Closure|mixed $custom_name |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setCustomName($custom_name) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->custom_name = $custom_name; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string|\Closure|mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getCustomName() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->custom_name; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return null|string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSearch() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->search) { |
|
62
|
|
|
return $this->search; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->getDisplay(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $search |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setSearch($search) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->search = $search; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get min symbols to search. |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getMinSymbols() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->min_symbols; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set min symbols to search. |
|
90
|
|
|
* @param $symbols |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setMinSymbols($symbols) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->min_symbols = $symbols; |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function mutateOptions() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getOptions(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getOptions() |
|
112
|
|
|
{ |
|
113
|
|
|
// get model, model configuration interface, model logic |
|
114
|
|
|
$model = $this->getModel(); |
|
|
|
|
|
|
115
|
|
|
$section = \AdminSection::getModel($this->getModel()); |
|
|
|
|
|
|
116
|
|
|
$payload = $section->getPayload(); |
|
117
|
|
|
$form_element_controller = new FormElementController(); |
|
118
|
|
|
$form = $form_element_controller->getModelLogicPayload($section, $model->id, $payload); |
|
119
|
|
|
|
|
120
|
|
|
if ($form instanceof FormInterface) { |
|
121
|
|
|
// if defined: get values of the depends form fields |
|
122
|
|
|
$depends = json_decode($this->getDataDepends(), true); |
|
123
|
|
|
if (is_array($depends) && count($depends)) { |
|
124
|
|
|
$data_depends = []; |
|
125
|
|
|
foreach ($depends as $depend) { |
|
126
|
|
|
$temp_element = $form->getElement($depend); |
|
127
|
|
|
$depend_value = null; |
|
128
|
|
|
if (mb_strpos($depend, '.')) { |
|
129
|
|
|
$parts = explode('.', $depend); |
|
130
|
|
|
$fieldName = array_pop($parts); |
|
131
|
|
|
$relationName = implode('.', $parts); |
|
132
|
|
|
if (! $model->relationLoaded($relationName)) { |
|
133
|
|
|
$model->load($relationName); |
|
134
|
|
|
} |
|
135
|
|
|
$temp = $model; |
|
136
|
|
|
$temp_fail = false; |
|
137
|
|
|
foreach ($parts as $part) { |
|
138
|
|
|
$temp = $temp->{$part}; |
|
139
|
|
|
if (! $temp) { |
|
140
|
|
|
$temp_fail = true; |
|
141
|
|
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
if (! $temp_fail) { |
|
145
|
|
|
$depend_value = $temp->{$fieldName}; |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
$depend_value = $model->{$depend}; |
|
149
|
|
|
} |
|
150
|
|
|
if (! $depend_value) { |
|
151
|
|
|
$depend_value = $temp_element->getDefaultValue(); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
$data_depends[$depend] = $depend_value; |
|
154
|
|
|
} |
|
155
|
|
|
$this->setAjaxParameters($data_depends); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// if defined: get model for options via callback |
|
160
|
|
|
if (is_callable($callback = $callback = $this->getModelForOptionsCallback())) { |
|
|
|
|
|
|
161
|
|
|
$result = $callback($this); |
|
162
|
|
|
if ($result) { |
|
163
|
|
|
$this->setModelForOptions($result); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
|
|
|
|
|
|
168
|
|
|
$this->setOptions( |
|
|
|
|
|
|
169
|
|
|
$this->loadOptions() |
|
|
|
|
|
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$options = Arr::except($this->options, $this->exclude); |
|
174
|
|
|
if ($this->isSortable()) { |
|
|
|
|
|
|
175
|
|
|
asort($options, $this->getSortableFlags()); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $options; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $key |
|
183
|
|
|
* |
|
184
|
|
|
* @return bool |
|
185
|
|
|
*/ |
|
186
|
|
|
public function hasDependKey($key) |
|
187
|
|
|
{ |
|
188
|
|
|
return Arr::has($this->params, $key); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $key |
|
193
|
|
|
* |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getDependValue($key) |
|
197
|
|
|
{ |
|
198
|
|
|
return Arr::get($this->params, $key, $this->getModel()->getAttribute($key)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return string Json |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getDataDepends() |
|
205
|
|
|
{ |
|
206
|
|
|
return json_encode($this->dataDepends); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getDataDependsArray() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->dataDepends; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param array|string $depends |
|
219
|
|
|
* |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setDataDepends($depends) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->dataDepends = is_array($depends) ? $depends : func_get_args(); |
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param array $params |
|
231
|
|
|
* |
|
232
|
|
|
* @return $this |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setAjaxParameters(array $params) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->params = $params; |
|
237
|
|
|
|
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return \Closure |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getModelForOptionsCallback() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->modelForOptionsCallback; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param \Closure|null|mixed $modelForOptionsCallback |
|
251
|
|
|
* |
|
252
|
|
|
* @return $this |
|
253
|
|
|
* @throws SelectException |
|
254
|
|
|
*/ |
|
255
|
|
|
public function setModelForOptionsCallback($modelForOptionsCallback) |
|
256
|
|
|
{ |
|
257
|
|
|
if (! is_callable($modelForOptionsCallback) && ! is_null($modelForOptionsCallback)) { |
|
258
|
|
|
throw new SelectException('Option for setModelForOptionsCallback must be Closure or null'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$this->modelForOptionsCallback = $modelForOptionsCallback; |
|
262
|
|
|
|
|
263
|
|
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|