clientside_validation.admin.inc 23.7 KB
Newer Older
1
2
<?php
/**
Jelle Sebreghts's avatar
Jelle Sebreghts committed
3
4
 * @file
 * Admin settings for Clientside Validation
5
6
 */

Peter Droogmans's avatar
Peter Droogmans committed
7
function clientside_validation_general_settings_form($form_id, $form_state) {
Peter Droogmans's avatar
Peter Droogmans committed
8
  drupal_set_title('Edit general settings');
9

10
11
  //jquery.validate.js settings
  $form['clientside_validation_min'] = array(
12
13
14
15
16
17
18
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Use minified version of jquery.validate.js'),
    '#description' => t('You can use the minified version of the library.'),
  );

19
  $form['clientside_validation_min']['clientside_validation_use_minified'] = array(
20
21
    '#type' => 'radios',
    '#options' => array(
22
      '1' => t('Yes'),
23
24
25
26
27
28
      '0' => t('No'),
    ),
    '#default_value' => variable_get('clientside_validation_use_minified', 0),
    '#title' => t('Use minified version?'),
  );

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
  //Page settings
  $form['clientside_validation_pages'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Paths'),
    '#description' => t('Include or exclude paths for Clientside Validation.'),
  );

  $form['clientside_validation_pages']['clientside_validation_filter_paths'] = array(
    '#type' => 'radios',
    '#options' => array(
      CLIENTSIDE_VALIDATION_EXCLUDE_PATHS => t('Add Clientside Validation on all paths except those specified below'),
      CLIENTSIDE_VALIDATION_INCLUDE_PATHS => t('Only add Clientside Validation on the paths specified below'),
    ),
    '#default_value' => variable_get('clientside_validation_filter_paths', CLIENTSIDE_VALIDATION_EXCLUDE_PATHS),
    '#title' => t('Include or exclude paths'),
  );

  $form['clientside_validation_pages']['clientside_validation_path_list'] = array(
    '#type' => 'textarea',
    '#default_value' => variable_get('clientside_validation_path_list', ''),
    '#title' => t("Enter paths"),
    '#description' => t("Enter one page per line as Drupal paths.
      The '*' character is a wildcard. Example paths are %blog for the blog page and
      %blog-wildcard for every personal blog. %front is the front page.",
      array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
  );

  //Form settings
  $form['clientside_validation_validate_forms'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Forms to validate'),
  );

  $form['clientside_validation_validate_forms']['clientside_validation_validate_all'] = array(
    '#type' => 'radios',
    '#default_value' => variable_get('clientside_validation_validate_all', CLIENTSIDE_VALIDATION_VALIDATE_ALL),
    '#title' => t('Select the forms to validate'),
    '#options' => array(
      CLIENTSIDE_VALIDATION_VALIDATE_ALL => t('Validate all forms'),
      CLIENTSIDE_VALIDATION_VALIDATE_SPECIFIC => t('Only validate forms listed below'),
      CLIENTSIDE_VALIDATION_VALIDATE_ALL_EXCEPT => t('Validate all forms except those listed below')
    )
  );

  $form['clientside_validation_validate_forms']['clientside_validation_validate_specific'] = array(
    '#type' => 'textarea',
    '#default_value' => variable_get('clientside_validation_validate_specific', ''),
    '#title' => t("Enter form IDs below"),
Peter Droogmans's avatar
Peter Droogmans committed
81
    '#description' => t('You can specify form IDs (one per line, use _) of forms that should or should not be validated.'),
82
83
84
85
86
87
88
89
90
91
    '#states' => array(
      'invisible' => array(
       'input[name="clientside_validation_validate_all"]' => array('value' => (string)CLIENTSIDE_VALIDATION_VALIDATE_ALL),
      ),
    ),
  );
  $form = system_settings_form($form);
  return $form;
}

Peter Droogmans's avatar
Peter Droogmans committed
92
function clientside_validation_settings_form($form, $form_state, $cvs_formid = NULL, $cvs_type = 'default') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
93
  $settings = clientside_validation_settings_load($cvs_type, $cvs_formid, TRUE);
94

95
96
97
98
  $form['new'] = array(
    '#type' => 'value',
    '#value' => FALSE,
  );
Peter Droogmans's avatar
Peter Droogmans committed
99
100
  if ($cvs_type == 'default') {
    drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, TRUE), PASS_THROUGH);
