Projet

Général

Profil

Paste
Télécharger (6,07 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.install @ 65389548

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
130
  // string.
131
  if ($range === "0") {
132
    return "";
133
  }
134

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

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

    
147
/**
148
 * Implements hook_update_N().
149
 */
150

    
151
/**
152
 * Adjust DB schema.
153
 *
154
 * Add column to track negated rules.
155
 * Remove explicit length limit on data field.
156
 * Remove regex escaping of blacklist data.
157
 */
158
function webform_validation_update_7101() {
159
  db_add_field('webform_validation_rule', 'negate', array(
160
    'type' => 'int',
161
    'size' => 'tiny',
162
    'unsigned' => TRUE,
163
    'description' => 'Validate the inverse of the rule',
164
    'not null' => TRUE,
165
    'default' => 0,
166
  ));
167

    
168
  db_change_field('webform_validation_rule', 'data', 'data', array(
169
    'type' => 'text',
170
    'description' => 'Additional rule data',
171
    'not null' => FALSE,
172
  ));
173

    
174
  $replace = array('.', '\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{',
175
    '}', '=', '!', '<', '>', '|', ':', '-', '/',
176
  );
177
  $search = array();
178
  foreach ($replace as $value) {
179
    $search[] = '\\' . $value;
180
  }
181
  $result = db_query("SELECT DISTINCT data FROM {webform_validation_rule} WHERE validator = 'blacklist'");
182
  foreach ($result as $row) {
183
    $data_new = str_replace($search, $replace, $row->data);
184
    if ($row->data !== $data_new) {
185
      db_update('webform_validation_rule')
186
        ->fields(array(
187
          'data' => $data_new,
188
        ))
189
        ->condition('data', $row->data, '=')
190
        ->execute();
191
    }
192
  }
193
}
194

    
195
/**
196
 * Rename oneoftwo and oneofseveral to someofseveral and default data to 1.
197
 */
198
function webform_validation_update_7104() {
199
  db_update('webform_validation_rule')
200
    ->fields(array(
201
      'validator' => 'someofseveral',
202
      'data' => 1,
203
    ))
204
    ->condition('validator', array('oneoftwo', 'oneofseveral'), 'IN')
205
    ->execute();
206
}
207

    
208
/**
209
 * Add a weight field to webform_validation rules, so they can be sorted.
210
 */
211
function webform_validation_update_7105() {
212
  $spec = array(
213
    'description' => 'Weight of the rule order.',
214
    'type' => 'int',
215
    'not null' => TRUE,
216
    'unsigned' => FALSE,
217
    'default' => 0,
218
  );
219
  db_add_field('webform_validation_rule', 'weight', $spec);
220
}
221

    
222
/**
223
 * Alter error_message field to allow longer than 255 chars error messages.
224
 */
225
function webform_validation_update_7106() {
226
  db_change_field('webform_validation_rule', 'error_message', 'error_message', array(
227
    'type' => 'text',
228
    'description' => 'Rule error message',
229
    'not null' => FALSE,
230
  ));
231
}