You are here

function apachesolr_block in Apache Solr Search 6

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_block()
  2. 5 apachesolr.module \apachesolr_block()
  3. 6.2 apachesolr.module \apachesolr_block()

Implementation of hook_block().

File

./apachesolr.module, line 754
Integration with the Apache Solr search application.

Code

function apachesolr_block($op = 'list', $delta = 0, $edit = array()) {
  static $access;
  switch ($op) {
    case 'list':

      // Get all of the moreLikeThis blocks that the user has created
      $blocks = apachesolr_mlt_list_blocks();

      // Add the sort block.
      $blocks['sort'] = array(
        'info' => t('Apache Solr Core: Sorting'),
        'cache' => BLOCK_CACHE_PER_PAGE,
      );
      return $blocks;
    case 'view':
      if ($delta != 'sort' && ($node = menu_get_object()) && (!arg(2) || arg(2) == 'view')) {
        $suggestions = array();

        // Determine whether the user can view the current node.
        if (!isset($access)) {
          $access = node_access('view', $node);
        }
        $block = apachesolr_mlt_load_block($delta);
        if ($access && $block) {
          $docs = apachesolr_mlt_suggestions($block, apachesolr_document_id($node->nid));
          if (!empty($docs)) {
            $suggestions['subject'] = check_plain($block['name']);
            $suggestions['content'] = theme('apachesolr_mlt_recommendation_block', $docs, $delta);
            if (user_access('administer search')) {
              $suggestions['content'] .= l(t('Configure this block'), 'admin/build/block/configure/apachesolr/' . $delta, array(
                'attributes' => array(
                  'class' => 'apachesolr-mlt-admin-link',
                ),
              ));
            }
          }
        }
        return $suggestions;
      }
      elseif (apachesolr_has_searched() && $delta == 'sort') {

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

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

            // We only sort by ascending score.
            $direction = '';
            $new_direction = 'asc';
          }
          elseif ($active) {
            $direction = $toggle[$solrsort['#direction']];
            $new_direction = $toggle[$solrsort['#direction']];
          }
          else {
            $direction = '';
            $new_direction = $sort['default'];
          }
          $new_query
            ->set_solrsort($name, $new_direction);
          $sort_links[$name] = array(
            'title' => $sort['title'],
            'path' => $path,
            'options' => array(
              'query' => $new_query
                ->get_url_queryvalues(),
            ),
            'active' => $active,
            'direction' => $direction,
          );
        }

        // Allow other modules to add or remove sorts.
        drupal_alter('apachesolr_sort_links', $sort_links);
        if (!empty($sort_links)) {
          foreach ($sort_links as $name => $link) {
            $themed_links[$name] = theme('apachesolr_sort_link', $link['title'], $link['path'], $link['options'], $link['active'], $link['direction']);
          }
          return array(
            'subject' => t('Sort by'),
            'content' => theme('apachesolr_sort_list', $themed_links),
          );
        }
      }
      break;
    case 'configure':
      if ($delta != 'sort') {
        require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
        return apachesolr_mlt_block_form($delta);
      }
      break;
    case 'save':
      if ($delta != 'sort') {
        require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
        apachesolr_mlt_save_block($edit, $delta);
      }
      break;
  }
}