101
  }
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
102
  else {
Peter Droogmans's avatar
Peter Droogmans committed
103
104
105
106
107
108
109
110
111
112
113
    if (!$settings || $settings['settings'] === FALSE || empty($settings['form_id'])) {
      $settings = clientside_validation_settings_load_defaults();
      drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, FALSE), PASS_THROUGH);
      $form['new'] = array(
        '#type' => 'value',
        '#value' => TRUE,
      );
    }
    else {
      drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, TRUE), PASS_THROUGH);
    }
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
114
115
116
117
118
119
120
  }

  // Make sure settings aren't nested.
  if (isset($settings['settings']) && is_array($settings['settings'])) {
    $settings = $settings['settings'];
  }

Peter Droogmans's avatar
Peter Droogmans committed
121
  $form['#tree'] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
122
  $form['cvs_formid'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
123
    '#type' => 'value',
Peter Droogmans's avatar
Peter Droogmans committed
124
    '#value' => $cvs_formid,
Peter Droogmans's avatar
Peter Droogmans committed
125
  );
Peter Droogmans's avatar
Peter Droogmans committed
126
  $form['cvs_type'] = array(
127
    '#type' => 'value',
Peter Droogmans's avatar
Peter Droogmans committed
128
    '#value' => $cvs_type,
129
130
131
132
  );

  //Validate options
  $form['validate_options'] = array(
133
134
135
136
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Validate options'),
Peter Droogmans's avatar
Peter Droogmans committed
137
    '#weight' => 10,
138
139
  );

140
  if (module_exists('clientside_validation_html5')) {
141
    $form['validate_options']['disable_html5'] = array(
142
143
144
145
146
147
      '#type' => 'radios',
      '#options' => array(
        '1' => t('Yes'),
        '0' => t('No'),
      ),
      '#title' => t('Replace HTML5 validation with Clientside Validation'),
Peter Droogmans's avatar
Peter Droogmans committed
148
      '#default_value' => $settings['validate_options']['disable_html5'],
149
150
151
      '#description' => t('Replace HTML5 validation with Clientside Validation on forms that have Clientside Validation enabled'),
    );
  }
152

153
  $form['validate_options']['validate_onsubmit'] = array(
154
155
156
157
158
159
160
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on submit'),
    '#description' => t('"No" disables onsubmit validation, allowing the user to submit whatever he wants, while still validating on keyup/blur/click events (if not specified otherwise).'),
Peter Droogmans's avatar
Peter Droogmans committed
161
    '#default_value' => $settings['validate_options']['validate_onsubmit'],
162
163
  );

164
  $form['validate_options']['validate_onblur'] = array(
165
166
167
168
169
170
171
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on blur'),
    '#description' => t('Validate elements (except checkboxes/radio buttons) on blur. <strong>If nothing is entered, all rules are skipped, except when the field was already marked as invalid.</strong>'),
Peter Droogmans's avatar
Peter Droogmans committed
172
    '#default_value' => $settings['validate_options']['validate_onblur'],
173
174
  );

175
  $form['validate_options']['validate_onblur_always'] = array(
176
177
178
179
180
181
182
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Always validate fields on blur'),
    '#description' => t('Validate elements (except checkboxes/radio buttons) on blur, <strong>even nothing is entered before or if the fields hasn\'t been marked as invalid before.</strong>'),
Peter Droogmans's avatar
Peter Droogmans committed
183
    '#default_value' => $settings['validate_options']['validate_onblur_always'],
184
185
186
187
188
    '#states' => array(
      'visible' => array(
        ':input[name="validate_options[validate_onblur]"]' => array('value' => 1),
      ),
    ),
189
190
  );

