1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class FieldGroup |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The form that holds all the added fields. |
11
|
|
|
* |
12
|
|
|
* @var Form |
13
|
|
|
*/ |
14
|
|
|
protected $form; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The parent form. |
18
|
|
|
* |
19
|
|
|
* @var Form |
20
|
|
|
*/ |
21
|
|
|
protected $parent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FormHelper |
25
|
|
|
*/ |
26
|
|
|
protected $formHelper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* All options for the group. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $options = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The template name to be used. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $template; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* FieldGroup constructor. |
44
|
|
|
* @param Form $parent |
45
|
|
|
* @param array $options |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(Form $parent, array $options = []) |
48
|
|
|
{ |
49
|
1 |
|
$this->parent = $parent; |
50
|
1 |
|
$this->formHelper = $parent->getFormHelper(); |
51
|
1 |
|
$this->options = $this->formHelper->mergeOptions($this->getDefaults(), $options); |
52
|
1 |
|
$this->form = (clone $parent)->removeAll(); |
53
|
1 |
|
$this->template = $parent->getConfig($this->getTemplate(), $this->getTemplate()); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the template, can be config variable or view path. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
protected function getTemplate() |
62
|
|
|
{ |
63
|
1 |
|
return 'group'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Default options for field. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
protected function getDefaults() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
1 |
|
'label' => null, |
75
|
1 |
|
'label_attr' => ['class' => $this->parent->getConfig('defaults.label_class')], |
76
|
1 |
|
'wrapper' => ['class' => $this->parent->getConfig('defaults.group.wrapper_class') ?? $this->parent->getConfig('defaults.wrapper_class')], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Fields\FormField[] |
82
|
|
|
*/ |
83
|
1 |
|
public function getFields() |
84
|
|
|
{ |
85
|
1 |
|
return array_intersect_key($this->parent->getFields(), $this->form->getFields()); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function hasField(string $fieldName) |
89
|
|
|
{ |
90
|
1 |
|
return array_key_exists($fieldName, $this->getFields()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $name |
95
|
|
|
* @param string $type |
96
|
|
|
* @param array $options |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
1 |
|
public function add($name, $type = 'text', array $options = []) |
100
|
|
|
{ |
101
|
1 |
|
$this->form->add($name, $type, ['fieldGroup' => $this] + $options); |
102
|
|
|
|
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \Closure $callback |
108
|
|
|
* @param FieldGroup|null $fieldGroup |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
1 |
|
public function group(\Closure $callback, FieldGroup $fieldGroup = null) |
112
|
|
|
{ |
113
|
1 |
|
$this->form->group($callback, $fieldGroup, $this->parent); |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Render the field. |
120
|
|
|
* |
121
|
|
|
* @param array $options |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function render(array $options = []) |
125
|
|
|
{ |
126
|
1 |
|
$data = $this->getRenderData(); |
127
|
|
|
|
128
|
1 |
|
return $this->formHelper->getView()->make( |
129
|
1 |
|
$this->getViewTemplate(), |
130
|
|
|
$data + [ |
131
|
1 |
|
'group' => $this, |
132
|
1 |
|
'options' => $this->formHelper->mergeOptions($this->options, $options), |
133
|
1 |
|
'fields' => static::buildFieldsForRendering($this), |
134
|
|
|
] |
135
|
1 |
|
)->render(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return the extra render data for this form field, passed into the field's template directly. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
1 |
|
protected function getRenderData() |
144
|
|
|
{ |
145
|
1 |
|
return []; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
1 |
|
protected function getViewTemplate() |
152
|
|
|
{ |
153
|
1 |
|
return $this->parent->getTemplatePrefix() . $this->getOption('template', $this->template); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get single option from options array. Can be used with dot notation ('attr.class'). |
158
|
|
|
* |
159
|
|
|
* @param string $option |
160
|
|
|
* @param mixed|null $default |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
1 |
|
public function getOption($option, $default = null) |
164
|
|
|
{ |
165
|
1 |
|
return Arr::get($this->options, $option, $default); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return Form |
170
|
|
|
*/ |
171
|
|
|
public function getParent() |
172
|
|
|
{ |
173
|
|
|
return $this->parent; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return Form |
178
|
|
|
*/ |
179
|
1 |
|
public function getForm() |
180
|
|
|
{ |
181
|
1 |
|
return $this->form; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param Form|FieldGroup $fieldsContainer |
186
|
|
|
* @return array (Fields\FormField|FieldGroup)[] |
187
|
|
|
*/ |
188
|
8 |
|
public static function buildFieldsForRendering($fieldsContainer) |
189
|
|
|
{ |
190
|
8 |
|
$groupId = 0; |
191
|
8 |
|
$prevFieldGroup = null; |
192
|
8 |
|
$fieldsForRendering = []; |
193
|
|
|
|
194
|
8 |
|
foreach ($fieldsContainer->getFields() as $field) { |
195
|
6 |
|
$fieldGroup = $field->getOption('fieldGroup'); |
196
|
|
|
|
197
|
6 |
|
if ($fieldGroup === $fieldsContainer) { |
198
|
1 |
|
$fieldsForRendering[$field->getRealName()] = $field; |
199
|
1 |
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// init the $prevFieldGroup |
203
|
6 |
|
if ($fieldGroup && !$prevFieldGroup) { |
204
|
1 |
|
$prevFieldGroup = $fieldGroup; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// filter out sub-group |
208
|
6 |
|
if ($prevFieldGroup && $prevFieldGroup !== $fieldGroup && $prevFieldGroup->hasField($field->getRealName())) { |
209
|
1 |
|
continue; |
210
|
|
|
} |
211
|
|
|
|
212
|
6 |
|
if ($fieldGroup && $prevFieldGroup !== $fieldGroup) { |
213
|
1 |
|
$groupId++; |
214
|
|
|
} |
215
|
|
|
|
216
|
6 |
|
if ($fieldGroup) { |
217
|
1 |
|
$prevFieldGroup = $fieldGroup; |
218
|
1 |
|
$fieldsForRendering['group_' . $groupId] = $fieldGroup; |
219
|
1 |
|
continue; |
220
|
|
|
} |
221
|
|
|
|
222
|
6 |
|
$fieldsForRendering[$field->getRealName()] = $field; |
223
|
|
|
} |
224
|
|
|
|
225
|
8 |
|
return $fieldsForRendering; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |