Projet

Général

Profil

Paste
Télécharger (5,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.install @ 81b16cc2

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' => 'text',
49
        'description' => 'Rule error message',
50
        'not null' => FALSE,
51
      ),
52
      'negate' => array(
53
        'type' => 'int',
54
        'size' => 'tiny',
55
        'unsigned' => TRUE,
56
        'description' => 'Validate the inverse of the rule',
57
        'not null' => TRUE,
58
        'default' => 0,
59
      ),
60
      'weight' => array(
61
        'description' => 'Weight of the rule order.',
62
        'type' => 'int',
63
        'not null' => TRUE,
64
        'unsigned' => FALSE,
65
        'default' => 0,
66
      ),
67
    ),
68
    'primary key' => array('ruleid'),
69
    'indexes' => array(
70
      'nid' => array('nid'),
71
    ),
72
  );
73

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

    
97
/**
98
 * Implements hook_update_N().
99
 */
100

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

    
125
/**
126
 * Helper function: update numeric validator range to new syntax.
127
 */
128
function _webform_validation_update_range_syntax($range) {
129
  // No longer use "0" as indicator for no validation. This should be an empty string.
130
  if ($range === "0") {
131
    return "";
132
  }
133

    
134
  // Replace "0-VAL" with "|VAL" as indicator for less than or equal to.
135
  if (preg_match('/^0 ?-/', $range)) {
136
    $range_arr = explode('-', $range);
137
    $range_end = $range_arr[1];
138
    return "|" . trim($range_end);
139
  }
140

    
141
  // Replace "-" as separator between range values in favor of "|".
142
  $range = str_replace("-", "|", $range);
143
  return $range;
144
}
145

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

    
161
  db_change_field('webform_validation_rule', 'data', 'data', array(
162
    'type' => 'text',
163
    'description' => 'Additional rule data',
164
    'not null' => FALSE,
165
  ));
166

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

    
186
/**
187
 * Rename oneoftwo and oneofseveral to someofseveral and default data to 1.
188
 */
189
function webform_validation_update_7104() {
190
  db_update('webform_validation_rule')
191
    ->fields(array(
192
      'validator' => 'someofseveral',
193
      'data' => 1,
194
    ))
195
    ->condition('validator', array('oneoftwo', 'oneofseveral'), 'IN')
196
    ->execute();
197
}
198

    
199
/**
200
 * Add a weight field to webform_validation rules, so they can be sorted.
201
 */
202
function webform_validation_update_7105() {
203
  $spec = array(
204
    'description' => 'Weight of the rule order.',
205
    'type' => 'int',
206
    'not null' => TRUE,
207
    'unsigned' => FALSE,
208
    'default' => 0,
209
  );
210
  db_add_field('webform_validation_rule', 'weight', $spec);
211
}
212

    
213
/**
214
 * Alter error_message field to allow longer than 255 chars error messages.
215
 */
216
function webform_validation_update_7106() {
217
  db_change_field('webform_validation_rule', 'error_message', 'error_message', array(
218
    'type' => 'text',
219
    'description' => 'Rule error message',
220
    'not null' => FALSE,
221
  ));
222
}