-
Notifications
You must be signed in to change notification settings - Fork 554
/
Form.php
120 lines (91 loc) · 3.03 KB
/
Form.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
<?php
namespace A17\Twill\Services\Forms;
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
class Form extends Collection implements CanHaveSubfields
{
use HasSubFields;
public ?Fieldsets $fieldsets = null;
private ?Form $sideForm = null;
private bool $isCreate = false;
private bool $forBlocks = false;
public function withFieldSets(Fieldsets $fieldsets): static
{
$this->fieldsets = $fieldsets;
return $this;
}
public function addFieldset(Fieldset $fieldset): static
{
if (! $this->fieldsets) {
$this->fieldsets = Fieldsets::make();
}
$this->fieldsets->add($fieldset);
return $this;
}
public function toFrontend(?Form $sideFieldSets = null, bool $isCreate = false): static
{
$this->sideForm = $sideFieldSets;
$this->isCreate = $isCreate;
return $this;
}
public function getAdditionalFieldsets(): array
{
if (! $this->fieldsets) {
return [];
}
return $this->fieldsets->map(fn($fieldset): array => [
'fieldset' => $fieldset->id,
'label' => $fieldset->title,
])->toArray();
}
public function hasFieldsInBaseFieldset(): bool
{
return ! $this->isEmpty();
}
public function formToRenderArray(): array
{
$viewWithData = ['isCreate' => $this->isCreate];
if ($this->fieldsets) {
$viewWithData['renderFieldsets'] = $this->fieldsets;
$viewWithData['additionalFieldsets'] = $this->fieldsets->map(fn($fieldset): array => [
'fieldset' => $fieldset->id,
'label' => $fieldset->title,
])->toArray();
}
$viewWithData['disableContentFieldset'] = ! $this->hasFieldsInBaseFieldset();
$viewWithData['renderFields'] = $this;
return $viewWithData;
}
public function forBlocks(): bool
{
return $this->forBlocks;
}
public function renderForBlocks(bool $renderForBlocks = true): static
{
$this->forBlocks = $renderForBlocks;
return $this;
}
public function hasForm(): bool
{
return $this->isNotEmpty() || ($this->fieldsets && $this->fieldsets->isNotEmpty());
}
public function hasSideForm(): bool
{
return $this->sideForm && ($this->sideForm->isNotEmpty() || ($this->sideForm->fieldsets && $this->sideForm->fieldsets->isNotEmpty()));
}
public function renderBaseForm(): View
{
return view('twill::partials.form.renderer.base_form', $this->formToRenderArray());
}
public function renderSideForm(): View
{
return view('twill::partials.form.renderer.base_form', $this->sideForm->formToRenderArray());
}
public function registerDynamicRepeaters(): void
{
$this->registerDynamicRepeatersFor($this);
$this->fieldsets?->registerDynamicRepeaters();
}
}