191
  $form['validate_options']['validate_onkeyup'] = array(
192
193
194
195
196
197
198
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on key up'),
    '#description' => t('Validate elements on keyup. <strong>As long as the field is not marked as invalid, nothing happens</strong>. Otherwise, all rules are checked on each key up event.'),
Peter Droogmans's avatar
Peter Droogmans committed
199
    '#default_value' => $settings['validate_options']['validate_onkeyup'],
200
201
  );

202
  $form['validate_options']['show_messages'] = array(
203
204
205
206
207
208
209
    '#type' => 'radios',
    '#options' => array(
      '0' => t('Show all error messages'),
      '1' => t('Show only first error message'),
      '2' => t('Show only last error message'),
    ),
    '#title' => t('Show these error messages on validation'),
Peter Droogmans's avatar
Peter Droogmans committed
210
    '#default_value' => $settings['validate_options']['show_messages'],
211
    '#description' => t('Warning: Showing only the first or last message only works if the error messages are displayed on top of the form. When they are displayed inline this setting has some bugs.'),
212
213
  );

214
  //Error message settings
215
  $form['error'] = array(
216
217
218
219
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Error message settings'),
Peter Droogmans's avatar
Peter Droogmans committed
220
    '#weight' => 20,
221
222
  );

223
  $form['error']['prefix'] = array(
224
225
226
    '#title' => t('Field name prefix'),
    '#description' => t('The prefix of the field name in the error messages.'),
    '#type' => 'textfield',
Peter Droogmans's avatar
Peter Droogmans committed
227
    '#default_value' => $settings['error']['prefix'],
228
229
  );

230
  $form['error']['suffix'] = array(
231
232
233
    '#title' => t('Field name suffix'),
    '#description' => t('The suffix of the field name in the error messages.'),
    '#type' => 'textfield',
Peter Droogmans's avatar
Peter Droogmans committed
234
    '#default_value' => $settings['error']['suffix'],
235
236
  );

237
  $form['error']['example_image'] = array(
238
239
    '#type' => 'item',
    '#title' => t('Example'),
240
    '#markup' => '<img id="example_image" src="' . base_path() . drupal_get_path('module', 'clientside_validation') . '/errormsg.png" alt="' . t('Error message example') . '" />',
241
242
243
    '#description' => t('Filling in double quotes in both the above fields will give this result.'),
  );

244
  $form['error']['scrollto_errormessage'] = array(
245
246
247
    '#title' => t('Scroll to error message'),
    '#description' => t('If checked, the page will automatically scroll to the error messages when validation fails.'),
    '#type' => 'checkbox',
Peter Droogmans's avatar
Peter Droogmans committed
248
    '#default_value' => $settings['error']['scrollto_errormessage'],
249
250
  );

251
  $form['error']['scroll_speed'] = array(
252
253
254
    '#title' => t('Scroll speed'),
    '#description' => t('The scroll speed in milliseconds'),
    '#type' => 'textfield',
Peter Droogmans's avatar
Peter Droogmans committed
255
    '#default_value' => $settings['error']['scroll_speed'],
256
257
    '#states' => array(
      'visible' => array(
258
        ':input[name="error[scrollto_errormessage]"]' => array('checked' => TRUE),
259
260
261
262
      ),
    ),
  );

263
  $form['error']['error_element'] = array(
264
265
266
267
    '#title' => t('Error Element'),
    '#description' => t('The HTML element to wrap the errors in. Defaults to label.'),
    '#type' => 'select',
    '#options' => drupal_map_assoc(array('label', 'span', 'div')),
Peter Droogmans's avatar
Peter Droogmans committed
268
    '#default_value' => $settings['error']['error_element'],
269
270
  );

