Index: solr/project_solr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project/solr/project_solr.module,v
retrieving revision 1.76
diff -u -p -r1.76 project_solr.module
--- solr/project_solr.module	16 Aug 2010 21:28:25 -0000	1.76
+++ solr/project_solr.module	17 Aug 2010 17:05:57 -0000
@@ -222,7 +222,14 @@ function project_solr_apachesolr_prepare
     // Add a human-readable alias for the release filter.
     $query->add_field_aliases(array('im_project_release_api_tids' => variable_get('project_solr_project_release_api_tids_alias', 'api_version')));
   }
+  // Ideally we would filter based on project here, but add_field_aliases
+  // destroys any manually set filters, so setting filters before
+  // hook_prepare_query() is basically impossible at present. If we can
+  // fix the behavior of Solr_Base_Query::parse_filters(), then we can move
+  // the filters before the prepare query, and this check should work here.
+  //if ($query->has_filter('type', 'project_project')) {
   project_solr_add_sorts($query);
+  //}
 }
 
 //----------------------------------------
@@ -722,7 +729,7 @@ function project_solr_browse_projects_fo
 
   // Add all project-specific sorts so that the sort set in set_solrsort() will
   // work as intended.
-  project_solr_add_sorts($query);
+  project_solr_add_sorts($query, variable_get('project_solr_project_release_api_tids_alias', 'api_version'));
   $query->set_solrsort($solrsort[0], $solrsort[1]);
   $query_values = $query->get_url_queryvalues();
   if (!empty($form_state['values']['text'])) {
@@ -795,19 +802,44 @@ function project_sort_freetext_submit($f
  * site-wide search to sort by any project-specific sorting method. This is
  * called by hook_prepare_query() to add the sorts into all queries, but
  * separated out so that a hook_prepare_query() invocation is not necessary
- * if only the project sorts are desired.
+ * if only the project sorts are desired. We filter the list of sorts to only
+ * provide meaningful options for Latest release and Recent activity so that
+ * we don't end up with multiple sorts with the same name. This is particularly
+ * relevant for how the sorts display in the apachesolr generated sort block.
  * 
  * @param object &$query
  *   An existing query object to add sorts into.
+ * @param string $api_filter
+ *   A string representing the name of the filter where our version tids will
+ *   be located. This is necessary since, at times, both the base string name
+ *   im_project_release_api_tids and the alias are passed in.
+ *   
+ * @see apachesolr_block
  */
-function project_solr_add_sorts(&$query) {
+function project_solr_add_sorts(&$query, $api_filter = 'im_project_release_api_tids') {
   if (module_exists('project_release')) {
-    $query->set_available_sort('ds_project_latest_release', array('title' => t('Last release'), 'default' => 'desc'));
-    $query->set_available_sort('ds_project_latest_activity', array('title' => t('Recent activity'), 'default' => 'desc'));
-    $active_terms = project_release_compatibility_list();
-    foreach ($active_terms as $tid => $term_name) {
-      $query->set_available_sort('ds_project_latest_release_' . $tid, array('title' => t('Last release'), 'default' => 'desc'));
-      $query->set_available_sort('ds_project_latest_activity_' . $tid, array('title' => t('Recent activity'), 'default' => 'desc'));
+    // Pull any existing filter on version.
+    $versions = $query->get_filters($api_filter);
+    $tids = array();
+    foreach ($versions as $version) {
+      $tids[] = $version['#value'];
+    }
+    // If we have no version selected, only present the base sorts.
+    if (empty($tids)) {
+      $query->set_available_sort('ds_project_latest_release', array('title' => t('Last release'), 'default' => 'desc'));
+      $query->set_available_sort('ds_project_latest_activity', array('title' => t('Recent activity'), 'default' => 'desc'));
+    }
+    else {
+      // If we have versions selected, only present sorts for our selected
+      // versions. Traditionally, this will only be one version, but this
+      // allows for multiple potential versions to be selected.
+      $active_terms = project_release_compatibility_list();
+      foreach ($tids as $tid) {
+        if (isset($active_terms[$tid])) {
+          $query->set_available_sort('ds_project_latest_release_' . $tid, array('title' => t('Last release'), 'default' => 'desc'));
+          $query->set_available_sort('ds_project_latest_activity_' . $tid, array('title' => t('Recent activity'), 'default' => 'desc'));
+        }
+      }
     }
   }
   if (module_exists('project_usage')) {
