Index: solr/project_solr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project/solr/project_solr.module,v
retrieving revision 1.62
diff -u -r1.62 project_solr.module
--- solr/project_solr.module	22 Sep 2009 21:52:30 -0000	1.62
+++ solr/project_solr.module	19 Mar 2010 01:19:13 -0000
@@ -201,7 +201,7 @@
 
     include_once drupal_get_path('module', 'project_solr') .'/ProjectSolrQuery.php';
 
-    $query = new ProjectSolrQuery(apachesolr_get_solr(), $text_query, $filters, $sort);
+    $query = new ProjectSolrQuery(apachesolr_get_solr(), $text_query, $filters, $sort, 'project/' . drupal_strtolower($parent_term->name));
     if (is_null($query)) {
       throw new Exception(t('Could not construct a Solr query.'));
     }
@@ -227,6 +227,10 @@
     // 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);
+
     // This hook allows modules to modify the query and params objects.
     apachesolr_modify_query($query, $params, 'project_solr');
     if (!$query) {
@@ -244,20 +248,19 @@
     // We add 'fq' (filter query) parameters here to include all the constant
     // filters for the query -- project nodes of the given top-level type that
     // have releases (if project_release is enabled).
-    $fq[] = 'type:project_project';
-    $fq[] = 'im_vid_'. _project_get_vid() .':'. $parent_term->tid;
+    $params['fq'][] = 'type:project_project';
+    $params['fq'][] = 'im_vid_'. _project_get_vid() .':'. $parent_term->tid;
     if (module_exists('project_release')) {
-      $fq[] = 'is_project_has_releases:1';
+      $params['fq'][] = 'is_project_has_releases:1';
     }
-    $params['fq'][] = '('. implode(' AND ', $fq) .')';
 
     $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params);
 
     // The response is cached so that it is accessible to the blocks and anything
     // else that needs it beyond the initial search.
     $total = $response->response->numFound;
-
-    project_solr_response_cache(array($query, $response, $parent_term));
+    apachesolr_static_response_cache($response);
+    apachesolr_has_searched(TRUE);
 
     // Set breadcrumb
     $breadcrumb = menu_get_active_breadcrumb();
@@ -287,14 +290,6 @@
   return $output;
 }
 
-function project_solr_response_cache($set = FALSE) {
-  static $cache = NULL;
-  if ($set !== FALSE) {
-    $cache = $set;
-  }
-  return $cache;
-}
-
 //----------------------------------------
 // Blocks
 //----------------------------------------
@@ -324,10 +319,26 @@
     );
   }
 
-  if ($op == 'view' && ($search = project_solr_response_cache())) {
-    list($query, $response, $parent_term) = $search;
-
+  if ($op == 'view' && apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
     if ($delta == 'project_solr_categories') {
+      // Find the parent term for this query.
+      $project_vid = _project_get_vid();
+      foreach (isset($response->responseHeader->params->fq) ? $response->responseHeader->params->fq : array() as $query_snippet) {
+        if (preg_match('/^tid:(.*)$/', $query_snippet, $matches)) {
+          $tid = trim($matches[1]);
+          $term = taxonomy_get_term($tid);
+          if ($term->vid == $project_vid) {
+            $parent_term = $term;
+            break;
+          }
+        }
+      }
+
+      if (!isset($parent_term)) {
+        // This facet cannot process generic queries.
+        return;
+      }
+
       $facet = 'im_vid_'.  _project_get_vid();
       $terms = array();
 
@@ -354,7 +365,7 @@
         $term = taxonomy_get_term($tid);
         $new_query = clone $query;
         
-        $path = 'project/' . drupal_strtolower($parent_term->name);
+        $path = $new_query->get_path();
         $options = array();
         if ($active) {
           $new_query->remove_filter('tid', $term->tid);
@@ -389,7 +400,7 @@
 
         return array(
           'subject' => $vocab->name,
-          'content' => theme('apachesolr_facet_list', $terms, 200),
+          'content' => theme('apachesolr_facet_list', $terms, 4),
         );
       }
       return;
@@ -399,8 +410,8 @@
       $solrsort = $query->get_solrsort();
 
       $sort_links = array();
-      $path = 'project/' . drupal_strtolower($parent_term->name);
       $new_query = clone $query;
+      $path = $new_query->get_path();
       $toggle = array('asc' => 'desc', 'desc' => 'asc');
       foreach ($sorts as $name => $sort) {
         $active = $solrsort['#name'] == $name;
@@ -421,7 +432,7 @@
       $active_terms = array_reverse(project_release_compatibility_list(FALSE), TRUE);
 
       $active_term_counts = array();
-      foreach ($response->facet_counts->facet_fields->$facet as $tid => $count) {
+      foreach (isset($response->facet_counts->facet_fields->$facet) ? $response->facet_counts->facet_fields->$facet : array() as $tid => $count) {
         if (!empty($active_terms[$tid])) {
           $active_term_counts[$tid] = $count;
         }
@@ -430,8 +441,8 @@
       foreach ($active_terms as $tid => $term_name) {
         if (!empty($active_term_counts[$tid])) {
           $active = $query->has_filter('im_project_release_api_tids', $tid);
-          $path = 'project/' . drupal_strtolower($parent_term->name);
           $new_query = clone $query;
+          $path = $new_query->get_path();
           $new_query->remove_filter('im_project_release_api_tids', $term->tid);
           $options = array();
           if ($active) {
@@ -457,8 +468,8 @@
     }
     else if ($delta == 'project_solr_text') {
       return array(
-        'subject' => t('Search @project_type', array('@project_type' => drupal_strtolower($parent_term->name))),
-        'content' => drupal_get_form('project_sort_freetext'),
+        'subject' => t('Search @project_type', array('@project_type' => drupal_strtolower($query->get_query_basic()))),
+        'content' => drupal_get_form('project_sort_freetext', $query->get_path()),
       );
     }
   }
@@ -481,9 +492,10 @@
 /**
  * Form callback; display a free text form.
  */
-function project_sort_freetext() {
-  list($query, $response, $parent_term) = project_solr_response_cache();
-
+function project_sort_freetext(&$form_state, $base_path) {
+  $response = apachesolr_static_response_cache();
+  $query    = apachesolr_current_query();
+  
   $form = array();
   $form['text'] = array(
     '#type' => 'textfield',
@@ -492,7 +504,7 @@
   );
   $form['path'] = array(
     '#type' => 'value',
-    '#value' => 'project/' . drupal_strtolower($parent_term->name),
+    '#value' => $base_path,
   );
   $form['query'] = array(
     '#type' => 'value',
