-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_validation.install
212 lines (199 loc) · 5.71 KB
/
webform_validation.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* @file
* webform_validation installation file
*/
/**
* Implements hook_schema().
*/
function webform_validation_schema() {
$schema['webform_validation_rule'] = array(
'description' => 'Stores rule definitions',
'fields' => array(
'ruleid' => array(
'type' => 'serial',
'description' => 'Unique identifier for a rule',
'unsigned' => TRUE,
'not null' => TRUE,
),
'rulename' => array(
'type' => 'varchar',
'description' => 'Name for the rule',
'not null' => TRUE,
'default' => '',
'length' => 255,
),
'nid' => array(
'type' => 'int',
'description' => 'The webform {node}.nid',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
),
'validator' => array(
'type' => 'varchar',
'description' => 'The validator key',
'not null' => TRUE,
'default' => '',
'length' => 255,
),
'data' => array(
'type' => 'text',
'description' => 'Additional rule data',
'not null' => FALSE,
),
'error_message' => array(
'type' => 'varchar',
'description' => 'Rule error message',
'not null' => FALSE,
'length' => 255,
),
'negate' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'description' => 'Validate the inverse of the rule',
'not null' => TRUE,
'default' => 0,
),
'weight' => array(
'description' => 'Weight of the rule order.',
'type' => 'int',
'not null' => TRUE,
'unsigned' => FALSE,
'default' => 0,
),
),
'primary key' => array('ruleid'),
'indexes' => array(
'nid' => array('nid'),
),
);
$schema['webform_validation_rule_components'] = array(
'description' => 'Stores components linked to a rule',
'fields' => array(
'ruleid' => array(
'type' => 'int',
'description' => 'Unique identifier for a rule',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
),
'cid' => array(
'type' => 'int',
'description' => 'The webform component id',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
),
),
'primary key' => array('ruleid', 'cid'),
);
return $schema;
}
/**
* Implements hook_update_N().
*/
/**
* Update numeric validator range to new syntax.
*/
function webform_validation_update_1() {
$ret = array();
$has_numeric_validator = FALSE;
$result = db_query("SELECT ruleid, data FROM {webform_validation_rule} WHERE validator = 'numeric'");
foreach ($result as $row) {
$has_numeric_validator = TRUE;
$range = $row->data;
$range = _webform_validation_update_range_syntax($range);
db_update('webform_validation_rule')
->fields(array(
'data' => $range,
))
->condition('ruleid', $row->ruleid, '=')
->execute();
}
if ($has_numeric_validator) {
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');
}
return $ret;
}
/**
* Helper function: update numeric validator range to new syntax
*/
function _webform_validation_update_range_syntax($range) {
// no longer use "0" as indicator for no validation. This should be an empty string
if ($range === "0") {
return "";
}
// replace "0-VAL" with "|VAL" as indicator for less than or equal to
if (preg_match('/^0 ?-/', $range)) {
$range_arr = explode('-', $range);
$range_end = $range_arr[1];
return "|" . trim($range_end);
}
// replace "-" as separator between range values in favor of "|"
$range = str_replace("-", "|", $range);
return $range;
}
/**
* Add column to track negated rules.
* Remove explicit length limit on data field.
* Remove regex escaping of blacklist data.
*/
function webform_validation_update_7101() {
db_add_field('webform_validation_rule', 'negate', array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'description' => 'Validate the inverse of the rule',
'not null' => TRUE,
'default' => 0,
));
db_change_field('webform_validation_rule', 'data', 'data', array(
'type' => 'text',
'description' => 'Additional rule data',
'not null' => FALSE,
));
$replace = array('.', '\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '/');
$search = array();
foreach ($replace as $value) {
$search[] = '\\' . $value;
}
$result = db_query("SELECT DISTINCT data FROM {webform_validation_rule} WHERE validator = 'blacklist'");
foreach ($result as $row) {
$data_new = str_replace($search, $replace, $row->data);
if ($row->data !== $data_new) {
db_update('webform_validation_rule')
->fields(array(
'data' => $data_new,
))
->condition('data', $row->data, '=')
->execute();
}
}
}
/**
* Rename oneoftwo and oneofseveral to someofseveral and default data to 1.
*/
function webform_validation_update_7104() {
db_update('webform_validation_rule')
->fields(array(
'validator' => 'someofseveral',
'data' => 1,
))
->condition('validator', array('oneoftwo', 'oneofseveral'), 'IN')
->execute();
}
/**
* Add a weight field to webform_validation rules, so they can be sorted.
*/
function webform_validation_update_7105() {
$spec = array(
'description' => 'Weight of the rule order.',
'type' => 'int',
'not null' => TRUE,
'unsigned' => FALSE,
'default' => 0,
);
db_add_field('webform_validation_rule', 'weight', $spec);
}