forked from nigelgbanks/objective_forms
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormValues.inc
executable file
·146 lines (133 loc) · 3.55 KB
/
FormValues.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
<?php
/**
* @file
* Associates submitted form values with array.
*/
module_load_include('inc', 'objective_forms', 'FormValueTracker');
module_load_include('inc', 'php_lib', 'DrupalFormHelpers');
/**
* This class stores all submitted values. It provides a mechanism for
* accessing submitted values with the FormElements hashes.
*/
class FormValues {
/**
* A map of submitted form values where the key is the FormElement's hash.
*
* @var array
*/
protected $values;
/**
* A helper class that matches values to elements.
*
* @var FormValueTracker
*/
protected $tracker;
/**
* Checks to see if any form values were submitted to the server.
*
* @param array $form_state
* The Drupal Form State.
*
* @return bool
* TRUE if $form_state['values'] exists, FALSE otherwise.
*/
public static function Exists(array &$form_state) {
return isset($form_state['values']);
}
/**
* Create a FormValues instance.
*
* @param array $form_state
* Drupal Form state.
*
* @param array $root
* The elements to associate with submitted values.
*/
public function __construct(array &$form_state, array &$root, FormElementRegistry $registry) {
$this->values = array();
if (self::Exists($form_state)) {
$this->tracker = new FormValueTracker($form_state['values'], $registry);
$this->setValues($root, $this->tracker);
}
}
/**
* Utilizes the FormValueTracker to store all the submitted values.
*
* @param array $element
* An element in the Drupal Form.
*
* @param FormValueTracker $tracker
* A helper class for extracting submitted values.
*/
protected function setValues(array &$element, FormValueTracker $tracker) {
$this->setValue($element, $tracker);
$children = element_children($element);
foreach ($children as $key) {
$child = &$element[$key];
$this->setValues($child, clone $tracker);
}
}
/**
* Associate the given array &, with a submitted value.
*
* @param array $element
* An element in the Drupal Form.
*
* @param FormValueTracker $tracker
* A helper class for extracting submitted values.
*/
protected function setValue(array &$element, FormValueTracker $tracker) {
$value = $tracker->getValue($element);
if (isset($element['#hash'])) {
$this->values[$element['#hash']] = $value;
}
}
/**
* Is there a value associated with the given form element.
*
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*
* @return bool
* TRUE if there is a value for the given FormElement.
*/
public function hasValue($hash) {
return isset($this->values[$hash]);
}
/**
* Get the value associated with the given element.
*
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*
* @return mixed
* The submitted value for the given element if found, NULL otherwise.
*/
public function getValue($hash) {
if ($this->hasValue($hash)) {
$value = $this->values[$hash];
if (is_array($value)) {
foreach ($value as &$v) {
if (is_string($v)) {
$v = trim($v);
}
}
}
elseif (is_string($value)) {
$value = trim($value);
}
return $value;
}
return NULL;
}
/**
* Returns all values.
*
* @return array
* The keys of the array are the FormElement's hashs, and the values are the
* submitted form values.
*/
public function getValues() {
return $this->values;
}
}