271
272
  if (module_exists('fapi_validation') && module_exists('fapi')) {
    $form['error']['scroll_speed'] += array('#rules' => array('numeric'));
273
274
  }

275
  //Error Placement
276
  $form['error_placement'] = array(
277
278
279
280
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Error message placement'),
Peter Droogmans's avatar
Peter Droogmans committed
281
    '#weight' => 30,
282
283
  );

284
  $form['error_placement']['error_placement_default'] = array(
285
286
    '#type' => 'select',
    '#title' => t('Default location'),
287
288
289
    '#description' => t('Default location to show the error messages.
      If you choose "jQuery selector", provide a valid jQuery selector in the appropriate textfield.
      If you choose "Custom function", provide the name of the custom javascript function.
290
291
292
      The function needs to be declared on the Drupal.clientsideValidation object prototype. You can find
      further details in the clientside_validation.api.js file.
      This function will be given to !errorPlacement.',
293
294
      array(
        '!errorPlacement' =>
Jelle Sebreghts's avatar
Jelle Sebreghts committed
295
        l(t('the errorPlacement option of jQuery.validate()'), 'http://docs.jquery.com/Plugins/Validation/validate#options')
296
297
      )
    ),
298
299
300
301
302
303
304
305
    '#options' => array(
      CLIENTSIDE_VALIDATION_JQUERY_SELECTOR => t('jQuery selector'),
      CLIENTSIDE_VALIDATION_TOP_OF_FORM => t('Top of form'),
      CLIENTSIDE_VALIDATION_BEFORE_LABEL => t('Before label'),
      CLIENTSIDE_VALIDATION_AFTER_LABEL => t('After label'),
      CLIENTSIDE_VALIDATION_BEFORE_INPUT => t('Before input'),
      CLIENTSIDE_VALIDATION_AFTER_INPUT => t('After input'),
      CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM => t('Top of first form'),
306
      CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION => t('Custom function'),
307
    ),
Peter Droogmans's avatar
Peter Droogmans committed
308
    '#default_value' => $settings['error_placement']['error_placement_default'],
309
310
  );

311
  $form['error_placement']['jquery_selector'] = array(
312
313
314
315
316
    '#type' => 'textfield',
    '#title' => t('jQuery selector'),
    '#description' => t('Enter a jQuery selector here if you selected "jQuery selector" in the previous step.
                      The error messages will be shown in this div if it exists.
                      If it doesn\'t, error messages will be shown above the first form on the page.'),
Peter Droogmans's avatar
Peter Droogmans committed
317
    '#default_value' => $settings['error_placement']['jquery_selector'],
Peter Droogmans's avatar
Peter Droogmans committed
318
319
320
321
322
    '#states' => array(
      'visible' => array(
        ':input[name="error_placement[error_placement_default]"]' => array('value' => CLIENTSIDE_VALIDATION_JQUERY_SELECTOR),
      ),
    ),
323
324
  );

325
  $form['error_placement']['custom_error_function'] = array(
326
327
328
329
330
331
    '#type' => 'textfield',
    '#title' => t('Custom function name'),
    '#description' => t('If you selected "Custom function" in the previous step, provide the name of the custom javascript function.
      This function will be given to !errorPlacement',
      array(
        '!errorPlacement' =>
Jelle Sebreghts's avatar
Jelle Sebreghts committed
332
        l(t('the errorPlacement option of jQuery.validate()'), 'http://docs.jquery.com/Plugins/Validation/validate#options')
333
      )),
Peter Droogmans's avatar
Peter Droogmans committed
334
    '#default_value' => $settings['error_placement']['custom_error_function'],
Peter Droogmans's avatar
Peter Droogmans committed
335
336
337
338
339
    '#states' => array(
      'visible' => array(
        ':input[name="error_placement[error_placement_default]"]' => array('value' => CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION),
      ),
    ),
340
341
342
  );

  //Hidden fields and vertical tabs
