1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form; |
4
|
|
|
|
5
|
|
|
use Nip\Form\Traits\MagicMethodElementsFormTrait; |
6
|
|
|
use Nip\View; |
7
|
|
|
use Nip_Form_Button_Abstract as ButtonAbstract; |
8
|
|
|
use Nip_Form_DisplayGroup; |
9
|
|
|
use Nip_Form_Element_Abstract as ElementAbstract; |
10
|
|
|
use Nip_Form_Renderer_Abstract as AbstractRenderer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractForm |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractForm |
17
|
|
|
{ |
18
|
|
|
use MagicMethodElementsFormTrait; |
19
|
|
|
|
20
|
|
|
const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
21
|
|
|
const ENCTYPE_MULTIPART = 'multipart/form-data'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $methods = ['delete', 'get', 'post', 'put']; |
27
|
|
|
|
28
|
|
|
protected $_attribs = []; |
29
|
|
|
protected $_options = []; |
30
|
|
|
protected $_displayGroups = []; |
31
|
|
|
|
32
|
|
|
protected $_elements = []; |
33
|
|
|
protected $_elementsLabel; |
34
|
|
|
protected $_elementsOrder = []; |
35
|
|
|
|
36
|
|
|
protected $_buttons; |
37
|
|
|
|
38
|
|
|
protected $_decorators = []; |
39
|
|
|
protected $_renderer; |
40
|
|
|
protected $_messages = [ |
41
|
|
|
'error' => [], |
42
|
|
|
]; |
43
|
|
|
protected $_messageTemplates = []; |
44
|
|
|
protected $_cache; |
45
|
|
|
|
46
|
|
|
protected $__controllerView = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* AbstractForm constructor. |
50
|
|
|
*/ |
51
|
1 |
|
public function __construct() |
52
|
|
|
{ |
53
|
1 |
|
$this->init(); |
54
|
1 |
|
$this->postInit(); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function init() |
58
|
|
|
{ |
59
|
1 |
|
$this->setAction(current_url()); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $action |
64
|
|
|
* @return AbstractForm |
65
|
|
|
*/ |
66
|
1 |
|
public function setAction($action) |
67
|
|
|
{ |
68
|
1 |
|
return $this->setAttrib('action', (string)$action); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $key |
73
|
|
|
* @param $value |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
1 |
|
public function setAttrib($key, $value) |
77
|
|
|
{ |
78
|
1 |
|
$key = (string)$key; |
79
|
1 |
|
$this->_attribs[$key] = $value; |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function postInit() |
85
|
|
|
{ |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $name |
90
|
|
|
* @param bool $label |
91
|
|
|
* @param string $type |
92
|
|
|
* @param bool $isRequired |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
1 |
View Code Duplication |
public function add($name, $label = false, $type = 'input', $isRequired = false) |
|
|
|
|
96
|
|
|
{ |
97
|
1 |
|
$label = ($label) ? $label : ucfirst($name); |
98
|
1 |
|
$element = $this->getNewElement($type) |
99
|
1 |
|
->setName($name) |
100
|
1 |
|
->setLabel($label) |
101
|
1 |
|
->setRequired($isRequired); |
102
|
1 |
|
$this->addElement($element); |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $type |
109
|
|
|
* @return ElementAbstract |
110
|
|
|
*/ |
111
|
1 |
|
public function getNewElement($type) |
112
|
|
|
{ |
113
|
1 |
|
$className = $this->getElementClassName($type); |
114
|
|
|
|
115
|
1 |
|
return $this->getNewElementByClass($className); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $type |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
public function getElementClassName($type) |
123
|
|
|
{ |
124
|
1 |
|
return 'Nip_Form_Element_' . ucfirst($type); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $className |
129
|
|
|
* @return ElementAbstract |
130
|
|
|
*/ |
131
|
1 |
|
public function getNewElementByClass($className) |
132
|
|
|
{ |
133
|
1 |
|
$element = new $className($this); |
134
|
|
|
|
135
|
1 |
|
return $this->initNewElement($element); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param ElementAbstract $element |
140
|
|
|
* @return ElementAbstract |
141
|
|
|
*/ |
142
|
1 |
|
public function initNewElement($element) |
143
|
|
|
{ |
144
|
1 |
|
$element->setForm($this); |
145
|
|
|
|
146
|
1 |
|
return $element; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param ElementAbstract $element |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
1 |
|
public function addElement(ElementAbstract $element) |
154
|
|
|
{ |
155
|
1 |
|
$name = $element->getUniqueId(); |
156
|
1 |
|
$this->_elements[$name] = $element; |
157
|
1 |
|
$this->_elementsLabel[$element->getLabel()] = $name; |
158
|
1 |
|
$this->_elementsOrder[] = $name; |
159
|
|
|
|
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $name |
165
|
|
|
* @return ElementAbstract|null |
166
|
|
|
*/ |
167
|
1 |
|
public function __get($name) |
168
|
|
|
{ |
169
|
1 |
|
$element = $this->getElement($name); |
170
|
1 |
|
if ($element) { |
171
|
1 |
|
return $element; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $name |
179
|
|
|
* @return ElementAbstract |
180
|
|
|
*/ |
181
|
1 |
|
public function getElement($name) |
182
|
|
|
{ |
183
|
1 |
|
if (array_key_exists($name, $this->_elements)) { |
184
|
1 |
|
return $this->_elements[$name]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return null; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param $className |
192
|
|
|
* @param $name |
193
|
|
|
* @param bool $label |
194
|
|
|
* @param bool $isRequired |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function addCustom($className, $name, $label = false, $isRequired = false) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$label = ($label) ? $label : ucfirst($name); |
200
|
|
|
$element = $this->getNewElementByClass($className) |
201
|
|
|
->setName($name) |
202
|
|
|
->setLabel($label) |
203
|
|
|
->setRequired($isRequired); |
204
|
|
|
$this->addElement($element); |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $name |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
|
|
public function removeElement($name) |
214
|
|
|
{ |
215
|
|
|
unset($this->_elements[$name]); |
216
|
|
|
|
217
|
|
|
$key = array_search($name, $this->_elementsOrder); |
218
|
|
|
if ($key) { |
219
|
|
|
unset($this->_elementsOrder[$key]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Add a display group |
227
|
|
|
* Groups named elements for display purposes. |
228
|
|
|
* @param array $elements |
229
|
|
|
* @param $name |
230
|
|
|
* @return $this |
231
|
|
|
*/ |
232
|
|
|
public function addDisplayGroup(array $elements, $name) |
233
|
|
|
{ |
234
|
|
|
$group = $this->newDisplayGroup(); |
235
|
|
|
foreach ($elements as $element) { |
236
|
|
|
if (isset($this->_elements[$element])) { |
237
|
|
|
$add = $this->getElement($element); |
238
|
|
|
if (null !== $add) { |
239
|
|
|
$group->addElement($add); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
if (empty($group)) { |
244
|
|
|
trigger_error('No valid elements specified for display group'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$name = (string)$name; |
248
|
|
|
$group->setLegend($name); |
249
|
|
|
|
250
|
|
|
$this->_displayGroups[$name] = $group; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return Nip_Form_DisplayGroup |
257
|
|
|
*/ |
258
|
|
|
public function newDisplayGroup() |
259
|
|
|
{ |
260
|
|
|
$group = new Nip_Form_DisplayGroup(); |
261
|
|
|
$group->setForm($this); |
262
|
|
|
|
263
|
|
|
return $group; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return Nip_Form_DisplayGroup |
268
|
|
|
*/ |
269
|
|
|
public function getDisplayGroup($name) |
270
|
|
|
{ |
271
|
|
|
if (array_key_exists($name, $this->_displayGroups)) { |
272
|
|
|
return $this->_displayGroups[$name]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return null; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
public function getDisplayGroups() |
282
|
|
|
{ |
283
|
|
|
return $this->_displayGroups; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param $name |
288
|
|
|
* @param bool $label |
289
|
|
|
* @param string $type |
290
|
|
|
* @return $this |
291
|
|
|
*/ |
292
|
|
|
public function addButton($name, $label = false, $type = 'button') |
293
|
|
|
{ |
294
|
|
|
$this->_buttons[$name] = $this->newButton($name, $label, $type); |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param $name |
301
|
|
|
* @param bool $label |
302
|
|
|
* @param string $type |
303
|
|
|
* @return mixed |
304
|
|
|
*/ |
305
|
|
|
protected function newButton($name, $label = false, $type = 'button') |
306
|
|
|
{ |
307
|
|
|
$class = 'Nip_Form_Button_' . ucfirst($type); |
308
|
|
|
/** @var ButtonAbstract $button */ |
309
|
|
|
$button = new $class($this); |
310
|
|
|
$button->setName($name) |
311
|
|
|
->setLabel($label); |
312
|
|
|
return $button; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param $name |
317
|
|
|
* @return ElementAbstract |
318
|
|
|
*/ |
319
|
|
|
public function getButton($name) |
320
|
|
|
{ |
321
|
|
|
if (array_key_exists($name, $this->_buttons)) { |
322
|
|
|
return $this->_buttons[$name]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return null; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param $name |
330
|
|
|
* @return bool |
331
|
|
|
*/ |
332
|
|
|
public function hasElement($name) |
333
|
|
|
{ |
334
|
|
|
return array_key_exists($name, $this->_elements); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param $label |
339
|
|
|
* @return ElementAbstract |
340
|
|
|
*/ |
341
|
|
|
public function getElementByLabel($label) |
342
|
|
|
{ |
343
|
|
|
if (array_key_exists($label, $this->_elementsLabel)) { |
344
|
|
|
return $this->_elements[$this->_elementsLabel[$label]]; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return null; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param $element |
352
|
|
|
* @param $neighbour |
353
|
|
|
* @param string $type |
354
|
|
|
* @return $this |
355
|
|
|
*/ |
356
|
|
|
public function setElementOrder($element, $neighbour, $type = 'bellow') |
357
|
|
|
{ |
358
|
|
|
if (in_array($element, $this->_elementsOrder) && in_array($neighbour, $this->_elementsOrder)) { |
359
|
|
|
$newOrder = []; |
360
|
|
|
foreach ($this->_elementsOrder as $current) { |
361
|
|
|
if ($current == $element) { |
|
|
|
|
362
|
|
|
} elseif ($current == $neighbour) { |
363
|
|
|
if ($type == 'above') { |
364
|
|
|
$newOrder[] = $element; |
365
|
|
|
$newOrder[] = $neighbour; |
366
|
|
|
} else { |
367
|
|
|
$newOrder[] = $neighbour; |
368
|
|
|
$newOrder[] = $element; |
369
|
|
|
} |
370
|
|
|
} else { |
371
|
|
|
$newOrder[] = $current; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
$this->_elementsOrder = $newOrder; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function getButtons() |
381
|
|
|
{ |
382
|
|
|
return $this->_buttons; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @param bool $params |
387
|
|
|
* @return array |
388
|
|
|
*/ |
389
|
|
|
public function findElements($params = false) |
390
|
|
|
{ |
391
|
|
|
$elements = []; |
392
|
|
|
foreach ($this->_elements as $element) { |
393
|
|
|
if (isset($params['type'])) { |
394
|
|
|
if ($element->getType() != $params['type']) { |
395
|
|
|
continue; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
if (isset($params['attribs']) && is_array($params['attribs'])) { |
399
|
|
|
foreach ($params['attribs'] as $name => $value) { |
400
|
|
|
if ($element->getAttrib($name) != $value) { |
401
|
|
|
continue(2); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
$elements[$element->getUniqueId()] = $element; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return $elements; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param $key |
413
|
|
|
* @param $value |
414
|
|
|
* @return $this |
415
|
|
|
*/ |
416
|
|
|
public function setOption($key, $value) |
417
|
|
|
{ |
418
|
|
|
$key = (string)$key; |
419
|
|
|
$this->_options[$key] = $value; |
420
|
|
|
|
421
|
|
|
return $this; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @param $key |
426
|
|
|
* @return mixed|null |
427
|
|
|
*/ |
428
|
|
View Code Duplication |
public function getOption($key) |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
$key = (string)$key; |
431
|
|
|
if (!isset($this->_options[$key])) { |
432
|
|
|
return null; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
return $this->_options[$key]; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @return $this |
440
|
|
|
*/ |
441
|
|
View Code Duplication |
public function addClass() |
|
|
|
|
442
|
|
|
{ |
443
|
|
|
$classes = func_get_args(); |
444
|
|
|
if (is_array($classes)) { |
445
|
|
|
$oldClasses = explode(' ', $this->getAttrib('class')); |
446
|
|
|
$classes = array_merge($classes, $oldClasses); |
447
|
|
|
$this->setAttrib('class', implode(' ', $classes)); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
return $this; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @param $key |
455
|
|
|
* @return mixed|null |
456
|
|
|
*/ |
457
|
|
View Code Duplication |
public function getAttrib($key) |
|
|
|
|
458
|
|
|
{ |
459
|
|
|
$key = (string)$key; |
460
|
|
|
if (!isset($this->_attribs[$key])) { |
461
|
|
|
return null; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
return $this->_attribs[$key]; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @return $this |
469
|
|
|
*/ |
470
|
|
View Code Duplication |
public function removeClass() |
|
|
|
|
471
|
|
|
{ |
472
|
|
|
$removeClasses = func_get_args(); |
473
|
|
|
if (is_array($removeClasses)) { |
474
|
|
|
$classes = explode(' ', $this->getAttrib('class')); |
475
|
|
|
foreach ($removeClasses as $class) { |
476
|
|
|
$key = array_search($class, $classes); |
477
|
|
|
if ($key !== false) { |
478
|
|
|
unset($classes[$key]); |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
$this->setAttrib('class', implode(' ', $classes)); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
return $this; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* @param $class |
489
|
|
|
* @return bool |
490
|
|
|
*/ |
491
|
|
|
public function hasClass($class) |
492
|
|
|
{ |
493
|
|
|
return in_array($class, explode(' ', $this->getAttrib('class'))); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @return array |
498
|
|
|
*/ |
499
|
|
|
public function getAttribs() |
500
|
|
|
{ |
501
|
|
|
return $this->_attribs; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @param array $attribs |
506
|
|
|
* @return $this |
507
|
|
|
*/ |
508
|
|
|
public function setAttribs(array $attribs) |
509
|
|
|
{ |
510
|
|
|
$this->clearAttribs(); |
511
|
|
|
|
512
|
|
|
return $this->addAttribs($attribs); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* @return $this |
517
|
|
|
*/ |
518
|
|
|
public function clearAttribs() |
519
|
|
|
{ |
520
|
|
|
$this->_attribs = []; |
521
|
|
|
|
522
|
|
|
return $this; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* @param array $attribs |
527
|
|
|
* @return $this |
528
|
|
|
*/ |
529
|
|
|
public function addAttribs(array $attribs) |
530
|
|
|
{ |
531
|
|
|
foreach ($attribs as $key => $value) { |
532
|
|
|
$this->setAttrib($key, $value); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
return $this; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* @param $key |
540
|
|
|
* @return bool |
541
|
|
|
*/ |
542
|
|
View Code Duplication |
public function removeAttrib($key) |
|
|
|
|
543
|
|
|
{ |
544
|
|
|
if (isset($this->_attribs[$key])) { |
545
|
|
|
unset($this->_attribs[$key]); |
546
|
|
|
|
547
|
|
|
return true; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
return false; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @param $method |
555
|
|
|
* @return AbstractForm |
556
|
|
|
*/ |
557
|
|
|
public function setMethod($method) |
558
|
|
|
{ |
559
|
|
|
if (in_array($method, $this->methods)) { |
560
|
|
|
return $this->setAttrib('method', $method); |
561
|
|
|
} |
562
|
|
|
trigger_error('Method is not valid', E_USER_ERROR); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @return bool |
567
|
|
|
*/ |
568
|
|
|
public function execute() |
569
|
|
|
{ |
570
|
|
|
if ($this->submited()) { |
571
|
|
|
return $this->processRequest(); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
return false; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* @return bool |
579
|
|
|
*/ |
580
|
|
|
public function submited() |
|
|
|
|
581
|
|
|
{ |
582
|
|
|
$request = $this->getAttrib('method') == 'post' ? $_POST : $_GET; |
583
|
|
|
if (count($request)) { |
584
|
|
|
return true; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
return false; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* @return bool |
592
|
|
|
*/ |
593
|
|
|
public function processRequest() |
594
|
|
|
{ |
595
|
|
|
if ($this->validate()) { |
596
|
|
|
$this->process(); |
597
|
|
|
|
598
|
|
|
return true; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
return false; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* @return bool |
606
|
|
|
*/ |
607
|
|
|
public function validate() |
|
|
|
|
608
|
|
|
{ |
609
|
|
|
$request = $this->getAttrib('method') == 'post' ? $_POST : $_GET; |
610
|
|
|
$this->getDataFromRequest($request); |
611
|
|
|
$this->processValidation(); |
612
|
|
|
|
613
|
|
|
return $this->isValid(); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* @param $request |
618
|
|
|
*/ |
619
|
|
|
protected function getDataFromRequest($request) |
620
|
|
|
{ |
621
|
|
|
$elements = $this->getElements(); |
622
|
|
|
if (is_array($elements)) { |
623
|
|
|
foreach ($elements as $name => $element) { |
624
|
|
|
if ($element->isGroup() && $element->isRequestArray()) { |
625
|
|
|
$name = str_replace('[]', '', $name); |
626
|
|
|
$data = is_array($request[$name]) ? $request[$name] : [$request[$name]]; |
627
|
|
|
$element->getData($data, 'request'); |
628
|
|
|
} else { |
629
|
|
|
$value = $request[$name]; |
630
|
|
|
if (strpos($name, '[') && strpos($name, ']')) { |
631
|
|
|
$arrayPrimary = substr($name, 0, strpos($name, '[')); |
632
|
|
|
$arrayKeys = str_replace($arrayPrimary, '', $name); |
633
|
|
|
|
634
|
|
|
preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $arr_matches, PREG_PATTERN_ORDER); |
635
|
|
|
$value = $request[$arrayPrimary]; |
636
|
|
|
foreach ($arr_matches[1] as $dimension) { |
637
|
|
|
$value = $value[$dimension]; |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
$element->getData($value, 'request'); |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* @return array |
648
|
|
|
*/ |
649
|
|
|
public function getElements() |
650
|
|
|
{ |
651
|
|
|
$return = []; |
652
|
|
|
foreach ($this->_elementsOrder as $current) { |
653
|
|
|
$return[$current] = $this->_elements[$current]; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
return $return; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
public function processValidation() |
660
|
|
|
{ |
661
|
|
|
$elements = $this->getElements(); |
662
|
|
|
if (is_array($elements)) { |
663
|
|
|
foreach ($elements as $name => $element) { |
664
|
|
|
$element->validate(); |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* @return bool |
671
|
|
|
*/ |
672
|
|
|
public function isValid() |
673
|
|
|
{ |
674
|
|
|
return count($this->getErrors()) > 0 ? false : true; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @return array |
679
|
|
|
*/ |
680
|
|
|
public function getErrors() |
681
|
|
|
{ |
682
|
|
|
$errors = array_merge((array)$this->getMessagesType('error'), $this->getElementsErrors()); |
683
|
|
|
|
684
|
|
|
return $errors; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* @param string $type |
689
|
|
|
* @return mixed |
690
|
|
|
*/ |
691
|
|
|
public function getMessagesType($type = 'error') |
692
|
|
|
{ |
693
|
|
|
return $this->_messages[$type]; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* @return array |
698
|
|
|
*/ |
699
|
|
View Code Duplication |
public function getElementsErrors() |
|
|
|
|
700
|
|
|
{ |
701
|
|
|
$elements = $this->getElements(); |
702
|
|
|
$errors = []; |
703
|
|
|
if (is_array($elements)) { |
704
|
|
|
foreach ($elements as $name => $element) { |
705
|
|
|
$errors = array_merge($errors, $element->getErrors()); |
706
|
|
|
} |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
return $errors; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
public function process() |
713
|
|
|
{ |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* @param $message |
718
|
|
|
* @return $this |
719
|
|
|
*/ |
720
|
|
|
public function addError($message) |
721
|
|
|
{ |
722
|
|
|
$this->_messages['error'][] = $message; |
723
|
|
|
|
724
|
|
|
return $this; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* @param $message |
729
|
|
|
* @param string $type |
730
|
|
|
* @return $this |
731
|
|
|
*/ |
732
|
|
|
public function addMessage($message, $type = 'error') |
733
|
|
|
{ |
734
|
|
|
$this->_messages[$type][] = $message; |
735
|
|
|
|
736
|
|
|
return $this; |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* @return array |
741
|
|
|
*/ |
742
|
|
|
public function getMessages() |
743
|
|
|
{ |
744
|
|
|
$messages = $this->_messages; |
745
|
|
|
$messages['error'] = $this->getErrors(); |
746
|
|
|
|
747
|
|
|
return $messages; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* @param $name |
752
|
|
|
* @return mixed |
753
|
|
|
*/ |
754
|
|
|
public function getMessageTemplate($name) |
755
|
|
|
{ |
756
|
|
|
return $this->_messageTemplates[$name]; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @param $type |
762
|
|
|
* @return $this |
763
|
|
|
*/ |
764
|
|
|
public function setRendererType($type) |
765
|
|
|
{ |
766
|
|
|
$this->_renderer = $this->getNewRenderer($type); |
767
|
|
|
|
768
|
|
|
return $this; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* @param string $type |
773
|
|
|
* @return AbstractRenderer |
774
|
|
|
*/ |
775
|
|
|
public function getNewRenderer($type = 'basic') |
776
|
|
|
{ |
777
|
|
|
$name = 'Nip_Form_Renderer_' . ucfirst($type); |
778
|
|
|
/** @var AbstractRenderer $renderer */ |
779
|
|
|
$renderer = new $name(); |
780
|
|
|
$renderer->setForm($this); |
781
|
|
|
|
782
|
|
|
return $renderer; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* @param $key |
787
|
|
|
* @return mixed |
788
|
|
|
*/ |
789
|
1 |
|
public function getCache($key) |
790
|
|
|
{ |
791
|
1 |
|
return $this->_cache[$key]; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* @param $key |
796
|
|
|
* @param $value |
797
|
|
|
*/ |
798
|
1 |
|
public function setCache($key, $value) |
799
|
|
|
{ |
800
|
1 |
|
$this->_cache[$key] = $value; |
801
|
1 |
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* @param $key |
805
|
|
|
* @return bool |
806
|
|
|
*/ |
807
|
|
|
public function isCache($key) |
808
|
|
|
{ |
809
|
|
|
return isset($this->_cache[$key]); |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* @return string |
814
|
|
|
*/ |
815
|
|
|
public function getName() |
816
|
|
|
{ |
817
|
|
|
return get_class($this); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* @return null |
822
|
|
|
*/ |
823
|
|
|
public function __toString() |
824
|
|
|
{ |
825
|
|
|
$backtrace = debug_backtrace(); |
826
|
|
|
if ($backtrace[1]['class'] == 'Monolog\Formatter\NormalizerFormatter') { |
827
|
|
|
return null; |
828
|
|
|
} |
829
|
|
|
trigger_error('form __toString', E_USER_WARNING); |
830
|
|
|
|
831
|
|
|
return $this->render(); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* @return string |
836
|
|
|
*/ |
837
|
|
|
public function render() |
838
|
|
|
{ |
839
|
|
|
return $this->getRenderer()->render(); |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* @return AbstractRenderer |
844
|
|
|
*/ |
845
|
|
|
public function getRenderer() |
846
|
|
|
{ |
847
|
|
|
if (!$this->_renderer) { |
848
|
|
|
$this->_renderer = $this->getNewRenderer(); |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
return $this->_renderer; |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* @return View|null |
856
|
|
|
*/ |
857
|
|
|
public function getControllerView() |
858
|
|
|
{ |
859
|
|
|
if (!$this->_controllerView) { |
|
|
|
|
860
|
|
|
$this->_controllerView = app('app')->getDispatcher()->getCurrentController()->getView(); |
|
|
|
|
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
return $this->_controllerView; |
|
|
|
|
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
/** |
867
|
|
|
* @return array |
868
|
|
|
*/ |
869
|
|
View Code Duplication |
protected function getData() |
|
|
|
|
870
|
|
|
{ |
871
|
|
|
$data = []; |
872
|
|
|
$elements = $this->getElements(); |
873
|
|
|
if (is_array($elements)) { |
874
|
|
|
foreach ($elements as $name => $element) { |
875
|
|
|
$data[$name] = $element->getValue(); |
876
|
|
|
} |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
return $data; |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.