webform_validation.install 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * webform_validation installation file
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function webform_validation_schema() {
  10. $schema['webform_validation_rule'] = array(
  11. 'description' => 'Stores rule definitions',
  12. 'fields' => array(
  13. 'ruleid' => array(
  14. 'type' => 'serial',
  15. 'description' => 'Unique identifier for a rule',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'rulename' => array(
  20. 'type' => 'varchar',
  21. 'description' => 'Name for the rule',
  22. 'not null' => TRUE,
  23. 'default' => '',
  24. 'length' => 255,
  25. ),
  26. 'nid' => array(
  27. 'type' => 'int',
  28. 'description' => 'The webform {node}.nid',
  29. 'not null' => TRUE,
  30. 'default' => 0,
  31. 'unsigned' => TRUE,
  32. ),
  33. 'validator' => array(
  34. 'type' => 'varchar',
  35. 'description' => 'The validator key',
  36. 'not null' => TRUE,
  37. 'default' => '',
  38. 'length' => 255,
  39. ),
  40. 'data' => array(
  41. 'type' => 'varchar',
  42. 'description' => 'Additional rule data',
  43. 'not null' => FALSE,
  44. 'length' => 255,
  45. ),
  46. 'error_message' => array(
  47. 'type' => 'varchar',
  48. 'description' => 'Rule error message',
  49. 'not null' => FALSE,
  50. 'length' => 255,
  51. ),
  52. ),
  53. 'primary key' => array('ruleid'),
  54. 'indexes' => array(
  55. 'nid' => array('nid'),
  56. ),
  57. );
  58. $schema['webform_validation_rule_components'] = array(
  59. 'description' => 'Stores components linked to a rule',
  60. 'fields' => array(
  61. 'ruleid' => array(
  62. 'type' => 'int',
  63. 'description' => 'Unique identifier for a rule',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. 'unsigned' => TRUE,
  67. ),
  68. 'cid' => array(
  69. 'type' => 'int',
  70. 'description' => 'The webform component id',
  71. 'not null' => TRUE,
  72. 'default' => 0,
  73. 'unsigned' => TRUE,
  74. ),
  75. ),
  76. 'primary key' => array('ruleid', 'cid'),
  77. );
  78. return $schema;
  79. }
  80. /**
  81. * Implementation of hook_update_X().
  82. */
  83. function webform_validation_update_1() {
  84. $ret = array();
  85. $has_numeric_validator = FALSE;
  86. $result = db_query("SELECT ruleid, data FROM {webform_validation_rule} WHERE validator = 'numeric'");
  87. foreach ($result as $row) {
  88. $has_numeric_validator = TRUE;
  89. $range = $row->data;
  90. $range = _webform_validation_update_range_syntax($range);
  91. db_update('webform_validation_rule')
  92. ->fields(array(
  93. 'data' => $range,
  94. ))
  95. ->condition('ruleid', $row->ruleid, '=')
  96. ->execute();
  97. }
  98. if ($has_numeric_validator) {
  99. $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!');
  100. drupal_set_message($warning_message, 'warning');
  101. }
  102. return $ret;
  103. }
  104. /**
  105. * Helper function: update numeric validator range to new syntax
  106. */
  107. function _webform_validation_update_range_syntax($range) {
  108. // no longer use "0" as indicator for no validation. This should be an empty string
  109. if ($range === "0") {
  110. return "";
  111. }
  112. // replace "0-VAL" with "|VAL" as indicator for less than or equal to
  113. if (substr($range, 0, 2) == "0-" || (substr($range, 0, 3) == "0 -")) {
  114. $range_arr = explode('-', $range);
  115. $range_end = $range_arr[1];
  116. return "|" . trim($range_end);
  117. }
  118. // replace "-" as separator between range values in favor of "|"
  119. $range = str_replace("-", "|", $range);
  120. return $range;
  121. }