1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use Nip\Form\AbstractForm;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Class Nip_Form_Element_Abstract
|
7
|
|
|
*/
|
8
|
|
|
abstract class Nip_Form_Element_Abstract implements Nip_Form_Element_Interface
|
|
|
|
|
9
|
|
|
{
|
10
|
|
|
|
11
|
|
|
protected $_form;
|
12
|
|
|
|
13
|
|
|
protected $_attribs;
|
14
|
|
|
protected $_options;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var null|string
|
18
|
|
|
*/
|
19
|
|
|
protected $_uniqueID = null;
|
20
|
|
|
|
21
|
|
|
protected $_isRequired;
|
22
|
|
|
protected $_isRendered = false;
|
23
|
|
|
protected $_errors = [];
|
24
|
|
|
protected $_decorators;
|
25
|
|
|
protected $_policies;
|
26
|
|
|
|
27
|
|
|
protected $_type = 'abstract';
|
28
|
|
|
|
29
|
1 |
|
public function __construct($form)
|
30
|
|
|
{
|
31
|
1 |
|
$this->setForm($form);
|
32
|
1 |
|
$this->init();
|
33
|
1 |
|
}
|
34
|
|
|
|
35
|
1 |
|
public function init()
|
36
|
|
|
{
|
37
|
1 |
|
}
|
38
|
|
|
|
39
|
|
|
public function setId($id)
|
40
|
|
|
{
|
41
|
|
|
$this->setAttrib('id', $id);
|
42
|
|
|
|
43
|
|
|
return $this;
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @return Nip_Form_Element_Abstract
|
48
|
|
|
*/
|
49
|
1 |
|
public function setAttrib($key, $value)
|
50
|
|
|
{
|
51
|
1 |
|
$key = (string)$key;
|
52
|
1 |
|
$this->_attribs[$key] = $value;
|
53
|
|
|
|
54
|
1 |
|
return $this;
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
public function getId()
|
58
|
|
|
{
|
59
|
|
|
return $this->getAttrib('id');
|
60
|
|
|
}
|
61
|
|
|
|
62
|
1 |
View Code Duplication |
public function getAttrib($key)
|
|
|
|
|
63
|
|
|
{
|
64
|
1 |
|
$key = (string)$key;
|
65
|
1 |
|
if (!isset($this->_attribs[$key])) {
|
66
|
|
|
return null;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
1 |
|
return $this->_attribs[$key];
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* @return mixed
|
74
|
|
|
*/
|
75
|
|
|
public function getJSID()
|
76
|
|
|
{
|
77
|
|
|
$name = $this->getUniqueId();
|
|
|
|
|
78
|
|
|
|
79
|
|
|
return str_replace(['][', '[', ']'], ['-', '-', ''], $this->getUniqueId());
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @return null|string
|
84
|
|
|
*/
|
85
|
1 |
|
public function getUniqueId()
|
86
|
|
|
{
|
87
|
1 |
|
if (!$this->_uniqueID) {
|
|
|
|
|
88
|
1 |
|
$this->initUniqueId();
|
89
|
|
|
}
|
90
|
|
|
|
91
|
1 |
|
return $this->_uniqueID;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @param null|string $uniqueID
|
96
|
|
|
*/
|
97
|
1 |
|
public function setUniqueID($uniqueID)
|
98
|
|
|
{
|
99
|
1 |
|
$this->_uniqueID = $uniqueID;
|
100
|
1 |
|
}
|
101
|
|
|
|
102
|
1 |
|
protected function initUniqueId()
|
103
|
|
|
{
|
104
|
1 |
|
$this->setUniqueID($this->generateUniqueId());
|
105
|
1 |
|
}
|
106
|
|
|
|
107
|
|
|
/**
|
108
|
|
|
* @return null|string
|
109
|
|
|
*/
|
110
|
1 |
|
protected function generateUniqueId()
|
111
|
|
|
{
|
112
|
1 |
|
$name = $this->getName();
|
113
|
1 |
|
$registeredNames = (array)$this->getForm()->getCache('elements_names');
|
114
|
1 |
|
if (in_array($name, $registeredNames)) {
|
115
|
|
|
$name = uniqid($name);
|
116
|
|
|
}
|
117
|
1 |
|
$registeredNames[] = $name;
|
118
|
1 |
|
$this->getForm()->setCache('elements_names', $registeredNames);
|
119
|
1 |
|
return $name;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
1 |
|
public function getName()
|
123
|
|
|
{
|
124
|
1 |
|
return $this->getAttrib('name');
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
/**
|
128
|
|
|
* @return AbstractForm
|
129
|
|
|
*/
|
130
|
1 |
|
public function getForm()
|
131
|
|
|
{
|
132
|
1 |
|
return $this->_form;
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* @param AbstractForm $form
|
137
|
|
|
* @return $this
|
138
|
|
|
*/
|
139
|
1 |
|
public function setForm(AbstractForm $form)
|
140
|
|
|
{
|
141
|
1 |
|
$this->_form = $form;
|
142
|
|
|
|
143
|
1 |
|
return $this;
|
144
|
|
|
}
|
145
|
|
|
|
146
|
1 |
|
public function setName($name)
|
147
|
|
|
{
|
148
|
1 |
|
$this->setAttrib('name', $name);
|
149
|
|
|
|
150
|
1 |
|
return $this;
|
151
|
|
|
}
|
152
|
|
|
|
153
|
1 |
|
public function setLabel($label)
|
154
|
|
|
{
|
155
|
1 |
|
$this->setAttrib('label', $label);
|
156
|
|
|
|
157
|
1 |
|
return $this;
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
/**
|
161
|
|
|
* @param $data
|
162
|
|
|
* @param string $source
|
163
|
|
|
* @return Nip_Form_Element_Abstract
|
164
|
|
|
*/
|
165
|
|
|
public function getData($data, $source = 'abstract')
|
166
|
|
|
{
|
167
|
|
|
if ($source == 'model') {
|
168
|
|
|
return $this->getDataFromModel($data);
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
return $this->getDataFromRequest($data);
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
/**
|
175
|
|
|
* @param $data
|
176
|
|
|
* @return $this
|
177
|
|
|
*/
|
178
|
|
|
public function getDataFromModel($data)
|
179
|
|
|
{
|
180
|
|
|
$this->setValue($data);
|
181
|
|
|
|
182
|
|
|
return $this;
|
183
|
|
|
}
|
184
|
|
|
|
185
|
|
|
/**
|
186
|
|
|
* @param $value
|
187
|
|
|
* @return $this
|
188
|
|
|
*/
|
189
|
|
|
public function setValue($value)
|
190
|
|
|
{
|
191
|
|
|
$this->setAttrib('value', $value);
|
192
|
|
|
|
193
|
|
|
return $this;
|
194
|
|
|
}
|
195
|
|
|
|
196
|
|
|
/**
|
197
|
|
|
* @param $request
|
198
|
|
|
* @return $this
|
199
|
|
|
*/
|
200
|
|
|
public function getDataFromRequest($request)
|
201
|
|
|
{
|
202
|
|
|
$request = clean($request);
|
203
|
|
|
$this->setValue($request);
|
204
|
|
|
|
205
|
|
|
return $this;
|
206
|
|
|
}
|
207
|
|
|
|
208
|
|
|
/**
|
209
|
|
|
* @param $isRequired
|
210
|
|
|
* @return $this
|
211
|
|
|
*/
|
212
|
1 |
|
public function setRequired($isRequired)
|
213
|
|
|
{
|
214
|
1 |
|
$this->_isRequired = (bool)$isRequired;
|
215
|
|
|
|
216
|
1 |
|
return $this;
|
217
|
|
|
}
|
218
|
|
|
|
219
|
|
|
/**
|
220
|
|
|
* @param $isRendered
|
221
|
|
|
* @return $this
|
222
|
|
|
*/
|
223
|
|
|
public function setRendered($isRendered)
|
224
|
|
|
{
|
225
|
|
|
$this->_isRendered = (bool)$isRendered;
|
226
|
|
|
|
227
|
|
|
return $this;
|
228
|
|
|
}
|
229
|
|
|
|
230
|
|
|
/**
|
231
|
|
|
* @return bool
|
232
|
|
|
*/
|
233
|
|
|
public function isRendered()
|
234
|
|
|
{
|
235
|
|
|
return (bool)$this->_isRendered;
|
236
|
|
|
}
|
237
|
|
|
|
238
|
|
|
/**
|
239
|
|
|
* @return $this
|
240
|
|
|
*/
|
241
|
|
View Code Duplication |
public function addClass()
|
|
|
|
|
242
|
|
|
{
|
243
|
|
|
$classes = func_get_args();
|
244
|
|
|
if (is_array($classes)) {
|
245
|
|
|
$oldClasses = explode(' ', $this->getAttrib('class'));
|
246
|
|
|
$classes = array_merge($classes, $oldClasses);
|
247
|
|
|
$this->setAttrib('class', implode(' ', $classes));
|
248
|
|
|
}
|
249
|
|
|
|
250
|
|
|
return $this;
|
251
|
|
|
}
|
252
|
|
|
|
253
|
|
|
/**
|
254
|
|
|
* @return $this
|
255
|
|
|
*/
|
256
|
|
View Code Duplication |
public function removeClass()
|
|
|
|
|
257
|
|
|
{
|
258
|
|
|
$removeClasses = func_get_args();
|
259
|
|
|
if (is_array($removeClasses)) {
|
260
|
|
|
$classes = explode(' ', $this->getAttrib('class'));
|
261
|
|
|
foreach ($removeClasses as $class) {
|
262
|
|
|
$key = array_search($class, $classes);
|
263
|
|
|
if ($key !== false) {
|
264
|
|
|
unset($classes[$key]);
|
265
|
|
|
}
|
266
|
|
|
}
|
267
|
|
|
$this->setAttrib('class', implode(' ', $classes));
|
268
|
|
|
}
|
269
|
|
|
|
270
|
|
|
return $this;
|
271
|
|
|
}
|
272
|
|
|
|
273
|
|
|
public function validate()
|
274
|
|
|
{
|
275
|
|
|
if ($this->isRequired() && !$this->getValue()) {
|
276
|
|
|
$message = $this->getForm()->getMessageTemplate('no-'.$this->getName());
|
277
|
|
|
if (!$message) {
|
278
|
|
|
$translateSlug = 'general.form.errors.required';
|
279
|
|
|
$message = app('translator')->translate($translateSlug, array('label' => $this->getLabel()));
|
280
|
|
|
if ($message == $translateSlug) {
|
281
|
|
|
$message = $message ? $message : 'The field `'.$this->getLabel().'` is mandatory.';
|
282
|
|
|
}
|
283
|
|
|
}
|
284
|
|
|
$this->addError($message);
|
285
|
|
|
}
|
286
|
|
|
}
|
287
|
|
|
|
288
|
|
|
/**
|
289
|
|
|
* @return bool
|
290
|
|
|
*/
|
291
|
|
|
public function isRequired()
|
292
|
|
|
{
|
293
|
|
|
return (bool)$this->_isRequired;
|
294
|
|
|
}
|
295
|
|
|
|
296
|
|
|
/**
|
297
|
|
|
* @param string $requester
|
298
|
|
|
* @return null
|
299
|
|
|
*/
|
300
|
|
|
public function getValue($requester = 'abstract')
|
301
|
|
|
{
|
302
|
|
|
return $this->getAttrib('value');
|
303
|
|
|
}
|
304
|
|
|
|
305
|
|
|
/**
|
306
|
|
|
* @return null
|
307
|
|
|
*/
|
308
|
1 |
|
public function getLabel()
|
309
|
|
|
{
|
310
|
1 |
|
return $this->getAttrib('label');
|
311
|
|
|
}
|
312
|
|
|
|
313
|
|
|
/**
|
314
|
|
|
* @param $message
|
315
|
|
|
* @return $this
|
316
|
|
|
*/
|
317
|
|
|
public function addError($message)
|
318
|
|
|
{
|
319
|
|
|
$this->_errors[] = $message;
|
320
|
|
|
|
321
|
|
|
return $this;
|
322
|
|
|
}
|
323
|
|
|
|
324
|
|
|
/**
|
325
|
|
|
* @return array
|
326
|
|
|
*/
|
327
|
|
|
public function getErrors()
|
328
|
|
|
{
|
329
|
|
|
return $this->_errors;
|
330
|
|
|
}
|
331
|
|
|
|
332
|
|
|
/**
|
333
|
|
|
* @return bool
|
334
|
|
|
*/
|
335
|
|
|
public function isError()
|
336
|
|
|
{
|
337
|
|
|
return count($this->_errors) > 0;
|
338
|
|
|
}
|
339
|
|
|
|
340
|
|
|
/**
|
341
|
|
|
* @return bool
|
342
|
|
|
*/
|
343
|
|
|
public function isGroup()
|
344
|
|
|
{
|
345
|
|
|
return false;
|
346
|
|
|
}
|
347
|
|
|
|
348
|
|
|
/**
|
349
|
|
|
* @return string
|
350
|
|
|
*/
|
351
|
|
|
public function getType()
|
352
|
|
|
{
|
353
|
|
|
return $this->_type;
|
354
|
|
|
}
|
355
|
|
|
|
356
|
|
|
/**
|
357
|
|
|
* @param $key
|
358
|
|
|
* @return bool
|
359
|
|
|
*/
|
360
|
|
|
public function delAttrib($key)
|
361
|
|
|
{
|
362
|
|
|
$key = (string)$key;
|
363
|
|
|
unset($this->_attribs[$key]);
|
364
|
|
|
|
365
|
|
|
return true;
|
366
|
|
|
}
|
367
|
|
|
|
368
|
|
|
/**
|
369
|
|
|
* @return mixed
|
370
|
|
|
*/
|
371
|
|
|
public function getAttribs()
|
372
|
|
|
{
|
373
|
|
|
return $this->_attribs;
|
374
|
|
|
}
|
375
|
|
|
|
376
|
|
|
/**
|
377
|
|
|
* @param array $attribs
|
378
|
|
|
* @return Nip_Form_Element_Abstract
|
379
|
|
|
*/
|
380
|
|
|
public function setAttribs(array $attribs)
|
381
|
|
|
{
|
382
|
|
|
$this->clearAttribs();
|
383
|
|
|
|
384
|
|
|
return $this->addAttribs($attribs);
|
385
|
|
|
}
|
386
|
|
|
|
387
|
|
|
/**
|
388
|
|
|
* @return Nip_Form_Element_Abstract
|
389
|
|
|
*/
|
390
|
|
|
public function clearAttribs()
|
391
|
|
|
{
|
392
|
|
|
$this->_attribs = [];
|
393
|
|
|
|
394
|
|
|
return $this;
|
395
|
|
|
}
|
396
|
|
|
|
397
|
|
|
/**
|
398
|
|
|
* @param array $attribs
|
399
|
|
|
* @return Nip_Form_Element_Abstract
|
400
|
|
|
*/
|
401
|
|
|
public function addAttribs(array $attribs)
|
402
|
|
|
{
|
403
|
|
|
foreach ($attribs as $key => $value) {
|
404
|
|
|
$this->setAttrib($key, $value);
|
405
|
|
|
}
|
406
|
|
|
|
407
|
|
|
return $this;
|
408
|
|
|
}
|
409
|
|
|
|
410
|
|
|
/**
|
411
|
|
|
* @return bool
|
412
|
|
|
*/
|
413
|
|
View Code Duplication |
public function removeAttrib($key)
|
|
|
|
|
414
|
|
|
{
|
415
|
|
|
if (isset($this->_attribs[$key])) {
|
416
|
|
|
unset($this->_attribs[$key]);
|
417
|
|
|
|
418
|
|
|
return true;
|
419
|
|
|
}
|
420
|
|
|
|
421
|
|
|
return false;
|
422
|
|
|
}
|
423
|
|
|
|
424
|
|
|
/**
|
425
|
|
|
* @param $key
|
426
|
|
|
* @param $value
|
427
|
|
|
* @return $this
|
428
|
|
|
*/
|
429
|
|
|
public function setOption($key, $value)
|
430
|
|
|
{
|
431
|
|
|
$key = (string)$key;
|
432
|
|
|
$this->_options[$key] = $value;
|
433
|
|
|
|
434
|
|
|
return $this;
|
435
|
|
|
}
|
436
|
|
|
|
437
|
|
|
/**
|
438
|
|
|
* @param $key
|
439
|
|
|
* @return null
|
440
|
|
|
*/
|
441
|
|
View Code Duplication |
public function getOption($key)
|
|
|
|
|
442
|
|
|
{
|
443
|
|
|
$key = (string)$key;
|
444
|
|
|
if (!isset($this->_options[$key])) {
|
445
|
|
|
return null;
|
446
|
|
|
}
|
447
|
|
|
|
448
|
|
|
return $this->_options[$key];
|
449
|
|
|
}
|
450
|
|
|
|
451
|
|
|
/**
|
452
|
|
|
* @param string $type
|
453
|
|
|
* @return mixed
|
454
|
|
|
*/
|
455
|
|
|
public function newDecorator($type = '')
|
456
|
|
|
{
|
457
|
|
|
$name = 'Nip_Form_Decorator_Elements_'.ucfirst($type);
|
458
|
|
|
$decorator = new $name();
|
459
|
|
|
$decorator->setElement($this);
|
460
|
|
|
|
461
|
|
|
return $decorator;
|
462
|
|
|
}
|
463
|
|
|
|
464
|
|
|
/**
|
465
|
|
|
* @param Nip_Form_Decorator_Elements_Abstract $decorator
|
466
|
|
|
* @param string $position
|
467
|
|
|
* @param bool $name
|
468
|
|
|
* @return $this
|
469
|
|
|
*/
|
470
|
|
|
public function attachDecorator(
|
471
|
|
|
Nip_Form_Decorator_Elements_Abstract $decorator,
|
472
|
|
|
$position = 'element',
|
473
|
|
|
$name = false
|
474
|
|
|
) {
|
475
|
|
|
$decorator->setElement($this);
|
476
|
|
|
$name = $name ? $name : $decorator->getName();
|
477
|
|
|
$this->_decorators[$position][$name] = $decorator;
|
478
|
|
|
|
479
|
|
|
return $this;
|
480
|
|
|
}
|
481
|
|
|
|
482
|
|
|
/**
|
483
|
|
|
* @param $position
|
484
|
|
|
* @return mixed
|
485
|
|
|
*/
|
486
|
|
|
public function getDecoratorsByPosition($position)
|
487
|
|
|
{
|
488
|
|
|
return $this->_decorators[$position];
|
489
|
|
|
}
|
490
|
|
|
|
491
|
|
|
/**
|
492
|
|
|
* @param $name
|
493
|
|
|
* @param bool $position
|
494
|
|
|
* @return bool
|
495
|
|
|
*/
|
496
|
|
|
public function getDecorator($name, $position = false)
|
497
|
|
|
{
|
498
|
|
View Code Duplication |
if ($position) {
|
|
|
|
|
499
|
|
|
return $this->_decorators[$position][$name];
|
500
|
|
|
} else {
|
501
|
|
|
foreach ($this->_decorators as $position => $decorators) {
|
502
|
|
|
if (isset($decorators[$name])) {
|
503
|
|
|
return $decorators[$name];
|
504
|
|
|
}
|
505
|
|
|
}
|
506
|
|
|
}
|
507
|
|
|
|
508
|
|
|
return false;
|
509
|
|
|
}
|
510
|
|
|
|
511
|
|
|
/**
|
512
|
|
|
* @param $name
|
513
|
|
|
* @param bool $position
|
514
|
|
|
* @return $this
|
515
|
|
|
*/
|
516
|
|
|
public function removeDecorator($name, $position = false)
|
517
|
|
|
{
|
518
|
|
View Code Duplication |
if ($position) {
|
|
|
|
|
519
|
|
|
unset ($this->_decorators[$position][$name]);
|
520
|
|
|
} else {
|
521
|
|
|
foreach ($this->_decorators as $position => $decorators) {
|
522
|
|
|
if (isset($decorators[$name])) {
|
523
|
|
|
unset($decorators[$name]);
|
524
|
|
|
|
525
|
|
|
return $this;
|
526
|
|
|
}
|
527
|
|
|
}
|
528
|
|
|
}
|
529
|
|
|
|
530
|
|
|
return $this;
|
531
|
|
|
}
|
532
|
|
|
|
533
|
|
|
/**
|
534
|
|
|
* @return bool
|
535
|
|
|
*/
|
536
|
|
|
public function hasCustomRenderer()
|
537
|
|
|
{
|
538
|
|
|
return false;
|
539
|
|
|
}
|
540
|
|
|
|
541
|
|
|
/**
|
542
|
|
|
* @return mixed
|
543
|
|
|
*/
|
544
|
|
|
public function render()
|
545
|
|
|
{
|
546
|
|
|
return $this->getRenderer()->render($this);
|
547
|
|
|
}
|
548
|
|
|
|
549
|
|
|
/**
|
550
|
|
|
* @return mixed
|
551
|
|
|
*/
|
552
|
|
|
public function getRenderer()
|
553
|
|
|
{
|
554
|
|
|
return $this->getForm()->getRenderer()->getElementRenderer($this);
|
555
|
|
|
}
|
556
|
|
|
|
557
|
|
|
/**
|
558
|
|
|
* @return mixed
|
559
|
|
|
*/
|
560
|
|
|
public function renderElement()
|
561
|
|
|
{
|
562
|
|
|
return $this->getRenderer()->renderElement($this);
|
563
|
|
|
}
|
564
|
|
|
|
565
|
|
|
/**
|
566
|
|
|
* @return mixed
|
567
|
|
|
*/
|
568
|
|
|
public function renderErrors()
|
569
|
|
|
{
|
570
|
|
|
return $this->getRenderer()->renderErrors($this);
|
571
|
|
|
}
|
572
|
|
|
|
573
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.