/[drupal]/contributions/modules/form_builder/form_builder.module
ViewVC logotype

Contents of /contributions/modules/form_builder/form_builder.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Sat Jun 20 06:18:15 2009 UTC (5 months ago) by quicksketch
Branch: MAIN
CVS Tags: DRUPAL-6--0-6, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.12: +7 -1 lines
File MIME type: text/x-php
Adding support for "markup" type field, plus tentative #input_format and #markup property forms in preparation for D7.
1 <?php
2 // $Id: form_builder.module,v 1.12 2009/06/20 00:13:04 quicksketch Exp $
3
4 /**
5 * @file form_builder.module
6 * Generic form building framework and user interface.
7 */
8
9 define('FORM_BUILDER_ROOT', 0);
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function form_builder_menu() {
15 $items = array();
16
17 $items['admin/build/form-builder/add'] = array(
18 'title' => 'Add field',
19 'description' => 'Add a field to a form.',
20 'page callback' => 'form_builder_add_page',
21 'access callback' => 'form_builder_menu_field_access',
22 'access arguments' => array('add', 4, 5, 6),
23 'file' => 'includes/form_builder.admin.inc',
24 'type' => MENU_CALLBACK,
25 );
26
27 $items['admin/build/form-builder/configure'] = array(
28 'title' => 'Configure field',
29 'description' => 'Configure a field within a form.',
30 'page callback' => 'form_builder_configure_page',
31 'access callback' => 'form_builder_menu_field_access',
32 'access arguments' => array('configure', 4, 5, 6),
33 'file' => 'includes/form_builder.admin.inc',
34 'type' => MENU_CALLBACK,
35 );
36
37 $items['admin/build/form-builder/remove'] = array(
38 'title' => 'Remove field',
39 'description' => 'Remove a field from a form.',
40 'page callback' => 'form_builder_remove_page',
41 'access callback' => 'form_builder_menu_field_access',
42 'access arguments' => array('remove', 4, 5, 6),
43 'file' => 'includes/form_builder.admin.inc',
44 'type' => MENU_CALLBACK,
45 );
46
47 $items['admin/build/form-builder/json'] = array(
48 'title' => 'JSON representation',
49 'description' => 'Display a form field as a JSON string.',
50 'page callback' => 'form_builder_field_json',
51 'access callback' => 'form_builder_menu_field_access',
52 'access arguments' => array('view', 4, 5, 6),
53 'file' => 'includes/form_builder.admin.inc',
54 'type' => MENU_CALLBACK,
55 );
56
57 return $items;
58 }
59
60 /**
61 * Implementation of hook_theme().
62 */
63 function form_builder_theme() {
64 return array(
65 'form_builder_preview' => array(
66 'arguments' => array('form' => NULL),
67 'file' => 'includes/form_builder.admin.inc',
68 ),
69 'form_builder_element_prefix' => array(
70 'arguments' => array('element' => NULL),
71 'file' => 'includes/form_builder.admin.inc',
72 ),
73 'form_builder_element_suffix' => array(
74 'arguments' => array('element' => NULL),
75 'file' => 'includes/form_builder.admin.inc',
76 ),
77 'form_builder_empty_fieldset' => array(
78 'arguments' => array(),
79 'file' => 'includes/form_builder.admin.inc',
80 ),
81 'form_builder_field_configure' => array(
82 'arguments' => array('form' => NULL),
83 'file' => 'includes/form_builder.admin.inc',
84 ),
85 'form_builder_field_palette' => array(
86 'arguments' => array('fields' => NULL, 'groups' => NULL, 'form_type' => NULL, 'form_id' => NULL),
87 'file' => 'includes/form_builder.admin.inc',
88 ),
89 );
90 }
91
92 /**
93 * Implementation of hook_block().
94 */
95 function form_builder_block($op = 'list', $delta = 0, $edit = array()) {
96 if ($op == 'list') {
97 $blocks['fields'] = array(
98 'info' => t('Form builder fields'),
99 'weight' => 0,
100 );
101
102 return $blocks;
103 }
104
105 if ($op == 'view') {
106 switch($delta) {
107 case 'fields':
108 if ($active = form_builder_active_form()) {
109 $fields = form_builder_get_form_type($active['form_type']);
110 $groups = module_invoke_all('form_builder_palette_groups');
111 // TODO: We shouldn't have to clear the cache here.
112 $form = form_builder_cache_load($active['form_type'], $active['form_id'], NULL, TRUE);
113 $active_fields = form_builder_get_element_ids($form);
114 foreach ($fields as $key => $field) {
115 if ($field['unique'] && in_array($key, $active_fields)) {
116 $fields[$key]['in_use'] = TRUE;
117 }
118 if ($field['addable'] == FALSE) {
119 unset($fields[$key]);
120 }
121 }
122 return array(
123 'content' => theme('form_builder_field_palette', $fields, $groups, $active['form_type'], $active['form_id']),
124 );
125 }
126 break;
127 }
128 }
129 }
130
131 /**
132 * Access callback for field configuration, viewing, addition, and deletion.
133 */
134 function form_builder_menu_field_access($op, $form_type, $form_id, $element_id) {
135 module_load_include('inc', 'form_builder', 'includes/form_builder.api');
136 module_load_include('inc', 'form_builder', 'includes/form_builder.cache');
137 $element = form_builder_cache_field_load($form_type, $form_id, $element_id);
138 $access = FALSE;
139
140 if ($op == 'add' || $op == 'view') {
141 $access = TRUE;
142 }
143 if ($op == 'configure' && !empty($element['#form_builder']['configurable'])) {
144 $access = TRUE;
145 }
146 if ($op == 'remove' && !empty($element['#form_builder']['removable'])) {
147 $access = TRUE;
148 }
149
150 $module_accesses = module_invoke_all('form_builder_field_access', $op, $form_type, $form_id, $element);
151 if (!empty($module_accesses)) {
152 $access = array_pop($module_accesses);
153 }
154
155 return $access;
156
157 }
158
159 /**
160 * Implementation of hook_form_builder_properties().
161 */
162 function form_builder_form_builder_properties($form_type) {
163 module_load_include('inc', 'form_builder', 'includes/form_builder.properties');
164
165 return array(
166 'key' => array(
167 'form' => 'form_builder_property_key_form',
168 ),
169 'title' => array(
170 'form' => 'form_builder_property_title_form',
171 ),
172 'description' => array(
173 'form' => 'form_builder_property_description_form',
174 ),
175 'weight' => array(
176 'form' => 'form_builder_property_weight_form',
177 ),
178 'default_value' => array(
179 'form' => 'form_builder_property_default_value_form',
180 ),
181 'markup' => array(
182 'form' => 'form_builder_property_markup_form',
183 ),
184 'input_format' => array(
185 'form' => 'form_builder_property_input_format_form',
186 ),
187 'required' => array(
188 'form' => 'form_builder_property_required_form',
189 ),
190 'options' => array(
191 'form' => 'form_builder_property_options_form',
192 'submit' => array('form_builder_property_options_form_submit'),
193 ),
194 'size' => array(
195 'form' => 'form_builder_property_size_form',
196 ),
197 'rows' => array(
198 'form' => 'form_builder_property_rows_form',
199 ),
200 'cols' => array(
201 'form' => 'form_builder_property_cols_form',
202 ),
203 'field_prefix' => array(
204 'form' => 'form_builder_property_field_prefix_form',
205 ),
206 'field_suffix' => array(
207 'form' => 'form_builder_property_field_suffix_form',
208 ),
209 'collapsible' => array(
210 'form' => 'form_builder_property_collapsible_form',
211 ),
212 'collapsed' => array(
213 'form' => 'form_builder_property_collapsed_form',
214 ),
215 );
216 }
217
218 /**
219 * Implementation of hook_form_builder_property_groups().
220 */
221 function form_builder_form_builder_property_groups($form_type) {
222 return array(
223 'default' => array(
224 'weight' => 0,
225 'title' => t('Properties'),
226 ),
227 'hidden' => array(
228 'weight' => 100,
229 'title' => t('Advanced'),
230 'collapsed' => TRUE,
231 'collapsible' => TRUE,
232 ),
233 'display' => array(
234 'weight' => 1,
235 'title' => t('Display'),
236 ),
237 'options' => array(
238 'weight' => 2,
239 'title' => t('Options'),
240 ),
241 'validation' => array(
242 'weight' => 3,
243 'title' => t('Validation'),
244 ),
245 );
246 }
247
248 /**
249 * Implementation of hook_form_builder_palette_groups().
250 */
251 function form_builder_form_builder_palette_groups() {
252 return array(
253 'default' => array(
254 'weight' => 0,
255 'title' => t('Standard'),
256 ),
257 'special' => array(
258 'weight' => 5,
259 'title' => t('Special'),
260 ),
261 );
262 }
263
264 /**
265 * Implementation of hook_form_builder_validators().
266 */
267 function form_builder_form_builder_validators($form_type) {
268 return array(
269 'form_validate_integer' => array(
270 'form' => 'form_builder_validate_integer',
271 ),
272 'form_validate_decimal' => array(
273 'form' => 'form_builder_validate_decimal',
274 ),
275 'form_validate_email' => array(
276 'form' => 'form_builder_validate_email',
277 ),
278 'form_validate_url' => array(
279 'form' => 'form_builder_validate_url',
280 ),
281 );
282 }
283
284 /**
285 * Static storage of the current type of form being edited (if any).
286 *
287 * @param $new_type_name
288 * The name of the type being edited. If this value is passed in, the static
289 * variable is set. If this parameter is ommited, the current type is
290 * returned. Pass in FALSE to reset current type.
291 */
292 function form_builder_active_form($new_type = NULL, $new_id = NULL) {
293 static $active_form = FALSE;
294
295 if (isset($new_type) && isset($new_id)) {
296 if (!$new_type && !$new_id) {
297 $active_form = FALSE;
298 }
299 else {
300 $active_form['form_type'] = $new_type;
301 $active_form['form_id'] = $new_id;
302 }
303 }
304
305 return $active_form;
306 }
307
308 /**
309 * Generic validation function to check that an element has a integer value.
310 */
311 function form_validate_integer(&$element, &$form_state) {
312 $value = $element['#value'];
313
314 // Remove commas from numbers.
315 $new_value = str_replace(array(' ', ','), '', $element);
316 if (is_int($new_value) && $new_value != $value) {
317 form_set_value($element, $new_value, $form_state);
318 drupal_set_message(t('Commas and spaces were removed from the %title field.', array('%title' => $element['#title'])));
319 }
320 elseif (!is_int($new_value)) {
321 form_error($element, t('The %title field value must be an integer.', array('%title' => $element['#title'])));
322 }
323 }
324
325 /**
326 * Generic validation function to check that an element has a decimal value.
327 */
328 function form_validate_decimal(&$element, &$form_state) {
329 $value = $element['#value'];
330
331 // Remove commas from numbers.
332 $new_value = str_replace(array(' ', ','), '', $element);
333 if (is_int($new_value) && $new_value != $value) {
334 form_set_value($element, $new_value, $form_state);
335 drupal_set_message(t('Commas and spaces were removed from the %title field.', array('%title' => $element['#title'])));
336 }
337 elseif (!is_int($new_value)) {
338 form_error($element, t('The %title field value must be a decimal.', array('%title' => $element['#title'])));
339 }
340 }
341
342 /**
343 * Generic validation function to check for a valid e-mail value.
344 */
345 function form_validate_email(&$element, &$form_state) {
346 if (valid_email_address($element['#value'])) {
347 form_error($element, t('The %title field value must be a valid e-mail address.', array('%title' => $element['#title'])));
348 }
349 }
350
351 /**
352 * Generic validation function to check for a valid url value.
353 */
354 function form_validate_url(&$element, &$form_state) {
355 if (valid_url($element['#value'], isset($element['#absolute_url']) ? $element['#absolute_url'] : TRUE)) {
356 form_error($element, t('The %title field value must be a valid URL.', array('%title' => $element['#title'])));
357 }
358 }

  ViewVC Help
Powered by ViewVC 1.1.2