Project

General

Profile

Paste
Download (3.85 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.rules.inc @ 65389548

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
function webform_validation_get_rule($ruleid) {
12
  $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));
13
  $rule = $result->fetchAssoc();
14
  $rule['components'] = webform_validation_get_rule_components($ruleid, $rule['nid']);
15
  $rule['negate'] = (bool) $rule['negate'];
16
  return $rule;
17
}
18

    
19
/**
20
 * Get an array of rules assigned to a webform node.
21
 *
22
 * @param int $nid
23
 *   The node ID.
24
 *
25
 * @return array
26
 *   The rules for a node keyed by their rule ID.
27
 *
28
 * @see webform_validation_get_node_rules_assoc()
29
 */
30
function webform_validation_get_node_rules($nid) {
31
  $rules = array();
32
  $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));
33
  foreach ($result as $rule) {
34
    $rule['components'] = webform_validation_get_rule_components($rule['ruleid'], $rule['nid']);
35
    $rule['negate'] = (bool) $rule['negate'];
36
    $rules[$rule['ruleid']] = $rule;
37
  }
38
  return $rules;
39
}
40

    
41
/**
42
 * Get an array of rules assigned to a webform node keyed by their rulename.
43
 *
44
 * @param int $nid
45
 *   The node ID.
46
 *
47
 * @return array
48
 *   The rules for a node keyed by their rulename.
49
 *
50
 * @see webform_validation_get_node_rules()
51
 */
52
function webform_validation_get_node_rules_assoc($nid) {
53
  $assoc_rules = array();
54
  if ($rules = webform_validation_get_node_rules($nid)) {
55
    foreach ($rules as $rule) {
56
      $components = array();
57
      foreach ($rule['components'] as $cid => $component) {
58
        $components[$cid] = $cid;
59
      }
60
      ksort($components);
61
      $rule['components'] = $components;
62
      $assoc_rules[$rule['rulename']] = $rule;
63
    }
64
    ksort($assoc_rules);
65
  }
66
  return $assoc_rules;
67
}
68

    
69
/**
70
 * Get an array of components linked to a rule.
71
 */
72
function webform_validation_get_rule_components($ruleid, $nid) {
73
  $cids = array();
74
  $components = array();
75
  $result = db_query("SELECT cid FROM {webform_validation_rule_components} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid));
76
  foreach ($result as $row) {
77
    $cids[] = $row->cid;
78
  }
79

    
80
  if ($cids) {
81
    $all_components = webform_validation_get_all_components($nid);
82
    $all_component_keys = array_keys($all_components);
83
    foreach ($cids as $cid) {
84
      if (in_array($cid, $all_component_keys)) {
85
        $components[$cid] = $all_components[$cid];
86
      }
87
    }
88
  }
89
  return $components;
90
}
91

    
92
/**
93
 * Get info on all components that are available on a webform.
94
 */
95
function webform_validation_get_all_components($nid) {
96
  $components = array();
97
  $result = db_query("SELECT * FROM {webform_component} WHERE nid = :nid", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
98
  foreach ($result as $row) {
99
    $components[$row['cid']] = $row;
100
  }
101
  return $components;
102
}
103

    
104
/**
105
 * Create basic version of component info.
106
 *
107
 * This helper function takes a list of full component info arrays and returns a
108
 * basic representation of it for output purposes.
109
 */
110
function webform_validation_rule_components_basic($components) {
111
  $ret = array();
112
  if ($components) {
113
    foreach ($components as $cid => $component) {
114
      $ret[$cid] = _webform_filter_xss($component['name']);
115
    }
116
  }
117
  return $ret;
118
}
119

    
120
/**
121
 * Delete a rule and dependencies.
122
 *
123
 * @param int $ruleid
124
 *   The ruleid of the rule to delete.
125
 */
126
function webform_dynamic_delete_rule($ruleid) {
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
}