343
  $form['include_hidden'] = array(
344
    '#type' => 'fieldset',
345
    '#title' => t('Hidden fields and vertical tabs'),
346
347
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
Peter Droogmans's avatar
Peter Droogmans committed
348
    '#weight' => 40,
349
350
  );

Peter Droogmans's avatar
Peter Droogmans committed
351
352
353
354
355
356
  $form['include_hidden']['include_hidden_fields'] = array(
    '#type' => 'checkbox',
    '#default_value' => $settings['include_hidden']['include_hidden_fields'],
    '#title' => t('Validate hidden fields'),
    '#description' => t('Validate hidden fields as well.'),
    '#weight' => 2,
357
  );
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
358

359
  $form['include_hidden']['validate_tabs'] = array(
360
    '#type' => 'checkbox',
Peter Droogmans's avatar
Peter Droogmans committed
361
    '#default_value' => $settings['include_hidden']['validate_tabs'],
362
363
364
365
    '#title' => t('Validate all vertical tabs'),
    '#description' => t('If you check this, all tabs will be validated on submit
                      (including the hidden ones). Warning: if there are other hidden
                      fields (e.g. conditional fields), those will be validated too.'),
Peter Droogmans's avatar
Peter Droogmans committed
366
    '#weight' => 3,
367
  );
Peter Droogmans's avatar
Peter Droogmans committed
368
  if ($cvs_type == 'default') {
Peter Droogmans's avatar
Peter Droogmans committed
369
370
371
372
373
374
375
376
377
    $form['include_hidden']['include_hidden'] = array(
      '#type' => 'textarea',
      '#default_value' => $settings['include_hidden']['include_hidden'],
      '#title' => t("Don't ignore hidden fields on the following forms"),
      '#description' => t('You can specify form IDs (one per line) of forms that should have hidden elements validated.'),
      '#weight' => 1,
    );
  }
  else {
378
379
380
381
382
383
384
385
386
    $fieldsets = array(
      'validate_options' => 'Validate options',
      'error' => 'Error message settings',
      'error_placement' => 'Error message placement',
      'include_hidden' => 'Hidden fields and vertical tabs',
    );
    foreach ($fieldsets as $key => $fieldset) {
      $statefield = ':input[name="' . $key . '_override_default"]';
      $form[$key]['#states'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
387
388
389
390
391
        'visible' => array(
          $statefield => array('checked' => TRUE),
        ),
      );

392
      $form[$key . '_override_default'] = array(
393
        '#type' => 'checkbox',
394
        '#title' => t('Override default options for %fieldset', array('%fieldset' => $fieldset)),
395
        '#description' => t('Check this to override the default validate options'),
396
397
        '#default_value' => isset($settings[$key . '_override_default']) && $settings[$key . '_override_default'] ,
        '#weight' => $form[$key]['#weight'] - 1,
398
399
400
      );
    }
  }
Peter Droogmans's avatar
Peter Droogmans committed
401

402
403
404
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
Peter Droogmans's avatar
Peter Droogmans committed
405
    '#weight' => 50,
406
407
  );

408
409
  return $form;
}
410

