You are here

function ca_conditions_form_add_condition_submit in Ubercart 6.2

Submit handler for button to add a condition to a condition group.

See also

ca_conditions_form()

1 string reference to 'ca_conditions_form_add_condition_submit'
_ca_conditions_form_tree in ca/ca.admin.inc
Recursively add logical groups to the conditions form as fieldsets.

File

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

Code

function ca_conditions_form_add_condition_submit($form, &$form_state) {
  $group_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;
    }
  }

  // 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) {
    drupal_set_message(t('An error occurred when trying to add the condition.  Please try again.'), 'error');
  }
  else {

    // Otherwise add the condition to the specified group.
    $name = $form_state['values']['conditions']['conditions'][$group_key]['condition'];
    ca_add_condition($form_state['values']['pid'], $name, $group_key);
    $condition = ca_load_condition($name);
    drupal_set_message(t('%title condition added.', array(
      '%title' => $condition['#title'],
    )));
  }
}