-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_validation.rules.inc
96 lines (88 loc) · 2.94 KB
/
webform_validation.rules.inc
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
<?php
/**
* @file
* provides API and management functions for the webform validation rules
*/
/**
* Get a rule entry
*/
function webform_validation_get_rule($ruleid) {
$result = db_query("SELECT ruleid, rulename, nid, validator, data, error_message, negate, weight FROM {webform_validation_rule} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid), array('fetch' => PDO::FETCH_ASSOC));
$rule = $result->fetchAssoc();
$rule['components'] = webform_validation_get_rule_components($ruleid, $rule['nid']);
$rule['negate'] = (bool) $rule['negate'];
return $rule;
}
/**
* Get an array of rules assigned to a webform node
*/
function webform_validation_get_node_rules($nid) {
$rules = array();
$result = db_query("SELECT ruleid, rulename, nid, validator, data, error_message, negate, weight FROM {webform_validation_rule} WHERE nid = :nid ORDER BY weight ASC, ruleid DESC", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $rule) {
$rule['components'] = webform_validation_get_rule_components($rule['ruleid'], $rule['nid']);
$rule['negate'] = (bool) $rule['negate'];
$rules[$rule['ruleid']] = $rule;
}
return $rules;
}
/**
* Get an array of components linked to a rule
*/
function webform_validation_get_rule_components($ruleid, $nid) {
$cids = array();
$components = array();
$result = db_query("SELECT cid FROM {webform_validation_rule_components} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid));
foreach ($result as $row) {
$cids[] = $row->cid;
}
if ($cids) {
$all_components = webform_validation_get_all_components($nid);
$all_component_keys = array_keys($all_components);
foreach ($cids as $cid) {
if (in_array($cid, $all_component_keys)) {
$components[$cid] = $all_components[$cid];
}
}
}
return $components;
}
/**
* Get info on all components that are available on a webform
*/
function webform_validation_get_all_components($nid) {
$components = array();
$result = db_query("SELECT * FROM {webform_component} WHERE nid = :nid", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $row) {
$components[$row['cid']] = $row;
}
return $components;
}
/**
* This helper function takes a list of full component info arrays and returns a basic representation of it for output purposes.
*/
function webform_validation_rule_components_basic($components) {
$ret = array();
if ($components) {
foreach ($components as $cid => $component) {
$ret[$cid] = _webform_filter_xss($component['name']);
}
}
return $ret;
}
/**
* Delete a rule and dependencies.
*
* @param int $ruleid
* The ruleid of the rule to delete.
*/
function webform_dynamic_delete_rule($ruleid) {
// Delete rule.
db_delete('webform_validation_rule')
->condition('ruleid', $ruleid)
->execute();
// Delete rule components.
db_delete('webform_validation_rule_components')
->condition('ruleid', $ruleid)
->execute();
}