|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Form\NestedForm; |
|
6
|
|
|
|
|
7
|
|
|
class Table extends HasMany |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $view = 'admin::form.hasmanytable'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Table constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $column |
|
18
|
|
|
* @param array $arguments |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($column, $arguments = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->column = $column; |
|
23
|
|
|
|
|
24
|
|
|
if (count($arguments) == 1) { |
|
25
|
|
|
$this->label = $this->formatLabel(); |
|
26
|
|
|
$this->builder = $arguments[0]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (count($arguments) == 2) { |
|
30
|
|
|
list($this->label, $this->builder) = $arguments; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function buildRelatedForms() |
|
38
|
|
|
{ |
|
39
|
|
|
if (is_null($this->form)) { |
|
40
|
|
|
return []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$forms = []; |
|
44
|
|
|
|
|
45
|
|
|
if ($values = old($this->column)) { |
|
|
|
|
|
|
46
|
|
|
foreach ($values as $key => $data) { |
|
47
|
|
|
if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data); |
|
52
|
|
|
} |
|
53
|
|
|
} else { |
|
54
|
|
|
foreach ($this->value as $key => $data) { |
|
55
|
|
|
$forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $forms; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function prepare($input) |
|
63
|
|
|
{ |
|
64
|
|
|
$form = $this->buildNestedForm($this->column, $this->builder); |
|
65
|
|
|
|
|
66
|
|
|
$prepare = $form->prepare($input); |
|
67
|
|
|
|
|
68
|
|
|
return collect($prepare)->reject(function ($item) { |
|
69
|
|
|
return $item[NestedForm::REMOVE_FLAG_NAME] == 1; |
|
70
|
|
|
})->map(function ($item) { |
|
71
|
|
|
unset($item[NestedForm::REMOVE_FLAG_NAME]); |
|
72
|
|
|
return $item; |
|
73
|
|
|
})->toArray(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function getKeyName() |
|
77
|
|
|
{ |
|
78
|
|
|
if (is_null($this->form)) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return 'id'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
protected function buildNestedForm($column, \Closure $builder, $key = null) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$form = new NestedForm($column); |
|
88
|
|
|
|
|
89
|
|
|
$form->setForm($this->form) |
|
90
|
|
|
->setKey($key); |
|
91
|
|
|
|
|
92
|
|
|
call_user_func($builder, $form); |
|
93
|
|
|
|
|
94
|
|
|
$form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS); |
|
95
|
|
|
|
|
96
|
|
|
return $form; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function render() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->renderTable(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.