You are here

function webform_conditional_element_add in Webform 7.4

Submit handler for webform_conditional elements to add a new rule or action.

2 string references to 'webform_conditional_element_add'
_webform_conditional_action_expand in includes/webform.conditionals.inc
Helper. Generate form elements for one action.
_webform_conditional_add_expand in includes/webform.conditionals.inc
Helper. Generate the add_subconditional (+) or add + button.

File

includes/webform.conditionals.inc, line 732
Form elements and menu callbacks to provide conditional handling in Webform.

Code

function webform_conditional_element_add($form, &$form_state) {
  $button = $form_state['clicked_button'];
  $parents = $button['#parents'];
  array_pop($parents);
  $rid = array_pop($parents);

  // Recurse through the form values until we find the Webform conditional rules
  // or actions. Save the conditional prior to descending to rules/actions.
  $parent_values =& $form_state['values'];
  $input_values =& $form_state['input'];
  foreach ($parents as $key) {
    if (array_key_exists($key, $parent_values)) {
      $conditional = $parent_values;
      $parent_values =& $parent_values[$key];
    }
    if (array_key_exists($key, $input_values)) {
      $input_values =& $input_values[$key];
    }
  }

  // Split the list of rules/actions in this conditional and inject into the
  // right spot.
  $rids = array_keys($parent_values);
  $offset = array_search($rid, $rids);
  $default_rule = isset($button['#subconditional']) ? array(
    'source' => NULL,
    'source_type' => 'component',
    'operator' => NULL,
    'value' => NULL,
  ) : array(
    'target_type' => 'component',
    'target' => NULL,
    'invert' => NULL,
    'action' => NULL,
    'argument' => NULL,
  );
  if (empty($button['#subconditional'])) {
    $new[0] = isset($parent_values[$rid]['source_type']) && $parent_values[$rid]['source_type'] == 'component' ? $parent_values[$rid] : $default_rule;
  }
  else {

    // The default andor operator is opposite of current subconditional's
    // operatior.
    $parent_rid = _webform_conditional_find_start($parent_values, $rid, -1);
    $current_op = $parent_rid < 0 ? $conditional['andor'] : $parent_values[$parent_rid]['operator'];
    $current_op = $current_op == 'and' ? 'or' : 'and';
    $new = array(
      array(
        'source_type' => 'conditional_start',
        'operator' => $current_op,
      ) + $default_rule,
      $default_rule,
      $default_rule,
      array(
        'source_type' => 'conditional_end',
      ) + $default_rule,
    );
  }

  // Update both $form_state['values'] and ['input] so that FAPI can merge
  // input values from the POST into the new form.
  $parent_values = array_merge(array_slice($parent_values, 0, $offset + 1), $new, array_slice($parent_values, $offset + 1));
  $input_values = array_merge(array_slice($input_values, 0, $offset + 1), $new, array_slice($input_values, $offset + 1));
  $form_state['rebuild'] = TRUE;
}