<?php

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

/**
 * Implements 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'");
  foreach ($result as $row) {
    $has_numeric_validator = TRUE;
    $range = $row->data;
    $range = _webform_validation_update_range_syntax($range);
    db_update('webform_validation_rule')
    ->fields(array(
      'data' => $range,
    ))
    ->condition('ruleid', $row->ruleid, '=')
    ->execute();
  }
  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;
}
