|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Encore\Admin\Exception\Handler; |
|
7
|
|
|
use Encore\Admin\Form\Builder; |
|
8
|
|
|
use Encore\Admin\Form\Field; |
|
9
|
|
|
use Encore\Admin\Form\Field\File; |
|
10
|
|
|
use Encore\Admin\Form\Row; |
|
11
|
|
|
use Encore\Admin\Form\Tab; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Illuminate\Support\Arr; |
|
16
|
|
|
use Illuminate\Support\Facades\DB; |
|
17
|
|
|
use Illuminate\Support\Facades\Input; |
|
18
|
|
|
use Illuminate\Support\MessageBag; |
|
19
|
|
|
use Illuminate\Support\Str; |
|
20
|
|
|
use Illuminate\Validation\Validator; |
|
21
|
|
|
use Spatie\EloquentSortable\Sortable; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Form. |
|
26
|
|
|
* |
|
27
|
|
|
* @method Field\Text text($column, $label = '') |
|
28
|
|
|
* @method Field\Checkbox checkbox($column, $label = '') |
|
29
|
|
|
* @method Field\Radio radio($column, $label = '') |
|
30
|
|
|
* @method Field\Select select($column, $label = '') |
|
31
|
|
|
* @method Field\MultipleSelect multipleSelect($column, $label = '') |
|
32
|
|
|
* @method Field\Textarea textarea($column, $label = '') |
|
33
|
|
|
* @method Field\Hidden hidden($column, $label = '') |
|
34
|
|
|
* @method Field\Id id($column, $label = '') |
|
35
|
|
|
* @method Field\Ip ip($column, $label = '') |
|
36
|
|
|
* @method Field\Url url($column, $label = '') |
|
37
|
|
|
* @method Field\Color color($column, $label = '') |
|
38
|
|
|
* @method Field\Email email($column, $label = '') |
|
39
|
|
|
* @method Field\Mobile mobile($column, $label = '') |
|
40
|
|
|
* @method Field\Slider slider($column, $label = '') |
|
41
|
|
|
* @method Field\Map map($latitude, $longitude, $label = '') |
|
42
|
|
|
* @method Field\Editor editor($column, $label = '') |
|
43
|
|
|
* @method Field\File file($column, $label = '') |
|
44
|
|
|
* @method Field\Image image($column, $label = '') |
|
45
|
|
|
* @method Field\Date date($column, $label = '') |
|
46
|
|
|
* @method Field\Datetime datetime($column, $label = '') |
|
47
|
|
|
* @method Field\Time time($column, $label = '') |
|
48
|
|
|
* @method Field\Year year($column, $label = '') |
|
49
|
|
|
* @method Field\Month month($column, $label = '') |
|
50
|
|
|
* @method Field\DateRange dateRange($start, $end, $label = '') |
|
51
|
|
|
* @method Field\DateTimeRange datetimeRange($start, $end, $label = '') |
|
52
|
|
|
* @method Field\TimeRange timeRange($start, $end, $label = '') |
|
53
|
|
|
* @method Field\Number number($column, $label = '') |
|
54
|
|
|
* @method Field\Currency currency($column, $label = '') |
|
55
|
|
|
* @method Field\HasMany hasMany($relationName, $callback) |
|
56
|
|
|
* @method Field\SwitchField switch($column, $label = '') |
|
57
|
|
|
* @method Field\Display display($column, $label = '') |
|
58
|
|
|
* @method Field\Rate rate($column, $label = '') |
|
59
|
|
|
* @method Field\Divide divider() |
|
60
|
|
|
* @method Field\Password password($column, $label = '') |
|
61
|
|
|
* @method Field\Decimal decimal($column, $label = '') |
|
62
|
|
|
* @method Field\Html html($html, $label = '') |
|
63
|
|
|
* @method Field\Tags tags($column, $label = '') |
|
64
|
|
|
* @method Field\Icon icon($column, $label = '') |
|
65
|
|
|
* @method Field\Embeds embeds($column, $label = '') |
|
66
|
|
|
* @method Field\MultipleImage multipleImage($column, $label = '') |
|
67
|
|
|
* @method Field\MultipleFile multipleFile($column, $label = '') |
|
68
|
|
|
* @method Field\Captcha captcha($column, $label = '') |
|
69
|
|
|
* @method Field\Listbox listbox($column, $label = '') |
|
70
|
|
|
*/ |
|
71
|
|
|
class Form |
|
72
|
|
|
{ |
|
73
|
|
|
/** |
|
74
|
|
|
* Eloquent model of the form. |
|
75
|
|
|
* |
|
76
|
|
|
* @var Model |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $model; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var \Illuminate\Validation\Validator |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $validator; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var Builder |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $builder; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Submitted callback. |
|
92
|
|
|
* |
|
93
|
|
|
* @var Closure |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $submitted; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Saving callback. |
|
99
|
|
|
* |
|
100
|
|
|
* @var Closure |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $saving; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Saved callback. |
|
106
|
|
|
* |
|
107
|
|
|
* @var Closure |
|
108
|
|
|
*/ |
|
109
|
|
|
protected $saved; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Data for save to current model from input. |
|
113
|
|
|
* |
|
114
|
|
|
* @var array |
|
115
|
|
|
*/ |
|
116
|
|
|
protected $updates = []; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Data for save to model's relations from input. |
|
120
|
|
|
* |
|
121
|
|
|
* @var array |
|
122
|
|
|
*/ |
|
123
|
|
|
protected $relations = []; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Input data. |
|
127
|
|
|
* |
|
128
|
|
|
* @var array |
|
129
|
|
|
*/ |
|
130
|
|
|
protected $inputs = []; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Available fields. |
|
134
|
|
|
* |
|
135
|
|
|
* @var array |
|
136
|
|
|
*/ |
|
137
|
|
|
public static $availableFields = []; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Ignored saving fields. |
|
141
|
|
|
* |
|
142
|
|
|
* @var array |
|
143
|
|
|
*/ |
|
144
|
|
|
protected $ignored = []; |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Collected field assets. |
|
148
|
|
|
* |
|
149
|
|
|
* @var array |
|
150
|
|
|
*/ |
|
151
|
|
|
protected static $collectedAssets = []; |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @var Form\Tab |
|
155
|
|
|
*/ |
|
156
|
|
|
protected $tab = null; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Remove flag in `has many` form. |
|
160
|
|
|
*/ |
|
161
|
|
|
const REMOVE_FLAG_NAME = '_remove_'; |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Field rows in form. |
|
165
|
|
|
* |
|
166
|
|
|
* @var array |
|
167
|
|
|
*/ |
|
168
|
|
|
public $rows = []; |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Create a new form instance. |
|
172
|
|
|
* |
|
173
|
|
|
* @param $model |
|
174
|
|
|
* @param \Closure $callback |
|
175
|
|
|
*/ |
|
176
|
|
|
public function __construct($model, Closure $callback) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->model = $model; |
|
179
|
|
|
|
|
180
|
|
|
$this->builder = new Builder($this); |
|
181
|
|
|
|
|
182
|
|
|
$callback($this); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param Field $field |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
*/ |
|
190
|
|
|
public function pushField(Field $field) |
|
191
|
|
|
{ |
|
192
|
|
|
$field->setForm($this); |
|
193
|
|
|
|
|
194
|
|
|
$this->builder->fields()->push($field); |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return Model |
|
201
|
|
|
*/ |
|
202
|
|
|
public function model() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->model; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return Builder |
|
209
|
|
|
*/ |
|
210
|
|
|
public function builder() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->builder; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Generate a edit form. |
|
217
|
|
|
* |
|
218
|
|
|
* @param $id |
|
219
|
|
|
* |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
|
|
public function edit($id) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->builder->setMode(Builder::MODE_EDIT); |
|
225
|
|
|
$this->builder->setResourceId($id); |
|
226
|
|
|
|
|
227
|
|
|
$this->setFieldValue($id); |
|
228
|
|
|
|
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param $id |
|
234
|
|
|
* |
|
235
|
|
|
* @return $this |
|
236
|
|
|
*/ |
|
237
|
|
|
public function view($id) |
|
238
|
|
|
{ |
|
239
|
|
|
$this->builder->setMode(Builder::MODE_VIEW); |
|
240
|
|
|
$this->builder->setResourceId($id); |
|
241
|
|
|
|
|
242
|
|
|
$this->setFieldValue($id); |
|
243
|
|
|
|
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Use tab to split form. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $title |
|
251
|
|
|
* @param Closure $content |
|
252
|
|
|
* |
|
253
|
|
|
* @return $this |
|
254
|
|
|
*/ |
|
255
|
|
|
public function tab($title, Closure $content, $active = false) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->getTab()->append($title, $content, $active); |
|
258
|
|
|
|
|
259
|
|
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Get Tab instance. |
|
264
|
|
|
* |
|
265
|
|
|
* @return Tab |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getTab() |
|
268
|
|
|
{ |
|
269
|
|
|
if (is_null($this->tab)) { |
|
270
|
|
|
$this->tab = new Tab($this); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return $this->tab; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Destroy data entity and remove files. |
|
278
|
|
|
* |
|
279
|
|
|
* @param $id |
|
280
|
|
|
* |
|
281
|
|
|
* @return mixed |
|
282
|
|
|
*/ |
|
283
|
|
|
public function destroy($id) |
|
284
|
|
|
{ |
|
285
|
|
|
$ids = explode(',', $id); |
|
286
|
|
|
|
|
287
|
|
|
foreach ($ids as $id) { |
|
288
|
|
|
if (empty($id)) { |
|
289
|
|
|
continue; |
|
290
|
|
|
} |
|
291
|
|
|
$this->deleteFilesAndImages($id); |
|
292
|
|
|
$this->model->find($id)->delete(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
return true; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Remove files or images in record. |
|
300
|
|
|
* |
|
301
|
|
|
* @param $id |
|
302
|
|
|
*/ |
|
303
|
|
|
protected function deleteFilesAndImages($id) |
|
304
|
|
|
{ |
|
305
|
|
|
$data = $this->model->with($this->getRelations()) |
|
|
|
|
|
|
306
|
|
|
->findOrFail($id)->toArray(); |
|
307
|
|
|
|
|
308
|
|
|
$this->builder->fields()->filter(function ($field) { |
|
309
|
|
|
return $field instanceof Field\File; |
|
310
|
|
|
})->each(function (File $file) use ($data) { |
|
311
|
|
|
$file->setOriginal($data); |
|
312
|
|
|
|
|
313
|
|
|
$file->destroy(); |
|
314
|
|
|
}); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Store a new record. |
|
319
|
|
|
* |
|
320
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
|
321
|
|
|
*/ |
|
322
|
|
|
public function store() |
|
323
|
|
|
{ |
|
324
|
|
|
$data = Input::all(); |
|
325
|
|
|
|
|
326
|
|
|
// Handle validation errors. |
|
327
|
|
|
if ($validationMessages = $this->validationMessages($data)) { |
|
328
|
|
|
return back()->withInput()->withErrors($validationMessages); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
if (($response = $this->prepare($data)) instanceof Response) { |
|
332
|
|
|
return $response; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
View Code Duplication |
DB::transaction(function () { |
|
|
|
|
|
|
336
|
|
|
$inserts = $this->prepareInsert($this->updates); |
|
337
|
|
|
|
|
338
|
|
|
foreach ($inserts as $column => $value) { |
|
339
|
|
|
$this->model->setAttribute($column, $value); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
$this->model->save(); |
|
343
|
|
|
|
|
344
|
|
|
$this->updateRelation($this->relations); |
|
345
|
|
|
}); |
|
346
|
|
|
|
|
347
|
|
|
if (($response = $this->complete($this->saved)) instanceof Response) { |
|
348
|
|
|
return $response; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
|
352
|
|
|
return $response; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
return $this->redirectAfterStore(); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Get RedirectResponse after store. |
|
360
|
|
|
* |
|
361
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
362
|
|
|
*/ |
|
363
|
|
View Code Duplication |
protected function redirectAfterStore() |
|
|
|
|
|
|
364
|
|
|
{ |
|
365
|
|
|
admin_toastr(trans('admin.save_succeeded')); |
|
366
|
|
|
|
|
367
|
|
|
$url = Input::get(Builder::PREVIOUS_URL_KEY) ?: $this->resource(0); |
|
368
|
|
|
|
|
369
|
|
|
return redirect($url); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Get ajax response. |
|
374
|
|
|
* |
|
375
|
|
|
* @param string $message |
|
376
|
|
|
* |
|
377
|
|
|
* @return bool|\Illuminate\Http\JsonResponse |
|
378
|
|
|
*/ |
|
379
|
|
|
protected function ajaxResponse($message) |
|
380
|
|
|
{ |
|
381
|
|
|
$request = Request::capture(); |
|
382
|
|
|
|
|
383
|
|
|
// ajax but not pjax |
|
384
|
|
|
if ($request->ajax() && !$request->pjax()) { |
|
385
|
|
|
return response()->json([ |
|
386
|
|
|
'status' => true, |
|
387
|
|
|
'message' => $message, |
|
388
|
|
|
]); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
return false; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Prepare input data for insert or update. |
|
396
|
|
|
* |
|
397
|
|
|
* @param array $data |
|
398
|
|
|
* |
|
399
|
|
|
* @return mixed |
|
400
|
|
|
*/ |
|
401
|
|
|
protected function prepare($data = []) |
|
402
|
|
|
{ |
|
403
|
|
|
if (($response = $this->callSubmitted()) instanceof Response) { |
|
404
|
|
|
return $response; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
$this->inputs = $this->removeIgnoredFields($data); |
|
408
|
|
|
|
|
409
|
|
|
if (($response = $this->callSaving()) instanceof Response) { |
|
410
|
|
|
return $response; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
$this->relations = $this->getRelationInputs($this->inputs); |
|
414
|
|
|
|
|
415
|
|
|
$this->updates = array_except($this->inputs, array_keys($this->relations)); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Remove ignored fields from input. |
|
420
|
|
|
* |
|
421
|
|
|
* @param array $input |
|
422
|
|
|
* |
|
423
|
|
|
* @return array |
|
424
|
|
|
*/ |
|
425
|
|
|
protected function removeIgnoredFields($input) |
|
426
|
|
|
{ |
|
427
|
|
|
array_forget($input, $this->ignored); |
|
428
|
|
|
|
|
429
|
|
|
return $input; |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
/** |
|
433
|
|
|
* Get inputs for relations. |
|
434
|
|
|
* |
|
435
|
|
|
* @param array $inputs |
|
436
|
|
|
* |
|
437
|
|
|
* @return array |
|
438
|
|
|
*/ |
|
439
|
|
|
protected function getRelationInputs($inputs = []) |
|
440
|
|
|
{ |
|
441
|
|
|
$relations = []; |
|
442
|
|
|
|
|
443
|
|
|
foreach ($inputs as $column => $value) { |
|
444
|
|
|
if (method_exists($this->model, $column)) { |
|
445
|
|
|
$relation = call_user_func([$this->model, $column]); |
|
446
|
|
|
|
|
447
|
|
|
if ($relation instanceof Relation) { |
|
448
|
|
|
$relations[$column] = $value; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
return $relations; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Call submitted callback. |
|
458
|
|
|
* |
|
459
|
|
|
* @return mixed |
|
460
|
|
|
*/ |
|
461
|
|
|
protected function callSubmitted() |
|
462
|
|
|
{ |
|
463
|
|
|
if ($this->submitted instanceof Closure) { |
|
464
|
|
|
return call_user_func($this->submitted, $this); |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Call saving callback. |
|
470
|
|
|
* |
|
471
|
|
|
* @return mixed |
|
472
|
|
|
*/ |
|
473
|
|
|
protected function callSaving() |
|
474
|
|
|
{ |
|
475
|
|
|
if ($this->saving instanceof Closure) { |
|
476
|
|
|
return call_user_func($this->saving, $this); |
|
477
|
|
|
} |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* Callback after saving a Model. |
|
482
|
|
|
* |
|
483
|
|
|
* @param Closure|null $callback |
|
484
|
|
|
* |
|
485
|
|
|
* @return mixed|null |
|
486
|
|
|
*/ |
|
487
|
|
|
protected function complete(Closure $callback = null) |
|
488
|
|
|
{ |
|
489
|
|
|
if ($callback instanceof Closure) { |
|
490
|
|
|
return $callback($this); |
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* Handle update. |
|
496
|
|
|
* |
|
497
|
|
|
* @param int $id |
|
498
|
|
|
* |
|
499
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
500
|
|
|
*/ |
|
501
|
|
|
public function update($id) |
|
502
|
|
|
{ |
|
503
|
|
|
$data = Input::all(); |
|
504
|
|
|
|
|
505
|
|
|
$isEditable = $this->isEditable($data); |
|
506
|
|
|
|
|
507
|
|
|
$data = $this->handleEditable($data); |
|
508
|
|
|
|
|
509
|
|
|
$data = $this->handleFileDelete($data); |
|
510
|
|
|
|
|
511
|
|
|
if ($this->handleOrderable($id, $data)) { |
|
512
|
|
|
return response([ |
|
|
|
|
|
|
513
|
|
|
'status' => true, |
|
514
|
|
|
'message' => trans('admin.update_succeeded'), |
|
515
|
|
|
]); |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/* @var Model $this->model */ |
|
519
|
|
|
$this->model = $this->model->with($this->getRelations())->findOrFail($id); |
|
|
|
|
|
|
520
|
|
|
|
|
521
|
|
|
$this->setFieldOriginalValue(); |
|
522
|
|
|
|
|
523
|
|
|
// Handle validation errors. |
|
524
|
|
|
if ($validationMessages = $this->validationMessages($data)) { |
|
525
|
|
|
if (!$isEditable) { |
|
526
|
|
|
return back()->withInput()->withErrors($validationMessages); |
|
527
|
|
|
} else { |
|
528
|
|
|
return response(['errors' => array_dot($validationMessages->getMessages())], 422); |
|
|
|
|
|
|
529
|
|
|
} |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
if (($response = $this->prepare($data)) instanceof Response) { |
|
533
|
|
|
return $response; |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
View Code Duplication |
DB::transaction(function () { |
|
|
|
|
|
|
537
|
|
|
$updates = $this->prepareUpdate($this->updates); |
|
538
|
|
|
|
|
539
|
|
|
foreach ($updates as $column => $value) { |
|
540
|
|
|
/* @var Model $this->model */ |
|
541
|
|
|
$this->model->setAttribute($column, $value); |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
$this->model->save(); |
|
545
|
|
|
|
|
546
|
|
|
$this->updateRelation($this->relations); |
|
547
|
|
|
}); |
|
548
|
|
|
|
|
549
|
|
|
if (($result = $this->complete($this->saved)) instanceof Response) { |
|
550
|
|
|
return $result; |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
if ($response = $this->ajaxResponse(trans('admin.update_succeeded'))) { |
|
554
|
|
|
return $response; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
return $this->redirectAfterUpdate(); |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* Get RedirectResponse after update. |
|
562
|
|
|
* |
|
563
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
564
|
|
|
*/ |
|
565
|
|
View Code Duplication |
protected function redirectAfterUpdate() |
|
|
|
|
|
|
566
|
|
|
{ |
|
567
|
|
|
admin_toastr(trans('admin.update_succeeded')); |
|
568
|
|
|
|
|
569
|
|
|
$url = Input::get(Builder::PREVIOUS_URL_KEY) ?: $this->resource(-1); |
|
570
|
|
|
|
|
571
|
|
|
return redirect($url); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Check if request is from editable. |
|
576
|
|
|
* |
|
577
|
|
|
* @param array $input |
|
578
|
|
|
* |
|
579
|
|
|
* @return bool |
|
580
|
|
|
*/ |
|
581
|
|
|
protected function isEditable(array $input = []) |
|
582
|
|
|
{ |
|
583
|
|
|
if (array_key_exists('_editable', $input)) { |
|
584
|
|
|
return true; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
return false; |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
/** |
|
591
|
|
|
* Handle editable update. |
|
592
|
|
|
* |
|
593
|
|
|
* @param array $input |
|
594
|
|
|
* |
|
595
|
|
|
* @return array |
|
596
|
|
|
*/ |
|
597
|
|
|
protected function handleEditable(array $input = []) |
|
598
|
|
|
{ |
|
599
|
|
|
if (array_key_exists('_editable', $input)) { |
|
600
|
|
|
$name = $input['name']; |
|
601
|
|
|
$value = $input['value']; |
|
602
|
|
|
|
|
603
|
|
|
array_forget($input, ['pk', 'value', 'name']); |
|
604
|
|
|
array_set($input, $name, $value); |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
return $input; |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* @param array $input |
|
612
|
|
|
* |
|
613
|
|
|
* @return array |
|
614
|
|
|
*/ |
|
615
|
|
|
protected function handleFileDelete(array $input = []) |
|
616
|
|
|
{ |
|
617
|
|
|
if (array_key_exists(Field::FILE_DELETE_FLAG, $input)) { |
|
618
|
|
|
$input[Field::FILE_DELETE_FLAG] = $input['key']; |
|
619
|
|
|
unset($input['key']); |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
Input::replace($input); |
|
623
|
|
|
|
|
624
|
|
|
return $input; |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
/** |
|
628
|
|
|
* Handle orderable update. |
|
629
|
|
|
* |
|
630
|
|
|
* @param int $id |
|
631
|
|
|
* @param array $input |
|
632
|
|
|
* |
|
633
|
|
|
* @return bool |
|
634
|
|
|
*/ |
|
635
|
|
|
protected function handleOrderable($id, array $input = []) |
|
636
|
|
|
{ |
|
637
|
|
|
if (array_key_exists('_orderable', $input)) { |
|
638
|
|
|
$model = $this->model->find($id); |
|
639
|
|
|
|
|
640
|
|
|
if ($model instanceof Sortable) { |
|
|
|
|
|
|
641
|
|
|
$input['_orderable'] == 1 ? $model->moveOrderUp() : $model->moveOrderDown(); |
|
642
|
|
|
|
|
643
|
|
|
return true; |
|
644
|
|
|
} |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
return false; |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
/** |
|
651
|
|
|
* Update relation data. |
|
652
|
|
|
* |
|
653
|
|
|
* @param array $relationsData |
|
654
|
|
|
* |
|
655
|
|
|
* @return void |
|
656
|
|
|
*/ |
|
657
|
|
|
protected function updateRelation($relationsData) |
|
658
|
|
|
{ |
|
659
|
|
|
foreach ($relationsData as $name => $values) { |
|
660
|
|
|
if (!method_exists($this->model, $name)) { |
|
661
|
|
|
continue; |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
|
|
$relation = $this->model->$name(); |
|
665
|
|
|
|
|
666
|
|
|
$oneToOneRelation = $relation instanceof \Illuminate\Database\Eloquent\Relations\HasOne |
|
667
|
|
|
|| $relation instanceof \Illuminate\Database\Eloquent\Relations\MorphOne; |
|
668
|
|
|
|
|
669
|
|
|
$prepared = $this->prepareUpdate([$name => $values], $oneToOneRelation); |
|
670
|
|
|
|
|
671
|
|
|
if (empty($prepared)) { |
|
672
|
|
|
continue; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
switch (get_class($relation)) { |
|
676
|
|
|
case \Illuminate\Database\Eloquent\Relations\BelongsToMany::class: |
|
677
|
|
|
case \Illuminate\Database\Eloquent\Relations\MorphToMany::class: |
|
678
|
|
|
if (isset($prepared[$name])) { |
|
679
|
|
|
$relation->sync($prepared[$name]); |
|
680
|
|
|
} |
|
681
|
|
|
break; |
|
682
|
|
|
case \Illuminate\Database\Eloquent\Relations\HasOne::class: |
|
|
|
|
|
|
683
|
|
|
|
|
684
|
|
|
$related = $this->model->$name; |
|
685
|
|
|
|
|
686
|
|
|
// if related is empty |
|
687
|
|
|
if (is_null($related)) { |
|
688
|
|
|
$related = $relation->getRelated(); |
|
689
|
|
|
$related->{$relation->getForeignKeyName()} = $this->model->{$this->model->getKeyName()}; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
foreach ($prepared[$name] as $column => $value) { |
|
693
|
|
|
$related->setAttribute($column, $value); |
|
694
|
|
|
} |
|
695
|
|
|
|
|
696
|
|
|
$related->save(); |
|
697
|
|
|
break; |
|
698
|
|
|
case \Illuminate\Database\Eloquent\Relations\MorphOne::class: |
|
699
|
|
|
$related = $this->model->$name; |
|
700
|
|
|
if (is_null($related)) { |
|
701
|
|
|
$related = $relation->make(); |
|
702
|
|
|
} |
|
703
|
|
|
foreach ($prepared[$name] as $column => $value) { |
|
704
|
|
|
$related->setAttribute($column, $value); |
|
705
|
|
|
} |
|
706
|
|
|
$related->save(); |
|
707
|
|
|
break; |
|
708
|
|
|
case \Illuminate\Database\Eloquent\Relations\HasMany::class: |
|
709
|
|
|
case \Illuminate\Database\Eloquent\Relations\MorphMany::class: |
|
|
|
|
|
|
710
|
|
|
|
|
711
|
|
|
foreach ($prepared[$name] as $related) { |
|
712
|
|
|
$relation = $this->model()->$name(); |
|
713
|
|
|
|
|
714
|
|
|
$keyName = $relation->getRelated()->getKeyName(); |
|
715
|
|
|
|
|
716
|
|
|
$instance = $relation->findOrNew(array_get($related, $keyName)); |
|
717
|
|
|
|
|
718
|
|
|
if ($related[static::REMOVE_FLAG_NAME] == 1) { |
|
719
|
|
|
$instance->delete(); |
|
720
|
|
|
|
|
721
|
|
|
continue; |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
array_forget($related, static::REMOVE_FLAG_NAME); |
|
725
|
|
|
|
|
726
|
|
|
$instance->fill($related); |
|
727
|
|
|
|
|
728
|
|
|
$instance->save(); |
|
729
|
|
|
} |
|
730
|
|
|
|
|
731
|
|
|
break; |
|
732
|
|
|
} |
|
733
|
|
|
} |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
/** |
|
737
|
|
|
* Prepare input data for update. |
|
738
|
|
|
* |
|
739
|
|
|
* @param array $updates |
|
740
|
|
|
* @param bool $oneToOneRelation If column is one-to-one relation. |
|
741
|
|
|
* |
|
742
|
|
|
* @return array |
|
743
|
|
|
*/ |
|
744
|
|
|
protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
|
745
|
|
|
{ |
|
746
|
|
|
$prepared = []; |
|
747
|
|
|
|
|
748
|
|
|
foreach ($this->builder->fields() as $field) { |
|
749
|
|
|
$columns = $field->column(); |
|
750
|
|
|
|
|
751
|
|
|
// If column not in input array data, then continue. |
|
752
|
|
|
if (!array_has($updates, $columns)) { |
|
753
|
|
|
continue; |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
if ($this->invalidColumn($columns, $oneToOneRelation)) { |
|
757
|
|
|
continue; |
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
|
|
$value = $this->getDataByColumn($updates, $columns); |
|
761
|
|
|
|
|
762
|
|
|
$value = $field->prepare($value); |
|
763
|
|
|
|
|
764
|
|
|
if (is_array($columns)) { |
|
765
|
|
|
foreach ($columns as $name => $column) { |
|
766
|
|
|
array_set($prepared, $column, $value[$name]); |
|
767
|
|
|
} |
|
768
|
|
|
} elseif (is_string($columns)) { |
|
769
|
|
|
array_set($prepared, $columns, $value); |
|
770
|
|
|
} |
|
771
|
|
|
} |
|
772
|
|
|
|
|
773
|
|
|
return $prepared; |
|
774
|
|
|
} |
|
775
|
|
|
|
|
776
|
|
|
/** |
|
777
|
|
|
* @param string|array $columns |
|
778
|
|
|
* @param bool $oneToOneRelation |
|
779
|
|
|
* |
|
780
|
|
|
* @return bool |
|
781
|
|
|
*/ |
|
782
|
|
|
protected function invalidColumn($columns, $oneToOneRelation = false) |
|
783
|
|
|
{ |
|
784
|
|
|
foreach ((array) $columns as $column) { |
|
785
|
|
|
if ((!$oneToOneRelation && Str::contains($column, '.')) || |
|
786
|
|
|
($oneToOneRelation && !Str::contains($column, '.'))) { |
|
787
|
|
|
return true; |
|
788
|
|
|
} |
|
789
|
|
|
} |
|
790
|
|
|
|
|
791
|
|
|
return false; |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
|
|
/** |
|
795
|
|
|
* Prepare input data for insert. |
|
796
|
|
|
* |
|
797
|
|
|
* @param $inserts |
|
798
|
|
|
* |
|
799
|
|
|
* @return array |
|
800
|
|
|
*/ |
|
801
|
|
|
protected function prepareInsert($inserts) |
|
802
|
|
|
{ |
|
803
|
|
|
if ($this->isHasOneRelation($inserts)) { |
|
804
|
|
|
$inserts = array_dot($inserts); |
|
805
|
|
|
} |
|
806
|
|
|
|
|
807
|
|
|
foreach ($inserts as $column => $value) { |
|
808
|
|
|
if (is_null($field = $this->getFieldByColumn($column))) { |
|
809
|
|
|
unset($inserts[$column]); |
|
810
|
|
|
continue; |
|
811
|
|
|
} |
|
812
|
|
|
|
|
813
|
|
|
$inserts[$column] = $field->prepare($value); |
|
814
|
|
|
} |
|
815
|
|
|
|
|
816
|
|
|
$prepared = []; |
|
817
|
|
|
|
|
818
|
|
|
foreach ($inserts as $key => $value) { |
|
819
|
|
|
array_set($prepared, $key, $value); |
|
820
|
|
|
} |
|
821
|
|
|
|
|
822
|
|
|
return $prepared; |
|
823
|
|
|
} |
|
824
|
|
|
|
|
825
|
|
|
/** |
|
826
|
|
|
* Is input data is has-one relation. |
|
827
|
|
|
* |
|
828
|
|
|
* @param array $inserts |
|
829
|
|
|
* |
|
830
|
|
|
* @return bool |
|
831
|
|
|
*/ |
|
832
|
|
|
protected function isHasOneRelation($inserts) |
|
833
|
|
|
{ |
|
834
|
|
|
$first = current($inserts); |
|
835
|
|
|
|
|
836
|
|
|
if (!is_array($first)) { |
|
837
|
|
|
return false; |
|
838
|
|
|
} |
|
839
|
|
|
|
|
840
|
|
|
if (is_array(current($first))) { |
|
841
|
|
|
return false; |
|
842
|
|
|
} |
|
843
|
|
|
|
|
844
|
|
|
return Arr::isAssoc($first); |
|
845
|
|
|
} |
|
846
|
|
|
|
|
847
|
|
|
/** |
|
848
|
|
|
* Set submitted callback. |
|
849
|
|
|
* |
|
850
|
|
|
* @param Closure $callback |
|
851
|
|
|
* |
|
852
|
|
|
* @return void |
|
853
|
|
|
*/ |
|
854
|
|
|
public function submitted(Closure $callback) |
|
855
|
|
|
{ |
|
856
|
|
|
$this->submitted = $callback; |
|
857
|
|
|
} |
|
858
|
|
|
|
|
859
|
|
|
/** |
|
860
|
|
|
* Set saving callback. |
|
861
|
|
|
* |
|
862
|
|
|
* @param Closure $callback |
|
863
|
|
|
* |
|
864
|
|
|
* @return void |
|
865
|
|
|
*/ |
|
866
|
|
|
public function saving(Closure $callback) |
|
867
|
|
|
{ |
|
868
|
|
|
$this->saving = $callback; |
|
869
|
|
|
} |
|
870
|
|
|
|
|
871
|
|
|
/** |
|
872
|
|
|
* Set saved callback. |
|
873
|
|
|
* |
|
874
|
|
|
* @param callable $callback |
|
875
|
|
|
* |
|
876
|
|
|
* @return void |
|
877
|
|
|
*/ |
|
878
|
|
|
public function saved(Closure $callback) |
|
879
|
|
|
{ |
|
880
|
|
|
$this->saved = $callback; |
|
881
|
|
|
} |
|
882
|
|
|
|
|
883
|
|
|
/** |
|
884
|
|
|
* Ignore fields to save. |
|
885
|
|
|
* |
|
886
|
|
|
* @param string|array $fields |
|
887
|
|
|
* |
|
888
|
|
|
* @return $this |
|
889
|
|
|
*/ |
|
890
|
|
|
public function ignore($fields) |
|
891
|
|
|
{ |
|
892
|
|
|
$this->ignored = array_merge($this->ignored, (array) $fields); |
|
893
|
|
|
|
|
894
|
|
|
return $this; |
|
895
|
|
|
} |
|
896
|
|
|
|
|
897
|
|
|
/** |
|
898
|
|
|
* @param array $data |
|
899
|
|
|
* @param string|array $columns |
|
900
|
|
|
* |
|
901
|
|
|
* @return array|mixed |
|
902
|
|
|
*/ |
|
903
|
|
View Code Duplication |
protected function getDataByColumn($data, $columns) |
|
|
|
|
|
|
904
|
|
|
{ |
|
905
|
|
|
if (is_string($columns)) { |
|
906
|
|
|
return array_get($data, $columns); |
|
907
|
|
|
} |
|
908
|
|
|
|
|
909
|
|
|
if (is_array($columns)) { |
|
910
|
|
|
$value = []; |
|
911
|
|
|
foreach ($columns as $name => $column) { |
|
912
|
|
|
if (!array_has($data, $column)) { |
|
913
|
|
|
continue; |
|
914
|
|
|
} |
|
915
|
|
|
$value[$name] = array_get($data, $column); |
|
916
|
|
|
} |
|
917
|
|
|
|
|
918
|
|
|
return $value; |
|
919
|
|
|
} |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
/** |
|
923
|
|
|
* Find field object by column. |
|
924
|
|
|
* |
|
925
|
|
|
* @param $column |
|
926
|
|
|
* |
|
927
|
|
|
* @return mixed |
|
928
|
|
|
*/ |
|
929
|
|
|
protected function getFieldByColumn($column) |
|
930
|
|
|
{ |
|
931
|
|
|
return $this->builder->fields()->first( |
|
932
|
|
|
function (Field $field) use ($column) { |
|
933
|
|
|
if (is_array($field->column())) { |
|
934
|
|
|
return in_array($column, $field->column()); |
|
935
|
|
|
} |
|
936
|
|
|
|
|
937
|
|
|
return $field->column() == $column; |
|
938
|
|
|
} |
|
939
|
|
|
); |
|
940
|
|
|
} |
|
941
|
|
|
|
|
942
|
|
|
/** |
|
943
|
|
|
* Set original data for each field. |
|
944
|
|
|
* |
|
945
|
|
|
* @return void |
|
946
|
|
|
*/ |
|
947
|
|
|
protected function setFieldOriginalValue() |
|
948
|
|
|
{ |
|
949
|
|
|
// static::doNotSnakeAttributes($this->model); |
|
|
|
|
|
|
950
|
|
|
|
|
951
|
|
|
$values = $this->model->toArray(); |
|
952
|
|
|
|
|
953
|
|
|
$this->builder->fields()->each(function (Field $field) use ($values) { |
|
954
|
|
|
$field->setOriginal($values); |
|
955
|
|
|
}); |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
/** |
|
959
|
|
|
* Set all fields value in form. |
|
960
|
|
|
* |
|
961
|
|
|
* @param $id |
|
962
|
|
|
* |
|
963
|
|
|
* @return void |
|
964
|
|
|
*/ |
|
965
|
|
|
protected function setFieldValue($id) |
|
966
|
|
|
{ |
|
967
|
|
|
$relations = $this->getRelations(); |
|
968
|
|
|
|
|
969
|
|
|
$this->model = $this->model->with($relations)->findOrFail($id); |
|
|
|
|
|
|
970
|
|
|
|
|
971
|
|
|
// static::doNotSnakeAttributes($this->model); |
|
|
|
|
|
|
972
|
|
|
|
|
973
|
|
|
$data = $this->model->toArray(); |
|
974
|
|
|
|
|
975
|
|
|
$this->builder->fields()->each(function (Field $field) use ($data) { |
|
976
|
|
|
$field->fill($data); |
|
977
|
|
|
}); |
|
978
|
|
|
} |
|
979
|
|
|
|
|
980
|
|
|
/** |
|
981
|
|
|
* Don't snake case attributes. |
|
982
|
|
|
* |
|
983
|
|
|
* @param Model $model |
|
984
|
|
|
* |
|
985
|
|
|
* @return void |
|
986
|
|
|
*/ |
|
987
|
|
|
protected static function doNotSnakeAttributes(Model $model) |
|
988
|
|
|
{ |
|
989
|
|
|
$class = get_class($model); |
|
990
|
|
|
|
|
991
|
|
|
$class::$snakeAttributes = false; |
|
992
|
|
|
} |
|
993
|
|
|
|
|
994
|
|
|
/** |
|
995
|
|
|
* Get validation messages. |
|
996
|
|
|
* |
|
997
|
|
|
* @param array $input |
|
998
|
|
|
* |
|
999
|
|
|
* @return MessageBag|bool |
|
1000
|
|
|
*/ |
|
1001
|
|
|
protected function validationMessages($input) |
|
1002
|
|
|
{ |
|
1003
|
|
|
$failedValidators = []; |
|
1004
|
|
|
|
|
1005
|
|
|
foreach ($this->builder->fields() as $field) { |
|
1006
|
|
|
if (!$validator = $field->getValidator($input)) { |
|
1007
|
|
|
continue; |
|
1008
|
|
|
} |
|
1009
|
|
|
|
|
1010
|
|
|
if (($validator instanceof Validator) && !$validator->passes()) { |
|
1011
|
|
|
$failedValidators[] = $validator; |
|
1012
|
|
|
} |
|
1013
|
|
|
} |
|
1014
|
|
|
|
|
1015
|
|
|
$message = $this->mergeValidationMessages($failedValidators); |
|
1016
|
|
|
|
|
1017
|
|
|
return $message->any() ? $message : false; |
|
1018
|
|
|
} |
|
1019
|
|
|
|
|
1020
|
|
|
/** |
|
1021
|
|
|
* Merge validation messages from input validators. |
|
1022
|
|
|
* |
|
1023
|
|
|
* @param \Illuminate\Validation\Validator[] $validators |
|
1024
|
|
|
* |
|
1025
|
|
|
* @return MessageBag |
|
1026
|
|
|
*/ |
|
1027
|
|
|
protected function mergeValidationMessages($validators) |
|
1028
|
|
|
{ |
|
1029
|
|
|
$messageBag = new MessageBag(); |
|
1030
|
|
|
|
|
1031
|
|
|
foreach ($validators as $validator) { |
|
1032
|
|
|
$messageBag = $messageBag->merge($validator->messages()); |
|
1033
|
|
|
} |
|
1034
|
|
|
|
|
1035
|
|
|
return $messageBag; |
|
1036
|
|
|
} |
|
1037
|
|
|
|
|
1038
|
|
|
/** |
|
1039
|
|
|
* Get all relations of model from callable. |
|
1040
|
|
|
* |
|
1041
|
|
|
* @return array |
|
1042
|
|
|
*/ |
|
1043
|
|
|
public function getRelations() |
|
1044
|
|
|
{ |
|
1045
|
|
|
$relations = $columns = []; |
|
1046
|
|
|
|
|
1047
|
|
|
foreach ($this->builder->fields() as $field) { |
|
1048
|
|
|
$columns[] = $field->column(); |
|
1049
|
|
|
} |
|
1050
|
|
|
|
|
1051
|
|
|
foreach (array_flatten($columns) as $column) { |
|
1052
|
|
|
if (str_contains($column, '.')) { |
|
1053
|
|
|
list($relation) = explode('.', $column); |
|
1054
|
|
|
|
|
1055
|
|
|
if (method_exists($this->model, $relation) && |
|
1056
|
|
|
$this->model->$relation() instanceof Relation |
|
1057
|
|
|
) { |
|
1058
|
|
|
$relations[] = $relation; |
|
1059
|
|
|
} |
|
1060
|
|
|
} elseif (method_exists($this->model, $column) && |
|
1061
|
|
|
!method_exists(Model::class, $column) |
|
1062
|
|
|
) { |
|
1063
|
|
|
$relations[] = $column; |
|
1064
|
|
|
} |
|
1065
|
|
|
} |
|
1066
|
|
|
|
|
1067
|
|
|
return array_unique($relations); |
|
1068
|
|
|
} |
|
1069
|
|
|
|
|
1070
|
|
|
/** |
|
1071
|
|
|
* Set action for form. |
|
1072
|
|
|
* |
|
1073
|
|
|
* @param string $action |
|
1074
|
|
|
* |
|
1075
|
|
|
* @return $this |
|
1076
|
|
|
*/ |
|
1077
|
|
|
public function setAction($action) |
|
1078
|
|
|
{ |
|
1079
|
|
|
$this->builder()->setAction($action); |
|
1080
|
|
|
|
|
1081
|
|
|
return $this; |
|
1082
|
|
|
} |
|
1083
|
|
|
|
|
1084
|
|
|
/** |
|
1085
|
|
|
* Set field and label width in current form. |
|
1086
|
|
|
* |
|
1087
|
|
|
* @param int $fieldWidth |
|
1088
|
|
|
* @param int $labelWidth |
|
1089
|
|
|
* |
|
1090
|
|
|
* @return $this |
|
1091
|
|
|
*/ |
|
1092
|
|
|
public function setWidth($fieldWidth = 8, $labelWidth = 2) |
|
1093
|
|
|
{ |
|
1094
|
|
|
$this->builder()->fields()->each(function ($field) use ($fieldWidth, $labelWidth) { |
|
1095
|
|
|
/* @var Field $field */ |
|
1096
|
|
|
$field->setWidth($fieldWidth, $labelWidth); |
|
1097
|
|
|
}); |
|
1098
|
|
|
|
|
1099
|
|
|
$this->builder()->setWidth($fieldWidth, $labelWidth); |
|
1100
|
|
|
|
|
1101
|
|
|
return $this; |
|
1102
|
|
|
} |
|
1103
|
|
|
|
|
1104
|
|
|
/** |
|
1105
|
|
|
* Set view for form. |
|
1106
|
|
|
* |
|
1107
|
|
|
* @param string $view |
|
1108
|
|
|
* |
|
1109
|
|
|
* @return $this |
|
1110
|
|
|
*/ |
|
1111
|
|
|
public function setView($view) |
|
1112
|
|
|
{ |
|
1113
|
|
|
$this->builder()->setView($view); |
|
1114
|
|
|
|
|
1115
|
|
|
return $this; |
|
1116
|
|
|
} |
|
1117
|
|
|
|
|
1118
|
|
|
/** |
|
1119
|
|
|
* Add a row in form. |
|
1120
|
|
|
* |
|
1121
|
|
|
* @param Closure $callback |
|
1122
|
|
|
* |
|
1123
|
|
|
* @return $this |
|
1124
|
|
|
*/ |
|
1125
|
|
|
public function row(Closure $callback) |
|
1126
|
|
|
{ |
|
1127
|
|
|
$this->rows[] = new Row($callback, $this); |
|
1128
|
|
|
|
|
1129
|
|
|
return $this; |
|
1130
|
|
|
} |
|
1131
|
|
|
|
|
1132
|
|
|
/** |
|
1133
|
|
|
* Tools setting for form. |
|
1134
|
|
|
* |
|
1135
|
|
|
* @param Closure $callback |
|
1136
|
|
|
*/ |
|
1137
|
|
|
public function tools(Closure $callback) |
|
1138
|
|
|
{ |
|
1139
|
|
|
$callback->call($this, $this->builder->getTools()); |
|
1140
|
|
|
} |
|
1141
|
|
|
|
|
1142
|
|
|
/** |
|
1143
|
|
|
* Disable form submit. |
|
1144
|
|
|
* |
|
1145
|
|
|
* @return $this |
|
1146
|
|
|
*/ |
|
1147
|
|
|
public function disableSubmit() |
|
1148
|
|
|
{ |
|
1149
|
|
|
$this->builder()->options(['enableSubmit' => false]); |
|
1150
|
|
|
|
|
1151
|
|
|
return $this; |
|
1152
|
|
|
} |
|
1153
|
|
|
|
|
1154
|
|
|
/** |
|
1155
|
|
|
* Disable form reset. |
|
1156
|
|
|
* |
|
1157
|
|
|
* @return $this |
|
1158
|
|
|
*/ |
|
1159
|
|
|
public function disableReset() |
|
1160
|
|
|
{ |
|
1161
|
|
|
$this->builder()->options(['enableReset' => false]); |
|
1162
|
|
|
|
|
1163
|
|
|
return $this; |
|
1164
|
|
|
} |
|
1165
|
|
|
|
|
1166
|
|
|
/** |
|
1167
|
|
|
* Get current resource route url. |
|
1168
|
|
|
* |
|
1169
|
|
|
* @param int $slice |
|
1170
|
|
|
* |
|
1171
|
|
|
* @return string |
|
1172
|
|
|
*/ |
|
1173
|
|
|
public function resource($slice = -2) |
|
1174
|
|
|
{ |
|
1175
|
|
|
$segments = explode('/', trim(app('request')->getUri(), '/')); |
|
1176
|
|
|
|
|
1177
|
|
|
if ($slice != 0) { |
|
1178
|
|
|
$segments = array_slice($segments, 0, $slice); |
|
1179
|
|
|
} |
|
1180
|
|
|
|
|
1181
|
|
|
return implode('/', $segments); |
|
1182
|
|
|
} |
|
1183
|
|
|
|
|
1184
|
|
|
/** |
|
1185
|
|
|
* Render the form contents. |
|
1186
|
|
|
* |
|
1187
|
|
|
* @return string |
|
1188
|
|
|
*/ |
|
1189
|
|
|
public function render() |
|
1190
|
|
|
{ |
|
1191
|
|
|
try { |
|
1192
|
|
|
return $this->builder->render(); |
|
1193
|
|
|
} catch (\Exception $e) { |
|
1194
|
|
|
return Handler::renderException($e); |
|
1195
|
|
|
} |
|
1196
|
|
|
} |
|
1197
|
|
|
|
|
1198
|
|
|
/** |
|
1199
|
|
|
* Get or set input data. |
|
1200
|
|
|
* |
|
1201
|
|
|
* @param string $key |
|
1202
|
|
|
* @param null $value |
|
1203
|
|
|
* |
|
1204
|
|
|
* @return array|mixed |
|
1205
|
|
|
*/ |
|
1206
|
|
|
public function input($key, $value = null) |
|
1207
|
|
|
{ |
|
1208
|
|
|
if (is_null($value)) { |
|
1209
|
|
|
return array_get($this->inputs, $key); |
|
1210
|
|
|
} |
|
1211
|
|
|
|
|
1212
|
|
|
return array_set($this->inputs, $key, $value); |
|
1213
|
|
|
} |
|
1214
|
|
|
|
|
1215
|
|
|
/** |
|
1216
|
|
|
* Register builtin fields. |
|
1217
|
|
|
* |
|
1218
|
|
|
* @return void |
|
1219
|
|
|
*/ |
|
1220
|
|
|
public static function registerBuiltinFields() |
|
1221
|
|
|
{ |
|
1222
|
|
|
$map = [ |
|
1223
|
|
|
'button' => \Encore\Admin\Form\Field\Button::class, |
|
1224
|
|
|
'checkbox' => \Encore\Admin\Form\Field\Checkbox::class, |
|
1225
|
|
|
'color' => \Encore\Admin\Form\Field\Color::class, |
|
1226
|
|
|
'currency' => \Encore\Admin\Form\Field\Currency::class, |
|
1227
|
|
|
'date' => \Encore\Admin\Form\Field\Date::class, |
|
1228
|
|
|
'dateRange' => \Encore\Admin\Form\Field\DateRange::class, |
|
1229
|
|
|
'datetime' => \Encore\Admin\Form\Field\Datetime::class, |
|
1230
|
|
|
'dateTimeRange' => \Encore\Admin\Form\Field\DatetimeRange::class, |
|
1231
|
|
|
'datetimeRange' => \Encore\Admin\Form\Field\DatetimeRange::class, |
|
1232
|
|
|
'decimal' => \Encore\Admin\Form\Field\Decimal::class, |
|
1233
|
|
|
'display' => \Encore\Admin\Form\Field\Display::class, |
|
1234
|
|
|
'divider' => \Encore\Admin\Form\Field\Divide::class, |
|
1235
|
|
|
'divide' => \Encore\Admin\Form\Field\Divide::class, |
|
1236
|
|
|
'embeds' => \Encore\Admin\Form\Field\Embeds::class, |
|
1237
|
|
|
'editor' => \Encore\Admin\Form\Field\Editor::class, |
|
1238
|
|
|
'email' => \Encore\Admin\Form\Field\Email::class, |
|
1239
|
|
|
'file' => \Encore\Admin\Form\Field\File::class, |
|
1240
|
|
|
'hasMany' => \Encore\Admin\Form\Field\HasMany::class, |
|
1241
|
|
|
'hidden' => \Encore\Admin\Form\Field\Hidden::class, |
|
1242
|
|
|
'id' => \Encore\Admin\Form\Field\Id::class, |
|
1243
|
|
|
'image' => \Encore\Admin\Form\Field\Image::class, |
|
1244
|
|
|
'ip' => \Encore\Admin\Form\Field\Ip::class, |
|
1245
|
|
|
'map' => \Encore\Admin\Form\Field\Map::class, |
|
1246
|
|
|
'mobile' => \Encore\Admin\Form\Field\Mobile::class, |
|
1247
|
|
|
'month' => \Encore\Admin\Form\Field\Month::class, |
|
1248
|
|
|
'multipleSelect' => \Encore\Admin\Form\Field\MultipleSelect::class, |
|
1249
|
|
|
'number' => \Encore\Admin\Form\Field\Number::class, |
|
1250
|
|
|
'password' => \Encore\Admin\Form\Field\Password::class, |
|
1251
|
|
|
'radio' => \Encore\Admin\Form\Field\Radio::class, |
|
1252
|
|
|
'rate' => \Encore\Admin\Form\Field\Rate::class, |
|
1253
|
|
|
'select' => \Encore\Admin\Form\Field\Select::class, |
|
1254
|
|
|
'slider' => \Encore\Admin\Form\Field\Slider::class, |
|
1255
|
|
|
'switch' => \Encore\Admin\Form\Field\SwitchField::class, |
|
1256
|
|
|
'text' => \Encore\Admin\Form\Field\Text::class, |
|
1257
|
|
|
'textarea' => \Encore\Admin\Form\Field\Textarea::class, |
|
1258
|
|
|
'time' => \Encore\Admin\Form\Field\Time::class, |
|
1259
|
|
|
'timeRange' => \Encore\Admin\Form\Field\TimeRange::class, |
|
1260
|
|
|
'url' => \Encore\Admin\Form\Field\Url::class, |
|
1261
|
|
|
'year' => \Encore\Admin\Form\Field\Year::class, |
|
1262
|
|
|
'html' => \Encore\Admin\Form\Field\Html::class, |
|
1263
|
|
|
'tags' => \Encore\Admin\Form\Field\Tags::class, |
|
1264
|
|
|
'icon' => \Encore\Admin\Form\Field\Icon::class, |
|
1265
|
|
|
'multipleFile' => \Encore\Admin\Form\Field\MultipleFile::class, |
|
1266
|
|
|
'multipleImage' => \Encore\Admin\Form\Field\MultipleImage::class, |
|
1267
|
|
|
'captcha' => \Encore\Admin\Form\Field\Captcha::class, |
|
1268
|
|
|
'listbox' => \Encore\Admin\Form\Field\Listbox::class, |
|
1269
|
|
|
]; |
|
1270
|
|
|
|
|
1271
|
|
|
foreach ($map as $abstract => $class) { |
|
1272
|
|
|
static::extend($abstract, $class); |
|
1273
|
|
|
} |
|
1274
|
|
|
} |
|
1275
|
|
|
|
|
1276
|
|
|
/** |
|
1277
|
|
|
* Register custom field. |
|
1278
|
|
|
* |
|
1279
|
|
|
* @param string $abstract |
|
1280
|
|
|
* @param string $class |
|
1281
|
|
|
* |
|
1282
|
|
|
* @return void |
|
1283
|
|
|
*/ |
|
1284
|
|
|
public static function extend($abstract, $class) |
|
1285
|
|
|
{ |
|
1286
|
|
|
static::$availableFields[$abstract] = $class; |
|
1287
|
|
|
} |
|
1288
|
|
|
|
|
1289
|
|
|
/** |
|
1290
|
|
|
* Remove registered field. |
|
1291
|
|
|
* |
|
1292
|
|
|
* @param array|string $abstract |
|
1293
|
|
|
*/ |
|
1294
|
|
|
public static function forget($abstract) |
|
1295
|
|
|
{ |
|
1296
|
|
|
array_forget(static::$availableFields, $abstract); |
|
1297
|
|
|
} |
|
1298
|
|
|
|
|
1299
|
|
|
/** |
|
1300
|
|
|
* Find field class. |
|
1301
|
|
|
* |
|
1302
|
|
|
* @param string $method |
|
1303
|
|
|
* |
|
1304
|
|
|
* @return bool|mixed |
|
1305
|
|
|
*/ |
|
1306
|
|
View Code Duplication |
public static function findFieldClass($method) |
|
|
|
|
|
|
1307
|
|
|
{ |
|
1308
|
|
|
$class = array_get(static::$availableFields, $method); |
|
1309
|
|
|
|
|
1310
|
|
|
if (class_exists($class)) { |
|
1311
|
|
|
return $class; |
|
1312
|
|
|
} |
|
1313
|
|
|
|
|
1314
|
|
|
return false; |
|
1315
|
|
|
} |
|
1316
|
|
|
|
|
1317
|
|
|
/** |
|
1318
|
|
|
* Collect assets required by registered field. |
|
1319
|
|
|
* |
|
1320
|
|
|
* @return array |
|
1321
|
|
|
*/ |
|
1322
|
|
|
public static function collectFieldAssets() |
|
1323
|
|
|
{ |
|
1324
|
|
|
if (!empty(static::$collectedAssets)) { |
|
1325
|
|
|
return static::$collectedAssets; |
|
1326
|
|
|
} |
|
1327
|
|
|
|
|
1328
|
|
|
$css = collect(); |
|
1329
|
|
|
$js = collect(); |
|
1330
|
|
|
|
|
1331
|
|
|
foreach (static::$availableFields as $field) { |
|
1332
|
|
|
if (!method_exists($field, 'getAssets')) { |
|
1333
|
|
|
continue; |
|
1334
|
|
|
} |
|
1335
|
|
|
|
|
1336
|
|
|
$assets = call_user_func([$field, 'getAssets']); |
|
1337
|
|
|
|
|
1338
|
|
|
$css->push(array_get($assets, 'css')); |
|
1339
|
|
|
$js->push(array_get($assets, 'js')); |
|
1340
|
|
|
} |
|
1341
|
|
|
|
|
1342
|
|
|
return static::$collectedAssets = [ |
|
1343
|
|
|
'css' => $css->flatten()->unique()->filter()->toArray(), |
|
1344
|
|
|
'js' => $js->flatten()->unique()->filter()->toArray(), |
|
1345
|
|
|
]; |
|
1346
|
|
|
} |
|
1347
|
|
|
|
|
1348
|
|
|
/** |
|
1349
|
|
|
* Getter. |
|
1350
|
|
|
* |
|
1351
|
|
|
* @param string $name |
|
1352
|
|
|
* |
|
1353
|
|
|
* @return array|mixed |
|
1354
|
|
|
*/ |
|
1355
|
|
|
public function __get($name) |
|
1356
|
|
|
{ |
|
1357
|
|
|
return $this->input($name); |
|
1358
|
|
|
} |
|
1359
|
|
|
|
|
1360
|
|
|
/** |
|
1361
|
|
|
* Setter. |
|
1362
|
|
|
* |
|
1363
|
|
|
* @param string $name |
|
1364
|
|
|
* @param $value |
|
1365
|
|
|
*/ |
|
1366
|
|
|
public function __set($name, $value) |
|
1367
|
|
|
{ |
|
1368
|
|
|
$this->input($name, $value); |
|
1369
|
|
|
} |
|
1370
|
|
|
|
|
1371
|
|
|
/** |
|
1372
|
|
|
* Generate a Field object and add to form builder if Field exists. |
|
1373
|
|
|
* |
|
1374
|
|
|
* @param string $method |
|
1375
|
|
|
* @param array $arguments |
|
1376
|
|
|
* |
|
1377
|
|
|
* @return Field|void |
|
1378
|
|
|
*/ |
|
1379
|
|
View Code Duplication |
public function __call($method, $arguments) |
|
|
|
|
|
|
1380
|
|
|
{ |
|
1381
|
|
|
if ($className = static::findFieldClass($method)) { |
|
1382
|
|
|
$column = array_get($arguments, 0, ''); //[0]; |
|
1383
|
|
|
|
|
1384
|
|
|
$element = new $className($column, array_slice($arguments, 1)); |
|
1385
|
|
|
|
|
1386
|
|
|
$this->pushField($element); |
|
1387
|
|
|
|
|
1388
|
|
|
return $element; |
|
1389
|
|
|
} |
|
1390
|
|
|
} |
|
1391
|
|
|
|
|
1392
|
|
|
/** |
|
1393
|
|
|
* Render the contents of the form when casting to string. |
|
1394
|
|
|
* |
|
1395
|
|
|
* @return string |
|
1396
|
|
|
*/ |
|
1397
|
|
|
public function __toString() |
|
1398
|
|
|
{ |
|
1399
|
|
|
return $this->render(); |
|
1400
|
|
|
} |
|
1401
|
|
|
} |
|
1402
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: