1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides API and management functions for the webform validation rules.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Get a rule entry.
|
10
|
*
|
11
|
* @deprecated in webform_validation:7.x-1.18 and is removed from webform_validation:7.x-2.0. Use webform_validation_rule_load().
|
12
|
* @see https://www.drupal.org/project/webform_validation/issues/3104320
|
13
|
*/
|
14
|
function webform_validation_get_rule($ruleid) {
|
15
|
return webform_validation_rule_load($ruleid);
|
16
|
}
|
17
|
|
18
|
/**
|
19
|
* Get an array of rules assigned to a webform node.
|
20
|
*
|
21
|
* @param int $nid
|
22
|
* The node ID.
|
23
|
*
|
24
|
* @return array
|
25
|
* The rules for a node keyed by their rule ID.
|
26
|
*
|
27
|
* @see webform_validation_get_node_rules_assoc()
|
28
|
*/
|
29
|
function webform_validation_get_node_rules($nid) {
|
30
|
$rules = array();
|
31
|
$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));
|
32
|
foreach ($result as $rule) {
|
33
|
$rule['components'] = webform_validation_get_rule_components($rule['ruleid'], $rule['nid']);
|
34
|
$rule['negate'] = (bool) $rule['negate'];
|
35
|
$rules[$rule['ruleid']] = $rule;
|
36
|
}
|
37
|
return $rules;
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Get an array of rules assigned to a webform node keyed by their rulename.
|
42
|
*
|
43
|
* @param int $nid
|
44
|
* The node ID.
|
45
|
*
|
46
|
* @return array
|
47
|
* The rules for a node keyed by their rulename.
|
48
|
*
|
49
|
* @see webform_validation_get_node_rules()
|
50
|
*/
|
51
|
function webform_validation_get_node_rules_assoc($nid) {
|
52
|
$assoc_rules = array();
|
53
|
if ($rules = webform_validation_get_node_rules($nid)) {
|
54
|
foreach ($rules as $rule) {
|
55
|
$components = array();
|
56
|
foreach ($rule['components'] as $cid => $component) {
|
57
|
$components[$cid] = $cid;
|
58
|
}
|
59
|
ksort($components);
|
60
|
$rule['components'] = $components;
|
61
|
$assoc_rules[$rule['rulename']] = $rule;
|
62
|
}
|
63
|
ksort($assoc_rules);
|
64
|
}
|
65
|
return $assoc_rules;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Get an array of components linked to a rule.
|
70
|
*/
|
71
|
function webform_validation_get_rule_components($ruleid, $nid) {
|
72
|
$cids = array();
|
73
|
$components = array();
|
74
|
$result = db_query("SELECT cid FROM {webform_validation_rule_components} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid));
|
75
|
foreach ($result as $row) {
|
76
|
$cids[] = $row->cid;
|
77
|
}
|
78
|
|
79
|
if ($cids) {
|
80
|
$all_components = webform_validation_get_all_components($nid);
|
81
|
$all_component_keys = array_keys($all_components);
|
82
|
foreach ($cids as $cid) {
|
83
|
if (in_array($cid, $all_component_keys)) {
|
84
|
$components[$cid] = $all_components[$cid];
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
return $components;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Get info on all components that are available on a webform.
|
93
|
*/
|
94
|
function webform_validation_get_all_components($nid) {
|
95
|
$components = array();
|
96
|
$result = db_query("SELECT * FROM {webform_component} WHERE nid = :nid", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
|
97
|
foreach ($result as $row) {
|
98
|
$components[$row['cid']] = $row;
|
99
|
}
|
100
|
return $components;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Create basic version of component info.
|
105
|
*
|
106
|
* This helper function takes a list of full component info arrays and returns a
|
107
|
* basic representation of it for output purposes.
|
108
|
*/
|
109
|
function webform_validation_rule_components_basic($components) {
|
110
|
$ret = array();
|
111
|
if ($components) {
|
112
|
foreach ($components as $cid => $component) {
|
113
|
$ret[$cid] = _webform_filter_xss($component['name']);
|
114
|
}
|
115
|
}
|
116
|
return $ret;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Delete a rule and dependencies.
|
121
|
*
|
122
|
* @param int $ruleid
|
123
|
* The ruleid of the rule to delete.
|
124
|
*/
|
125
|
function webform_dynamic_delete_rule($ruleid) {
|
126
|
$transaction = db_transaction();
|
127
|
// Delete rule.
|
128
|
db_delete('webform_validation_rule')
|
129
|
->condition('ruleid', $ruleid)
|
130
|
->execute();
|
131
|
// Delete rule components.
|
132
|
db_delete('webform_validation_rule_components')
|
133
|
->condition('ruleid', $ruleid)
|
134
|
->execute();
|
135
|
}
|