You are here

function apachesolr_multisitesearch_block in Apache Solr Search 5

Implementation of hook_block().

2 calls to apachesolr_multisitesearch_block()
apachesolr_multisitesearch_apachesolr_facets in contrib/apachesolr_multisitesearch/apachesolr_multisitesearch.module
theme_apachesolr_breadcrumb_hash in contrib/apachesolr_multisitesearch/apachesolr_multisitesearch.module
Return the site from $hash

File

contrib/apachesolr_multisitesearch/apachesolr_multisitesearch.module, line 162

Code

function apachesolr_multisitesearch_block($op = 'list', $delta = 0, $edit = array()) {

  // A mapping between hashes and site URLs. Needed for themeing breadcrumbs.
  static $sites;
  switch ($op) {

    // Special $op so we can get site from hash at the theme layer.
    case 'get site':
      $response =& apachesolr_static_response_cache();
      if (empty($response)) {
        return $delta;
      }

      // Calculate the hashes of the sites for lookukp. This is why
      // we ask for the site facet in addition to the hash facet, and
      // we trust that they are the same.
      $sites = array();
      foreach ($response->facet_counts->facet_fields->site as $site => $count) {
        $sites[md5($site)] = $site;
      }
      return $sites[$delta];
    case 'list':
      $blocks['name'] = array(
        'info' => t('ApacheSolr Multisite: Filter by author name'),
      );
      $blocks['hash'] = array(
        'info' => t('ApacheSolr Multisite: Filter by site'),
      );
      $blocks['taxonomy_name'] = array(
        'info' => t('ApacheSolr Multisite: Filter by taxonomy term name'),
      );
      return $blocks;
    case 'view':
      if (arg(1) == 'apachesolr_multisitesearch' && apachesolr_has_searched()) {

        // Get the query and response. Without these no blocks make sense.
        $response =& apachesolr_static_response_cache();
        if (empty($response)) {
          return;
        }
        $query =& apachesolr_drupal_query();

        // Get information needed by the rest of the blocks about limits.
        $facet_display_limits = variable_get('apachesolr_facet_query_limits', array());
        switch ($delta) {
          case 'name':
            $filter_by = t('Filter by author name');
            return apachesolr_facet_block($response, $query, $delta, $filter_by);
          case 'taxonomy_name':
            $filter_by = t('Filter by term name');
            return apachesolr_facet_block($response, $query, $delta, $filter_by);
          case 'hash':
            if (is_object($response->facet_counts->facet_fields->{$delta})) {
              $contains_active = FALSE;

              // Calculate the hashes of the sites for lookukp. This is why
              // we ask for the site facet in addition to the hash facet, and
              // we trust that they are the same.
              $sites = array();
              foreach ($response->facet_counts->facet_fields->site as $site => $count) {
                $sites[md5($site)] = $site;
              }
              $hashes = array();
              foreach ($response->facet_counts->facet_fields->{$delta} as $hash => $count) {
                $unclick_link = '';
                unset($active);
                $new_query = clone $query;
                if ($active = $query
                  ->has_field('hash', $hash)) {
                  $contains_active = TRUE;
                  $new_query
                    ->remove_field('hash', $hash);
                  $path = 'search/' . arg(1) . '/' . $new_query
                    ->get_query();
                  $unclick_link = theme('apachesolr_unclick_link', $path);
                }
                else {
                  $new_query
                    ->add_field('hash', $hash);
                  $path = 'search/' . arg(1) . '/' . $new_query
                    ->get_query();
                }
                $countsort = $count == 0 ? '' : 1 / $count;

                // if numdocs == 1 and !active, don't add.
                if ($response->numFound == 1 && !$active) {

                  // skip
                }
                else {
                  $hashes[$active ? $countsort . $hash : 1 + $countsort . $hash] = theme('apachesolr_facet_item', $sites[$hash], $count, $path, $active, $unclick_link, $response->numFound);
                }
              }
              if (count($hashes) > 0) {
                ksort($hashes);
                $facet_display_limit = isset($facet_display_limits[$delta]) ? $facet_display_limits[$delta] : 10;
                $hashes = array_slice($hashes, 0, $facet_display_limit == -1 ? NULL : $facet_display_limit);
                $output = theme('apachesolr_facet_list', $hashes);
                return array(
                  'subject' => t('Filter by site'),
                  'content' => $output,
                );
              }
            }
            break;
          default:
            break;
        }
        break;
      }
      break;
    case 'configure':
      if ($delta != 'sort') {
        return apachesolr_facetcount_form($delta);
      }
      break;
    case 'save':
      if ($delta != 'sort') {
        apachesolr_facetcount_save($delta, $edit);
      }
      break;
  }
}