webform_validation.rules.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @file
  4. * provides API and management functions for the webform validation rules
  5. */
  6. /**
  7. * Get a rule entry
  8. */
  9. function webform_validation_get_rule($ruleid) {
  10. $result = db_query("SELECT ruleid, rulename, nid, validator, data, error_message FROM {webform_validation_rule} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid), array('fetch' => PDO::FETCH_ASSOC));
  11. $rule = $result->fetchAssoc();
  12. $rule['components'] = webform_validation_get_rule_components($ruleid, $rule['nid']);
  13. return $rule;
  14. }
  15. /**
  16. * Get an array of rules assigned to a webform node
  17. */
  18. function webform_validation_get_node_rules($nid) {
  19. $rules = array();
  20. $result = db_query("SELECT ruleid, rulename, nid, validator, data, error_message FROM {webform_validation_rule} WHERE nid = :nid ORDER BY ruleid DESC", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
  21. foreach ($result as $rule) {
  22. $rule['components'] = webform_validation_get_rule_components($rule['ruleid'], $rule['nid']);
  23. $rules[$rule['ruleid']] = $rule;
  24. }
  25. return $rules;
  26. }
  27. /**
  28. * Get an array of components linked to a rule
  29. */
  30. function webform_validation_get_rule_components($ruleid, $nid) {
  31. $cids = array();
  32. $components = array();
  33. $result = db_query("SELECT cid FROM {webform_validation_rule_components} WHERE ruleid = :ruleid", array(':ruleid' => $ruleid));
  34. foreach ($result as $row) {
  35. $cids[] = $row->cid;
  36. }
  37. if ($cids) {
  38. $all_components = webform_validation_get_all_components($nid);
  39. $all_component_keys = array_keys($all_components);
  40. foreach ($cids as $cid) {
  41. if (in_array($cid, $all_component_keys)) {
  42. $components[$cid] = $all_components[$cid];
  43. }
  44. }
  45. }
  46. return $components;
  47. }
  48. /**
  49. * Get info on all components that are available on a webform
  50. */
  51. function webform_validation_get_all_components($nid) {
  52. $components = array();
  53. $result = db_query("SELECT * FROM {webform_component} WHERE nid = :nid", array(':nid' => $nid), array('fetch' => PDO::FETCH_ASSOC));
  54. foreach ($result as $row) {
  55. $components[$row['cid']] = $row;
  56. }
  57. return $components;
  58. }
  59. /**
  60. * This helper function takes a list of full component info arrays and returns a basic representation of it for output purposes.
  61. */
  62. function webform_validation_rule_components_basic($components) {
  63. $ret = array();
  64. if ($components) {
  65. foreach ($components as $cid => $component) {
  66. $ret[$cid] = $component["name"];
  67. }
  68. }
  69. return $ret;
  70. }
  71. /**
  72. * Delete a rule and dependencies
  73. */
  74. function webform_dynamic_delete_rule($ruleid) {
  75. // delete rule
  76. db_delete('webform_validation_rule')
  77. ->condition('ruleid', $ruleid)
  78. ->execute();
  79. // delete rule components
  80. db_delete('webform_validation_rule_components')
  81. ->condition('ruleid', $ruleid)
  82. ->execute();
  83. }