You are here

function views_ui_rearrange_filter_form_submit in Views (for Drupal 7) 6.3

Same name and namespace in other branches
  1. 8.3 views_ui/admin.inc \views_ui_rearrange_filter_form_submit()
  2. 7.3 includes/admin.inc \views_ui_rearrange_filter_form_submit()

Submit handler for rearranging form

File

includes/admin.inc, line 2916
admin.inc Provides the Views' administrative interface.

Code

function views_ui_rearrange_filter_form_submit($form, &$form_state) {
  $types = views_object_types();
  $display =& $form_state['view']->display[$form_state['display_id']];
  $remember_groups = array();
  if (!empty($form_state['view']->form_cache)) {
    $old_fields = $form_state['view']->form_cache['handlers'];
  }
  else {
    $old_fields = $display->handler
      ->get_option($types[$form_state['type']]['plural']);
  }
  $count = 0;
  $groups = $form_state['values']['filter_groups'];

  // Whatever button was clicked, re-calculate field information.
  $new_fields = $order = array();

  // Make an array with the weights
  foreach ($form_state['values']['filters'] as $field => $info) {

    // add each value that is a field with a weight to our list, but only if
    // it has had its 'removed' checkbox checked.
    if (is_array($info) && empty($info['removed'])) {
      if (isset($info['weight'])) {
        $order[$field] = $info['weight'];
      }
      if (isset($info['group'])) {
        $old_fields[$field]['group'] = $info['group'];
        $remember_groups[$info['group']][] = $field;
      }
    }
  }

  // Sort the array
  asort($order);

  // Create a new list of fields in the new order.
  foreach (array_keys($order) as $field) {
    $new_fields[$field] = $old_fields[$field];
  }

  // Save if the actual update button was clicked.
  if (!empty($form_state['clicked_button']['#group'])) {
    if ($form_state['clicked_button']['#group'] == 'add') {

      // Add a new group
      $groups['groups'][] = 'AND';
    }
    else {

      // Renumber groups above the removed one down.
      foreach (array_keys($groups['groups']) as $group_id) {
        if ($group_id >= $form_state['clicked_button']['#group']) {
          $old_group = $group_id + 1;
          if (isset($groups['groups'][$old_group])) {
            $groups['groups'][$group_id] = $groups['groups'][$old_group];
            if (isset($remember_groups[$old_group])) {
              foreach ($remember_groups[$old_group] as $id) {
                $new_fields[$id]['group'] = $group_id;
              }
            }
          }
          else {

            // If this is the last one, just unset it.
            unset($groups['groups'][$group_id]);
          }
        }
      }
    }

    // Update our cache with values so that cancel still works the way
    // people expect.
    $form_state['view']->form_cache = array(
      'key' => 'rearrange-filter',
      'groups' => $groups,
      'handlers' => $new_fields,
    );

    // Return to this form except on actual Update.
    views_ui_add_form_to_stack('rearrange-filter', $form_state['view'], $form_state['display_id'], array(
      $form_state['type'],
    ));
  }
  else {

    // Actually write changed handler values.
    $display->handler
      ->set_option($types[$form_state['type']]['plural'], $new_fields);
    $display->handler
      ->set_option('filter_groups', $groups);
    if (isset($form_state['view']->form_cache)) {
      unset($form_state['view']->form_cache);
    }
  }

  // Store in cache
  views_ui_cache_set($form_state['view']);
}