webform_validation.admin.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @file
  4. * Manages validation rules administration UI
  5. */
  6. /**
  7. * Menu callback function to show an overview of the existing validation rules, and the option to add a rule
  8. */
  9. function webform_validation_manage($node) {
  10. $rules = webform_validation_get_webform_rules($node);
  11. $output = '';
  12. $output .= theme('webform_validation_manage_overview', array('rules' => $rules, 'node' => $node));
  13. $output .= theme('webform_validation_manage_add_rule', array('nid' => $node->nid));
  14. return $output;
  15. }
  16. /**
  17. * Get the list of rules associated with the webform
  18. */
  19. function webform_validation_get_webform_rules($node) {
  20. if (in_array($node->type, webform_variable_get('webform_node_types'))) {
  21. $webform_nid = $node->nid;
  22. $rules = webform_validation_get_node_rules($node->nid);
  23. }
  24. return $rules;
  25. }
  26. /**
  27. * Themable function to list the rules assigned to a webform
  28. */
  29. function theme_webform_validation_manage_overview($variables) {
  30. $rules = $variables['rules'];
  31. $node = $variables['node'];
  32. $header = array(t('Rule name'), t('Validator'), t('Components'), array(
  33. 'data' => t('Operations'),
  34. 'colspan' => 2,
  35. ));
  36. $validators = webform_validation_get_validators_selection();
  37. if (!empty($rules)) {
  38. foreach ($rules as $rule) {
  39. $component_info = webform_validation_rule_components_basic($rule['components']);
  40. $row = array();
  41. $row[] = array(
  42. 'data' => check_plain($rule['rulename']),
  43. );
  44. $row[] = array(
  45. 'data' => $validators[$rule['validator']],
  46. );
  47. $row[] = array(
  48. 'data' => theme('item_list', array('items' => $component_info)),
  49. );
  50. $row[] = array(
  51. 'data' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
  52. );
  53. $row[] = array(
  54. 'data' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
  55. );
  56. $rows[] = $row;
  57. }
  58. }
  59. else {
  60. $rows[][] = array(
  61. 'data' => t('No validation rules available.'),
  62. 'colspan' => 5,
  63. );
  64. }
  65. return theme('table', array('header' => $header, 'rows' => $rows));
  66. }
  67. /**
  68. * Callback function to add or edit a validation rule
  69. */
  70. function webform_validation_manage_rule($form, $form_state, $node, $action = 'add', $validator, $rule = NULL) {
  71. $form = array();
  72. $rule_validator = webform_validation_get_validator_info($validator);
  73. $form['rule'] = array(
  74. '#type' => 'fieldset',
  75. '#title' => ($action == 'edit') ? t('Edit rule') : t('Add rule'),
  76. '#collapsible' => FALSE,
  77. '#collapsed' => FALSE,
  78. );
  79. $form['rule']['validator'] = array(
  80. '#type' => 'hidden',
  81. '#value' => $validator,
  82. );
  83. $form['rule']['action'] = array(
  84. '#type' => 'hidden',
  85. '#value' => $action,
  86. );
  87. if ($action == 'edit' && $rule) {
  88. $form['rule']['ruleid'] = array(
  89. '#type' => 'hidden',
  90. '#value' => $rule['ruleid'],
  91. );
  92. $form['rule']['nid'] = array(
  93. '#type' => 'hidden',
  94. '#value' => $rule['nid'],
  95. );
  96. }
  97. else {
  98. $form['rule']['nid'] = array(
  99. '#type' => 'hidden',
  100. '#value' => $node->nid,
  101. );
  102. }
  103. $form['rule']['rulename'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Rule name'),
  106. '#default_value' => (isset($rule['rulename'])) ? $rule['rulename'] : '',
  107. '#required' => TRUE,
  108. '#size' => 60,
  109. '#maxlength' => 255,
  110. '#weight' => 1,
  111. );
  112. $form['rule']['rule_components'] = array(
  113. '#type' => 'checkboxes',
  114. '#title' => t('Components'),
  115. '#weight' => 3,
  116. '#description' => t('Select the components to be validated by this validation rule'),
  117. '#options' => webform_validation_get_webform_components($node, $validator),
  118. '#default_value' => (isset($rule['components'])) ? array_keys($rule['components']) : array(),
  119. );
  120. if (isset($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
  121. $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
  122. $form['rule']['data'] = array(
  123. '#type' => 'textfield',
  124. '#title' => $rule_validator['custom_data']['label'],
  125. '#description' => $rule_validator['custom_data']['description'],
  126. '#required' => ($required !== FALSE) ? TRUE : FALSE,
  127. '#size' => 60,
  128. '#maxlength' => 255,
  129. '#default_value' => $rule['data'],
  130. '#weight' => 4,
  131. );
  132. }
  133. if (isset($rule_validator['custom_error'])) {
  134. $form['rule']['error_message'] = array(
  135. '#type' => 'textfield',
  136. '#title' => t('Custom error message'),
  137. '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
  138. '#required' => TRUE,
  139. '#size' => 60,
  140. '#maxlength' => 255,
  141. '#default_value' => $rule['error_message'],
  142. '#weight' => 5,
  143. );
  144. }
  145. $form['rule']['submit'] = array(
  146. '#type' => 'submit',
  147. '#value' => (isset($rule['ruleid'])) ? t('Edit rule') : t('Add rule'),
  148. '#weight' => 25,
  149. );
  150. return $form;
  151. }
  152. /**
  153. * Validation handler to add / edit a rule
  154. */
  155. function webform_validation_manage_rule_validate($form, &$form_state) {
  156. $values = $form_state['values'];
  157. if ($values['action'] == 'edit') {
  158. if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
  159. form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
  160. }
  161. }
  162. $rule_validator = webform_validation_get_validator_info($values['validator']);
  163. $selected_components = count(array_filter($values['rule_components']));
  164. // check validator min_components property
  165. if (isset($rule_validator['min_components']) && $rule_validator['min_components'] > 0) {
  166. if ($rule_validator['min_components'] > $selected_components) {
  167. form_set_error('rule_components', format_plural($rule_validator['min_components'], 'You need to select at least @count component', 'You need to select at least @count components'));
  168. }
  169. }
  170. // check validator max_components property
  171. if (isset($rule_validator['max_components']) && $rule_validator['max_components'] > 0) {
  172. if ($rule_validator['max_components'] < $selected_components) {
  173. form_set_error('rule_components', format_plural($rule_validator['max_components'], 'You can select @count component at most', 'You can select @count components at most'));
  174. }
  175. }
  176. }
  177. /**
  178. * Submit handler to add / edit a rule
  179. */
  180. function webform_validation_manage_rule_submit($form, &$form_state) {
  181. $values = $form_state['values'];
  182. webform_validation_rule_save($values);
  183. }
  184. /**
  185. * Get a list of components for a specific webform, filtered by the validator settings
  186. */
  187. function webform_validation_get_webform_components($node, $validator) {
  188. $ret = array();
  189. $components = $node->webform['components'];
  190. $valid_components = webform_validation_valid_component_types($validator);
  191. if ($components) {
  192. foreach ($components as $cid => $component) {
  193. if (in_array($component['type'], $valid_components)) {
  194. $ret[$cid] = $component['name'];
  195. }
  196. }
  197. }
  198. return $ret;
  199. }
  200. /**
  201. * Confirmation form to delete a rule
  202. */
  203. function webform_validation_delete_rule($form, &$form_state, $rule) {
  204. if (isset($rule['ruleid'])) {
  205. $form['ruleid'] = array(
  206. '#type' => 'value',
  207. '#value' => $rule['ruleid'],
  208. );
  209. }
  210. return confirm_form($form,
  211. t('Are you sure you want to delete the rule %name?', array('%name' => $rule['rulename'])),
  212. isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'],
  213. t('This action cannot be undone.'),
  214. t('Delete'),
  215. t('Cancel')
  216. );
  217. }
  218. /**
  219. * Submit handler to delete a rule
  220. */
  221. function webform_validation_delete_rule_submit($form, &$form_state) {
  222. $ruleid = $form_state['values']['ruleid'];
  223. webform_dynamic_delete_rule($ruleid);
  224. module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
  225. }