-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTabular.php
129 lines (95 loc) · 3.16 KB
/
Tabular.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Laravolt\SemanticForm\Elements;
use DeepCopy\DeepCopy;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class Tabular extends Element
{
protected $schema = [];
protected $labels = [];
protected $rows = 3;
protected $allowAddition = false;
protected $allowRemoval = false;
protected $name;
protected $source = [];
public function __construct($name, $schema)
{
$this->name = $name;
$this->schema = collect($schema)->transform(function ($item) {
// produce something like "person[data][$index][name]", to make it easier to handling by Laravel Request
if (Str::contains($item['name'], '[]')) {
$name = Str::before($item['name'], '[]');
$item['name'] = "{$this->name}[%s][{$name}][]";
} else {
$item['name'] = "{$this->name}[%s][{$item['name']}]";
}
return $item;
})->toArray();
}
public function rows(int $rows)
{
$this->rows = $rows;
return $this;
}
public function allowAddition(bool $flag)
{
$this->allowAddition = $flag;
return $this;
}
public function allowRemoval(bool $flag)
{
$this->allowRemoval = $flag;
return $this;
}
public function source($source)
{
$this->source = $source;
return $this;
}
public function displayValue()
{
$data = $this->source;
$headers = collect(Arr::first($data))->keys();
return view('semantic-form::tabular.table', compact('data', 'headers'));
}
public function render()
{
if ($this->label) {
$copier = new DeepCopy();
$element = $copier->copy($this);
$element->label = false;
return $this->decorateField(new Field($this->label, $element))->addClass($this->fieldWidth)->render();
}
$this->beforeRender();
$rowCount = old($this->name) ? count(old($this->name)) : $this->rows;
$fields = collect(form()->make($this->schema)->all())
->transform(function ($item) {
$this->labels[] = (string) $item->label;
$item->label(null);
return $item;
});
// reset old values row index
$data = [$this->name => array_values(old($this->name, $this->source))];
$rows = [];
for ($i = 0; $i < $rowCount; $i++) {
$rows[] = $fields->map(function ($field) use ($data, $i) {
$copier = new DeepCopy();
$newField = $copier->copy($field);
$newField->bindAttribute('name', $i);
$newField->populateValue($data);
return $newField;
});
}
$payload = [
'fields' => $fields,
'name' => $this->name,
'rows' => $rows,
'labels' => $this->labels,
'limit' => $rowCount,
'allowAddition' => $this->allowAddition,
'allowRemoval' => $this->allowRemoval,
];
return view('semantic-form::tabular.form', $payload)->render();
}
}