forked from islandora-deprecated/islandora_form_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormPopulator.inc
323 lines (301 loc) · 9.21 KB
/
FormPopulator.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
<?php
// $Id$
/**
* @file
*
* Contains a class that populates form values from the specified fedora object.
*/
module_load_include('inc', 'islandora_form_builder', 'Utilities');
module_load_include('inc', 'islandora_form_builder_elements', 'Utilities');
/**
* Populates the values of a drupal form using #form_builder attributes, and the fedora object's
* Content Model.
*/
class FormPopulator {
/**
* The fedora object to edit, datastreams from this item are used to populate the form.
*
* @var Fedora_Item
*/
protected $item;
/**
* The datastream we are editing. This is used to populate existing form elements.
*
* @var DOMDocument
*/
protected $xml;
/**
* The state of the drupal form.
*
* @var array
*/
protected $formState;
/**
* Persistant data used by the form builder module.
*
* @var array
*/
protected $storage;
/**
* A list of the allowed drupal form elements.
*
* @var array
*/
protected $allowedFormElements;
/**
* The default prefix for the default namespaces of the document.
* @var string
*/
protected $defaultPrefix;
/**
* Generates a hash and xpath that can be used to identify this node.
*
* @param DOMNode $node
* Node to generate the hash/path for.
* @param string $default_prefix
* Default prefix to use if the node does not have one.
*
* @return array
* An two index array where the first is the hash of the node and the second is an xpath to the node.
*/
public static function generateHashPath(&$node, $default_prefix) {
$path = self::getNodeXPath($node, $default_prefix);
$index = self::findXPathIndexInParentElement($node);
$path = get_class($node) == 'DOMAttr' ? "@$path" : "{$path}[{$index}]";
$parent = $node;
while (($parent = $parent->parentNode) && (get_class($parent) == 'DOMElement')) {
$parent_path = self::getNodeXPath($parent, $default_prefix);
$index = self::findXPathIndexInParentElement($parent);
$path = "{$parent_path}[$index]/$path";
}
$path = "/$path";
return array(md5($path), $path);
}
/**
* Find's the index of the child within it's parent element.
* Base 1 index, since it will be used by xpath.
*
* @param DOMNode $node
*
* @return int
*/
private static function findXPathIndexInParentElement(&$node) {
$count = 1;
$name = $node->nodeName;
$next = $node;
while ($next = $next->previousSibling) {
if (get_class($next) == 'DOMElement' && $next->nodeName == $name) {
$count++;
}
}
return $count;
}
private static function getNodeXPath(&$node, &$default_prefix) {
$node->nodeName;
if($node->prefix == '' && get_class($node) != 'DOMAttr') {
return "{$default_prefix}:{$node->nodeName}";
}
else {
return $node->nodeName;
}
}
/**
* Creates a FormPopulator.
*
* @param array $form_state
* The state of the drupal form.
* @param string $pid
* The pid of the fedora object to be edited/used for populating form fields.
* @param string $dsid
* The datastream ID of the datastream to be edited.
*/
function __construct(&$form_state, $pid, $dsid, $form_name) {
$this->initializeStorage($form_state, $pid, $dsid);
$this->xml = new DOMDocument();
$this->xml->loadXML($this->storage['xml']);
$this->xpath = new DOMXPath($this->xml);
$this->registerNamespaces($form_name);
$this->allowedFormElements = islandora_form_builder_elements_get_allowed_form_elements();
}
/**
*
* @param array $form_state
* @param string $pidUt
* @param string $dsid
*/
private function initializeStorage(&$form_state, $pid, $dsid) {
$this->formState = &$form_state;
$this->storage = &$this->formState['storage'][STORAGE_KEY];
if (!isset($this->storage['xml'])) {
$this->item = new fedora_item($pid);
$this->storage['xml'] = $this->item->get_datastream_dissemination($dsid);
}
}
/**
*
* @param string $form_name
*/
private function registerNamespaces(&$form_name) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
$content_model = ContentModel::loadFromObject($this->item->pid);
$form = $content_model->getForm($form_name);
$module = $form->getAttribute('document_module');
$file = $form->getAttribute('document_file');
$class = $form->getAttribute('document_class');
$path = drupal_get_path('module', $module);
require_once "$path/$file";
list($default_prefix) = call_user_func(array($class, "getDefaultNamespace"));
$this->defaultPrefix = $default_prefix;
$namespaces = call_user_func(array($class, "getRequiredNamespaces"));
foreach ($namespaces as $prefix => $uri) { // Default namespaces not supported yet...
$this->xpath->registerNamespace($prefix, $uri);
}
}
/**
* Uses the existing datastream to populate for elements.
*
* @param array $form
*/
public function populate(&$form) {
$parent_node = $this->xml->documentElement;
$this->populateFormElements($form, $parent_node);
}
/**
*
* @param array $form
*/
private function populateFormElements(&$form, &$parent_node) {
foreach ($form as &$form_element) {
if ($this->isFormElement($form_element)) {
$this->populateFormElement($form_element, $parent_node);
if ($this->hasChildFormElements($form_element)) {
$this->populateChildFormElements($form_element, $parent_node);
}
}
}
}
/**
* Checks to see if a form element can be processed.
*
* @param array $form
* Drupal form.
*
* @return boolean
* TRUE if this $form can be processed false otherwhise.
*/
private function isFormElement(&$form) {
$has_type = isset($form['#type']);
return $has_type ? array_search($form['#type'], $this->allowedFormElements) !== FALSE : FALSE;
}
/**
* Checks if the form element $form, has child elements.
*
* @param array $form
*
* @return boolean
* TRUE if the form element has children FALSE otherwise.
*/
private function hasChildFormElements(&$form) {
return isset($form['content']) ? TRUE : FALSE;
}
/**
* Recursively populates all the child elements.
*
* @param array $form
* @param DOMNode $parent_node
*/
private function populateChildFormElements(&$form, &$parent_node) {
$content = &$form['content'];
$properties = isset($form['#form_builder']) ? $form['#form_builder'] : NULL;
if ($properties) {
$node_list = $this->queryPath($properties, $parent_node);
for ($i = 0; $i < $node_list->length; $i++) {
$node = $node_list->item($i);
$this->populateFormElements($content[$i], $node);
}
}
else { // Not all elements have to have form_builder properties
foreach ($content as &$child) {
$this->populateFormElements($child, $parent_node);
}
}
}
/**
*
* @param array $form
*/
private function populateFormElement(&$form, &$parent_node) {
$type = $form['#type'];
$properties = isset($form['#form_builder']) ? $form['#form_builder'] : NULL;
if ($properties) {
$node_name = $parent_node->nodeName;
$node_list = $this->queryPath($properties, $parent_node);
$registered_nodes = $this->registerNodes($node_list);
if (count($registered_nodes) > 0) {
$this->populateType($type, $form, $registered_nodes);
}
}
}
/**
*
* @param array $properties
* @param DOMNode $parent_node
* @return DOMNodeList
*/
private function queryPath(&$properties, &$parent_node) {
list($path, $full) = $properties['path'];
return $full ? $this->xpath->query($path) : $this->xpath->query($path, $parent_node);
}
/**
* Register all the nodes that are used to populate elements.
*
* @param DOMNodeList $node_list
*
* @return array
* An map of the generated hashes and their respective DOMNodes.
*/
private function registerNodes(&$node_list) {
$registered_nodes = array();
for ($i = 0; $i < $node_list->length; $i++) {
$node = $node_list->item($i);
list($hash, $path) = self::generateHashPath($node, $this->defaultPrefix);
$this->registerHashPath($hash, $path);
$registered_nodes[] = array($hash, $node);
}
return $registered_nodes;
}
/**
*
* @param DOMNode $node
* @param hash $hash
*/
private function registerHashPath($hash, $path) {
$this->storage['hashes'][$hash] = $path;
}
/**
*
* @param <type> $type
* @param <type> $registered_nodes
*/
private function populateType($type, &$form, &$registered_nodes) {
list($class, $filename) = islandora_form_builder_elements_get_class_name_and_file_name_of_form_element_type($type);
if ($filename != '') {
require_once $filename;
if (is_callable("$class::populate")) {
call_user_func_array(array($class, "populate"), array(&$form, &$registered_nodes));
return;
}
}
$this->defaultPopulateType($form, $registered_nodes);
}
/**
*
* @param <type> $form
* @param <type> $registered_nodes
*/
private function defaultPopulateType(&$form, &$registered_nodes) {
list($hash, $node) = $registered_nodes[0];
$form['#default_value'] = trim($node->textContent);
$form['#form_builder']['hash'] = $hash;
}
}