-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseForm.php
More file actions
345 lines (293 loc) · 8.39 KB
/
Copy pathBaseForm.php
File metadata and controls
345 lines (293 loc) · 8.39 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace ADT\Forms;
use Exception;
use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\SubmitterControl;
use Nette\Utils\ArrayHash;
use Nette\Utils\Callback;
use Nette\Utils\Type;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
/**
* @method onBeforeInitForm($form)
* @method onAfterInitForm($form)
* @method onBeforeValidateForm($form)
* @method onBeforeProcessForm($form)
* @method onSuccess($form)
*/
abstract class BaseForm extends Control
{
protected Form $form;
protected bool $isAjax = true;
protected bool $emptyHiddenToggleControls = true;
/** @var callable[] */
protected array $paramResolvers = [];
/**
* @internal
* @var callable[]
*/
public array $onBeforeInitForm = [];
/**
* @internal
* @var callable[]
*/
public array $onAfterInitForm = [];
/**
* @internal
* @var callable[]
*/
public array $onBeforeValidateForm = [];
/**
* @internal
* @var callable[]
*/
public array $onBeforeProcessForm = [];
/**
* @internal
* @var callable[]
*/
public array $onSuccess = [];
public function __construct()
{
$this->paramResolvers[] = function(string $type, object|array|null $values) {
if ($type === Form::class || is_subclass_of($type, Form::class)) {
return $this->form;
} elseif ($type === Presenter::class || is_subclass_of($type, Presenter::class)) {
return $this->presenter;
} elseif ($values) {
if ($type === ArrayHash::class) {
return $values;
} elseif ($type === 'array') {
return $this->convertArrayHashToArray($values);
}
}
return false;
};
$this->monitor(Presenter::class, function() {
$form = $this->form = $this['form'];
/** @link BaseForm::validateFormCallback() */
$form->onValidate[] = [$this, 'validateFormCallback'];
/** @link BaseForm::processFormCallback() */
$form->onSuccess[] = [$this, 'processFormCallback'];
foreach ($this->onBeforeInitForm as $_callback) {
$_callback($form);
}
if (method_exists($this, 'onBeforeInitForm')) {
$this->onBeforeInitForm($form);
}
if (!method_exists($this, 'initForm')) {
throw new Exception('Please define the "initForm($form)" method.');
}
$this->invokeHandler([$this, 'initForm']);
foreach ($this->onAfterInitForm as $_callback) {
$_callback($form);
}
if (method_exists($this, 'onAfterInitForm')) {
$this->onAfterInitForm($form);
}
if ($form->isSubmitted()) {
if (is_bool($form->isSubmitted()) || $form->isSubmitted()->isDisabled()) {
$form->addError('The form must be submitted using the specific submit button.');
}
elseif ($form->isSubmitted()->getValidationScope() !== null) {
$form->onValidate = [];
}
}
});
}
/**
* @throws ReflectionException
*/
final public function validateFormCallback(Form $form): void
{
// pridal jsem kvuli sobit pokladne, kde jsme meli ajax select a kde nam to na
// $validItems = $this->getAjaxEntity()->formatValues($this->getAjaxEntity()->hydrateValues($validValues, $this->getForm()->getValues('array')));
// hazelo
// Nette\Forms\Container::getValues() invoked but the form is not valid (form 'form')
// primarni problem je ten, ze $form->getUntrustedValues() vraci hodnoty jen z inputu
// ktere uz jsou vykreslene a tudiz tam jde treba jen pulka hodnot
// aby nemusely byt submit buttony definovane pred tim ajax selectem, tak jeste pridavame
// !$form->isSubmitted() instanceof SubmitterControl::class
if (!$form->isSubmitted() instanceof SubmitterControl || $form->isSubmitted()->getValidationScope() !== null) {
return;
}
$this->onBeforeValidateForm($form);
if ($form->isValid() && method_exists($this, 'validateForm')) {
$this->invokeHandler([$this, 'validateForm'], $form->getUntrustedValues());
}
}
/**
* @throws Exception
*/
final public function processFormCallback(Form $form): void
{
if ($form->isSubmitted()->getValidationScope() !== null) {
return;
}
$this->processToggles($form, emptyValue: true);
$this->onBeforeProcessForm($form);
if ($form->isValid()) {
if (method_exists($this, 'processForm')) {
$this->invokeHandler([$this, 'processForm'], $form->getValues());
}
if ($form->isValid()) {
foreach ($this->onSuccess as $_handler) {
$this->invokeHandler($_handler, $form->getValues());
}
}
}
}
/**
* @throws ReflectionException
*/
public function render(): void
{
if ($this->isAjax) {
$this->form->getElementPrototype()->class[] = 'ajax';
}
if (method_exists($this, 'renderForm')) {
$this->invokeHandler([$this, 'renderForm']);
}
$this->getTemplate()->templateFile = $this->getTemplateFile();
$this->getTemplate()->setFile(static::getDefaultTemplateFile());
$this->getTemplate()->render();
}
protected function createComponentForm()
{
return new Form();
}
protected function _()
{
return call_user_func_array([$this->form->getTranslator(), 'translate'], func_get_args());
}
public function setOnBeforeInitForm(callable $onBeforeInitForm): static
{
$this->onBeforeInitForm[] = $onBeforeInitForm;
return $this;
}
public function setOnAfterInitForm(callable $onAfterInitForm): static
{
$this->onAfterInitForm[] = $onAfterInitForm;
return $this;
}
public function setOnBeforeValidateForm(callable $onBeforeValidateForm): static
{
$this->onBeforeValidateForm[] = $onBeforeValidateForm;
return $this;
}
public function setOnBeforeProcessForm(callable $onBeforeProcessForm): static
{
$this->onBeforeProcessForm[] = $onBeforeProcessForm;
return $this;
}
public function setOnSuccess(callable $onSuccess): static
{
$this->onSuccess[] = $onSuccess;
return $this;
}
/**
* @throws ReflectionException
* @throws Exception
*/
protected function invokeHandler(callable $handler, object|array|null $formValues = null)
{
$types = array_map(function(ReflectionParameter $param) {
return Type::resolve($param->getType()->getName(), $param);
}, Callback::toReflection($handler)->getParameters());
$params = [];
foreach ($types as $_type) {
if (empty($_type)) {
throw new Exception('All parameter types must be specified.');
}
$param = null;
foreach ($this->paramResolvers as $_paramResolver) {
if (($param = $_paramResolver($_type, $formValues)) !== false) {
$params[] = $param;
break;
}
}
if ($param === false) {
throw new Exception('No resolver found for type ' . $_type . '.');
}
}
return $handler(...$params);
}
/**
* @throws Exception
*/
protected function processToggles(Form $form, bool $emptyValue): void
{
if ($this->emptyHiddenToggleControls) {
$toggles = $form->getToggles();
foreach ($this->getSections() as $_section) {
if (isset($toggles[$_section->getHtmlId()]) && $toggles[$_section->getHtmlId()] === false) {
foreach ($_section->getControls() as $_control) {
if (!$_control instanceof BaseControl) {
continue;
}
$_control->setOption('hidden', true);
if ($emptyValue) {
if (method_exists($_control, 'setNullable')) {
$_control->setNullable();
}
$_control->setValue(null);
}
}
}
}
}
}
/**
* @return Section[]
*/
protected function getSections(): array
{
$sections = [];
foreach ($this->form->getSections() as $_section) {
$sections[] = $_section;
}
foreach ($this->form->getComponentTree() as $_component) {
if (!$_component instanceof StaticContainer) {
continue;
}
foreach ($_component->getSections() as $_section) {
$sections[] = $_section;
}
}
return $sections;
}
protected function getTemplateFile(): ?string
{
$reflectionClass = new ReflectionClass($this);
$templateName = $reflectionClass->getShortName() .'.latte';
$templateFile = dirname($reflectionClass->getFileName()) . '/' . $templateName;
if (file_exists($templateFile)) {
return $templateFile;
}
return null;
}
protected function convertArrayHashToArray($data)
{
if ($data instanceof ArrayHash) {
$data = (array) $data;
}
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = $this->convertArrayHashToArray($value);
}
}
return $data;
}
public static function getDefaultTemplateFile(): string
{
return __DIR__ . '/BaseForm.latte';
}
public function redrawControl(?string $snippet = null, bool $redraw = true): void
{
parent::redrawControl('formArea');
parent::redrawControl($snippet, $redraw);
}
}