? .svn ? conditional_elements.patch ? webform.module_15.patch ? webform.sql ? components/.svn ? tests/.svn ? translations/.svn Index: webform.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.module,v retrieving revision 1.136 diff -u -p -r1.136 webform.module --- webform.module 20 Jun 2009 23:04:58 -0000 1.136 +++ webform.module 6 Jul 2009 20:39:00 -0000 @@ -1262,16 +1262,56 @@ function webform_client_form($form_state ); } } - // Recursively add components to the form. Microweights keep things in webform order. $microweight = 0.001; - foreach ($component_tree['children'] as $cid => $component) { - $component_value = isset($form_state['values']['submitted'][$component['form_key']]) ? $form_state['values']['submitted'][$component['form_key']] : NULL; - _webform_client_form_add_component($cid, $component, $component_value, $form['submitted'], $form, $submission, $page_num, $enabled, $filter); - if (isset($form['submitted'][$component['form_key']])) { - $form['submitted'][$component['form_key']]['#weight'] += $microweight; - $microweight += 0.001; + $skip_page = FALSE; + // Check prior pagebreak for display rules. + if ($page_num > 1) { + $previous_pagebreak = NULL; + foreach ($node->webform['components'] as $component) { + if ($component['type'] == 'pagebreak' && $component['page_num'] == $page_num - 1) { + $previous_pagebreak = $component; + break; + } } + $skip_page = !_webform_component_rule_check($form_state, $previous_pagebreak, $page_num - 1); + } + if (!$skip_page) { + $an_element_has_been_added = FALSE; + foreach ($component_tree['children'] as $cid => $component) { + $component_value = isset($form_state['values']['submitted'][$component['form_key']]) ? $form_state['values']['submitted'][$component['form_key']] : NULL; + $prior_count = count($form['submitted']); + $show_component = _webform_component_rule_check($form_state, $component, $page_num); + _webform_client_form_add_component($cid, $component, $component_value, $form['submitted'], $form, $submission, $page_num, $enabled, $filter); + // If an element was added to the form, set flag to TRUE to display this page. + if ($show_component && $page_num == $component['page_num'] && $component['type'] != 'pagebreak') { + $an_element_has_been_added = TRUE; + } + if (isset($form['submitted'][$component['form_key']])) { + $form['submitted'][$component['form_key']]['#weight'] += $microweight; + $microweight += 0.001; + } + } + $skip_page = !$an_element_has_been_added; + } + // Skip this page if no elements have been added, or if a pagebreak has a display rule that evaluated FALSE. + if ($skip_page && $form_state['rebuild'] == TRUE) { + unset($form_state['pagebreak']); + // Needed, or the previous button won't work. + if ($form_state['values']['op'] == $prev_page && $page_num > 1) { + $form_state['values']['details']['page_num']--; + } + else { + $form_state['values']['details']['page_num']++; + } + // If we can't display the last page, submit the form and be done. + if ($form_state['values']['details']['page_num'] == $page_count) { + $form_state['values']['op'] = empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text']; + webform_client_form_submit($form, $form_state); + list($path, $args) = $form_state['redirect']; + drupal_goto($path, $args); + } + return webform_client_form($form_state, $node, $submission, $enabled, $preview); } $form['details']['nid'] = array( @@ -1289,6 +1329,20 @@ function webform_client_form($form_state return $form; } +function _webform_component_rule_check($form_state, $component, $page_num) { + $show_component = TRUE; + if (isset($component['extra']['cond_values']) && strlen(trim($component['extra']['cond_values'])) && $component['page_num'] == $page_num) { + $input_value = $form_state['values']['submitted'][$component['extra']['cond_component']]; + $test_values = array_map('trim', explode("\n", $component['extra']['cond_values'])); + $show_component = in_array($input_value, $test_values); + if ($component['extra']['cond_operator'] == '!=') { + $show_component = !$show_component; + } + } + return $show_component; +} + + function _webform_client_form_add_component($cid, $component, $component_value, &$parent_fieldset, &$form, $submission, $page_num, $enabled = FALSE, $filter = TRUE) { // Load with submission information if necessary. if (!$enabled) { Index: includes/webform.components.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/webform/includes/webform.components.inc,v retrieving revision 1.6 diff -u -p -r1.6 webform.components.inc --- includes/webform.components.inc 4 Jul 2009 04:49:20 -0000 1.6 +++ includes/webform.components.inc 6 Jul 2009 20:39:00 -0000 @@ -409,6 +409,75 @@ function webform_component_edit_form($fo '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'), '#weight' => 4, ); + + // Add conditional fields. + $component_options = array(); + $counter = 0; + $last_pagebreak_slice = 0; + foreach ($node->webform['components'] as $cid => $test_component) { + // Only components before the pagebreak can be considered. + if ($test_component['type'] == 'pagebreak') { + $last_pagebreak_slice = $counter; + } + if ($test_component['name'] == $component['name']) { + break; + } + if ($test_component['type'] != 'pagebreak') { + $component_options[$test_component['form_key']] = $test_component['name']; + } + $counter++; + } + if ($component['type'] != 'pagebreak') { + $fieldset_description = t('Create a rule to control whether or not to skip this page.'); + } + else { + $fieldset_description = t('Create a rule to control whether or not to show this form element.'); + } + $component_options = array_slice($component_options, 0, $last_pagebreak_slice, TRUE); + $form['advanced']['conditional'] = array( + '#weight' => 5, + '#type' => 'fieldset', + '#title' => t('Display Rules'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Create a rule to control whether or not to show this form element.'), + '#tree' => FALSE, + ); + $form['advanced']['conditional']['extra'] = array( + '#tree' => TRUE, + ); + $form['advanced']['conditional']['extra']['cond_component'] = array( + '#type' => 'select', + '#title' => t('Component'), + '#options' => $component_options, + '#description' => t('Select another component to decide whether to show or hide this component. You can only select components occurring before the most recent pagebreak.'), + '#default_value' => $component['extra']['cond_component'], + ); + $form['advanced']['conditional']['extra']['cond_operator'] = array( + '#type' => 'select', + '#title' => t('Operator'), + '#options' => array( + '=' => t('Is one of'), + '!=' => t('Is not one of') + ), + '#description' => t('Determines whether the list below is inclusive or exclusive.'), + '#default_value' => $component['extra']['cond_operator'], + ); + $form['advanced']['conditional']['extra']['cond_values'] = array( + '#type' => 'textarea', + '#title' => t('Values'), + '#description' => t('List values, one per line, that will trigger this action. If you leave this blank, this component will always display.'), + '#default_value' => $component['extra']['cond_values'], + ); + if (empty($component_options)) { + $form['advanced']['conditional']['extra']['cond_component']['#options'] = array(''); + $form['advanced']['conditional']['#description'] = t('You cannot set display options for a component appearing before the first pagebreak.'); + foreach ($form['advanced']['conditional']['extra'] as &$el) { + if (is_array($el)) { + $el['#disabled'] = TRUE; + } + } + } // Add the fields specific to this component type: webform_load_components(); // Load all component types.