1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Routing\Controller; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use SleepingOwl\Admin\Form\Element\SelectAjax; |
10
|
|
|
use SleepingOwl\Admin\Form\Element\DependentSelect; |
11
|
|
|
use SleepingOwl\Admin\Form\Element\MultiSelectAjax; |
12
|
|
|
use SleepingOwl\Admin\Form\Element\MultiDependentSelect; |
13
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
14
|
|
|
|
15
|
|
|
class FormElementController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param ModelConfigurationInterface $model |
19
|
|
|
* @param null $id |
|
|
|
|
20
|
|
|
* @return JsonResponse|mixed |
21
|
|
|
*/ |
22
|
|
|
public function getModelLogic(ModelConfigurationInterface $model, $id = null) |
23
|
|
|
{ |
24
|
|
|
if (! is_null($id)) { |
|
|
|
|
25
|
|
|
$item = $model->getRepository()->find($id); |
26
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
27
|
|
|
return new JsonResponse([ |
28
|
|
|
'message' => trans('lang.message.access_denied'), |
29
|
|
|
], 403); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $model->fireEdit($id); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (! $model->isCreatable()) { |
36
|
|
|
return new JsonResponse([ |
37
|
|
|
'message' => trans('lang.message.access_denied'), |
38
|
|
|
], 403); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $model->fireCreate(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ModelConfigurationInterface $model |
46
|
|
|
* @param null $id |
|
|
|
|
47
|
|
|
* @param mixed $payload |
48
|
|
|
* @return JsonResponse|mixed |
49
|
|
|
*/ |
50
|
|
|
public function getModelLogicPayload(ModelConfigurationInterface $model, $id = null, $payload = []) |
51
|
|
|
{ |
52
|
|
|
if (! is_null($id)) { |
|
|
|
|
53
|
|
|
$item = $model->getRepository()->find($id); |
54
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
55
|
|
|
return new JsonResponse([ |
56
|
|
|
'message' => trans('lang.message.access_denied'), |
57
|
|
|
], 403); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $model->fireEdit($id, $payload); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! $model->isCreatable()) { |
64
|
|
|
return new JsonResponse([ |
65
|
|
|
'message' => trans('lang.message.access_denied'), |
66
|
|
|
], 403); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $model->fireCreate($payload); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Request $request |
74
|
|
|
* @param ModelConfigurationInterface $model |
75
|
|
|
* @param string $field |
76
|
|
|
* @param int|null $id |
77
|
|
|
* |
78
|
|
|
* @return JsonResponse |
79
|
|
|
*/ |
80
|
|
|
public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
81
|
|
|
{ |
82
|
|
|
$form = $this->getModelLogic($model, $id); |
|
|
|
|
83
|
|
|
|
84
|
|
|
if ($form instanceof JsonResponse) { |
85
|
|
|
return $form; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// because field name in MultiDependentSelect ends with '[]' |
89
|
|
|
$fieldPrepared = str_replace('[]', '', $field); |
90
|
|
|
|
91
|
|
|
/** @var DependentSelect|MultiDependentSelect $element */ |
92
|
|
|
$element = $form->getElement($fieldPrepared); |
93
|
|
|
|
94
|
|
|
if (is_null($element)) { |
95
|
|
|
return new JsonResponse([ |
96
|
|
|
'message' => 'Element not found', |
97
|
|
|
], 404); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$element->setAjaxParameters( |
101
|
|
|
$request->input('depdrop_all_params', []) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$options = $element->getOptions(); |
105
|
|
|
|
106
|
|
|
if ($element->isNullable()) { |
107
|
|
|
$options = [null => trans('sleeping_owl::lang.select.nothing')] + $options; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return new JsonResponse([ |
111
|
|
|
'output' => collect($options)->map(function ($value, $key) { |
112
|
|
|
return ['id' => $key, 'name' => $value]; |
113
|
|
|
}), |
114
|
|
|
'selected' => $element->getValueFromModel(), |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Request $request |
120
|
|
|
* @param ModelConfigurationInterface $model |
121
|
|
|
* @param string $field |
122
|
|
|
* @param int|null $id |
123
|
|
|
* |
124
|
|
|
* @return JsonResponse |
125
|
|
|
*/ |
126
|
|
|
public function multiselectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
127
|
|
|
{ |
128
|
|
|
return $this->selectSearch($request, $model, $field, $id); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Request $request |
133
|
|
|
* @param ModelConfigurationInterface $model |
134
|
|
|
* @param string $field |
135
|
|
|
* @param int|null $id |
136
|
|
|
* |
137
|
|
|
* @return JsonResponse |
138
|
|
|
*/ |
139
|
|
|
public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
140
|
|
|
{ |
141
|
|
|
$form = $this->getModelLogic($model, $id); |
|
|
|
|
142
|
|
|
|
143
|
|
|
if ($form instanceof JsonResponse) { |
144
|
|
|
return $form; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// because field name in MultiSelectAjax ends with '[]' |
148
|
|
|
$fieldPrepared = str_replace('[]', '', $field); |
149
|
|
|
// process fields with relations: user[role] |
150
|
|
|
$fieldPrepared = strtr($fieldPrepared, ['[' => '.', ']' => '']); |
151
|
|
|
|
152
|
|
|
/** @var SelectAjax|MultiSelectAjax $element */ |
153
|
|
|
$element = $form->getElement($fieldPrepared); |
154
|
|
|
|
155
|
|
|
if (is_null($element)) { |
156
|
|
|
return new JsonResponse([ |
157
|
|
|
'message' => 'Element not found', |
158
|
|
|
], 404); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$params = $request->input('depdrop_all_params', []); |
162
|
|
|
$temp = []; |
163
|
|
|
foreach ($params as $key => $val) { |
164
|
|
|
$key = strtr($key, ['[' => '.', ']' => '']); |
165
|
|
|
$key = strtr($key, ['__' => '.']); |
166
|
|
|
$temp[$key] = $val; |
167
|
|
|
} |
168
|
|
|
$params = $temp; |
169
|
|
|
|
170
|
|
|
$element->setAjaxParameters($params); |
171
|
|
|
|
172
|
|
|
if (is_callable($closure = $element->getModelForOptionsCallback())) { |
173
|
|
|
$model_classname = $closure($element); |
174
|
|
|
} else { |
175
|
|
|
$model_classname = $element->getModelForOptions(); |
176
|
|
|
} |
177
|
|
|
if (is_object($model_classname)) { |
178
|
|
|
$model_classname = get_class($model_classname); |
179
|
|
|
} |
180
|
|
|
if ($model_classname && class_exists($model_classname)) { |
181
|
|
|
$model = new $model_classname; |
182
|
|
|
|
183
|
|
|
$search = $element->getSearch(); |
184
|
|
|
if (is_callable($search)) { |
185
|
|
|
$search = $search($element); |
186
|
|
|
} |
187
|
|
|
$display = $element->getDisplay(); |
188
|
|
|
$custom_name = $element->getCustomName(); |
189
|
|
|
$exclude = $element->getExclude(); |
190
|
|
|
|
191
|
|
|
if ($request->q && is_object($model)) { |
192
|
|
|
$query = $model; |
193
|
|
|
|
194
|
|
|
// search logic |
195
|
|
|
$model_table = $model->getTable(); |
196
|
|
|
$q = $request->q; |
197
|
|
|
if (is_array($search)) { |
198
|
|
|
$query = $query->where(function ($query) use ($model_table, $search, $q) { |
199
|
|
|
foreach ($search as $key => $val) { |
200
|
|
|
if (is_numeric($key)) { |
201
|
|
|
$srch = $val; |
202
|
|
|
$value = '%'.$q.'%'; |
203
|
|
|
} else { |
204
|
|
|
$srch = $key; |
205
|
|
|
switch ($val) { |
206
|
|
|
case 'equal': |
207
|
|
|
$value = $q; |
208
|
|
|
break; |
209
|
|
|
case 'begins_with': |
210
|
|
|
$value = $q.'%'; |
211
|
|
|
break; |
212
|
|
|
case 'ends_with': |
213
|
|
|
$value = '%'.$q; |
214
|
|
|
break; |
215
|
|
|
case 'contains': |
216
|
|
|
default: |
217
|
|
|
$value = '%'.$q.'%'; |
218
|
|
|
break; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
$query = $query->orWhere($model_table.'.'.$srch, 'LIKE', $value); |
222
|
|
|
} |
223
|
|
|
}); |
224
|
|
|
} else { |
225
|
|
|
$query = $query->where($model_table.'.'.$search, 'LIKE', "%{$request->q}%"); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// exclude result elements by id |
229
|
|
|
if (count($exclude)) { |
230
|
|
|
$query = $query->whereNotIn($model_table.'.'.$model->getKeyName(), $exclude); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// call the pre load options query preparer if has be set |
234
|
|
|
if (is_callable($preparer = $element->getLoadOptionsQueryPreparer())) { |
235
|
|
|
$query = $preparer($element, $query); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return new JsonResponse( |
239
|
|
|
$query |
240
|
|
|
->get() |
241
|
|
|
->map(function (Model $item) use ($display, $element, $custom_name) { |
242
|
|
|
if (is_string($display)) { |
|
|
|
|
243
|
|
|
$value = $item->{$display}; |
244
|
|
|
} elseif (is_callable($display)) { |
245
|
|
|
$value = $display($item, $element); |
246
|
|
|
} else { |
247
|
|
|
$value = null; |
248
|
|
|
} |
249
|
|
|
if (is_string($custom_name)) { |
250
|
|
|
$custom_name_value = $item->{$custom_name}; |
251
|
|
|
} elseif (is_callable($custom_name)) { |
252
|
|
|
$custom_name_value = $custom_name($item, $element); |
253
|
|
|
} else { |
254
|
|
|
$custom_name_value = null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return [ |
258
|
|
|
'tag_name' => $value, |
259
|
|
|
'id' => $item->id, |
260
|
|
|
'custom_name' => $custom_name_value, |
261
|
|
|
]; |
262
|
|
|
}) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return new JsonResponse([]); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|