You are here

function apachesolr_search_browse in Apache Solr Search 6.2

Execute an empty search (match all documents) and show a listing of all enabled facets.

1 call to apachesolr_search_browse()
apachesolr_search_view in ./apachesolr_search.module
Re-implementation of search_view().
2 string references to 'apachesolr_search_browse'
apachesolr_search_form_apachesolr_settings_alter in ./apachesolr_search.module
Implementation of hook_form_[form_id]_alter().
apachesolr_search_view in ./apachesolr_search.module
Re-implementation of search_view().

File

./apachesolr_search.module, line 295
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_browse($keys = '', $filters = '', $solrsort = '', $base_path = '') {
  global $user, $theme_key;
  $query = apachesolr_drupal_query($keys, $filters, $solrsort, $base_path);
  $params = array(
    'start' => 0,
    'rows' => 0,
    'facet' => 'true',
    'facet.mincount' => 1,
    'facet.sort' => 'true',
  );
  apachesolr_search_add_facet_params($params, $query);

  // Allow modules to alter the query prior to statically caching it.
  // This can e.g. be used to add available sorts.
  foreach (module_implements('apachesolr_prepare_query') as $module) {
    $function_name = $module . '_apachesolr_prepare_query';
    $function_name($query, $params, 'apachesolr');
  }
  $solr = apachesolr_get_solr();
  apachesolr_current_query($query);
  apachesolr_modify_query($query, $params, 'apachesolr');
  $response = $solr
    ->search('', $params['start'], $params['rows'], $params);
  if (empty($response)) {
    return;
  }
  apachesolr_static_response_cache($response);
  apachesolr_has_searched(TRUE);

  // Get blocks for all enabled filters
  $blocks = array();
  $rids = array_keys($user->roles);
  foreach (apachesolr_get_enabled_facets() as $module => $module_facets) {
    if (!module_exists($module)) {

      // When modules are disabled their facet settings may remain.
      continue;
    }
    foreach ($module_facets as $delta => $facet_field) {
      if ($delta == 'currentsearch') {
        continue;
      }
      if (count((array) $response->facet_counts->facet_fields->{$facet_field}) > 0 || count((array) $response->facet_counts->facet_dates->{$facet_field}) > 0) {

        // This bit is modeled on block.module, block_list().
        $hook_block = (object) module_invoke($module, 'block', 'view', $delta);
        $result = db_query_range(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b\n          LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta\n          WHERE b.module = '%s' AND b.delta = '%s' AND b.theme = '%s' AND b.status = 1\n            AND (r.rid IN (" . db_placeholders($rids) . ") OR r.rid IS NULL)", 'b', 'bid'), array_merge(array(
          $module,
          $delta,
          $theme_key,
        ), $rids), 0, 1);
        $block = db_fetch_object($result);
        $block->content = $hook_block->content;
        $block->subject = str_replace(t('Filter by '), t('Browse by '), $hook_block->subject);

        // We can safely assume these values, since we're taking over the block display.
        $block->visibility = TRUE;
        $block->enabled = TRUE;
        $block->module = $module;

        // This hook is made up. It should be in Drupal core, and it is quite useful.
        // Including it here because it has saved the day on projects.
        drupal_alter('block', $block);
        if ($block->enabled && $block->visibility) {
          $blocks["{$module}_{$delta}"] = $block;
        }
      }
    }
  }
  apachesolr_has_searched(FALSE);
  usort($blocks, create_function('$a, $b', 'return $a->weight - $b->weight;'));
  return $blocks;
}