/[drupal]/contributions/modules/validation_api/validation_api.admin.inc
ViewVC logotype

Contents of /contributions/modules/validation_api/validation_api.admin.inc

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


Revision 1.22 - (show annotations) (download) (as text)
Sun Jul 27 22:51:17 2008 UTC (16 months ago) by tapocol
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.21: +103 -45 lines
File MIME type: text/x-php
Messages can now be overwritten on the field level. Also, the fields admin UI has been turned into a two-step process for adding new fields.
1 <?php
2 // $Id: validation_api.admin.inc,v 1.21 2008/07/24 22:29:03 tapocol Exp $
3
4 /**
5 * @file
6 */
7
8 /**
9 * Admin Page
10 */
11 function validation_api_admin() {
12 $content = array(
13 array(
14 'title' => t('Validators'),
15 'href' => 'admin/build/validation_api/validators',
16 'localized_options' => array('attributes' => array('title' => t('Create and manage validator rules, messages, etc.'))),
17 'description' => t('Create and manage validator rules, messages, etc.'),
18 ),
19 array(
20 'title' => t('Fields'),
21 'href' => 'admin/build/validation_api/fields',
22 'localized_options' => array('attributes' => array('title' => t('Create and manage a form field\'s validator, argument, etc.'))),
23 'description' => t('Create and manage a form field\'s validator, argument, etc.'),
24 ),
25 array(
26 'title' => t('Settings'),
27 'href' => 'admin/build/validation_api/settings',
28 'localized_options' => array('attributes' => array('title' => t('Modify settings for the Validation API system.'))),
29 'description' => t('Modify settings for the Validation API system.'),
30 )
31 );
32
33 return theme('admin_block_content', $content);
34 }
35
36 /**
37 * Settings Form
38 */
39 function validation_api_admin_settings_form() {
40 $form['ajax_submission'] = array(
41 '#type' => 'checkbox',
42 '#title' => t('AJAX Submission on all forms'),
43 '#description' => t('This will validate all forms when submitting via AJAX before allowing the submit to continue.'),
44 '#default_value' => variable_get('ajax_submission', 1),
45 );
46 $form['submit'] = array(
47 '#type' => 'submit',
48 '#value' => t('Save Settings'),
49 );
50
51 return $form;
52 }
53
54 /**
55 * Submit for Settings Form
56 */
57 function validation_api_admin_settings_form_submit($form_id, &$form_state) {
58 variable_set('ajax_submission', $form_state['values']['ajax_submission']);
59
60 drupal_set_message(t('AJAX Submission is %s.', array('%s' => (($form_state['values']['ajax_submission'] == 1) ? t('enabled') : t('disabled')))));
61 }
62
63 /**
64 * Lists all validators for the Admin Page
65 */
66 function validation_api_admin_validators() {
67 $validators = _validation_api_validators();
68
69 if (isset($validators) && is_array($validators) && count($validators) > 0) {
70 $header = array(t('Name'), t('Type'), t('Message'), t('Ops'));
71 $rows = array();
72 $hook_validators = array();
73 $update_str = '';
74 foreach ($validators as $key => $validator) {
75 $update_str = '';
76 if (!empty($validator->module)) {
77 if (!isset($hook_validators[$validator->module])) {
78 $hook_validators[$validator->module] = module_invoke($validator->module, HOOK_ADD_VALIDATOR);
79 }
80 $fields = array('type', 'rule', 'message');
81 $code = _validation_api_export(array($validator), $fields);
82 $hook_code = '';
83 foreach ($fields as $field) {
84 $hook_code .= '$validators[0]->'. $field .' = \''. $hook_validators[$validator->module][$validator->delta]->$field ."';\n";
85 }
86 $hook_code .= 'return $validators;';
87 $update_str = ($code != $hook_code) ? ' '. l(t('update'), 'admin/build/validation_api/validators/update/'. $validator->vavid) : '';
88 }
89 $rows[] = array($validator->name, $validator->type, $validator->message, l(t('edit'), 'admin/build/validation_api/validators/edit/'. $validator->vavid) .' '. l(t('delete'), 'admin/build/validation_api/validators/delete/'. $validator->vavid) . $update_str);
90 }
91
92 $output = theme('table', $header, $rows);
93 }
94 else {
95 $output = t('There are no validators in the database. !add or !import a validator.', array('!add' => l(t('Add'), 'admin/build/validation_api/validators/add'), '!import' => l(t('Import'), 'admin/build/validation_api/validators/import')));
96 }
97
98 return $output;
99 }
100
101 /**
102 * The form for Adding or Editing Validators
103 */
104 function validation_api_admin_validators_form($form_state, $vavid = NULL) {
105 $defaults = array('name' => '', 'type' => '', 'rule' => '', 'message' => '');
106 if (!is_null($vavid)) {
107 drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), array(l(t('Validators'), 'admin/build/validation_api/validators'))));
108 if ($array = db_fetch_array(db_query('SELECT * FROM {validation_api_validators} WHERE vavid = %d', $vavid))) {
109 $defaults = $array;
110 }
111 else {
112 drupal_not_found();
113 exit;
114 }
115 }
116
117 if (isset($defaults['vavid'])) {
118 $form['#redirect'] = 'admin/build/validation_api/validators';
119 $form['vavid'] = array(
120 '#type' => 'value',
121 '#value' => $defaults['vavid'],
122 );
123 }
124 $form['name'] = array(
125 '#type' => 'textfield',
126 '#title' => t('Name'),
127 '#description' => t('A unique name for this validator.'),
128 '#required' => TRUE,
129 '#maxlength' => 32,
130 '#default_value' => $defaults['name'],
131 );
132 $form['type'] = array(
133 '#type' => 'select',
134 '#title' => t('Type'),
135 '#description' => t('The language to run your code in the rule.'),
136 '#options' => array('php' => 'PHP', 'regex' => 'Regular Expression'),
137 '#required' => TRUE,
138 '#default_value' => $defaults['type'],
139 );
140 $form['rule'] = array(
141 '#type' => 'textarea',
142 '#title' => t('Rule'),
143 '#description' => t('See the help section above this form.'),
144 '#required' => TRUE,
145 '#rows' => 3,
146 '#default_value' => $defaults['rule'],
147 );
148 $form['message'] = array(
149 '#type' => 'textarea',
150 '#title' => t('Default Message'),
151 '#description' => t('This is the default message sent to the user on validation error. However, this is overwritable for each field. To use placeholders, see the help section above this form.'),
152 '#required' => TRUE,
153 '#maxlength' => 256,
154 '#rows' => 3,
155 '#default_value' => $defaults['message'],
156 );
157 $form['submit'] = array(
158 '#type' => 'submit',
159 '#value' => t('Save Validator'),
160 );
161
162 return $form;
163 }
164
165 /**
166 * The Validate function for the Adding and Editing Validator Form
167 */
168 function validation_api_admin_validators_form_validate($form_id, &$form_state) {
169 $values = $form_state['values'];
170
171 // Make sure the type field is a valid input
172 switch ($values['type']) {
173 case 'regex':
174 // Make sure this is valid regex code
175 break;
176 case 'php':
177 // Make sure this is valid PHP code
178 break;
179 default:
180 // Send error when the type field does not use one of the above types
181 form_set_error('type', 'Must be a valid type: regex or php');
182 }
183 }
184
185 /**
186 * The Submit function for the Adding and Editing Validator Form
187 */
188 function validation_api_admin_validators_form_submit($form_id, &$form_state) {
189 $values = $form_state['values'];
190
191 // If vavid is present, then update it. Else, insert a new record
192 if (isset($values['vavid'])) {
193 // Run SQL query to update the vavid. Send message on success. Else, send error.
194 if (db_query('UPDATE {validation_api_validators} SET name = "%s", type = "%s", rule = "%s", message = "%s" WHERE vavid = %d', array($values['name'], $values['type'], $values['rule'], $values['message'], $values['vavid']))) {
195 drupal_set_message(t('%name has been updated', array('%name' => $values['name'])));
196 }
197 else {
198 drupal_set_message(t('%name could not be updated', array('%name' => $values['name'])), 'error');
199 }
200 }
201 else {
202 // Run SQL query to insert a new validator. Send message on success. Else, send error.
203 if (db_query('INSERT INTO {validation_api_validators} (name, type, rule, message) VALUES ("%s", "%s", "%s", "%s")', array($values['name'], $values['type'], $values['rule'], $values['message']))) {
204 drupal_set_message(t('New Validator Added'));
205 }
206 else {
207 drupal_set_message(t('Cound not add validator'), 'error');
208 }
209 }
210 }
211
212 /**
213 * The form for Deleting Validators
214 */
215 function validation_api_admin_validators_delete($form_state, $vavid) {
216 if (!$obj = db_fetch_object(db_query('SELECT name FROM {validation_api_validators} WHERE vavid = %d', $vavid))) {
217 drupal_not_found();
218 exit;
219 }
220 drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), array(l(t('Validators'), 'admin/build/validation_api/validators'))));
221
222 $form['#redirect'] = 'admin/build/validation_api/validators';
223 $form['vavid'] = array(
224 '#type' => 'value',
225 '#value' => $vavid,
226 );
227 $form['name'] = array(
228 '#type' => 'value',
229 '#value' => $obj->name,
230 );
231
232 return confirm_form($form, t('Are you sure you want to delete the validator %name?', array('%name' => $obj->name)), 'admin/build/validation_api/validators', '', t('Delete'));
233 }
234
235 /**
236 * The Submit function for the Validator Delete form
237 */
238 function validation_api_admin_validators_delete_submit($form_id, &$form_state) {
239 $values = $form_state['values'];
240
241 if (db_query('DELETE FROM {validation_api_validators} WHERE vavid = %d', $values['vavid'])) {
242 drupal_set_message(t('%name was deleted', array('%name' => $values['name'])));
243 }
244 else {
245 drupal_set_message(t('%name could not be deleted', array('%name' => $values['name'])));
246 }
247 }
248
249 /**
250 * Lists all fields for the Admin Page
251 */
252 function validation_api_admin_fields() {
253 $result = db_query('SELECT * FROM {validation_api_fields} ORDER BY form_id, form_field');
254 $validators = _validation_api_validators();
255
256 $header = array(t('Validator'), t('Argument'), t('Ops'));
257 while ($obj = db_fetch_object($result)) {
258 $forms[$obj->form_id][$obj->form_field][] = $obj;
259 }
260
261 $output = '';
262 if (isset($forms) && is_array($forms)) {
263 foreach ($forms as $form_id => $fields) {
264 foreach ($fields as $field_name => $field) {
265 $rows = array();
266 foreach ($field as $validator) {
267 $rows[] = array($validators[$validator->vavid]->name, $validator->arg, l(t('edit'), 'admin/build/validation_api/fields/edit/'. $validator->vafid) .' '. l(t('delete'), 'admin/build/validation_api/fields/delete/'. $validator->vafid));
268 }
269 $output .= theme('table', $header, $rows, array(), $form_id .' - '. $field_name);
270 }
271 }
272 }
273 else {
274 $output .= t('There are no fields being validated. !link.', array('!link' => l(t('Add a field'), 'admin/build/validation_api/fields/add')));
275 }
276
277 return $output;
278 }
279
280 /**
281 * The form for Adding or Editing Fields
282 */
283 function validation_api_admin_fields_form($form_state, $vafid = NULL) {
284 $validators = _validation_api_validators();
285
286 // Any validators in the database
287 if (count($validators) == 0) {
288 $form['validators'] = array(
289 '#type' => 'item',
290 '#value' => t('There are no validators in the database. !link.', array('!link' => l(t('Add a validator'), 'admin/build/validation_api/validators/add'))),
291 );
292 }
293
294 // Add Field Step 1
295 elseif ((!isset($form_state['rebuild']) || $form_state['rebuild'] !== TRUE) && is_null($vafid)) {
296 // Set defaults
297 $defaults['form_id'] = isset($_GET['form_id']) ? $_GET['form_id'] : '';
298 $defaults['form_field'] = isset($_GET['form_field']) ? $_GET['form_field'] : '';
299 $defaults['validator'] = isset($_GET['validator']) ? $_GET['validator'] : '';
300
301 $form['field_form_id'] = array(
302 '#type' => 'textfield',
303 '#title' => t('Form ID'),
304 '#description' => t('The ID of the field\'s form (e.g. comment_form). See the help section above this form.'),
305 '#required' => TRUE,
306 '#maxlength' => 64,
307 '#default_value' => $defaults['form_id'],
308 );
309 $form['form_field'] = array(
310 '#type' => 'textfield',
311 '#title' => t('Field Name'),
312 '#description' => t('The name of the field you want to be validated. See the help section above this form.'),
313 '#required' => TRUE,
314 '#maxlength' => 64,
315 '#default_value' => $defaults['form_field'],
316 );
317 foreach ($validators as $key => $validator) {
318 $options[$key] = $validator->name;
319 }
320 $form['validator'] = array(
321 '#type' => 'select',
322 '#title' => t('Validator'),
323 '#description' => t('The Validator to test on the field.'),
324 '#required' => TRUE,
325 '#options' => $options,
326 '#default_value' => $defaults['validator'],
327 );
328 $form['submit'] = array(
329 '#type' => 'submit',
330 '#value' => t('Save Field'),
331 );
332 }
333 else {
334 $form_state['rebuild'] = FALSE;
335
336 // Add Field Step 2 Defaults
337 if (is_null($vafid)) {
338 $form['#redirect'] = 'admin/build/validation_api/fields/add';
339 $defaults = $form_state['values'];
340 $defaults['arg'] = $defaults['allow_empty'] = '';
341 $defaults['message'] = $validators[$defaults['validator']]->message;
342
343 $form['step_back'] = array(
344 '#type' => 'item',
345 '#value' => l(t('Go Back to the previous step'), 'admin/build/validation_api/fields/add', array('query' => array('form_id' => $form_state['storage']['field_form_id'], 'form_field' => $form_state['storage']['form_field'], 'validator' => $form_state['storage']['validator']))),
346 );
347 }
348 // Edit Field Defaults
349 else {
350 if ($array = db_fetch_array(db_query('SELECT * FROM {validation_api_fields} WHERE vafid = %d', $vafid))) {
351 $defaults = $array;
352 $defaults['validator'] = $defaults['vavid'];
353 $defaults['field_form_id'] = $defaults['form_id'];
354
355 $form['#redirect'] = 'admin/build/validation_api/fields';
356 $form['vafid'] = array(
357 '#type' => 'hidden',
358 '#value' => $vafid,
359 );
360 }
361 else {
362 drupal_not_found();
363 exit;
364 }
365 }
366
367 $form['field_form_id'] = array(
368 '#type' => 'item',
369 '#title' => t('Form ID'),
370 '#value' => $defaults['field_form_id'],
371 );
372 $form['form_field'] = array(
373 '#type' => 'item',
374 '#title' => t('Field Name'),
375 '#value' => $defaults['form_field'],
376 );
377 $form['validator'] = array(
378 '#type' => 'item',
379 '#title' => t('Validator'),
380 '#value' => $validators[$defaults['validator']]->name,
381 );
382
383 $form['arg'] = array(
384 '#type' => 'textfield',
385 '#title' => t('Argument'),
386 '#description' => t('See the help section above this form.'),
387 '#maxlength' => 32,
388 '#default_value' => $defaults['arg'],
389 );
390 $form['allow_empty'] = array(
391 '#type' => 'checkbox',
392 '#title' => t('Allow this field to be empty?'),
393 '#description' => t('The validator will not be run if the field is empty. If the field is marked as required, then this option will not affect anything as it will be required either way.'),
394 '#default_value' => $defaults['allow_empty'],
395 );
396 $form['message'] = array(
397 '#type' => 'textfield',
398 '#title' => t('Message'),
399 '#description' => t('If this validator throws an error, then this will be the message returned.'),
400 '#maxlength' => 256,
401 '#default_value' => $defaults['message'],
402 );
403 $form['submit'] = array(
404 '#type' => 'submit',
405 '#value' => t('Save Field'),
406 );
407 }
408
409 return $form;
410 }
411
412 /**
413 * The Validate function for the Adding and Editing Field Form
414 */
415 function validation_api_admin_fields_form_validate($form_id, &$form_state) {
416
417 }
418
419 /**
420 * The Submit function for the Adding and Editing Field Form
421 */
422 function validation_api_admin_fields_form_submit($form_id, &$form_state) {
423 $values = $form_state['values'];
424 if (isset($values['arg']) && isset($values['allow_empty']) && isset($values['message'])) {
425 // If vafid is present, then update it. Else, insert a new record
426 if (isset($values['vafid'])) {
427 // Run SQL query to update the vavid. Send message on success. Else, send error.
428 if (db_query('UPDATE {validation_api_fields} SET arg = "%s", allow_empty = %d, message = "%s" WHERE vafid = %d', array($values['arg'], $values['allow_empty'], $values['message'], $values['vafid']))) {
429 drupal_set_message(t('The field has been updated.'));
430 }
431 else {
432 drupal_set_message(t('The field could not be updated.'), 'error');
433 }
434 }
435 else {
436 // Get values that were stored during the multi-step form.
437 $values['form_id'] = $form_state['storage']['field_form_id'];
438 $values['form_field'] = $form_state['storage']['form_field'];
439 $values['vavid'] = $form_state['storage']['validator'];
440 // Run SQL query to insert a new validator. Send message on success. Else, send error.
441 if (db_query('INSERT INTO {validation_api_fields} (form_id, form_field, vavid, arg, allow_empty, message) VALUES ("%s", "%s", %d, "%s", %d, "%s")', array($values['form_id'], $values['form_field'], $values['vavid'], $values['arg'], $values['allow_empty'], $values['message']))) {
442 drupal_set_message(t('New field added.'));
443 }
444 else {
445 drupal_set_message(t('Could not add field.'), 'error');
446 }
447 }
448 }
449 else {
450 $form_state['storage'] = $form_state['values'];
451 $form_state['rebuild'] = TRUE;
452 }
453 }
454
455 /**
456 * The form for Deleting Fields
457 */
458 function validation_api_admin_fields_delete($form_state, $vafid) {
459 if (!$obj = db_fetch_object(db_query('SELECT f.form_field AS form_field, v.name AS name FROM {validation_api_fields} AS f JOIN {validation_api_validators} AS v ON f.vavid = v.vavid WHERE vafid = %d', $vafid))) {
460 drupal_not_found();
461 exit;
462 }
463 drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), array(l(t('Validators'), 'admin/build/validation_api/fields'))));
464
465 $form['#redirect'] = 'admin/build/validation_api/fields';
466 $form['vafid'] = array(
467 '#type' => 'value',
468 '#value' => $vafid,
469 );
470 $form['form_field'] = array(
471 '#type' => 'value',
472 '#value' => $obj->form_field,
473 );
474
475 return confirm_form($form, t('Are you sure you want to delete the validator %validator from being used on the field %field?', array('%validator' => $obj->name, '%field' => $obj->form_field)), 'admin/build/validation_api/fields', '', t('Delete'));
476 }
477
478 /**
479 * The Submit function for the Field Delete form
480 */
481 function validation_api_admin_fields_delete_submit($form_id, &$form_state) {
482 $values = $form_state['values'];
483
484 if (db_query('DELETE FROM {validation_api_fields} WHERE vafid = %d', $values['vafid'])) {
485 drupal_set_message(t('%field was deleted', array('%field' => $values['form_field'])));
486 }
487 else {
488 drupal_set_message(t('%field could not be deleted', array('%field' => $values['form_field'])));
489 }
490 }
491
492 /**
493 * The Export form for validators. After submit, this will create a textarea for the export code.
494 */
495 function validation_api_admin_export_form($form_state) {
496 $validators = _validation_api_validators();
497 if (isset($validators) && is_array($validators) && count($validators) > 0) {
498 if ($form_state['submitted']) {
499 $form['export_code'] = array(
500 '#type' => 'textarea',
501 '#title' => t('Export Code'),
502 '#default_value' => $form_state['storage'],
503 '#rows' => 10,
504 );
505 }
506
507 foreach ($validators as $key => $validator) {
508 $options[$key] = $validator->name;
509 }
510
511 $form['validators'] = array(
512 '#title' => t('Validators to Export'),
513 '#type' => 'checkboxes',
514 '#options' => $options,
515 );
516 $form['submit'] = array(
517 '#type' => 'submit',
518 '#value' => 'Export',
519 );
520 }
521 else {
522 $form['validators'] = array(
523 '#type' => 'item',
524 '#value' => t('There are no validators in the database. !link.', array('!link' => l(t('Add a validator'), 'admin/build/validation_api/validators/add'))),
525 );
526 }
527
528 return $form;
529 }
530
531 /**
532 * The Submit function for the Validator Export form.
533 */
534 function validation_api_admin_export_form_submit($form_id, &$form_state) {
535 $form_state['storage'] = validation_api_export($form_state['values']['validators']);
536 }
537
538 /**
539 * The Import form for validators.
540 */
541 function validation_api_admin_import_form() {
542 $validators = _validation_api_validators_from_hook();
543
544 if (isset($validators) && is_array($validators) && count($validators) > 0) {
545 foreach ($validators as $key => $validator) {
546 $options[$key] = t('%name from %key', array('%name' => $validator->name, '%key' => $key));
547 }
548
549 $form['validators'] = array(
550 '#title' => t('Import Validators from Modules'),
551 '#type' => 'checkboxes',
552 '#options' => $options,
553 );
554 }
555 else {
556 $form['validators'] = array(
557 '#title' => t('Import Validators from Modules'),
558 '#type' => 'item',
559 '#value' => t('There are no validators to import from modules.'),
560 );
561 }
562
563 $form['code'] = array(
564 '#type' => 'textarea',
565 '#title' => t('Import Code'),
566 '#description' => t('The code for validators.'),
567 '#rows' => 15,
568 );
569 $form['submit'] = array(
570 '#type' => 'submit',
571 '#value' => 'Import',
572 );
573
574 return $form;
575 }
576
577 /**
578 * The Submit function for the Validator Import form.
579 */
580 function validation_api_admin_import_form_submit($form_id, &$form_state) {
581 $validators = eval($form_state['values']['code']);
582 if (isset($validators) && is_array($validators)) {
583 foreach ($validators as $key => $validator) {
584 if (isset($validator->module)) {
585 unset($validators[$key]->module);
586 }
587 if (isset($validator->delta)) {
588 unset($validators[$key]->delta);
589 }
590 }
591 }
592 if (isset($form_state['values']['validators']) && is_array($form_state['values']['validators'])) {
593 foreach ($form_state['values']['validators'] as $key) {
594 $array = explode('-', $key);
595 $module = array_shift($array);
596 $delta = (int) array_shift($array);
597 if (!isset($hook_validators[$module])) {
598 $hook_validators[$module] = module_invoke($module, HOOK_ADD_VALIDATOR);
599 }
600 $validators[$key] = $hook_validators[$module][$delta];
601 $validators[$key]->module = $module;
602 $validators[$key]->delta = $delta;
603 }
604 }
605 if (isset($validators) && is_array($validators)) {
606 _validation_api_import($validators);
607 }
608 else {
609 drupal_set_message(t('Validator(s) were not returned in a proper format. Needs to be an array.'), 'error');
610 }
611 }
612
613 /**
614 * The Clone form for validators.
615 */
616 function validation_api_admin_clone_form() {
617 $validators = _validation_api_validators();
618
619 if (isset($validators) && is_array($validators) && count($validators) > 0) {
620 foreach ($validators as $key => $validator) {
621 $options[$key] = $validator->name;
622 }
623
624 $form['validators'] = array(
625 '#title' => t('Validators to Clone'),
626 '#type' => 'checkboxes',
627 '#options' => $options,
628 );
629 $form['submit'] = array(
630 '#type' => 'submit',
631 '#value' => 'Export',
632 );
633 }
634 else {
635 $form['validators'] = array(
636 '#type' => 'item',
637 '#value' => t('There are no validators in the database. !link.', array('!link' => l(t('Add a validator'), 'admin/build/validation_api/validators/add'))),
638 );
639 }
640
641 return $form;
642 }
643
644 /**
645 * The Submit function for the Validator Clone form.
646 */
647 function validation_api_admin_clone_form_submit($form_id, &$form_state) {
648 _validation_api_clone($form_state['values']['validators']);
649 }
650
651 /**
652 * The form for Updating Validators
653 */
654 function validation_api_admin_update_form($form_state, $vavid) {
655 if (!$obj = db_fetch_object(db_query('SELECT * FROM {validation_api_validators} WHERE vavid = %d', $vavid))) {
656 drupal_not_found();
657 exit;
658 }
659 drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), array(l(t('Validators'), 'admin/build/validation_api/validators'))));
660
661 $form['#redirect'] = 'admin/build/validation_api/validators';
662 $form['vavid'] = array(
663 '#type' => 'value',
664 '#value' => $vavid,
665 );
666 $form['name'] = array(
667 '#type' => 'value',
668 '#value' => $obj->name,
669 );
670 $form['module'] = array(
671 '#type' => 'value',
672 '#value' => $obj->module,
673 );
674 $form['delta'] = array(
675 '#type' => 'value',
676 '#value' => $obj->delta,
677 );
678 $fields = array('type', 'rule', 'message');
679 $form['current_code'] = array(
680 '#type' => 'textarea',
681 '#title' => t('Current code'),
682 '#default_value' => _validation_api_export(array($obj), $fields),
683 );
684 $validators = module_invoke($obj->module, HOOK_ADD_VALIDATOR);
685 $validator = $validators[$obj->delta];
686 $hook_code_value .= '$validators[0]->type = \''. $validator->type ."';\n";
687 $hook_code_value .= '$validators[0]->rule = \''. $validator->rule ."';\n";
688 $hook_code_value .= '$validators[0]->message = \''. $validator->message ."';\n";
689 $hook_code_value .= 'return $validators;';
690 $form['hook_code'] = array(
691 '#type' => 'textarea',
692 '#title' => t('New code'),
693 '#default_value' => $hook_code_value,
694 );
695
696 return confirm_form($form, t('Are you sure you want to update the validator %name?', array('%name' => $obj->form_field)), 'admin/build/validation_api/validators', '', t('Update'));
697 }
698
699 /**
700 * The Submit function for the Validator Update form
701 */
702 function validation_api_admin_update_form_submit($form_id, &$form_state) {
703 $values = $form_state['values'];
704 $hook_validators = module_invoke($values['module'], HOOK_ADD_VALIDATOR);
705 $validator = $hook_validators[$values['delta']];
706
707 if (db_query('UPDATE {validation_api_validators} SET type = "%s", rule = "%s", message = "%s" WHERE vavid = %d', array($validator->type, $validator->rule, $validator->message, $values['vavid']))) {
708 drupal_set_message(t('%name was updated.', array('%name' => $values['name'])));
709 }
710 else {
711 drupal_set_message(t('%name could not be updated.', array('%name' => $values['name'])));
712 }
713 }
714
715 /**
716 * Export function that returns the export code using Validator IDs.
717 *
718 * @param $vavids
719 * An array of validator IDs.
720 *
721 * @return
722 * Returns export code that can be used to import the validator in other applications.
723 */
724 function validation_api_export($vavids) {
725 $wheres = array();
726 for ($i = 0; $i < count($vavids); $i++) {
727 $wheres[] = 'vavid = %d';
728 }
729 $result = db_query('SELECT name, type, rule, message FROM {validation_api_validators} WHERE '. implode(' OR ', $wheres), $vavids);
730
731 $validators = array();
732 while ($validator = db_fetch_object($result)) {
733 $validators[] = $validator;
734 }
735
736 return _validation_api_export($validators);
737 }
738
739 /**
740 * Export helper function that creates and returns the export code from validator objects.
741 *
742 * @param $validators
743 * An array of validator objects.
744 * @param $fields
745 * An array of fields that are included in the export code.
746 *
747 * @return
748 * Returns export code that can be used to import the validator in other applications.
749 */
750 function _validation_api_export($validators, $fields = array('name', 'type', 'rule', 'message')) {
751 $output = '';
752 $i = 0;
753 foreach ($validators as $validator) {
754 foreach ($fields as $field) {
755 $output .= '$validators['. $i .']->'. $field .' = \''. $validator->$field ."';\n";
756 }
757 $i++;
758 }
759 $output .= 'return $validators;';
760
761 return $output;
762 }
763
764 /**
765 * Helper function that inserts validators into the database from an array of validators.
766 *
767 * @param $validators
768 * An array of validators.
769 *
770 * @return
771 * Nothing is returned.
772 */
773 function _validation_api_import($validators) {
774 $db_validators = _validation_api_validators();
775 $validator_names = array();
776 foreach ($db_validators as $validator) {
777 $validator_names[] = $validator->name;
778 }
779 unset($db_validators);
780 $values = array();
781 $args = array();
782 foreach ($validators as $validator) {
783 if (isset($validator->name) && isset($validator->type) && isset($validator->rule) && isset($validator->message)) {
784 $str = '("%s", "%s", "%s", "%s"';
785 if (in_array($validator->name, $validator_names)) {
786 $i = 1;
787 $open_validator = FALSE;
788 do {
789 if (!in_array($validator->name . $i, $validator_names)) {
790 $validator->name .= $i;
791 $open_validator = TRUE;
792 }
793 else {
794 $i++;
795 }
796 } while (!$open_validator);
797 }
798 array_push($args, $validator->name, $validator->type, $validator->rule, $validator->message);
799 if (isset($validator->module) && isset($validator->delta)) {
800 $str .= ', "%s", %d)';
801 array_push($args, $validator->module, $validator->delta);
802 }
803 else {
804 $str .= ', "", 0)';
805 }
806 $values[] = $str;
807 }
808 }
809 if (count($values) > 0) {
810 if (db_query('INSERT INTO {validation_api_validators} (name, type, rule, message, module, delta) VALUES '. implode(', ', $values), $args)) {
811 drupal_set_message(t('Successfully created %count validator(s).', array('%count' => count($validators))));
812 }
813 else {
814 drupal_set_message(t('Unsuccessful at creating validator(s).'), 'error');
815 }
816 }
817 else {
818 drupal_set_message(t('There were not any valid validators to be created.'), 'error');
819 }
820 }
821
822 /**
823 * Helper function for cloning validators. It runs export, then runs import on the returned export code.
824 *
825 * @param $vavids
826 * An array of validator IDs.
827 *
828 * @return
829 * Nothing is returned.
830 */
831 function _validation_api_clone($vavids) {
832 $validators = eval(validation_api_export($vavids));
833 _validation_api_import($validators);
834 }

  ViewVC Help
Powered by ViewVC 1.1.2