Projet

Général

Profil

Paste
Télécharger (4,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.install @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * webform_validation installation file
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function webform_validation_schema() {
12
  $schema['webform_validation_rule'] = array(
13
    'description' => 'Stores rule definitions',
14
    'fields' => array(
15
      'ruleid' => array(
16
        'type' => 'serial',
17
        'description' => 'Unique identifier for a rule',
18
        'unsigned' => TRUE,
19
        'not null' => TRUE,
20
      ),
21
      'rulename' => array(
22
        'type' => 'varchar',
23
        'description' => 'Name for the rule',
24
        'not null' => TRUE,
25
        'default' => '',
26
        'length' => 255,
27
      ),
28
      'nid' => array(
29
        'type' => 'int',
30
        'description' => 'The webform {node}.nid',
31
        'not null' => TRUE,
32
        'default' => 0,
33
        'unsigned' => TRUE,
34
      ),
35
      'validator' => array(
36
        'type' => 'varchar',
37
        'description' => 'The validator key',
38
        'not null' => TRUE,
39
        'default' => '',
40
        'length' => 255,
41
      ),
42
      'data' => array(
43
        'type' => 'text',
44
        'description' => 'Additional rule data',
45
        'not null' => FALSE,
46
      ),
47
      'error_message' => array(
48
        'type' => 'varchar',
49
        'description' => 'Rule error message',
50
        'not null' => FALSE,
51
        'length' => 255,
52
      ),
53
      'negate' => array(
54
        'type' => 'int',
55
        'size' => 'tiny',
56
        'unsigned' => TRUE,
57
        'description' => 'Validate the inverse of the rule',
58
        'not null' => TRUE,
59
        'default' => 0,
60
      ),
61
    ),
62
    'primary key' => array('ruleid'),
63
    'indexes' => array(
64
      'nid' => array('nid'),
65
    ),
66
  );
67

    
68
  $schema['webform_validation_rule_components'] = array(
69
    'description' => 'Stores components linked to a rule',
70
    'fields' => array(
71
      'ruleid' => array(
72
        'type' => 'int',
73
        'description' => 'Unique identifier for a rule',
74
        'not null' => TRUE,
75
        'default' => 0,
76
        'unsigned' => TRUE,
77
      ),
78
      'cid' => array(
79
        'type' => 'int',
80
        'description' => 'The webform component id',
81
        'not null' => TRUE,
82
        'default' => 0,
83
        'unsigned' => TRUE,
84
      ),
85
    ),
86
    'primary key' => array('ruleid', 'cid'),
87
  );
88
  return $schema;
89
}
90

    
91
/**
92
 * Implements hook_update_N().
93
 */
94

    
95
/**
96
 * Update numeric validator range to new syntax.
97
 */
98
function webform_validation_update_1() {
99
  $ret = array();
100
  $has_numeric_validator = FALSE;
101
  $result = db_query("SELECT ruleid, data FROM {webform_validation_rule} WHERE validator = 'numeric'");
102
  foreach ($result as $row) {
103
    $has_numeric_validator = TRUE;
104
    $range = $row->data;
105
    $range = _webform_validation_update_range_syntax($range);
106
    db_update('webform_validation_rule')
107
    ->fields(array(
108
      'data' => $range,
109
    ))
110
    ->condition('ruleid', $row->ruleid, '=')
111
    ->execute();
112
  }
113
  if ($has_numeric_validator) {
114
    $warning_message = t('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!');
115
    drupal_set_message($warning_message, 'warning');
116
  }
117
  return $ret;
118
}
119

    
120
/**
121
 * Helper function: update numeric validator range to new syntax
122
 */
123
function _webform_validation_update_range_syntax($range) {
124
  // no longer use "0" as indicator for no validation. This should be an empty string
125
  if ($range === "0") {
126
    return "";
127
  }
128

    
129
  // replace "0-VAL" with "|VAL" as indicator for less than or equal to
130
  if (preg_match('/^0 ?-/', $range)) {
131
    $range_arr = explode('-', $range);
132
    $range_end = $range_arr[1];
133
    return "|" . trim($range_end);
134
  }
135

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

    
141
/**
142
 * Add column to track negated rules.
143
 * Remove explicit length limit on data field.
144
 * Remove regex escaping of blacklist data.
145
 */
146
function webform_validation_update_7101() {
147
  db_add_field('webform_validation_rule', 'negate', array(
148
    'type' => 'int',
149
    'size' => 'tiny',
150
    'unsigned' => TRUE,
151
    'description' => 'Validate the inverse of the rule',
152
    'not null' => TRUE,
153
    'default' => 0,
154
  ));
155

    
156
  db_change_field('webform_validation_rule', 'data', 'data', array(
157
    'type' => 'text',
158
    'description' => 'Additional rule data',
159
    'not null' => FALSE,
160
  ));
161

    
162
  $replace = array('.', '\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '/');
163
  $search = array();
164
  foreach ($replace as $value) {
165
    $search[] = '\\' . $value;
166
  }
167
  $result = db_query("SELECT DISTINCT data FROM {webform_validation_rule} WHERE validator = 'blacklist'");
168
  foreach ($result as $row) {
169
    $data_new = str_replace($search, $replace, $row->data);
170
    if ($row->data !== $data_new) {
171
      db_update('webform_validation_rule')
172
        ->fields(array(
173
          'data' => $data_new,
174
        ))
175
        ->condition('data', $row->data, '=')
176
        ->execute();
177
    }
178
  }
179
}