function apachesolr_facet_block in Apache Solr Search 6.2
Same name and namespace in other branches
- 5.2 apachesolr.module \apachesolr_facet_block()
- 5 apachesolr.module \apachesolr_facet_block()
- 6 apachesolr.module \apachesolr_facet_block()
Helper function for displaying a facet block.
1 call to apachesolr_facet_block()
- apachesolr_search_block in ./
apachesolr_search.module - Implementation of hook_block().
2 string references to 'apachesolr_facet_block'
- apachesolr_date_block in contrib/
apachesolr_date/ apachesolr_date.module - Implementation of hook_block().
- apachesolr_search_block in ./
apachesolr_search.module - Implementation of hook_block().
File
- ./
apachesolr.module, line 1078 - Integration with the Apache Solr search application.
Code
function apachesolr_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
if (!empty($response->facet_counts->facet_fields->{$facet_field})) {
$facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
$contains_active = FALSE;
$items = array();
foreach ($response->facet_counts->facet_fields->{$facet_field} as $facet => $count) {
$options = array(
'delta' => $delta,
);
$exclude = FALSE;
// Solr sends this back if it's empty.
if ($facet == '_empty_') {
$exclude = TRUE;
$facet = '[* TO *]';
$options['html'] = TRUE;
}
if ($facet_callback && function_exists($facet_callback)) {
$facet_text = $facet_callback($facet, $options);
}
elseif ($exclude) {
$facet_text = theme('placeholder', t('Missing this field'));
}
else {
$facet_text = $facet;
}
$active = $query
->has_filter($facet_field, $facet);
if ($active) {
// '*' sorts before all numbers.
$sortpre = '*';
}
elseif (isset($facet_query_sorts[$module][$delta]) && strpos($facet_query_sorts[$module][$delta], 'index key') === 0) {
// If this block is to be alphabetically sorted by key, change $sortpre.
$sortpre = $facet;
}
elseif (isset($facet_query_sorts[$module][$delta]) && strpos($facet_query_sorts[$module][$delta], 'index') === 0) {
// If this block is to be alphabetically/numerically sorted by value, change $sortpre.
$sortpre = $facet_text;
}
elseif ($exclude) {
// '-' sorts before all numbers, but after '*'.
$sortpre = '-';
}
else {
$sortpre = 1000000 - $count;
}
$new_query = clone $query;
if ($active) {
$contains_active = TRUE;
$new_query
->remove_filter($facet_field, $facet);
$options['query'] = $new_query
->get_url_queryvalues();
$link = theme('apachesolr_unclick_link', $facet_text, $new_query
->get_path(), $options);
}
else {
$new_query
->add_filter($facet_field, $facet, $exclude);
$options['query'] = $new_query
->get_url_queryvalues();
$link = theme('apachesolr_facet_link', $facet_text, $new_query
->get_path(), $options, $count, FALSE, $response->response->numFound);
}
if ($count || $active) {
$items[$sortpre . '*' . $facet_text] = $link;
}
}
// Unless a facet is active only display 2 or more.
if ($items && ($response->response->numFound > 1 || $contains_active)) {
switch ($facet_query_sorts[$module][$delta]) {
case 'index numeric asc':
ksort($items, SORT_NUMERIC);
break;
case 'index numeric desc':
krsort($items, SORT_NUMERIC);
break;
case 'index desc':
case 'index key desc':
krsort($items, SORT_STRING);
break;
case 'index asc':
case 'index key asc':
default:
ksort($items, SORT_STRING);
break;
}
// Get information needed by the rest of the blocks about limits.
$initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
$limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
$output = theme('apachesolr_facet_list', $items, $limit, $delta);
return array(
'subject' => $filter_by,
'content' => $output,
);
}
}
return NULL;
}