You are here

function apachesolr_search_block_view in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 apachesolr_search.module \apachesolr_search_block_view()
  2. 6.3 apachesolr_search.module \apachesolr_search_block_view()

Implements hook_block_view().

File

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

Code

function apachesolr_search_block_view($delta = '') {
  if ($delta == 'sort') {
    $environments = apachesolr_load_all_environments();
    foreach ($environments as $env_id => $environment) {
      if (apachesolr_has_searched($env_id) && !apachesolr_suppress_blocks($env_id) && $delta == 'sort') {
        $response = NULL;
        $query = apachesolr_current_query($env_id);
        if ($query) {

          // Get the query and response. Without these no blocks make sense.
          $response = apachesolr_static_response_cache($query
            ->getSearcher());
        }
        if (empty($response) || $response->response->numFound < 2) {
          return NULL;
        }
        $sorts = $query
          ->getAvailableSorts();

        // Get the current sort as an array.
        $solrsort = $query
          ->getSolrsort();
        $sort_links = array();
        $path = $query
          ->getPath();
        $new_query = clone $query;
        $toggle = array(
          'asc' => 'desc',
          'desc' => 'asc',
        );
        foreach ($sorts as $name => $sort) {
          $active = $solrsort['#name'] == $name;
          if ($name == 'score') {
            $direction = '';
            $new_direction = 'desc';

            // We only sort by descending score.
          }
          elseif ($active) {
            $direction = $toggle[$solrsort['#direction']];
            $new_direction = $toggle[$solrsort['#direction']];
          }
          else {
            $direction = '';
            $new_direction = $sort['default'];
          }
          $new_query
            ->setSolrsort($name, $new_direction);
          $sort_links[$name] = array(
            'text' => $sort['title'],
            'path' => $path,
            'options' => array(
              'query' => $new_query
                ->getSolrsortUrlQuery(),
            ),
            'active' => $active,
            'direction' => $direction,
          );
        }
        foreach ($sort_links as $name => $link) {
          $themed_links[$name] = theme('apachesolr_sort_link', $link);
        }
        return array(
          'subject' => t('Sort by'),
          'content' => theme('apachesolr_sort_list', array(
            'items' => $themed_links,
          )),
        );
      }
    }
  }
  elseif (($node = menu_get_object()) && (!arg(2) || arg(2) == 'view')) {
    $suggestions = array();

    // Determine whether the user can view the current node. Probably not necessary.
    $block = apachesolr_search_mlt_block_load($delta);
    if ($block && node_access('view', $node)) {

      // Get our specific environment for the MLT block
      $env_id = !empty($block['mlt_env_id']) ? $block['mlt_env_id'] : '';
      try {
        $solr = apachesolr_get_solr($env_id);
        $context['search_type'] = 'apachesolr_search_mlt';
        $context['block_id'] = $delta;
        $docs = apachesolr_search_mlt_suggestions($block, apachesolr_document_id($node->nid), $solr, $context);
        if (!empty($docs)) {
          $suggestions['subject'] = check_plain($block['name']);
          $suggestions['content'] = array(
            '#theme' => 'apachesolr_search_mlt_recommendation_block',
            '#docs' => $docs,
            '#delta' => $delta,
          );
        }
      } catch (Exception $e) {
        apachesolr_log_exception($env_id, $e);
      }
    }
    return $suggestions;
  }
}