411
412
function clientside_validation_settings_form_submit($form, &$form_state) {
  $values = $form_state['values'];
413

Peter Droogmans's avatar
Peter Droogmans committed
414
415
  $cvs_type = $values['cvs_type'];
  $cvs_formid = $values['cvs_formid'];
416
  $new = $values['new'];
Peter Droogmans's avatar
Peter Droogmans committed
417
418
419
420
421
422

  unset($values['submit']);
  unset($values['form_build_id']);
  unset($values['form_token']);
  unset($values['form_id']);
  unset($values['op']);
Peter Droogmans's avatar
Peter Droogmans committed
423
424
  unset($values['cvs_type']);
  unset($values['cvs_formid']);
425
  unset($values['new']);
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
426

Peter Droogmans's avatar
Peter Droogmans committed
427
  if ($cvs_type == 'default') {
428
429
430
    variable_set('clientside_validation_default_settings', $values);
  }
  else {
431
432
433
434
435
436
437
    // Remove not overloaded settings
    foreach (array('validate_options', 'error', 'error_placement', 'include_hidden') as $set) {
      if (isset($values[$set . '_override_default']) && $values[$set . '_override_default'] === 0) {
        unset($values[$set]);
      }
    }
    if (!$new) {
Peter Droogmans's avatar
Peter Droogmans committed
438
      clientside_validation_settings_update($cvs_type, $cvs_formid, $values);
Peter Droogmans's avatar
Peter Droogmans committed
439
440
    }
    else {
441
      clientside_validation_settings_add($cvs_type, $cvs_formid, $values, TRUE);
Peter Droogmans's avatar
Peter Droogmans committed
442
    }
443
  }
444
}
445

Peter Droogmans's avatar
Peter Droogmans committed
446
447
448
449
450
451
452
function _cv_setting_status($status) {
  if ($status) {
    return t('Enabled');
  }
  return t('Disabled');
}

Peter Droogmans's avatar
Peter Droogmans committed
453
function clientside_validation_settings_overview($form, $form_state, $cvs_type) {
Peter Droogmans's avatar
Peter Droogmans committed
454
  $form = array();
Jelle Sebreghts's avatar
Jelle Sebreghts committed
455
  $settings = clientside_validation_settings_load($cvs_type, NULL, TRUE);
456
  $definedforms = array();
Jelle Sebreghts's avatar
Jelle Sebreghts committed
457
  $empty_text = "";
Peter Droogmans's avatar
Peter Droogmans committed
458
  switch ($cvs_type) {
459
460
461
462
    case 'content-types':
      $destination = array('destination' => 'admin/config/validation/clientside_validation/content-types');
      $settinguri = 'admin/config/validation/clientside_validation/content-types/';
      $headerlabel = t('Content type');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
463
      $empty_text = t('No content types available');
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
464

465
466
467
468
469
470
471
472
      $content_types = node_type_get_types();
      foreach ($content_types as $content_type) {
        $definedforms[] = array(
          'id' => $content_type->type,
          'label' => t($content_type->name),
        );
      }
      break;
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
473

474
475
476
477
    case 'webforms':
      $destination = array('destination' => 'admin/config/validation/clientside_validation/webforms');
      $settinguri = 'admin/config/validation/clientside_validation/webforms/';
      $headerlabel = t('Webforms');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
478
      $empty_text = t('No webforms available');
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
479

480
481
482
483
484
485
486
487
      $webform_types = webform_variable_get('webform_node_types');
      $nodes = array();
      if ($webform_types) {
        $nodes = db_select('node', 'n')
          ->fields('n')
          ->condition('n.type', $webform_types, 'IN')
          ->execute()
          ->fetchAllAssoc('nid');
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
488

489
490
491
492
493
494
495
496
        foreach ($nodes as $node) {
          $definedforms[] = array(
            'id' => $node->nid,
            'label' => l($node->title, 'node/' . $node->nid),
          );
        }
      }
      break;
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
497

498
499
500
501
    case 'custom-forms':
      $destination = array('destination' => 'admin/config/validation/clientside_validation/custom-forms');
      $settinguri = 'admin/config/validation/clientside_validation/custom-forms/';
      $headerlabel = t('Form Id');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
502
      $empty_text = t('No custom forms added yet');
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
503

504
505
506
507
508
509
      foreach ($settings as $customform) {
        $definedforms[] = array(
          'id' => $customform->form_id,
          'label' => $customform->form_id,
        );
      }
Peter Droogmans's avatar
Peter Droogmans committed
510
      $form['cvs_type'] = array(
511
        '#type' => 'value',
Peter Droogmans's avatar
Peter Droogmans committed
512
        '#value' => $cvs_type,
513
514
515
516
517
518
519
520
      );
      $form['add_formid'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#title' => t('Add a new custom form'),
        '#description' => t('Enter the formid of the form you want to override.'),
      );
Peter Droogmans's avatar
Peter Droogmans committed
521
      $form['add_formid']['cvs_formid'] = array(
522
523
524
525
526
527
528
529
530
531
        '#title' => t('Form Id'),
        '#type' => 'textfield',
      );
      $form['add_formid']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Add Form Id'),
        '#weight' => 50,
      );
      break;
  }
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
532

