Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform_validation / webform_validation.rules.inc @ 76bdcd04

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
 * Create basic version of component info.
70
 *
71
 * This helper function takes a list of full component info arrays and returns a
72
 * basic representation of it for output purposes.
73
 */
74
function webform_validation_rule_components_basic($components) {
75
  $ret = array();
76
  if ($components) {
77
    foreach ($components as $cid => $component) {
78
      $ret[$cid] = _webform_filter_xss($component['name']);
79
    }
80
  }
81
  return $ret;
82
}
83

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