Index: apachesolr_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr_search.module,v
retrieving revision 1.1.2.6.2.90
diff -u -p -r1.1.2.6.2.90 apachesolr_search.module
--- apachesolr_search.module	6 May 2009 01:15:35 -0000	1.1.2.6.2.90
+++ apachesolr_search.module	8 May 2009 21:24:10 -0000
@@ -53,6 +53,156 @@ function apachesolr_search_menu() {
 }
 
 /**
+ * Implementation of hook_menu_alter().
+ */
+function apachesolr_search_menu_alter(&$menu) {
+  if (isset($menu['search/apachesolr_search/%menu_tail'])) {
+    $menu['search/apachesolr_search/%menu_tail']['page callback'] = 'apachesolr_search_view';
+    $menu['search/apachesolr_search'] = $menu['search/apachesolr_search/%menu_tail'];
+    unset($menu['search/apachesolr_search/%menu_tail']);
+  }
+  if (isset($menu['taxonomy/term/%'])) {
+    $menu['taxonomy/term/%']['page callback'] = 'apachesolr_search_override_taxonomy_term_page';
+    unset($menu['taxonomy/term/%']['file']);
+  }
+}
+
+/**
+ * Overrides taxonomy/term/X links
+ */
+function apachesolr_search_override_taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') {
+  $terms = taxonomy_terms_parse_string($str_tids);
+  $redirect_to_apachesolr = TRUE;
+  
+  // Only support one term, only page callbacks, and only depth = 0 (because of way Solr indexing works)
+  if ( (sizeof($terms['tids'])>1 && $terms['operator'] != 'and') || $op != 'page' || $depth != 0) {
+    $redirect_to_apachesolr = FALSE;
+  } else {
+    // Check if term blongs to vocabulary selected by admin as an available filter
+    $term = taxonomy_get_term($terms['tids'][0]);
+    $vocabulary_facet_name = 'im_vid_' . $term->vid;
+    if (! in_array($vocabulary_facet_name, apachesolr_get_enabled_facets('apachesolr_search') )) {
+      $redirect_to_apachesolr = FALSE;
+    }
+  }
+
+  if ($redirect_to_apachesolr) {
+    // Return an apachesolr search page
+    $_GET['q'] = 'search/apachesolr_search/';
+    $_GET['filters'] = 'tid:' . implode(' tid:', $terms['tids']);
+    return apachesolr_search_view('apachesolr_search');
+  } else{
+    // Fallback to normal taxonomy/term page
+    require_once("modules/taxonomy/taxonomy.pages.inc");
+    taxonomy_term_page($str_tids, $depth, $op);
+  }
+}
+
+/**
+ * Re-implementation of search_view().
+ */
+function apachesolr_search_view($type) {
+  $keys = trim(search_get_keys());
+  $filters = isset($_GET['filters']) ? trim($_GET['filters']) : '';
+  // Only perform search if there is non-whitespace search term or filters:
+  $results = '';
+  if ($keys || $filters) {
+    // Log the search keys:
+    watchdog('search', '%keys filters=%filters (@type).', array('%keys' => $keys, '%filters' => $filters, '@type' => t('Search')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
+  
+    // Collect the search results:
+    $results = search_data($keys, $type);
+  
+    if ($results) {
+      $results = theme('box', t('Search results'), $results);
+    }
+    else {
+      $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
+    }
+    // Construct the search form.
+    return drupal_get_form('search_form', NULL, $keys, $type) . $results;
+  } else {
+    // Show search form and browse-by blocks
+    $browse_by = apachesolr_search_browse();
+    return drupal_get_form('search_form', NULL, $keys, $type) . $browse_by;
+  }
+}
+
+/**
+ * Copying parts over from apachesolr_search_search() to get a facet listing of all enabled facets
+ */
+function apachesolr_search_browse() {
+  // No keys nor filters, so show the the browse-by blocks
+  $query = apachesolr_drupal_query('', '', '', 'search/' . arg(1));
+  $params = array(
+    'rows' => 0,
+    'facet' => 'true',
+    'facet.mincount' => 1,
+    'facet.sort' => 'true'
+  );
+  foreach (apachesolr_get_enabled_facets() as $module => $module_facets) {
+    foreach($module_facets as $delta => $facet_field) {
+      // TODO: generalize handling of date and range facets.
+      if ($module == 'apachesolr_search' && ($facet_field == 'created' || $facet_field == 'changed')) {
+        list($start, $end, $gap) = apachesolr_search_date_range($query, $facet_field);
+        if ($gap) {
+          $params['facet.date'][] = $facet_field;
+          $params['f.'. $facet_field .'.facet.date.start'] = $start;
+          $params['f.'. $facet_field .'.facet.date.end'] = $end;
+          $params['f.'. $facet_field .'.facet.date.gap'] = $gap;
+        }
+      }
+      else {
+        $params['facet.field'][] = $facet_field;
+        // Facet limits
+        if (isset($facet_query_limits[$module][$delta])) {
+          $params['f.' . $facet_field . '.facet.limit'] = $facet_query_limits[$module][$delta];
+        }
+        // Facet missing
+        if (!empty($facet_missing[$module][$delta])) {
+          $params['f.' . $facet_field . '.facet.missing'] = 'true';
+        }
+      }
+    }
+  }
+  if (!empty($params['facet.field'])) {
+    // Add a default limit for fields where no limit was set.
+    $params['facet.limit'] = variable_get('apachesolr_facet_query_limit_default', 20);
+  }
+  // This is the object that does the communication with the solr server.
+  $solr = apachesolr_get_solr();
+  
+  // Cache the built query. Since all the built queries go through
+  // this process, all the hook_invocations will happen later
+  apachesolr_current_query($query);
+  $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params);
+  apachesolr_static_response_cache($response);
+  apachesolr_has_searched(TRUE);
+  $result = "";
+  
+  // Get blocks
+  foreach (apachesolr_get_enabled_facets() as $module => $module_facets) {
+    foreach($module_facets as $delta => $facet_field) {
+      if ($delta == "currentsearch") {
+        continue;
+      }
+      $blocks[$facet_field] = (object)module_invoke($module, 'block', 'view', $delta);
+    }
+  }
+  
+  // Theme result
+  $result = "<div class='apachesolr_browse_block'><h2>" . t('Or browse available categories') .'</h2>';
+  $result .= t('Pick a category to launch a search.');
+  foreach ($blocks as $facet_field => $block) {
+     $result .= theme('block', $block);
+  }
+  $result .= '</div>';
+  apachesolr_has_searched(FALSE);
+  
+  return $result;
+}
+
+/**
  * Implementation of hook_update_index().
  */
 function apachesolr_search_update_index() {
@@ -90,12 +240,8 @@ function apachesolr_search_search($op = 
     case 'search':
 
       try {
-        if (empty($filters) && !empty($_GET['filters'])) {
-          $filters = $_GET['filters'];
-        }
-        if (empty($solrsort) && !empty($_GET['solrsort'])) {
-          $solrsort = $_GET['solrsort'];
-        }
+        $filters = isset($_GET['filters']) ? $_GET['filters'] : '';
+        $solrsort = isset($_GET['solrsort']) ? $_GET['solrsort'] : '';
         // This is the object that knows about the query coming from the user.
         $query = apachesolr_drupal_query($keys, $filters, $solrsort, 'search/' . arg(1));
         if (empty($query)) {
