Projet

Général

Profil

Paste
Télécharger (2,94 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.rules.inc @ 87dbc3bf

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
function webform_validation_get_node_rules($nid) {
23
  $rules = array();
24
  $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));
25
  foreach ($result as $rule) {
26
    $rule['components'] = webform_validation_get_rule_components($rule['ruleid'], $rule['nid']);
27
    $rule['negate'] = (bool) $rule['negate'];
28
    $rules[$rule['ruleid']] = $rule;
29
  }
30
  return $rules;
31
}
32

    
33
/**
34
 * Get an array of components linked to a rule
35
 */
36
function webform_validation_get_rule_components($ruleid, $nid) {
37
  $cids = array();
38
  $components = array();
39
  $result = db_query("SELECT cid FROM {webform_validation_rule_components} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid));
40
  foreach ($result as $row) {
41
    $cids[] = $row->cid;
42
  }
43

    
44
  if ($cids) {
45
    $all_components = webform_validation_get_all_components($nid);
46
    $all_component_keys = array_keys($all_components);
47
    foreach ($cids as $cid) {
48
      if (in_array($cid, $all_component_keys)) {
49
        $components[$cid] = $all_components[$cid];
50
      }
51
    }
52
  }
53
  return $components;
54
}
55

    
56
/**
57
 * Get info on all components that are available on a webform
58
 */
59
function webform_validation_get_all_components($nid) {
60
  $components = array();
61
  $result = db_query("SELECT * FROM {webform_component} WHERE nid = :nid", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
62
  foreach ($result as $row) {
63
    $components[$row['cid']] = $row;
64
  }
65
  return $components;
66
}
67

    
68
/**
69
 * This helper function takes a list of full component info arrays and returns a basic representation of it for output purposes.
70
 */
71
function webform_validation_rule_components_basic($components) {
72
  $ret = array();
73
  if ($components) {
74
    foreach ($components as $cid => $component) {
75
      $ret[$cid] = _webform_filter_xss($component['name']);
76
    }
77
  }
78
  return $ret;
79
}
80

    
81
/**
82
 * Delete a rule and dependencies.
83
 *
84
 * @param int $ruleid
85
 *   The ruleid of the rule to delete.
86
 */
87
function webform_dynamic_delete_rule($ruleid) {
88
  // Delete rule.
89
  db_delete('webform_validation_rule')
90
    ->condition('ruleid', $ruleid)
91
    ->execute();
92
  // Delete rule components.
93
  db_delete('webform_validation_rule_components')
94
    ->condition('ruleid', $ruleid)
95
    ->execute();
96
}