You are here

function apachesolr_search_block in Apache Solr Search 5

Same name and namespace in other branches
  1. 5.2 apachesolr_search.module \apachesolr_search_block()
  2. 6.3 apachesolr_search.module \apachesolr_search_block()
  3. 6 apachesolr_search.module \apachesolr_search_block()
  4. 6.2 apachesolr_search.module \apachesolr_search_block()

Implementation of hook_block().

1 call to apachesolr_search_block()
apachesolr_search_apachesolr_facets in ./apachesolr_search.module

File

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

Code

function apachesolr_search_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks['uid'] = array(
        'info' => t('ApacheSolr Search: Filter by author'),
      );

      // Get taxonomy vocabulary facets.
      if (module_exists('taxonomy')) {
        $vocabs = taxonomy_get_vocabularies();
        foreach ($vocabs as $vid => $vocab) {
          $blocks['imfield_vid' . $vid] = array(
            'info' => t('ApacheSolr Search: Filter by @name', array(
              '@name' => $vocab->name,
            )),
          );
        }
      }

      // Get CCK field facets.
      if ($fields = apachesolr_cck_fields()) {
        foreach ($fields as $name => $field) {
          $index_key = apachesolr_index_key($field);
          $label = db_result(db_query("SELECT label FROM {node_field_instance} WHERE field_name = '%s'", $name));

          // TODO: $index_key must be wrong here.
          $blocks[$index_key] = array(
            'info' => t('ApacheSolr Search: Filter by @field', array(
              '@field' => $label,
            )),
          );
        }
      }
      return $blocks;
    case 'view':
      if (arg(1) == 'apachesolr_search' && 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());

        // Handle taxonomy vocabulary facets
        if (strpos($delta, 'imfield_vid') === 0 && module_exists('taxonomy')) {
          if (is_object($response->facet_counts->facet_fields->{$delta})) {
            $contains_active = FALSE;
            $terms = array();
            foreach ($response->facet_counts->facet_fields->{$delta} as $tid => $count) {
              $unclick_link = '';
              unset($active);
              $term = taxonomy_get_term($tid);
              $new_query = clone $query;
              if ($active = $query
                ->has_field('tid', $tid)) {
                $contains_active = TRUE;
                $new_query
                  ->remove_field('tid', $term->tid);
                $path = 'search/' . arg(1) . '/' . $new_query
                  ->get_query();
                $unclick_link = theme('apachesolr_unclick_link', $path);
              }
              else {
                $new_query
                  ->add_field('tid', $term->tid);
                $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 {
                $terms[$term->vid][$active ? $countsort . $term->name : 1 + $countsort . $term->name] = theme('apachesolr_facet_item', $term->name, $count, $path, $active, $unclick_link, $response->numFound);
              }
            }
          }
          $vid = substr($delta, 11);
          $vocab = taxonomy_get_vocabulary($vid);
          if (is_numeric($vid) && is_array($terms) && isset($terms[$vid]) && is_array($terms[$vid])) {
            ksort($terms[$vid]);
            $facet_display_limit = isset($facet_display_limits[$delta]) ? $facet_display_limits[$delta] : 10;
            $terms[$vid] = array_slice($terms[$vid], 0, $facet_display_limit == -1 ? NULL : $facet_display_limit);
            return array(
              'subject' => t('Filter by @name', array(
                '@name' => $vocab->name,
              )),
              'content' => theme('apachesolr_facet_list', $terms[$vid]),
            );
          }
          else {
            return;
          }
        }
        switch ($delta) {
          case 'uid':
            $filter_by = t('Filter by author');
            return apachesolr_facet_block($response, $query, $delta, $filter_by, 'apachesolr_search_get_username');
          default:
            if ($fields = apachesolr_cck_fields()) {
              foreach ($fields as $name => $field) {
                $index_key = apachesolr_index_key($field);
                if ($index_key == $delta) {
                  if (!empty($response->facet_counts->facet_fields->{$index_key})) {
                    $contains_active = FALSE;
                    foreach ($response->facet_counts->facet_fields->{$index_key} as $facet => $count) {
                      $unclick_link = '';
                      unset($active);
                      $new_query = clone $query;
                      if ($active = $query
                        ->has_field($index_key, $facet)) {
                        $contains_active = TRUE;
                        $new_query
                          ->remove_field($index_key, $facet);
                        $path = 'search/' . arg(1) . '/' . $new_query
                          ->get_query();
                        $unclick_link = theme('apachesolr_unclick_link', $path);
                      }
                      else {
                        $new_query
                          ->add_field($index_key, $facet);
                        $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 {
                        $facets[$active ? $countsort . $facet : 1 + $countsort . $facet] = theme('apachesolr_facet_item', $facet, $count, $path, $active, $unclick_link, $response->numFound);
                      }
                    }
                    if (is_array($facets)) {
                      ksort($facets);
                      $facet_display_limit = isset($facet_display_limits[$delta]) ? $facet_display_limits[$delta] : 10;
                      $facets = array_slice($facets, 0, $facet_display_limit == -1 ? NULL : $facet_display_limit);
                      $output = theme('apachesolr_facet_list', $facets);
                      $label = db_result(db_query("SELECT label FROM {node_field_instance} WHERE field_name = '%s'", $name));
                      return array(
                        'subject' => t('Filter by @field', array(
                          '@field' => $label,
                        )),
                        'content' => $output,
                      );
                    }
                  }
                }
              }
            }
        }
        break;
      }
      break;
    case 'configure':
      if ($delta != 'sort') {
        return apachesolr_facetcount_form($delta);
      }
      break;
    case 'save':
      if ($delta != 'sort') {
        apachesolr_facetcount_save($delta, $edit);
      }
      break;
  }
}