This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FormGenerator.inc
276 lines (259 loc) · 8.29 KB
/
FormGenerator.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
<?php
// $Id$
/**
* @file
*
* Contains a class that generates a drupal from from a form definition stored in a content model.
*/
module_load_include('inc', 'islandora_form_builder', 'Utilities');
module_load_include('inc', 'islandora_form_builder_elements', 'Utilities');
module_load_include('inc', 'fedora_repository', 'ContentModel');
/**
* Creates a Drupal form from a XML form definition stored in a Content Model.
*/
class FormGenerator {
/**
* Content Model's XML that contains the form's definition.
*
* @var string
*/
protected $formDefinition;
/**
* DOMXPath for the $formDefinition.
*
* @var DOMXPath
*/
protected $xpath;
/**
* An array of allowed drupal form element types.
*
* @var array
*/
protected $formElementTypes;
/**
* Creates a FormGenerator from an object.
*
* @param string $pid
*
* @return FormGenerator
*/
public static function CreateFromObject($pid) {
$content_model = ContentModel::loadFromObject($pid);
return new FormGenerator($content_model);
}
/**
* Creates a FormGenerator from a model.
*
* @param string $content_model_pid
* @param string $content_model_dsid
*
* @return FormGenerator
*/
public static function CreateFromModel($content_model_pid, $content_model_dsid) {
$content_model = ContentModel::loadFromModel($content_model_pid, $content_model_dsid);
return new FormGenerator($content_model);
}
/**
* Create the FormGenerator.
*
* @param ContentModel $content_model
*/
private function __construct($content_model) {
$this->contentModel = $content_model;
$xml = $this->contentModel->dumpXml();
$this->formDefinition = new DOMDocument();
$this->formDefinition->loadXML($xml);
$this->xpath = new DOMXPath($this->formDefinition);
$this->xpath->registerNamespace('cm', "http://www.islandora.ca");
$this->formElementTypes = islandora_form_builder_elements_get_allowed_form_elements();
}
/**
* Generates a drupal form based on a XML definition in the content model.
*
* @param string $form_name
*
* @return array
* Drupal Form.
*/
public function generate($form_name) {
$form_element = $this->findFormElement($form_name);
if ($form_element) {
$form = array();
$result = $this->xpath->query('child::*', $form_element);
$form['#tree'] = TRUE;
for ($i = 0; $i < $result->length; $i++) { // Top level form elements.
$child = $result->item($i);
$form[] = $this->createDrupalFormElement($child);
}
return $form;
}
return FALSE;
}
/**
* Finds the named form in the content models definition.
*
* @param string $form_name
* Name of the form element to retrieve.
*
* @return DOMElement
* The form element if found, FALSE otherwise.
*/
private function findFormElement($form_name) {
$result = $this->xpath->query("//cm:form[@name='$form_name']");
if ($result->length > 1) {
drupal_set_message(t("Form names should be unique. Check the Content Modeler to ensure that form: $form_name is unique."), 'error');
return FALSE;
}
else if ($result->length == 0) {
drupal_set_message(t("Could not find form: $form_name. Please Check the Content Modeler to ensure that form: $form_name exists."), 'error');
return FALSE;
}
else {
return $result->item(0);
}
}
/**
* Creates a drupal form element from a xml definition.
*
* @param DOMElement $form_element
* XML definition of a drupal form element.
*
* @return array
* Drupal form element.
*/
private function createDrupalFormElement(&$form_element) {
$type = $form_element->tagName;
$form = array('#type' => $type);
$result = $this->xpath->query('child::*', $form_element);
for ($i = 0; $i < $result->length; $i++) {
$child = $result->item($i);
list($name, $value) = $this->createDrupalFormControl($child);
$form[$name] = $value;
}
$method_name = 'alter' . str_replace('_', '', $type) . 'FormElement';
if (method_exists($this, $method_name)) {
call_user_func_array(array(&$this, $method_name), array(&$form));
}
return $form;
}
/**
* Creates a drupal form control.
*
* @param DOMElement $form_element
* A XML definition for the form control.
*
* @return array
* A drupal from control such as #title or #form_builder
*/
private function createDrupalFormControl(&$form_element) {
$name = $form_element->tagName;
$method_name = 'create' . str_replace('_', '', $name) . 'FormControl';
if (method_exists($this, $method_name)) {
return call_user_func_array(array(&$this, $method_name), array(&$form_element));
}
else { // Default case the text in the element is its value.
return array("#$name", $form_element->textContent);
}
}
/**
* Creates a Content Form Control.
*
* @param DOMElement $form_element
* A XML definition for the form control.
*
* @return array
* The drupal from control.
*/
private function createContentFormControl(&$form_element) {
$content = array();
$result = $this->xpath->query('child::*', $form_element);
for ($i = 0; $i < $result->length; $i++) {
$child = $result->item($i);
$content[0][] = $this->createDrupalFormElement($child);
}
return array('content', $content);
}
/**
* Creates a Options Form Control.
*
* @param DOMElement $form_element
* A XML definition for the form control.
*
* @return array
* The drupal from control.
*/
private function createOptionsFormControl(&$form_element) {
// Get all options
$options = array();
$result = $this->xpath->query('child::*', $form_element);
for ($i = 0; $i < $result->length; $i++) {
$child = $result->item($i);
$options[$child->textContent] = $child->textContent;
}
// Should we use the first option as a prompt?
$ignore_first_option = $form_element->getAttribute('ignoreFirstOption') == 'true';
if ($ignore_first_option) {
$select_message = array_shift($options);
$options = array_merge(array(NULL => $select_message), $options);
}
return array('#options', $options);
}
/**
* Creates a Form Builder Form Control.
*
* @param DOMElement $form_element
* A XML definition for the form control.
*
* @return array
* The drupal from control.
*/
private function createFormBuilderFormControl(&$form_element) {
$form_builder = array();
$path = $this->xpath->query('descendant::cm:path', $form_element)->item(0);
$full = $path->getAttribute('full') == 'true';
$form_builder['path'] = array($path->textContent, $full);
if ($full) {
$parent_path = $this->xpath->query('descendant::cm:parent_path', $form_element)->item(0);
$form_builder['parent_path'] = $parent_path->textContent;
}
$xml = $this->xpath->query('descendant::cm:xml', $form_element);
if ($xml->length == 1) {
$form_builder['xml'] = $xml->item(0)->textContent;
}
$element = $this->xpath->query('descendant::cm:element', $form_element);
if ($element->length == 1) {
$form_builder['element'] = $element->item(0)->textContent;
}
$attribute = $this->xpath->query('descendant::cm:attribute', $form_element);
if ($attribute->length == 1) {
$form_builder['attribute'] = $attribute->item(0)->textContent;
}
$require_value = $this->xpath->query('descendant::cm:require_value', $form_element)->item(0);
$form_builder['require_value'] = $require_value->textContent;
return array('#form_builder', $form_builder);
}
/**
* Alter the generated TabPanel Form Element.
*
* @param DOMElement $form
* The Drupal Form element to be altered.
*/
private function alterTabPanelFormElement(&$form) {
if (isset($form['#form_builder'])) {
$form['content'][0]['#form_builder'] = $form['#form_builder'];
$form['content'][0]['#form_builder']['path'][0] .= '[1]';
}
}
/**
* Alter the generated FieldSet Form Element.
*
* @param DOMElement $form
* The Drupal Form element to be altered.
*/
private function alterFieldSetFormElement(&$form) {
if (isset($form['#form_builder'])) {
$form['content'][0]['#form_builder'] = $form['#form_builder'];
$form['content'][0]['#form_builder']['path'][0] .= '[1]';
}
}
}