function facetapi_apachesolr_facetapi_facet_date_build in Facet API 6
Implementation of hook_facetapi_facet_QUERY_TYPE_build().
File
- contrib/
facetapi_apachesolr/ facetapi_apachesolr.module, line 208 - The Apache Solr Search Integration module's implementation of the the Facet API.
Code
function facetapi_apachesolr_facetapi_facet_date_build(FacetapiAdapter $adapter, array $facet) {
$build = array();
if (!($response = apachesolr_static_response_cache())) {
return array();
}
// Gets total number of documents matched in search.
// NOTE: We need a Solr_Base_Query::get_solr() method.
static $total;
if (NULL === $total) {
if ($raw_response = json_decode($response
->getRawResponse())) {
$total = $raw_response->response->numFound;
}
else {
$total = 0;
}
}
// Gets the active date facets, starts to builds the "parent - child"
// relationships.
$parent = NULL;
foreach ($adapter
->getActiveItems($facet) as $value => $item) {
// Builds the raw facet "value", the count for selected items will be the
// total number of rows returned in the query.
$build[$value] = array(
'#count' => $total,
);
// If there is a previous item, there is a parent, uses a reference so the
// arrays are populated when they are updated.
if (NULL !== $parent) {
$build[$parent]['#item_children'][$value] =& $build[$value];
$build[$value]['#item_parents'][$parent] = $parent;
}
// Stores the last value iterated over.
$parent = $value;
}
// Gets raw facet data from the Solr server.
if (isset($response->facet_counts->facet_dates)) {
$raw_data = (array) $response->facet_counts->facet_dates->{$facet['field']};
}
else {
$raw_data = array();
}
$end = !empty($raw_data['end']) ? $raw_data['end'] : '';
$gap = !empty($raw_data['gap']) ? $raw_data['gap'] : '';
unset($raw_data['end']);
unset($raw_data['gap']);
// Treat each date facet as a range start, and use the next date facet
// as range end. Use 'end' for the final end.
$range_end = array();
$previous = NULL;
foreach ($raw_data as $value => $count) {
if (isset($previous)) {
$range_end[$previous] = $value;
}
$previous = $value;
}
$range_end[$previous] = $end;
// Builds facet counts object used by the server.
foreach ($raw_data as $value => $count) {
if ($count) {
$new_value = '[' . $value . ' TO ' . $range_end[$value] . ']';
$build[$new_value] = array(
'#count' => $count,
'#active' => 0,
);
if (NULL !== $parent) {
$build[$parent]['#item_children'][$new_value] =& $build[$new_value];
$build[$new_value]['#item_parents'][$parent] = $parent;
}
}
}
return $build;
}