You are here

function _webform_client_form_rule_check in Webform 6.3

Same name and namespace in other branches
  1. 7.3 webform.module \_webform_client_form_rule_check()

Check if a component should be displayed on the current page.

4 calls to _webform_client_form_rule_check()
webform_client_form in ./webform.module
Client form generation function. If this is displaying an existing submission, pass in the $submission variable with the contents of the submission to be displayed.
webform_client_form_pages in ./webform.module
Handle the processing of pages and conditional logic.
webform_submission_render in includes/webform.submissions.inc
Print a Webform submission for display on a page or in an e-mail.
_webform_client_form_add_component in ./webform.module
Add a component to a renderable array. Called recursively for fieldsets.

File

./webform.module, line 1922

Code

function _webform_client_form_rule_check($node, $component, $page_num, $form_state = NULL, $submission = NULL) {
  $conditional_values = isset($component['extra']['conditional_values']) ? $component['extra']['conditional_values'] : NULL;
  $conditional_component = isset($component['extra']['conditional_component']) && isset($node->webform['components'][$component['extra']['conditional_component']]) ? $node->webform['components'][$component['extra']['conditional_component']] : NULL;
  $conditional_cid = $conditional_component['cid'];

  // Check the rules for this entire page. Note individual page breaks are
  // checked down below in the individual component rule checks.
  $show_page = TRUE;
  if ($component['page_num'] > 1 && $component['type'] != 'pagebreak') {
    foreach ($node->webform['components'] as $cid => $page_component) {
      if ($page_component['type'] == 'pagebreak' && $page_component['page_num'] == $page_num) {
        $show_page = _webform_client_form_rule_check($node, $page_component, $page_num, $form_state, $submission);
        break;
      }
    }
  }

  // Check any parents' visibility rules.
  $show_parent = $show_page;
  if ($show_parent && $component['pid'] && isset($node->webform['components'][$component['pid']])) {
    $parent_component = $node->webform['components'][$component['pid']];
    $show_parent = _webform_client_form_rule_check($node, $parent_component, $page_num, $form_state, $submission);
  }

  // Check the individual component rules.
  $show_component = $show_parent;
  if ($show_component && ($page_num == 0 || $component['page_num'] == $page_num) && $conditional_component && strlen(trim($conditional_values))) {
    $input_values = array();
    if (isset($form_state)) {
      $input_value = isset($form_state['values']['submitted'][$conditional_cid]) ? $form_state['values']['submitted'][$conditional_cid] : NULL;
      $input_values = is_array($input_value) ? $input_value : array(
        $input_value,
      );
    }
    elseif (isset($submission)) {
      $input_values = isset($submission->data[$conditional_cid]['value']) ? $submission->data[$conditional_cid]['value'] : array();
    }
    $test_values = array_map('trim', explode("\n", $conditional_values));
    if (empty($input_values) && !empty($test_values)) {
      $show_component = FALSE;
    }
    else {
      foreach ($input_values as $input_value) {
        if ($show_component = in_array($input_value, $test_values)) {
          break;
        }
      }
    }
    if ($component['extra']['conditional_operator'] == '!=') {
      $show_component = !$show_component;
    }
  }
  return $show_component;
}