Index: drupalorg_search/drupalorg_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalorg/drupalorg_search/drupalorg_search.module,v
retrieving revision 1.21
diff -u -r1.21 drupalorg_search.module
--- drupalorg_search/drupalorg_search.module	9 Aug 2010 19:07:07 -0000	1.21
+++ drupalorg_search/drupalorg_search.module	9 Aug 2010 23:33:10 -0000
@@ -348,7 +348,9 @@
 function drupalorg_search_execute_base_query() {
   // Build a simple query.
   $base_path = 'project/modules';
-  $query = apachesolr_drupal_query('', '', '', $base_path);
+  
+  $filters = isset($_GET['filters']) ? $_GET['filters'] : '';  
+  $query = apachesolr_drupal_query('', $filters, '', $base_path);  
 
   $params = array(
     // The fields to return.
@@ -359,13 +361,14 @@
     // We need to be able to facet on the project vid value
     // so we can pull project categories.
     'facet.field' => array(
+      'type',
       'im_vid_' . _project_get_vid(),
+      'im_project_release_api_tids',
     ),
-    // Additional criterias.
-    'fq' => array(
-      'type:project_project',
-      'im_vid_' . _project_get_vid() . ':' . DRUPALORG_MODULE_TID,
-    ),
+    // Additional criterias are set below rather than via $params
+    // because any params passed to the constructor will override
+    // our $params set filter values.
+    'fq' => array(),
     'facet' => 'true',
     'facet.mincount' => 1,
     'facet.sort' => 'true',
@@ -373,6 +376,19 @@
 
   apachesolr_search_add_facet_params($params, $query);
 
+  // Allow modules to alter the query prior to statically caching it.
+  // This can e.g. be used to add available sorts.
+  foreach (module_implements('apachesolr_prepare_query') as $module) {
+    $function_name = $module . '_apachesolr_prepare_query';
+    $function_name($query, $params, 'drupalorg_search_execute_base_query');
+  }
+  
+  // We add our fields after the prepare_query because prepare_query
+  // generates a call to parse_filters which destroys anything that
+  // does not get passed into the query on construction.
+  $query->add_filter('type', 'project_project');
+  $query->add_filter('im_vid_' . _project_get_vid(), DRUPALORG_MODULE_TID);
+
   // Cache the built query. Since all the built queries go through
   // this process, all the hook_invocations will happen later.
   apachesolr_current_query($query);
@@ -405,6 +421,10 @@
   foreach ($facets as $delta => $module) {
     $variables[$delta] = drupalorg_block_render($module, $delta);
   }
+
+  if (module_exists('project_release')) {
+    $variables['version_form'] = drupal_get_form('drupalorg_search_version_form');
+  }
 }
 
 /**
@@ -451,3 +471,57 @@
     }
   }
 }
+
+/**
+ * Generate a form with a version selection to allow filtering page content
+ * based on the drupal version. Also includes path to allow the form to 
+ * potentially submit to other urls if desired.
+ */
+function drupalorg_search_version_form(&$form_state) {
+  $query = apachesolr_current_query();
+  if (module_exists('project_release')) {
+    $terms = array();
+    $active_terms = project_release_compatibility_list();
+    foreach ($active_terms as $tid => $term_name) {
+      $active = $query->has_filter('im_project_release_api_tids', $tid);
+      if ($active) {
+        $current_tid = $tid;
+      }
+      $terms[$tid] = $term_name;
+    }
+    if (!empty($terms)) {
+      $terms = array('' => t('- Any -')) + $terms;
+      $form['api_version'] = array(
+        '#title' => t('Filter by compatibility'),
+        '#type' => 'select',
+        '#options' => $terms,
+        '#default_value' => $current_tid,
+      );
+    }
+  }
+
+  $form['path'] = array(
+    '#type' => 'value',
+    '#value' => 'download',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Search'),
+  );
+  return $form;
+}
+
+/**
+ * Utilizing the existing query, remove the version filter if it exists,
+ * add any new version filtering if it exists, and direct back to the relevant
+ * page with the appropriate filter string.
+ */
+function drupalorg_search_version_form_submit($form, &$form_state) {
+  $query = apachesolr_current_query();
+  $query->remove_filter('im_project_release_api_tids');
+  if (!empty($form_state['values']['api_version'])) {
+    $query->add_filter(variable_get('project_solr_project_release_api_tids_alias', 'api_version'), $form_state['values']['api_version']);
+  }
+
+  $form_state['redirect'] = array($form['path']['#value'], $query->get_url_queryvalues());
+}
\ No newline at end of file
