diff -u solr/project_solr.module solr/project_solr.module --- solr/project_solr.module 18 Aug 2010 23:53:48 -0000 +++ solr/project_solr.module 19 Aug 2010 01:31:44 -0000 @@ -551,44 +551,6 @@ } /** - * Return a Form API array for an API version selector field. - * - * @param object $query - * An existing query object that we can check for existing filters. - * @param string $label - * Optional form label for the version selector field. - * - * @return - * Form array for the API version selector field. - */ -function project_solr_get_api_version_field($query, $label = NULL) { - if (module_exists('project_release')) { - $current_tid = ''; - $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; - if (!isset($label)) { - $label = t('Filter by compatibility'); - } - return array( - '#title' => $label, - '#type' => 'select', - '#options' => $terms, - '#default_value' => $current_tid, - ); - } - } -} - -/** * Build project browsing navigation form. */ function project_solr_browse_projects_form(&$form_state, $project_type, $path) { @@ -806,6 +768,117 @@ $form_state['redirect'] = array($form_state['values']['path'], $queryvalues); } +//---------------------------------------- +// Public helper methods +//---------------------------------------- + +/** + * Build a Solr query to find project nodes. + * + * @param object $project_type + * A taxonomy term object for the project type to query for. Must include at + * least 'tid' and 'name' fields. + * @param array $filters + * Optional array of Solr filters to add to the query. + * + * @return + * An ApacheSolr $query object. // TODO + */ +function project_solr_build_project_query($project_type, $filters = array()) { + // Build a simple query. + $base_path = 'project/' . drupal_strtolower($project_type->name); + + $filters = isset($_GET['filters']) ? $_GET['filters'] : ''; + $query = apachesolr_drupal_query('', $filters, '', $base_path); + + $params = array( + // The fields to return. + 'fl' => 'id,nid,title', + 'start' => 0, + 'rows' => 5, // TODO: make this configurable. + // 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', + ), + // Filters are set below via explicit calls to add_filter() rather than + // here in the $params array. This is because any filters passed to + // apachesolr_drupal_query() will override filters set via the $params + // array during the prepare_query() invocation. + 'fq' => array(), + 'facet' => 'true', + 'facet.mincount' => 1, + 'facet.sort' => 'true', + 'sort' => variable_get('project_solr_default_sort', 'sort_title asc'), + ); + + 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, 'project_solr_view_category_block'); + } + + // 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. + // We explicitly filter the blocks to only show projects with the right + // category. These values are not passed in the url, so we add them here + // ourselves to ensure we only get the content we want. + $query->add_filter('type', 'project_project'); + $query->add_filter('im_vid_' . _project_get_vid(), $project_type->tid); + + if (!empty($filters)) { + foreach ($filters as $filter) { + $query->add_filter($filter['key'], $filter['value']); + } + } + + return $query; +} + +/** + * Return a Form API array for an API version selector field. + * + * @param object $query + * An existing query object that we can check for existing filters. + * @param string $label + * Optional form label for the version selector field. + * + * @return + * Form array for the API version selector field. + */ +function project_solr_get_api_version_field($query, $label = NULL) { + if (module_exists('project_release')) { + $current_tid = ''; + $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; + if (!isset($label)) { + $label = t('Filter by compatibility'); + } + return array( + '#title' => $label, + '#type' => 'select', + '#options' => $terms, + '#default_value' => $current_tid, + ); + } + } +} + /** * Adds project-specific sorts to the query object * @@ -958,7 +1031,7 @@ $categories = array(); foreach ($tree as $category_term) { - $items = project_solr_fetch_category_items($project_type, $cateogry_term); + $items = project_solr_fetch_category_items($project_type, $category_term); if (!empty($items)) { $categories[$category_term->tid] = array( 'title' => check_plain($category_term->name), @@ -1015,52 +1088,16 @@ * Array of projects from the given category. */ function project_solr_fetch_category_items($project_type, $category_term) { - // Build a simple query. - $base_path = 'project/' . drupal_strtolower($project_type->name) . '/categories'; - - $filters = isset($_GET['filters']) ? $_GET['filters'] : ''; - $query = apachesolr_drupal_query('', $filters, '', $base_path); - - $params = array( - // The fields to return. - 'fl' => 'id,nid,title', - 'start' => 0, - 'rows' => 5, // TODO: make this configurable. - // 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', - ), - // Filters are set below via explicit calls to add_filter() rather than - // here in the $params array. This is because any filters passed to - // apachesolr_drupal_query() will override filters set via the $params - // array during the prepare_query() invocation. - 'fq' => array(), - 'facet' => 'true', - 'facet.mincount' => 1, - 'facet.sort' => 'true', - 'sort' => variable_get('project_solr_default_sort', 'sort_title asc'), + $filters = array(); + $filters[] = array( + 'key' => 'im_vid_' . _project_get_vid(), + 'value' => $category_term->tid, ); + $query = project_solr_build_project_query($project_type, $filters); - 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, 'project_solr_view_category_block'); - } - - // 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. - // We explicitly filter the blocks to only show projects with the right - // category. These values are not passed in the url, so we add them here - // ourselves to ensure we only get the content we want. - $query->add_filter('type', 'project_project'); - $query->add_filter('im_vid_' . _project_get_vid(), $category_term->tid); + // Before we call modify_query() we want to save the URL query values so + // that our 'More' links are cleaner and free of excess junk. + $query_values = $query->get_url_queryvalues(); // Cache the built query. Since all the built queries go through // this process, all the hook_invocations will happen later. @@ -1084,13 +1121,10 @@ if ($items) { // Add the "more" link. - // TODO -/* $items[] = array( - 'data' => l(t('More @title', array('@title' => $facet['info'])), $order_query->get_path(), array('query' => $query_values)), - 'class' => 'all', + 'data' => l(t('More @category', array('@category' => $category_term->name)), $query->get_path(), array('query' => $query_values)), + 'class' => 'more', ); -*/ } return $items; }