1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Form; |
7
|
|
|
use Encore\Admin\Form\Field; |
8
|
|
|
use Encore\Admin\Form\NestedForm; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany as Relation; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HasMany. |
16
|
|
|
*/ |
17
|
|
|
class HasMany extends Field |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Relation name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $relationName = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Form builder. |
28
|
|
|
* |
29
|
|
|
* @var \Closure |
30
|
|
|
*/ |
31
|
|
|
protected $builder = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Form data. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $value = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* View Mode. |
42
|
|
|
* |
43
|
|
|
* Supports `default` and `tab` currently. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $viewMode = 'default'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Available views for HasMany field. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $views = [ |
55
|
|
|
'default' => 'admin::form.hasmany', |
56
|
|
|
'tab' => 'admin::form.hasmanytab', |
57
|
|
|
'table' => 'admin::form.hasmanytable', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Options for template. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $options = [ |
66
|
|
|
'allowCreate' => true, |
67
|
|
|
'allowDelete' => true, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Distinct fields. |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $distinctFields = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create a new HasMany field instance. |
79
|
|
|
* |
80
|
|
|
* @param $relationName |
81
|
|
|
* @param array $arguments |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function __construct($relationName, $arguments = []) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->relationName = $relationName; |
86
|
|
|
|
87
|
|
|
$this->column = $relationName; |
88
|
|
|
|
89
|
|
|
if (count($arguments) == 1) { |
90
|
|
|
$this->label = $this->formatLabel(); |
91
|
|
|
$this->builder = $arguments[0]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (count($arguments) == 2) { |
95
|
|
|
list($this->label, $this->builder) = $arguments; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get validator for this field. |
101
|
|
|
* |
102
|
|
|
* @param array $input |
103
|
|
|
* |
104
|
|
|
* @return bool|\Illuminate\Contracts\Validation\Validator |
105
|
|
|
*/ |
106
|
|
|
public function getValidator(array $input) |
107
|
|
|
{ |
108
|
|
|
if (!array_key_exists($this->column, $input)) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$input = Arr::only($input, $this->column); |
113
|
|
|
|
114
|
|
|
$form = $this->buildNestedForm($this->column, $this->builder); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$rules = $attributes = []; |
117
|
|
|
|
118
|
|
|
/* @var Field $field */ |
119
|
|
|
foreach ($form->fields() as $field) { |
120
|
|
|
if (!$fieldRules = $field->getRules()) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$column = $field->column(); |
125
|
|
|
|
126
|
|
|
if (is_array($column)) { |
127
|
|
|
foreach ($column as $key => $name) { |
128
|
|
|
$rules[$name.$key] = $fieldRules; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->resetInputKey($input, $column); |
132
|
|
|
} else { |
133
|
|
|
$rules[$column] = $fieldRules; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$attributes = array_merge( |
137
|
|
|
$attributes, |
138
|
|
|
$this->formatValidationAttribute($input, $field->label(), $column) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
Arr::forget($rules, NestedForm::REMOVE_FLAG_NAME); |
143
|
|
|
|
144
|
|
|
if (empty($rules)) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$newRules = []; |
149
|
|
|
$newInput = []; |
150
|
|
|
|
151
|
|
|
foreach ($rules as $column => $rule) { |
152
|
|
|
foreach (array_keys($input[$this->column]) as $key) { |
153
|
|
|
$newRules["{$this->column}.$key.$column"] = $rule; |
154
|
|
|
if (isset($input[$this->column][$key][$column]) && |
155
|
|
|
is_array($input[$this->column][$key][$column])) { |
156
|
|
|
foreach ($input[$this->column][$key][$column] as $vkey => $value) { |
157
|
|
|
$newInput["{$this->column}.$key.{$column}$vkey"] = $value; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (empty($newInput)) { |
164
|
|
|
$newInput = $input; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->appendDistinctRules($newRules); |
168
|
|
|
|
169
|
|
|
return \validator($newInput, $newRules, $this->getValidationMessages(), $attributes); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set distinct fields. |
174
|
|
|
* |
175
|
|
|
* @param array $fields |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function distinctFields(array $fields) |
180
|
|
|
{ |
181
|
|
|
$this->distinctFields = $fields; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Append distinct rules. |
188
|
|
|
* |
189
|
|
|
* @param array $rules |
190
|
|
|
*/ |
191
|
|
|
protected function appendDistinctRules(array &$rules) |
192
|
|
|
{ |
193
|
|
|
foreach ($this->distinctFields as $field) { |
194
|
|
|
$rules["{$this->column}.*.$field"] = 'distinct'; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Format validation attributes. |
200
|
|
|
* |
201
|
|
|
* @param array $input |
202
|
|
|
* @param string $label |
203
|
|
|
* @param string $column |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
protected function formatValidationAttribute($input, $label, $column) |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$new = $attributes = []; |
210
|
|
|
|
211
|
|
|
if (is_array($column)) { |
212
|
|
|
foreach ($column as $index => $col) { |
213
|
|
|
$new[$col.$index] = $col; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
foreach (array_keys(Arr::dot($input)) as $key) { |
218
|
|
|
if (is_string($column)) { |
219
|
|
|
if (Str::endsWith($key, ".$column")) { |
220
|
|
|
$attributes[$key] = $label; |
221
|
|
|
} |
222
|
|
|
} else { |
223
|
|
|
foreach ($new as $k => $val) { |
224
|
|
|
if (Str::endsWith($key, ".$k")) { |
225
|
|
|
$attributes[$key] = $label."[$val]"; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $attributes; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Reset input key for validation. |
236
|
|
|
* |
237
|
|
|
* @param array $input |
238
|
|
|
* @param array $column $column is the column name array set |
239
|
|
|
* |
240
|
|
|
* @return void. |
|
|
|
|
241
|
|
|
*/ |
242
|
|
|
protected function resetInputKey(array &$input, array $column) |
243
|
|
|
{ |
244
|
|
|
/** |
245
|
|
|
* flip the column name array set. |
246
|
|
|
* |
247
|
|
|
* for example, for the DateRange, the column like as below |
248
|
|
|
* |
249
|
|
|
* ["start" => "created_at", "end" => "updated_at"] |
250
|
|
|
* |
251
|
|
|
* to: |
252
|
|
|
* |
253
|
|
|
* [ "created_at" => "start", "updated_at" => "end" ] |
254
|
|
|
*/ |
255
|
|
|
$column = array_flip($column); |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* $this->column is the inputs array's node name, default is the relation name. |
259
|
|
|
* |
260
|
|
|
* So... $input[$this->column] is the data of this column's inputs data |
261
|
|
|
* |
262
|
|
|
* in the HasMany relation, has many data/field set, $set is field set in the below |
263
|
|
|
*/ |
264
|
|
|
foreach ($input[$this->column] as $index => $set) { |
265
|
|
|
|
266
|
|
|
/* |
267
|
|
|
* foreach the field set to find the corresponding $column |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
foreach ($set as $name => $value) { |
|
|
|
|
270
|
|
|
/* |
271
|
|
|
* if doesn't have column name, continue to the next loop |
272
|
|
|
*/ |
273
|
|
|
if (!array_key_exists($name, $column)) { |
274
|
|
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* example: $newKey = created_atstart. |
279
|
|
|
* |
280
|
|
|
* Σ( ° △ °|||)︴ |
281
|
|
|
* |
282
|
|
|
* I don't know why a form need range input? Only can imagine is for range search.... |
283
|
|
|
*/ |
284
|
|
|
$newKey = $name.$column[$name]; |
285
|
|
|
|
286
|
|
|
/* |
287
|
|
|
* set new key |
288
|
|
|
*/ |
289
|
|
|
Arr::set($input, "{$this->column}.$index.$newKey", $value); |
290
|
|
|
/* |
291
|
|
|
* forget the old key and value |
292
|
|
|
*/ |
293
|
|
|
Arr::forget($input, "{$this->column}.$index.$name"); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Prepare input data for insert or update. |
300
|
|
|
* |
301
|
|
|
* @param array $input |
302
|
|
|
* |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
|
|
public function prepare($input) |
306
|
|
|
{ |
307
|
|
|
$form = $this->buildNestedForm($this->column, $this->builder); |
|
|
|
|
308
|
|
|
|
309
|
|
|
return $form->setOriginal($this->original, $this->getKeyName())->prepare($input); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Build a Nested form. |
314
|
|
|
* |
315
|
|
|
* @param string $column |
316
|
|
|
* @param \Closure $builder |
317
|
|
|
* @param null $model |
318
|
|
|
* |
319
|
|
|
* @return NestedForm |
320
|
|
|
*/ |
321
|
|
View Code Duplication |
protected function buildNestedForm($column, \Closure $builder, $model = null) |
|
|
|
|
322
|
|
|
{ |
323
|
|
|
$form = new Form\NestedForm($column, $model); |
324
|
|
|
|
325
|
|
|
$form->setForm($this->form); |
326
|
|
|
|
327
|
|
|
call_user_func($builder, $form); |
328
|
|
|
|
329
|
|
|
$form->hidden($this->getKeyName()); |
330
|
|
|
|
331
|
|
|
$form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS); |
332
|
|
|
|
333
|
|
|
return $form; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Get the HasMany relation key name. |
338
|
|
|
* |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
protected function getKeyName() |
342
|
|
|
{ |
343
|
|
|
if (is_null($this->form)) { |
344
|
|
|
return; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $this->form->model()->{$this->relationName}()->getRelated()->getKeyName(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Set view mode. |
352
|
|
|
* |
353
|
|
|
* @param string $mode currently support `tab` mode. |
354
|
|
|
* |
355
|
|
|
* @return $this |
356
|
|
|
* |
357
|
|
|
* @author Edwin Hui |
358
|
|
|
*/ |
359
|
|
|
public function mode($mode) |
360
|
|
|
{ |
361
|
|
|
$this->viewMode = $mode; |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Use tab mode to showing hasmany field. |
368
|
|
|
* |
369
|
|
|
* @return HasMany |
370
|
|
|
*/ |
371
|
|
|
public function useTab() |
372
|
|
|
{ |
373
|
|
|
return $this->mode('tab'); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Use table mode to showing hasmany field. |
378
|
|
|
* |
379
|
|
|
* @return HasMany |
380
|
|
|
*/ |
381
|
|
|
public function useTable() |
382
|
|
|
{ |
383
|
|
|
return $this->mode('table'); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Build Nested form for related data. |
388
|
|
|
* |
389
|
|
|
* @throws \Exception |
390
|
|
|
* |
391
|
|
|
* @return array |
392
|
|
|
*/ |
393
|
|
|
protected function buildRelatedForms() |
394
|
|
|
{ |
395
|
|
|
if (is_null($this->form)) { |
396
|
|
|
return []; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$model = $this->form->model(); |
400
|
|
|
|
401
|
|
|
$relation = call_user_func([$model, $this->relationName]); |
402
|
|
|
|
403
|
|
|
if (!$relation instanceof Relation && !$relation instanceof MorphMany) { |
404
|
|
|
throw new \Exception('hasMany field must be a HasMany or MorphMany relation.'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
$forms = []; |
408
|
|
|
|
409
|
|
|
/* |
410
|
|
|
* If redirect from `exception` or `validation error` page. |
411
|
|
|
* |
412
|
|
|
* Then get form data from session flash. |
413
|
|
|
* |
414
|
|
|
* Else get data from database. |
415
|
|
|
*/ |
416
|
|
|
if ($values = old($this->column)) { |
|
|
|
|
417
|
|
|
foreach ($values as $key => $data) { |
418
|
|
|
if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) { |
419
|
|
|
continue; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$model = $relation->getRelated()->replicate()->forceFill($data); |
423
|
|
|
|
424
|
|
|
$forms[$key] = $this->buildNestedForm($this->column, $this->builder, $model) |
|
|
|
|
425
|
|
|
->fill($data); |
426
|
|
|
} |
427
|
|
|
} else { |
428
|
|
|
if (empty($this->value)) { |
429
|
|
|
return []; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
foreach ($this->value as $data) { |
433
|
|
|
$key = Arr::get($data, $relation->getRelated()->getKeyName()); |
434
|
|
|
|
435
|
|
|
$model = $relation->getRelated()->replicate()->forceFill($data); |
436
|
|
|
|
437
|
|
|
$forms[$key] = $this->buildNestedForm($this->column, $this->builder, $model) |
|
|
|
|
438
|
|
|
->fill($data); |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $forms; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Setup script for this field in different view mode. |
447
|
|
|
* |
448
|
|
|
* @param string $script |
449
|
|
|
* |
450
|
|
|
* @return void |
451
|
|
|
*/ |
452
|
|
|
protected function setupScript($script) |
453
|
|
|
{ |
454
|
|
|
$method = 'setupScriptFor'.ucfirst($this->viewMode).'View'; |
455
|
|
|
|
456
|
|
|
call_user_func([$this, $method], $script); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Setup default template script. |
461
|
|
|
* |
462
|
|
|
* @param string $templateScript |
463
|
|
|
* |
464
|
|
|
* @return void |
465
|
|
|
*/ |
466
|
|
View Code Duplication |
protected function setupScriptForDefaultView($templateScript) |
|
|
|
|
467
|
|
|
{ |
468
|
|
|
$removeClass = NestedForm::REMOVE_FLAG_CLASS; |
469
|
|
|
$defaultKey = NestedForm::DEFAULT_KEY_NAME; |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* When add a new sub form, replace all element key in new sub form. |
473
|
|
|
* |
474
|
|
|
* @example comments[new___key__][title] => comments[new_{index}][title] |
475
|
|
|
* |
476
|
|
|
* {count} is increment number of current sub form count. |
477
|
|
|
*/ |
478
|
|
|
$script = <<<EOT |
479
|
|
|
var index = 0; |
480
|
|
|
$('#has-many-{$this->column}').off('click', '.add').on('click', '.add', function () { |
481
|
|
|
|
482
|
|
|
var tpl = $('template.{$this->column}-tpl'); |
483
|
|
|
|
484
|
|
|
index++; |
485
|
|
|
|
486
|
|
|
var template = tpl.html().replace(/{$defaultKey}/g, index); |
487
|
|
|
$('.has-many-{$this->column}-forms').append(template); |
488
|
|
|
{$templateScript} |
489
|
|
|
return false; |
490
|
|
|
}); |
491
|
|
|
|
492
|
|
|
$('#has-many-{$this->column}').off('click', '.remove').on('click', '.remove', function () { |
493
|
|
|
$(this).closest('.has-many-{$this->column}-form').hide(); |
494
|
|
|
$(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1); |
495
|
|
|
return false; |
496
|
|
|
}); |
497
|
|
|
|
498
|
|
|
EOT; |
499
|
|
|
|
500
|
|
|
Admin::script($script); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Setup tab template script. |
505
|
|
|
* |
506
|
|
|
* @param string $templateScript |
507
|
|
|
* |
508
|
|
|
* @return void |
509
|
|
|
*/ |
510
|
|
View Code Duplication |
protected function setupScriptForTabView($templateScript) |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
$removeClass = NestedForm::REMOVE_FLAG_CLASS; |
513
|
|
|
$defaultKey = NestedForm::DEFAULT_KEY_NAME; |
514
|
|
|
|
515
|
|
|
$script = <<<EOT |
516
|
|
|
|
517
|
|
|
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){ |
518
|
|
|
var \$navTab = $(this).siblings('a'); |
519
|
|
|
var \$pane = $(\$navTab.attr('href')); |
520
|
|
|
if( \$pane.hasClass('new') ){ |
521
|
|
|
\$pane.remove(); |
522
|
|
|
}else{ |
523
|
|
|
\$pane.removeClass('active').find('.$removeClass').val(1); |
524
|
|
|
} |
525
|
|
|
if(\$navTab.closest('li').hasClass('active')){ |
526
|
|
|
\$navTab.closest('li').remove(); |
527
|
|
|
$('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show'); |
528
|
|
|
}else{ |
529
|
|
|
\$navTab.closest('li').remove(); |
530
|
|
|
} |
531
|
|
|
}); |
532
|
|
|
|
533
|
|
|
var index = 0; |
534
|
|
|
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){ |
535
|
|
|
index++; |
536
|
|
|
var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index); |
537
|
|
|
var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index); |
538
|
|
|
$('#has-many-{$this->column} > .nav').append(navTabHtml); |
539
|
|
|
$('#has-many-{$this->column} > .tab-content').append(paneHtml); |
540
|
|
|
$('#has-many-{$this->column} > .nav > li:last-child a').tab('show'); |
541
|
|
|
{$templateScript} |
542
|
|
|
}); |
543
|
|
|
|
544
|
|
|
if ($('.has-error').length) { |
545
|
|
|
$('.has-error').parent('.tab-pane').each(function () { |
546
|
|
|
var tabId = '#'+$(this).attr('id'); |
547
|
|
|
$('li a[href="'+tabId+'"] i').removeClass('hide'); |
548
|
|
|
}); |
549
|
|
|
|
550
|
|
|
var first = $('.has-error:first').parent().attr('id'); |
551
|
|
|
$('li a[href="#'+first+'"]').tab('show'); |
552
|
|
|
} |
553
|
|
|
EOT; |
554
|
|
|
|
555
|
|
|
Admin::script($script); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Setup default template script. |
560
|
|
|
* |
561
|
|
|
* @param string $templateScript |
562
|
|
|
* |
563
|
|
|
* @return void |
564
|
|
|
*/ |
565
|
|
View Code Duplication |
protected function setupScriptForTableView($templateScript) |
|
|
|
|
566
|
|
|
{ |
567
|
|
|
$removeClass = NestedForm::REMOVE_FLAG_CLASS; |
568
|
|
|
$defaultKey = NestedForm::DEFAULT_KEY_NAME; |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* When add a new sub form, replace all element key in new sub form. |
572
|
|
|
* |
573
|
|
|
* @example comments[new___key__][title] => comments[new_{index}][title] |
574
|
|
|
* |
575
|
|
|
* {count} is increment number of current sub form count. |
576
|
|
|
*/ |
577
|
|
|
$script = <<<EOT |
578
|
|
|
var index = 0; |
579
|
|
|
$('#has-many-{$this->column}').on('click', '.add', function () { |
580
|
|
|
|
581
|
|
|
var tpl = $('template.{$this->column}-tpl'); |
582
|
|
|
|
583
|
|
|
index++; |
584
|
|
|
|
585
|
|
|
var template = tpl.html().replace(/{$defaultKey}/g, index); |
586
|
|
|
$('.has-many-{$this->column}-forms').append(template); |
587
|
|
|
{$templateScript} |
588
|
|
|
return false; |
589
|
|
|
}); |
590
|
|
|
|
591
|
|
|
$('#has-many-{$this->column}').on('click', '.remove', function () { |
592
|
|
|
$(this).closest('.has-many-{$this->column}-form').hide(); |
593
|
|
|
$(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1); |
594
|
|
|
return false; |
595
|
|
|
}); |
596
|
|
|
|
597
|
|
|
EOT; |
598
|
|
|
|
599
|
|
|
Admin::script($script); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Disable create button. |
604
|
|
|
* |
605
|
|
|
* @return $this |
606
|
|
|
*/ |
607
|
|
|
public function disableCreate() |
608
|
|
|
{ |
609
|
|
|
$this->options['allowCreate'] = false; |
610
|
|
|
|
611
|
|
|
return $this; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Disable delete button. |
616
|
|
|
* |
617
|
|
|
* @return $this |
618
|
|
|
*/ |
619
|
|
|
public function disableDelete() |
620
|
|
|
{ |
621
|
|
|
$this->options['allowDelete'] = false; |
622
|
|
|
|
623
|
|
|
return $this; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Render the `HasMany` field. |
628
|
|
|
* |
629
|
|
|
* @throws \Exception |
630
|
|
|
* |
631
|
|
|
* @return \Illuminate\View\View |
632
|
|
|
*/ |
633
|
|
|
public function render() |
634
|
|
|
{ |
635
|
|
|
if ($this->viewMode == 'table') { |
636
|
|
|
return $this->renderTable(); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
// specify a view to render. |
640
|
|
|
$this->view = $this->views[$this->viewMode]; |
641
|
|
|
|
642
|
|
|
list($template, $script) = $this->buildNestedForm($this->column, $this->builder) |
|
|
|
|
643
|
|
|
->getTemplateHtmlAndScript(); |
644
|
|
|
|
645
|
|
|
$this->setupScript($script); |
646
|
|
|
|
647
|
|
|
return parent::render()->with([ |
|
|
|
|
648
|
|
|
'forms' => $this->buildRelatedForms(), |
649
|
|
|
'template' => $template, |
650
|
|
|
'relationName' => $this->relationName, |
651
|
|
|
'options' => $this->options, |
652
|
|
|
]); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Render the `HasMany` field for table style. |
657
|
|
|
* |
658
|
|
|
* @throws \Exception |
659
|
|
|
* |
660
|
|
|
* @return mixed |
661
|
|
|
*/ |
662
|
|
|
protected function renderTable() |
663
|
|
|
{ |
664
|
|
|
$headers = []; |
665
|
|
|
$fields = []; |
666
|
|
|
$hidden = []; |
667
|
|
|
$scripts = []; |
668
|
|
|
|
669
|
|
|
/* @var Field $field */ |
670
|
|
|
foreach ($this->buildNestedForm($this->column, $this->builder)->fields() as $field) { |
|
|
|
|
671
|
|
|
if (is_a($field, Hidden::class)) { |
672
|
|
|
$hidden[] = $field->render(); |
673
|
|
|
} else { |
674
|
|
|
/* Hide label and set field width 100% */ |
675
|
|
|
$field->setLabelClass(['hidden']); |
676
|
|
|
$field->setWidth(12, 0); |
677
|
|
|
$fields[] = $field->render(); |
678
|
|
|
$headers[] = $field->label(); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/* |
682
|
|
|
* Get and remove the last script of Admin::$script stack. |
683
|
|
|
*/ |
684
|
|
|
if ($field->getScript()) { |
685
|
|
|
$scripts[] = array_pop(Admin::$script); |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/* Build row elements */ |
690
|
|
|
$template = array_reduce($fields, function ($all, $field) { |
691
|
|
|
$all .= "<td>{$field}</td>"; |
692
|
|
|
|
693
|
|
|
return $all; |
694
|
|
|
}, ''); |
695
|
|
|
|
696
|
|
|
/* Build cell with hidden elements */ |
697
|
|
|
$template .= '<td class="hidden">'.implode('', $hidden).'</td>'; |
698
|
|
|
|
699
|
|
|
$this->setupScript(implode("\r\n", $scripts)); |
700
|
|
|
|
701
|
|
|
// specify a view to render. |
702
|
|
|
$this->view = $this->views[$this->viewMode]; |
703
|
|
|
|
704
|
|
|
return parent::render()->with([ |
|
|
|
|
705
|
|
|
'headers' => $headers, |
706
|
|
|
'forms' => $this->buildRelatedForms(), |
707
|
|
|
'template' => $template, |
708
|
|
|
'relationName' => $this->relationName, |
709
|
|
|
'options' => $this->options, |
710
|
|
|
]); |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
|
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.