diff -u project.module project.module --- project.module 18 Aug 2010 23:08:12 -0000 +++ project.module 18 Aug 2010 23:53:48 -0000 @@ -1360,46 +1359,0 @@ -/** - * Render the HTML for a given block. - * - * This function takes a module and a delta, loads the block info from the DB, - * invokes the function to compute the block content, and then renders the - * block to HTML via theme('block'). This is basically a heavily pared back - * implementation of block_list() for a single block. - * - * @param string $module - * The module that defines the block we're rendering. - * - * @param string $delta - * The delta of the block that we're rendering. - * - * @return - * The rendered HTML for the given block. - * - * @see block_list() - * @see theme_block() - */ -function project_block_render($module, $delta) { - // Load the title, since that can be modified administratively. - $title = db_result(db_query("SELECT title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta)); - - $block_output = module_invoke($module, 'block', 'view', $delta); - foreach ($block_output as $k => $v) { - $block->$k = $v; - } - - // Set the module and delta so that the blocks contain - // at least the minimum of expected information. - $block->module = $module; - $block->delta = $delta; - - if (isset($block->content) && $block->content) { - // Override default block title if a custom display title is present. - if ($title) { - // Check plain admin-generated titles, but not titles set in code. - $block->subject = $title == '' ? '' : check_plain($title); - } - if (!isset($block->subject)) { - $block->subject = ''; - } - } - return theme('block', $block); -} diff -u solr/project_solr.module solr/project_solr.module --- solr/project_solr.module 18 Aug 2010 23:08:12 -0000 +++ solr/project_solr.module 18 Aug 2010 23:53:48 -0000 @@ -50,13 +50,13 @@ ), ), 'project_solr_category_page' => array( - 'template' => 'project-solr-category-page', - 'variables' => array( + 'arguments' => array( 'project_type' => NULL, 'categories' => array(), + 'version_form' => NULL, ), ), - 'project_solr_category_block' => array( + 'project_solr_category_list' => array( 'arguments' => array( 'items' => array(), ), @@ -386,37 +386,6 @@ return $output; } -/** - * Page callback for the listing of per-type categories. - */ -function project_solr_category_page($project_type) { - $tree = taxonomy_get_tree(_project_get_vid(), $project_type->tid); - if (empty($tree)) { - return drupal_not_found(); - } - $categories = array(); - foreach ($tree as $category_term) { - $categories[$category_term->tid] = $category_term->name; - } - return theme('project_solr_category_page', $project_type, $categories); -} - -/** - * Implementation of template_preprocess_project_solr_category_page(). - */ -function project_solr_preprocess_project_solr_category_page(&$variables) { - $project_type = $variables['project_type']; - $categories = $variables['categories']; - - foreach ($categories as $tid => $category_name) { - $delta = 'project_solr_category_block_' . $tid; - $variables[$delta] = project_block_render('project_solr', $delta); - } - - $search_path = 'project/' . drupal_strtolower($project_type->name) . '/categories'; - $variables['version_form'] = drupal_get_form('project_solr_version_form', $search_path); -} - //---------------------------------------- // Blocks //---------------------------------------- @@ -426,7 +395,7 @@ */ function project_solr_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { - $blocks = array( + return array( 'project_solr_categories' => array( 'info' => t('Project: categories'), 'cache' => BLOCK_CACHE_PER_PAGE, @@ -440,17 +409,6 @@ 'cache' => BLOCK_CACHE_PER_PAGE, ), ); - $vid = _project_get_vid(); - $tree = taxonomy_get_tree($vid, 0); - foreach ($tree as $term) { - if ($term->depth > 0) { - $blocks['project_solr_category_' . $term->tid] = array( - 'info' => t('Project Solr: Project category %category', array('%category' => $term->name)), - 'cache' => BLOCK_CACHE_GLOBAL, - ); - } - } - return $blocks; } if ($op == 'view' && apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) { @@ -590,124 +548,6 @@ ); } } - $matches = array(); - if ($op == 'view' && preg_match('/project_solr_category_(\d+)/', $delta)) { - return project_solr_view_category_block($matches[1]); - } -} - -/** - * Gather the content for a give project_solr category block. - * - * @tid - * The taxonomy term ID for the category to fetch the results of. - */ -function project_solr_view_category_block($tid) { - $term = taxonomy_get_term($tid); - $project_type = taxonomy_get_term($term->parent); - - // 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'), - ); - - 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(), $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); - - // This hook allows modules to modify the query and params objects. - apachesolr_modify_query($query, $params, 'project_solr_view_category_block'); - - $solr = apachesolr_get_solr(); - $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params); - - apachesolr_static_response_cache($response); - apachesolr_has_searched(TRUE); - - $items = array(); - if ($response->response->numFound > 0) { - foreach ($response->response->docs as $doc) { - $items[] = l($doc->title, 'node/' . $doc->nid); - } - } - - 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', - ); -*/ - return array( - 'subject' => check_plain($term->name), - 'content' => theme('project_solr_category_block', $items), - ); - } - return array(); -} - -/** - * Render the final markup for the content of a category block. - * - * @param array $items - * Array of links to projects in a given category block. - * - * @return - * Formatted HTML markup for the category block content. - * - * @see project_solr_view_category_block() - * @see theme_item_list() - */ -function theme_project_solr_category_block($items = array()) { - $output = ''; - if (!empty($items)) { - $output .= theme('item_list', $items); - } - return $output; } /** @@ -1096,0 +937,181 @@ + +//---------------------------------------- +// Category page and related functions +//---------------------------------------- + +/** + * Page callback for the listing of per-type categories. + * + * @param $project_type + * Fully-loaded taxonomy term object for the project type. + * + * @return + * Rendered page output for the project/%project_type/categories pages. + */ +function project_solr_category_page($project_type) { + $tree = taxonomy_get_tree(_project_get_vid(), $project_type->tid); + if (empty($tree)) { + return drupal_not_found(); + } + + drupal_set_title(t('@project_type categories', array('@project_type' => $project_type->name))); + + $categories = array(); + foreach ($tree as $category_term) { + $items = project_solr_fetch_category_items($project_type, $cateogry_term); + if (!empty($items)) { + $categories[$category_term->tid] = array( + 'title' => check_plain($category_term->name), + 'items' => $items, + ); + } + } + + $search_path = 'project/' . drupal_strtolower($project_type->name) . '/categories'; + $version_form = drupal_get_form('project_solr_version_form', $search_path); + + return theme('project_solr_category_page', $project_type, $categories, $version_form); +} + +/** + * Render the markup for the per-project type categories landing pages. + * + * @param $project_type + * Fully-loaded taxonomy term object for the project type. + * @param $categories + * Nested array of information about categories and projects. The keys are + * the taxonomy term IDs (tids) of each category for the given project_type + * that has projects associtated with it. The values are arrays with the + * keys 'title' for the human-readable (and sanitized) titles of each + * category, and 'items', which is an array of project links from the + * projects in the category. + * @param $version_form + * Optional rendered HTML of a version selector form to restrict results to + * a given API compatibility term. + */ +function theme_project_solr_category_page($project_type, $categories, $version_form = '') { + $output = ''; + if (!empty($version_form)) { + $output .= $version_form; + } + // TODO: This could probably use more slickness, a grid with multiple + // columns, etc. + foreach ($categories as $tid => $category) { + $output .= '

' . $category['title'] . '

'; + $output .= theme('project_solr_category_list', $category['items']); + } + return $output; +} + +/** + * Gather the items for a given project category. + * + * @param $project_type + * The fully-loaded taxonomy term for the project type. + * @param $category_term + * The fully-loaded taxonomy term for the category. + * + * @return + * 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'), + ); + + 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); + + // 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_view_category_block'); + + $solr = apachesolr_get_solr(); + $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params); + + apachesolr_static_response_cache($response); + apachesolr_has_searched(TRUE); + + $items = array(); + if ($response->response->numFound > 0) { + foreach ($response->response->docs as $doc) { + $items[] = l($doc->title, 'node/' . $doc->nid); + } + } + + 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', + ); +*/ + } + return $items; +} + +/** + * Render the final markup for a list of projects from a given category. + * + * @param array $items + * Array of links to projects in a given category. + * + * @return + * Formatted HTML markup for the list of project in a category. + * + * @see theme_project_solr_category_page() + * @see theme_item_list() + */ +function theme_project_solr_category_list($items = array()) { + $output = ''; + if (!empty($items)) { + $output .= theme('item_list', $items); + } + return $output; +} +