-
Notifications
You must be signed in to change notification settings - Fork 21
/
FormElement.inc
670 lines (634 loc) · 18.5 KB
/
FormElement.inc
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* @file
* Defines a FormElement class for encapsulating Drupal form elements. Each
* FormElement class has a unique #hash property that can be used to identify
* that FormElement.
*/
module_load_include('inc', 'php_lib', 'ReflectionHelpers');
module_load_include('inc', 'php_lib', 'ReadOnlyProtectedMembers');
module_load_include('inc', 'objective_forms', 'Utils');
module_load_include('inc', 'objective_forms', 'FormElementRegistry');
/**
* Encapsulates drupal form elements.
*/
class FormElement implements ArrayAccess {
/**
* Holds references to protected variables. Allows for external access and
* protected writes. parent -> Stores a reference to this FormElements Parent.
* NULL if no parent. hash -> A identifier that is unique to this object.
*
* @var ReadOnlyProtected
*/
protected $protected;
/**
* Child FormElements, only objects of type FormElement are stored in this
* array.
*
* @var array
*/
public $children;
/**
* Drupal form controls/properties. The values are mixed in this array.
*
* All control names are prefixed with '#' for example '#title'.
*
* @var array
*/
public $controls;
/**
* Element Registry.
*
* @var FormElementRegistry
*/
protected $registry;
/**
* Instantiate's a FormElement.
*
* @param array $form
* Drupal form definition.
*/
public function __construct(FormElementRegistry $registry = NULL /* yuck fix this.. */, array $form = NULL) {
$this->protected = new ReadOnlyProtectedMembers(array('parent' => NULL, 'hash' => NULL));
$this->registry = $registry;
$this->controls = array();
$this->children = array();
$this->initialize($form);
$this->hash = isset($this->controls['#hash']) ? $this->controls['#hash'] : spl_object_hash($this);
// Deals with this case this class is used in a non user context ....
if ($this->registry) {
$this->registry->registerCreated($this);
}
}
/**
* Initialize this object from a drupal form definition.
*
* Takes the controls/properties/children from the drupal form definition, and
* creates objects repersenting elements and properties where appropriate.
*
* @param array $form
* Drupal form definition.
*/
protected function initialize(array &$form = NULL) {
if (isset($form)) {
$this->initializeControls($form);
$this->initializeChildren($form);
}
}
/**
* Create controls/properties from a drupal form definition.
*
* @param array $form
* Drupal form definition.
*/
protected function initializeControls(array &$form) {
module_load_include('inc', 'objective_forms', 'FormProperty');
$properties = element_properties($form);
foreach ($properties as $key) {
// Objectify the property where appropriate.
$this->controls[$key] = FormProperty::Expand($key, $form[$key]);
}
}
/**
* Create child FormElements from a drupal form definition.
*
* @param array $form
* Drupal form definition.
*/
protected function initializeChildren(array &$form) {
$children = element_children($form);
foreach ($children as $key) {
$child = new FormElement($this->registry, $form[$key]);
$this->adopt($child, $key);
}
}
/**
* Clone this FormElement.
*
* Performs a deep clone on controls and children. The cloned element won't
* have a parent.
*/
public function __clone() {
$original_hash = $this->hash;
$this->protected = clone $this->protected;
// The parent was unaware of this cloned bastard child of science...
$this->parent = NULL;
// Maintain uniqueness.
$this->hash = spl_object_hash($this);
// Clone Controls.
foreach ($this->controls as $key => $control) {
if (is_array($control)) {
$this->controls[$key] = array_copy_recursive($control);
}
elseif (is_object($control)) {
$this->controls[$key] = clone $control;
}
}
// Clone Children.
$children = $this->children;
$this->children = array();
// Clone Children.
foreach ($children as $key => $child) {
$this->adopt(clone $child, $key);
}
// Deals with this case this class is used in a non user context ....
if ($this->registry) {
$this->registry->registerClone($original_hash, $this);
}
}
/**
* Search for FormElement identified by it's unique #hash property.
*
* Searches this element and all of its children recursively.
*
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*
* @return FormElement
* The FormElement if found NULL otherwise.
*/
public function findElement($hash) {
if ($this->hash == $hash) {
return $this;
}
foreach ($this->children as $child) {
$element = $child->findElement($hash);
if ($element) {
return $element;
}
}
return NULL;
}
/**
* Function flatten.
*
* Converts the tree like structure of a FormElement and its children to a
* flat array.
*
* @return array
* An array containing this FormElement and all of its children, where its
* keys are the FormElements unique #hash properties. For example:
* array(#hash=>FormElement)
*/
public function flatten() {
$elements = array();
$elements[$this->hash] = $this;
foreach ($this->children as $child) {
$elements = array_merge($elements, $child->flatten());
}
return $elements;
}
/**
* Function eachChild.
*
* Takes a callback, and calls it repeatedly passing each child FormElement
* to the callback as the first parameter. Any other parameters passed to this
* function will be passed to the callback as additional parameters.
*
* Unlike eachDecendant() only the immediate children of this FormElement will
* be used.
*
* If the callback returns FALSE, then processing stops and this function
* exits.
*
* @param callback $function
* The function to be called repeatedly.
*
* @return bool
* TRUE if the function was called for all child FormElements, FALSE
* otherwise.
*/
public function eachChild($function) {
// Remove function name from the argument list.
$param_arr = array_slice(func_get_args(), 1);
foreach ($this->children as $child) {
$args = array_merge(array($child), $param_arr);
if (call_user_func_array($function, $args) === FALSE) {
return FALSE;
}
}
return TRUE;
}
/**
* Function eachControl.
*
* Takes a callback, and calls it repeatedly passing each control/property to
* the callback with the name of the control/property as the first parameter
* and the value of the control/property as the second paramter. Any other
* parameters passed to this function will be passed to the callback as
* additional parameters.
*
* If the callback returns FALSE, then processing stops and this function
* exits.
*
* @param callback $function
* The function to be called repeatedly.
*
* @return bool
* TRUE if the function was called for all controls/properties, FALSE
* otherwise.
*/
public function eachControl($function) {
// Remove function name from the argument list.
$param_arr = array_slice(func_get_args(), 1);
foreach ($this->controls as $key => &$control) {
$args = array_merge(array($key, &$control), $param_arr);
if (call_user_func_array($function, $args) === FALSE) {
return FALSE;
}
}
return TRUE;
}
/**
* Function eachDecendant.
*
* Takes a callback, and calls it repeatedly passing each decendant
* FormElement to the callback as the first parameter. Any other parameters
* passed to this function will be passed to the callback as additional
* parameters.
*
* Unlike eachChild() even children of children will be used.
*
* If the callback returns FALSE, then processing stops and this function
* exits.
*
* @param callback $function
* The function to be called repeatedly.
*
* @return bool
* TRUE if the function was called for all decendants, FALSE otherwise.
*/
public function eachDecendant($function) {
$func_args = func_get_args();
// Remove function name from the argument list.
$param_arr = array_slice($func_args, 1);
$func_args = func_get_args();
foreach ($this->children as $child) {
$args = array_merge(array($child), $param_arr);
if (call_user_func_array($function, $args) === FALSE) {
// End processing.
return FALSE;
}
// Recurse...
if (call_user_func_array(array($child, 'eachDecendant'), $func_args) === FALSE) {
// End processing.
return FALSE;
}
}
// Continue processing.
return TRUE;
}
/**
* Function each.
*
* Takes a callback, and calls it repeatedly passing itself and each decendant
* FormElement to the callback as the first parameter. Any other parameters
* passed to this function will be passed to the callback as additional
* parameters.
*
* Unlike eachDecendant() this object is also used.
*
* If the callback returns FALSE, then processing stops and this function
* exits.
*
* @param callback $function
* The function to be called repeatedly.
*
* @return bool
* TRUE if the function was called for all decendants, FALSE otherwise.
*/
public function each($function) {
// Remove function name from the argument list.
$param_arr = array_slice(func_get_args(), 1);
$args = array_merge(array($this), $param_arr);
if (call_user_func_array($function, $args) === TRUE) {
return call_user_func_array(array($this, 'eachDecendant'), func_get_args());
}
return FALSE;
}
/**
* Function __get.
*
* Gets a child FormElement, control/property, or ProtectedReadOnlyMember if
* it exists.
*
* @param mixed $name
* The name of the child FormElement, control/property, or
* ProtectedReadOnlyMember.
*
* @return mixed
* The child FormElement, control/property, or ProtectedReadOnlyMember
* identified by $name if found, NULL otherwise.
*/
public function __get($name) {
if ($this->protected->has($name)) {
return $this->protected->$name;
}
// Control.
elseif ($this->offsetExists("#$name")) {
return $this->offsetGet("#$name");
}
// Child.
elseif ($this->offsetExists($name)) {
return $this->offsetGet($name);
}
return NULL;
}
/**
* Function __set.
*
* Adds/Sets the value for a child FormElement, control/property, or
* ProtectedReadOnlyMember.
*
* Can't add to the ProtectedReadOnlyMembers.
*
* @param mixed $name
* The name of the child FormElement, control/property, or
* ProtectedReadOnlyMember.
* @param mixed $value
* The the child FormElement, control/property, or ProtectedReadOnlyMember
* to set.
*/
public function __set($name, $value) {
if ($this->protected->has($name)) {
$this->protected->$name = $value;
}
else {
$name = is_or_descends_from($value, 'FormElement') ? $name : "#$name";
$this->offsetSet($name, $value);
}
}
/**
* Function __isset.
*
* Checks if a given child FormElement or a control/property identified by
* $name exists. Also checks for the existance of values stored in this
* objects ReadOnlyMembers.
*
* @param mixed $name
* The name of the child FormElement, control/property, or
* ProtectedReadOnlyMember.
*
* @return bool
* TRUE if the child FormElement, control/property, or
* ProtectedReadOnlyMember is set.
*/
public function __isset($name) {
if ($this->protected->has($name)) {
return isset($this->protected->$name);
}
return $this->offsetExists($name) || $this->offsetExists("#$name");
}
/**
* Function __unset.
*
* Unsets a given child FormElement, control/property, or
* ProtectedReadOnlyMember identified by $name.
*
* @param mixed $name
* The name of the child FormElement, control/property, or
* ProtectedReadOnlyMember.
*/
public function __unset($name) {
if ($this->protected->has($name)) {
unset($this->protected->$name);
}
// Child.
elseif ($this->offsetExists($name)) {
$this->offsetUnset($name);
}
// Control.
elseif ($this->offsetExists("#$name")) {
$this->offsetUnset("#$name");
}
}
/**
* Function offsetExists.
*
* Checks if a given child FormElement or a control/property identified by
* $offsets exists.
*
* @param mixed $offset
* The name of the child FormElement or control/property
*
* @return bool
* TRUE if the child FormElement or control/property exists.
*/
public function offsetExists($offset) {
$child_exists = isset($this->children[$offset]);
$control_exists = isset($this->controls[$offset]);
return $child_exists || $control_exists;
}
/**
* Function offsetGet.
*
* Gets a the child FormElement or a control/property identified by $offset
* if it exists.
*
* @param mixed $offset
* The name of the child FormElement or control/property.
*
* @return mixed
* If found, this function will return either a child FormElement or a
* FormControl.
*/
public function offsetGet($offset) {
if (array_key_exists($offset, $this->children)) {
return $this->children[$offset];
}
if (array_key_exists($offset, $this->controls)) {
return $this->controls[$offset];
}
return NULL;
}
/**
* Function offsetSet.
*
* Adds/Sets a child FormElement or a control/property identified by $offset
* if it exists.
*
* @param mixed $offset
* The name of the child FormElement or control/property.
* @param mixed $value
* Either a child FormElement or some mixed value that repersents a
* control/property.
*/
public function offsetSet($offset, $value) {
if (is_or_descends_from($value, 'FormElement')) {
$this->adopt($value, $offset);
}
elseif (element_property($offset)) {
$this->controls[$offset] = $value;
}
}
/**
* Function offsetUnset.
*
* Unsets a given child FormElement or control/property identified by $offset.
*
* @param mixed $offset
* The name of the child FormElement or control/property.
*/
public function offsetUnset($offset) {
if (isset($this->children[$offset])) {
unset($this->children[$offset]);
}
elseif (isset($this->controls[$offset])) {
unset($this->controls[$offset]);
}
}
/**
* Function adopt.
*
* Adopt the child FormElement, this forcefully orphans the child from its
* original parent.
*
* @param FormElement $child
* The FormElement to adopt.
* @param mixed $key
* The index where the child FormElement is stored, if NULL the child is
* append onto the end of the array.
*/
public function adopt(FormElement $child, $key = NULL) {
$child->setParent($this);
if (isset($key)) {
if (isset($this->children[$key])) {
$this->children[$key]->orphan();
}
$this->children[$key] = $child;
}
else {
$this->children[] = $child;
}
}
/**
* Function setParent.
*
* Sets this FormElements parent to the one provided. Orphaning this
* FormElement from its previous parent FormElement.
*
* @param FormElement $parent
* The new parent FormElement of this FormElement.
*/
protected function setParent(FormElement $parent) {
// There can only be one.
$this->orphan();
$this->parent = $parent;
}
/**
* Function orphan.
*
* Orphans this FormElement, and removes the relationship its parent
* FormElement has with it.
*
* @return bool
* TRUE if the element was orphaned FALSE otherwise.
*/
public function orphan() {
if (isset($this->parent)) {
$was_orphaned = $this->parent->orphanChild($this);
unset($this->parent);
return $was_orphaned;
}
return FALSE;
}
/**
* Function orphanChild.
*
* Removes the child element defined by $remove from this FormElement.
*
* @param FormElement $remove
* The child FormElement to remove.
*
* @return bool
* TRUE if the child was removed, FALSE otherwise.
*/
protected function orphanChild(FormElement $remove) {
foreach ($this->children as $index => $child) {
if ($remove === $child) {
unset($this->children[$index]);
return TRUE;
}
}
return FALSE;
}
/**
* Funciton getIndex.
*
* Get's the index of this FormElement in its parent's children array if it
* has a parent.
*
* @return mixed
* The index of this FormElement within its parents children array. NULL if
* has no parent.
*/
public function getIndex() {
if (isset($this->parent)) {
foreach ($this->parent->children as $index => $child) {
if ($child === $this) {
return $index;
}
}
}
return NULL;
}
/**
* Function getLocation.
*
* Gets the full path in the form definition to this element.
*
* @return string
* Describes the location of this FormElement within the Form.
* For example ['author']['name']
*/
public function getLocation() {
$index = $this->getIndex();
return isset($index) ? "{$this->parent->getLocation()}[$index]" : '';
}
/**
* Function toArray.
*
* Converts this FormElement into an array that can be used with the Drupal
* Form API, as a form definition.
*
* @return array
* array of children
*/
public function toArray() {
$output = array('#hash' => $this->hash);
$this->addControlsToArray($output);
$this->addChildrenToArray($output);
return $output;
}
/**
* Function addControlsToArray.
*
* Converts this FormElements controls/properties to an array that can be
* used by Drupal Form API.
*
* Adds the controls to the $output parameter.
*
* @param array &$output
* The Drupal Form API's repersentation of a form.
*/
protected function addControlsToArray(array &$output) {
foreach ($this->controls as $name => $value) {
$has_property_interface = has_interface($value, 'FormPropertyInterface');
$output[$name] = $has_property_interface ? $value->toDrupalForm() : $value;
}
}
/**
* Function addChildrenToArray.
*
* Converts this FormElements child FormElements to an array that can be used
* by Drupal Form API.
*
* @param array &$output
* The Drupal Form API's repersentation of a form.
*/
protected function addChildrenToArray(array &$output) {
foreach ($this->children as $name => $child) {
$output[$name] = $child->toArray();
}
}
}