You are here

function clientside_validation_update_7101 in Clientside Validation 7.2

Same name and namespace in other branches
  1. 7 clientside_validation.install \clientside_validation_update_7101()

The settings for Clientside Validation were restructured and put into a separate table, This update will create this table.

File

./clientside_validation.install, line 122

Code

function clientside_validation_update_7101() {
  if (!db_table_exists('clientside_validation_settings')) {
    $schema['clientside_validation_settings'] = array(
      'description' => 'Table for storing Clientside Validation Settings',
      'fields' => array(
        'cvsid' => array(
          'description' => 'The primary identifier for the clientside validation settings',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'type' => array(
          'description' => 'The type of setting: content_type, webform or custom',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
          'default' => '',
        ),
        'status' => array(
          'description' => 'The status for this form. 1 for enabled (validate), 0 for disabled (don\'t validate)',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'size' => 'tiny',
        ),
        'form_id' => array(
          'description' => 'The form id these settings apply to',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'settings' => array(
          'type' => 'blob',
          'not null' => TRUE,
          'size' => 'big',
          'translatable' => TRUE,
        ),
      ),
      'indexes' => array(
        'clientside_validation_settings_form_id' => array(
          'form_id',
        ),
        'clientside_validation_settings_type' => array(
          'type',
        ),
        'clientside_validation_settings_form_id_type' => array(
          'form_id',
          'type',
        ),
      ),
      'unique keys' => array(
        'cvs_form_id' => array(
          'form_id',
        ),
      ),
      'primary key' => array(
        'cvsid',
      ),
    );
    db_create_table('clientside_validation_settings', $schema['clientside_validation_settings']);
  }

  // @TODO: Convert old settings to new
  // 1. fill in clientside_validation_default_settings
  $default_settings = array(
    'validate_options' => array(
      'disable_html5' => variable_get('clientside_validation_disable_html5', 1),
      'validate_onsubmit' => variable_get('clientside_validation_validate_onsubmit', 1),
      'validate_onblur' => variable_get('clientside_validation_validate_onblur', 1),
      'validate_onblur_always' => variable_get('clientside_validation_validate_onblur_always', 0),
      'validate_onkeyup' => variable_get('clientside_validation_validate_onkeyup', 1),
      'show_messages' => variable_get('clientside_validation_show_messages', 0),
    ),
    'error' => array(
      'scrollto_errormessage' => variable_get('clientside_validation_scrollto_errormessage', 1),
      'scroll_speed' => variable_get('clientside_validation_scroll_speed', 1000),
      'error_element' => variable_get('clientside_validation_error_element', 'label'),
    ),
    'error_placement' => array(
      'error_placement_default' => variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FORM),
      'jquery_selector' => variable_get('clientside_validation_jquery_selector', ''),
      'custom_error_function' => variable_get('clientside_validation_custom_error_function', ''),
    ),
    'include_hidden' => array(
      'include_hidden' => variable_get('clientside_validation_include_hidden', ''),
      'include_hidden_fields' => FALSE,
      // New setting
      'validate_tabs' => variable_get('clientside_validation_validate_tabs', ''),
    ),
  );
  variable_set('clientside_validation_default_settings', $default_settings);

  // 2. check clientside_validation_error_placement_after_input
  $after_input = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_after_input', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($after_input as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_AFTER_INPUT;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 3. check clientside_validation_error_placement_after_label
  $after_label = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_after_label', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($after_label as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_AFTER_LABEL;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 4. check clientside_validation_error_placement_before_input
  $before_input = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_before_input', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($before_input as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_BEFORE_INPUT;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 5. check clientside_validation_error_placement_before_label
  $before_label = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_before_label', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($before_label as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_BEFORE_LABEL;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 6. check clientside_validation_error_placement_custom_function
  $custom_function = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_custom_function', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($custom_function as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 7. check clientside_validation_error_placement_jquery
  $jquery = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_jquery', ''), -1, PREG_SPLIT_NO_EMPTY);
  $jquery_selector = variable_get('clientside_validation_jquery_selector', '');
  if (!empty($jquery_selector)) {
    foreach ($jquery as $formid) {
      $settings = $default_settings;
      $settings['error_placement_override_default'] = TRUE;
      $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_JQUERY_SELECTOR;
      _clientside_validation_update_7101_save($formid, $settings);
    }
  }

  // 8. check clientside_validation_error_placement_top_of_form
  $top_of_form = preg_split('/(\\r\\n?|\\n)/', variable_get('clientside_validation_error_placement_top_of_form', ''), -1, PREG_SPLIT_NO_EMPTY);
  foreach ($after_input as $formid) {
    $settings = $default_settings;
    $settings['error_placement_override_default'] = TRUE;
    $settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_TOP_OF_FORM;
    _clientside_validation_update_7101_save($formid, $settings);
  }

  // 9. delete old vars? @TODO: good idea?
}