Peter Droogmans's avatar
Peter Droogmans committed
533
  $rows = array();
534
535
  if ($definedforms) {
    foreach ($definedforms as $definedform) {
Peter Droogmans's avatar
Peter Droogmans committed
536
      $row = array();
537
538
      $row[] = $definedform['label'];
      if (isset($settings[$definedform['id']]) && $settings[$definedform['id']]->status) {
Peter Droogmans's avatar
Peter Droogmans committed
539
        $cvs_formid = check_plain($definedform['id']);
Peter Droogmans's avatar
Peter Droogmans committed
540
541
542
543
        $row[] = _cv_setting_status(TRUE);
        $actions = array(
          'edit' => array(
            'title' => t('edit'),
Peter Droogmans's avatar
Peter Droogmans committed
544
            'href' => $settinguri . $cvs_formid . '/edit',
Peter Droogmans's avatar
Peter Droogmans committed
545
546
547
548
            'query' => $destination,
          ),
          'disable' => array(
            'title' => t('disable'),
Peter Droogmans's avatar
Peter Droogmans committed
549
            'href' => $settinguri . $cvs_formid . '/disable',
Peter Droogmans's avatar
Peter Droogmans committed
550
551
552
553
            'query' => $destination,
          ),
          'delete' => array(
            'title' => t('delete'),
Peter Droogmans's avatar
Peter Droogmans committed
554
            'href' => $settinguri . $cvs_formid . '/delete',
Peter Droogmans's avatar
Peter Droogmans committed
555
556
557
558
559
560
            'query' => $destination,
          ),
        );
      }
      else {
        $row[] = _cv_setting_status(FALSE);
561
        if (isset($settings[$definedform['id']])) {
Peter Droogmans's avatar
Peter Droogmans committed
562
          $cvs_formid = check_plain($definedform['id']);
Peter Droogmans's avatar
Peter Droogmans committed
563
564
565
          $actions = array(
            'edit' => array(
              'title' => t('edit'),
Peter Droogmans's avatar
Peter Droogmans committed
566
              'href' => $settinguri . $cvs_formid . '/edit',
Peter Droogmans's avatar
Peter Droogmans committed
567
568
569
570
              'query' => $destination,
            ),
            'enable' => array(
              'title' => t('enable'),
Peter Droogmans's avatar
Peter Droogmans committed
571
              'href' => $settinguri . $cvs_formid . '/enable',
Peter Droogmans's avatar
Peter Droogmans committed
572
573
574
575
              'query' => $destination,
            ),
            'delete' => array(
              'title' => t('delete'),
Peter Droogmans's avatar
Peter Droogmans committed
576
              'href' => $settinguri . $cvs_formid . '/delete',
Peter Droogmans's avatar
Peter Droogmans committed
577
578
579
580
581
582
583
584
              'query' => $destination,
            ),
          );
        }
        else {
          $actions = array(
            'edit' => array(
              'title' => t('create'),
585
              'href' => $settinguri . check_plain($definedform['id']) . '/edit',
Peter Droogmans's avatar
Peter Droogmans committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
              'query' => $destination,
            ),
          );
        }
      }
      $row[] = array(
        'data' => array(
          '#theme' => 'links__node_operations',
          '#links' => $actions,
          '#attributes' => array('class' => array('links', 'inline')),
        ),
      );
      $rows[] = $row;
    }
  }

  $header = array(
603
    $headerlabel,
Peter Droogmans's avatar
Peter Droogmans committed
604
605
606
    t('Status'),
    t('Actions'),
  );
