You are here

function mefibs_views_pre_build in MEFIBS - More exposed forms in blocks 7

Implements hook_views_pre_build().

File

./mefibs.module, line 19
Primarily Drupal hooks and global API functions to manipulate views and to provide an additional block with an exposed filter form.

Code

function mefibs_views_pre_build(&$view) {
  $display_id = $view->current_display;
  if (!mefibs_display_is_mefibs_enabled($view->display[$display_id]->handler)) {
    return;
  }
  $display = $view->display[$display_id]->handler;
  $mefibs = $display
    ->get_option('mefibs');
  if (!$mefibs) {
    return;
  }

  // Get the submitted form values
  $filters = $view
    ->get_exposed_input();

  // Retrieve the original filter values.
  $old_filters = array();
  if (isset($_SESSION['mefibs'][$view->name][$view->current_display]['filters'])) {
    $old_filters = $_SESSION['mefibs'][$view->name][$view->current_display]['filters'];
  }

  // Get enabled blocks.
  $mefibs_blocks = $display->extender['mefibs_blocks']
    ->get_enabled_blocks();
  if (!count($mefibs_blocks)) {

    // No blocks, nothing to do.
    return;
  }

  // Find out which block has been submitted.
  $block_id = 'default';
  foreach ($filters as $key => $value) {
    if (strpos($key, '-mefibs_block_id') !== FALSE) {
      $block_id = $value;
      break;
    }
  }

  // The expected items for this block
  $expected_items = mefibs_get_expected_items_for_exposed_form_block($view, $block_id);

  // Select the filters we want to use.
  $valid_filters = array();
  $block_id_clean = str_replace('_', '-', $block_id);
  $prefix = mefibs_get_element_name_prefix($block_id);
  foreach ($filters as $filter => $value) {
    $filter_name = $filter;
    if ($block_id != 'default' && strpos($filter, $prefix . '-') === FALSE) {

      // One of the additional forms has been used.
      continue;
    }
    $filter_name = str_replace($prefix . '-', '', $filter);

    // Check filters and extra stuff like items_per_page and offset.
    foreach (array(
      'filter',
      'other',
    ) as $type) {
      if ($type != 'filter' && in_array($filter_name, $expected_items[$type]) || $type == 'filter' && isset($expected_items[$type][$filter_name])) {

        // This is an expected argument.
        $valid_filters[$filter_name] = $value;
      }
    }

    // Check sort options.
    if ($filter_name == 'sort_by' && count($expected_items['sort']) && in_array($value, $expected_items['sort'])) {
      $valid_filters['sort_by'] = $value;
    }
    if ($filter_name == 'sort_order' && count($expected_items['sort'])) {
      $valid_filters['sort_order'] = $value;
    }
  }

  // Unset all old filters of expected items that should have been submitted if
  // they had been set.
  foreach ($expected_items as $type => $items) {
    foreach ($items as $key => $item) {
      if (isset($old_filters[$key])) {
        unset($old_filters[$key]);
      }

      // This is important for things like sort and items per page.
      if (isset($_GET[$key])) {
        unset($_GET[$key]);
      }
    }
  }

  // Fill in values from previous query if not overridden.
  $filters = $valid_filters + $old_filters;

  // Allow other modules to alter the filter values.
  drupal_alter('mefibs_filter', $filters);

  // Pass the filters on to the view. Also update the request parameters so
  // that views doesn't get confused. This is particularily important in order
  // to set the correct default values.
  $_GET = $filters + $_GET;
  $view
    ->set_exposed_input($filters);

  // And save them in the session for later reference.
  if (!empty($filters)) {

    // Check if one of the filters should be remembered,
    // if not, don't save them.
    $save = FALSE;
    foreach ($filters as $filter_identifier => $filter_value) {
      $filter_name = isset($expected_items['filter'][$filter_identifier]) ? $expected_items['filter'][$filter_identifier] : $filter_identifier;
      if (!empty($view->filter[$filter_name])) {
        $filter_options = $view->filter[$filter_name]->options;
        if (empty($filter_options['expose']['remember'])) {
          continue;
        }

        // Check if we store exposed value for current user.
        global $user;
        $allowed_rids = empty($filter_options['expose']['remember_roles']) ? array() : array_filter($filter_options['expose']['remember_roles']);
        $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
        if (empty($intersect_rids)) {
          continue;
        }
        $save = TRUE;
        break;
      }
    }
    if ($save) {
      $_SESSION['mefibs'][$view->name][$view->current_display]['filters'] = $filters;
    }
  }
  $pager = $view->display_handler
    ->get_plugin('pager');

  // Support for exposed items per page.
  if (isset($filters['items_per_page'])) {

    // Unsetting $_GET is ugly but necessary,
    // see views_plugin_pager_full::query().
    unset($_GET['items_per_page']);
    $view
      ->set_items_per_page($filters['items_per_page']);
  }
  if (isset($filters['offset'])) {
    unset($_GET['offset']);
    $view
      ->set_offset($filters['offset']);
  }
}