You are here

webform_validation.install in Webform Validation 6

Same filename and directory in other branches
  1. 7 webform_validation.install

webform_validation installation file

File

webform_validation.install
View source
<?php

/**
 * @file
 * webform_validation installation file
 */

/**
 * Implementation of hook_install().
 */
function webform_validation_install() {
  drupal_install_schema('webform_validation');
}

/**
 * Implementation of hook_uninstall().
 */
function webform_validation_uninstall() {
  drupal_uninstall_schema('webform_validation');
}

/**
 * Implementation of hook_schema().
 */
function webform_validation_schema() {
  $schema['webform_validation_rule'] = array(
    'description' => 'Stores rule definitions',
    'fields' => array(
      'ruleid' => array(
        'type' => 'serial',
        'description' => 'Unique identifier for a rule',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'rulename' => array(
        'type' => 'varchar',
        'description' => 'Name for the rule',
        'not null' => TRUE,
        'default' => '',
        'length' => 255,
      ),
      'nid' => array(
        'type' => 'int',
        'description' => 'The webform {node}.nid',
        'not null' => TRUE,
        'default' => 0,
        'unsigned' => TRUE,
      ),
      'validator' => array(
        'type' => 'varchar',
        'description' => 'The validator key',
        'not null' => TRUE,
        'default' => '',
        'length' => 255,
      ),
      'data' => array(
        'type' => 'varchar',
        'description' => 'Additional rule data',
        'not null' => FALSE,
        'length' => 255,
      ),
      'error_message' => array(
        'type' => 'varchar',
        'description' => 'Rule error message',
        'not null' => FALSE,
        'length' => 255,
      ),
    ),
    'primary key' => array(
      'ruleid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
  );
  $schema['webform_validation_rule_components'] = array(
    'description' => 'Stores components linked to a rule',
    'fields' => array(
      'ruleid' => array(
        'type' => 'int',
        'description' => 'Unique identifier for a rule',
        'not null' => TRUE,
        'default' => 0,
        'unsigned' => TRUE,
      ),
      'cid' => array(
        'type' => 'int',
        'description' => 'The webform component id',
        'not null' => TRUE,
        'default' => 0,
        'unsigned' => TRUE,
      ),
    ),
    'primary key' => array(
      'ruleid',
      'cid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_update_X().
 */
function webform_validation_update_1() {
  $ret = array();
  $has_numeric_validator = FALSE;
  $result = db_query("SELECT ruleid, data FROM {webform_validation_rule} WHERE validator = 'numeric'");
  while ($row = db_fetch_object($result)) {
    $has_numeric_validator = TRUE;
    $range = $row->data;
    $range = _webform_validation_update_range_syntax($range);
    db_query("UPDATE {webform_validation_rule} SET data = '%s' WHERE ruleid = %d", $range, $row->ruleid);
  }
  if ($has_numeric_validator) {
    $warning_message = t('IMPORTANT: the numeric validation rule range syntax has changed with this release. Existing numeric validation rules were found and updated. Please verify that they still work as expected!');
    drupal_set_message($warning_message, 'warning');
  }
  return $ret;
}

/**
 * Helper function: update numeric validator range to new syntax
 */
function _webform_validation_update_range_syntax($range) {

  // no longer use "0" as indicator for no validation. This should be an empty string
  if ($range === "0") {
    return "";
  }

  // replace "0-VAL" with "|VAL" as indicator for less than or equal to
  if (substr($range, 0, 2) == "0-" || substr($range, 0, 3) == "0 -") {
    $range_arr = explode('-', $range);
    $range_end = $range_arr[1];
    return "|" . trim($range_end);
  }

  // replace "-" as separator between range values in favor of "|"
  $range = str_replace("-", "|", $range);
  return $range;
}

Functions

Namesort descending Description
webform_validation_install Implementation of hook_install().
webform_validation_schema Implementation of hook_schema().
webform_validation_uninstall Implementation of hook_uninstall().
webform_validation_update_1 Implementation of hook_update_X().
_webform_validation_update_range_syntax Helper function: update numeric validator range to new syntax