Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform_validation / webform_validation.install @ 87dbc3bf

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
      'weight' => array(
62
        'description' => 'Weight of the rule order.',
63
        'type' => 'int',
64
        'not null' => TRUE,
65
        'unsigned' => FALSE,
66
        'default' => 0,
67
      ),
68
    ),
69
    'primary key' => array('ruleid'),
70
    'indexes' => array(
71
      'nid' => array('nid'),
72
    ),
73
  );
74

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

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

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

    
126
/**
127
 * Helper function: update numeric validator range to new syntax
128
 */
129
function _webform_validation_update_range_syntax($range) {
130
  // no longer use "0" as indicator for no validation. This should be an empty 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
 * Add column to track negated rules.
149
 * Remove explicit length limit on data field.
150
 * Remove regex escaping of blacklist data.
151
 */
152
function webform_validation_update_7101() {
153
  db_add_field('webform_validation_rule', 'negate', array(
154
    'type' => 'int',
155
    'size' => 'tiny',
156
    'unsigned' => TRUE,
157
    'description' => 'Validate the inverse of the rule',
158
    'not null' => TRUE,
159
    'default' => 0,
160
  ));
161

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

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

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

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