Jelle Sebreghts's avatar
Jelle Sebreghts committed
607
  $form[str_replace('-', '', $cvs_type)] = array(
Peter Droogmans's avatar
Peter Droogmans committed
608
609
610
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
Jelle Sebreghts's avatar
Jelle Sebreghts committed
611
    '#empty' => $empty_text,
Peter Droogmans's avatar
Peter Droogmans committed
612
  );
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
613

Peter Droogmans's avatar
Peter Droogmans committed
614
615
616
  return $form;
}

617
618
619
function clientside_validation_settings_overview_submit($form, &$form_state) {
  $values = $form_state['values'];

Peter Droogmans's avatar
Peter Droogmans committed
620
621
622
  $cvs_type = $values['cvs_type'];
  $cvs_formid = $values['cvs_formid'];
  clientside_validation_settings_add($cvs_type, $cvs_formid);
623
624
}

Peter Droogmans's avatar
Peter Droogmans committed
625
626
627
628
function clientside_validation_settings_confirmation_form($form, &$form_state, $cvs_formid, $cvs_type, $action) {
  $form['#cvs_action'] = $action;
  $form['#cvs_type'] = $cvs_type;
  $form['#cvs_formid'] = $cvs_formid;
Peter Droogmans's avatar
Peter Droogmans committed
629
630
631
632

  switch ($action) {
    case 'delete':
      $question = 'Are you sure you want to delete these settings';
633
634
      $info = 'This action cannot be undone.';
      break;
Peter Droogmans's avatar
Peter Droogmans committed
635
636
    case 'enable':
      $question = 'Are you sure you want to enable these settings';
637
638
      $info = 'You can always disable it later.';
      break;
Peter Droogmans's avatar
Peter Droogmans committed
639
640
    case 'disable':
      $question = 'Are you sure you want to disable these settings';
641
642
      $info = 'You can always enable it later.';
      break;
Peter Droogmans's avatar
Peter Droogmans committed
643
  }
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
644

Peter Droogmans's avatar
Peter Droogmans committed
645
646
  return confirm_form(
    $form,
647
    t($question),
Peter Droogmans's avatar
Peter Droogmans committed
648
    'admin/config/validation/clientside_validation', // destination is used
649
    t($info),
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
650
    t(drupal_ucfirst($action)),
Peter Droogmans's avatar
Peter Droogmans committed
651
652
653
654
655
656
    t('Cancel')
  );
}

function clientside_validation_settings_confirmation_form_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
Peter Droogmans's avatar
Peter Droogmans committed
657
658
659
    $action = $form['#cvs_action'];
    $cvs_type = $form['#cvs_type'];
    $cvs_formid = $form['#cvs_formid'];
Peter Droogmans's avatar
Peter Droogmans committed
660
661
662

    switch ($action) {
      case 'delete':
Peter Droogmans's avatar
Peter Droogmans committed
663
664
        clientside_validation_settings_delete($cvs_type, $cvs_formid);
        break;
Peter Droogmans's avatar
Peter Droogmans committed
665
      case 'enable':
Peter Droogmans's avatar
Peter Droogmans committed
666
667
        clientside_validation_settings_enable($cvs_type, $cvs_formid);
        break;
Peter Droogmans's avatar
Peter Droogmans committed
668
      case 'disable':
Peter Droogmans's avatar
Peter Droogmans committed
669
670
        clientside_validation_settings_disable($cvs_type, $cvs_formid);
        break;
Peter Droogmans's avatar
Peter Droogmans committed
671
    }
Peter Droogmans's avatar
Peter Droogmans committed
672
    $form_state['redirect'] = 'admin/config/validation/clientside_validation/' . $cvs_type;
Peter Droogmans's avatar
Peter Droogmans committed
673
674
  }
}