You are here

function ca_conditions_form_remove_condition_submit in Ubercart 6.2

Submit handler for button to remove a condition from a condition group.

See also

ca_conditions_form()

1 string reference to 'ca_conditions_form_remove_condition_submit'
_ca_conditions_form_condition in ca/ca.admin.inc
Add a single condition's fieldset to the conditions form.

File

ca/ca.admin.inc, line 945
Conditional actions overview UI.

Code

function ca_conditions_form_remove_condition_submit($form, &$form_state) {
  $group_key = FALSE;
  $cond_key = FALSE;

  // Loop through the array keys of the conditions group.
  foreach (array_keys($form['conditions']['conditions']) as $key => $value) {

    // If we find the key we're looking for...
    if (is_numeric($value) && $value == $form_state['clicked_button']['#array_parents'][2]) {

      // Store its position as the new group key once the conditions are saved.
      // This is necessary because the save function will truncate any holes so
      // the keys are in numerical order starting with 0.
      $group_key = $key;
    }
  }

  // Loop through the array keys of the conditions in the group.
  foreach (array_keys($form['conditions']['conditions'][$form_state['clicked_button']['#array_parents'][2]]['conditions']) as $key => $value) {

    // If we find the key we're looking for...
    if (is_numeric($value) && $value == $form_state['clicked_button']['#array_parents'][4]) {

      // Store its position as the new condition key once the conditions are
      // saved. This is necessary because the save function will truncate any
      // holes so the keys are in numerical order starting with 0.
      $cond_key = $key;
    }
  }

  // Save the existing conditions to preserve any changes.
  ca_conditions_form_update_conditions($form_state['values']['pid'], $form_state['values']['conditions']);

  // Fail if we didn't find a new group key for some reason.
  if ($group_key === FALSE || $cond_key === FALSE) {
    drupal_set_message(t('An error occurred when trying to remove the condition.  Please try again.'), 'error');
  }
  else {

    // Otherwise remove the condition from the specified group.
    ca_remove_condition($form_state['values']['pid'], $group_key, $cond_key);
    drupal_set_message(t('Condition removed.'));
  }
}