You are here

function apachesolr_search_block_view in Apache Solr Search 6.3

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

Vew a specific block according to the delta identifier

Parameters

string $delta:

Return value

string

1 call to apachesolr_search_block_view()
apachesolr_search_block in ./apachesolr_search.module
Implements hook_block().

File

./apachesolr_search.module, line 481
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' && ($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'] = theme('apachesolr_search_mlt_recommendation_block', $docs, $delta);
        }
      } catch (Exception $e) {
        watchdog('Apache Solr', nl2br(check_plain($e
          ->getMessage())), NULL, WATCHDOG_ERROR);
      }
    }
    return $suggestions;
  }
  else {
    $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;
        }
        $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);
        }
        $output = theme('apachesolr_sort_list', array(
          'items' => $themed_links,
        ));
        return array(
          'subject' => t('Sort by'),
          'content' => $output,
        );
      }
    }
  }
}