public function SearchApiFacetapiDate::build in Search API 7
Initializes the facet's build array.
Return value
array The initialized render array.
Overrides SearchApiFacetapiTerm::build
File
- contrib/
search_api_facetapi/ plugins/ facetapi/ query_type_date.inc, line 226 - Date query type plugin for the Search API adapter.
Class
- SearchApiFacetapiDate
- Plugin for "date" query types.
Code
public function build() {
$facet = $this->adapter
->getFacet($this->facet);
$search_ids = drupal_static('search_api_facetapi_active_facets', array());
$facet_key = $facet['name'] . '@' . $this->adapter
->getSearcher();
if (empty($search_ids[$facet_key]) || !search_api_current_search($search_ids[$facet_key])) {
return array();
}
$search_id = $search_ids[$facet_key];
$build = array();
$search = search_api_current_search($search_id);
$results = $search[1];
// Gets total number of documents matched in search.
$total = $results['result count'];
// Executes query, iterates over results.
if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['name']])) {
$values = $results['search_api_facets'][$this->facet['name']];
$mincount = $facet
->getSettings()->settings['facet_mincount'];
foreach ($values as $value) {
if ($value['count'] >= $mincount) {
$filter = $value['filter'];
// We only process single values further. The "missing" filter and
// range filters will be passed on unchanged.
if ($filter == '!') {
$build[$filter]['#count'] = $value['count'];
}
elseif ($filter[0] == '"') {
$filter = substr($value['filter'], 1, -1);
if ($filter) {
$raw_values[$filter] = $value['count'];
}
}
else {
$build[$filter]['#count'] = $value['count'];
}
}
}
}
$settings = $facet
->getSettings()->settings;
// Get the finest level of detail we're allowed to drill down to.
$max_granularity = FACETAPI_DATE_MINUTE;
if (isset($settings['date_granularity'])) {
$max_granularity = $settings['date_granularity'];
}
// Get the coarsest level of detail we're allowed to start at.
$min_granularity = FACETAPI_DATE_YEAR;
if (isset($settings['date_granularity_min'])) {
$min_granularity = $settings['date_granularity_min'];
}
// Gets active facets, starts building hierarchy.
$parent = $granularity = NULL;
$active_items = $this->adapter
->getActiveItems($this->facet);
foreach ($active_items as $value => $item) {
// If the item is active, the count is the result set count.
$build[$value] = array(
'#count' => $total,
);
// Gets next "gap" increment. Ignore any filters passed directly from the
// server (range or missing). We always create filters starting with a
// year.
$value = "{$value}";
if (!$value || !ctype_digit($value[0])) {
continue;
}
$granularity = search_api_facetapi_date_get_granularity($value);
if (!$granularity) {
continue;
}
$granularity = facetapi_get_next_date_gap($granularity, $max_granularity);
// 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;
}
}
if (empty($raw_values)) {
return $build;
}
ksort($raw_values);
// Mind the gap! Calculates gap from min and max timestamps.
$timestamps = array_keys($raw_values);
if (NULL === $parent) {
if (count($raw_values) > 1) {
$granularity = facetapi_get_timestamp_gap(min($timestamps), max($timestamps), $max_granularity);
// Array of numbers used to determine whether the next gap is smaller than
// the minimum gap allowed in the drilldown.
$gap_numbers = array(
FACETAPI_DATE_YEAR => 6,
FACETAPI_DATE_MONTH => 5,
FACETAPI_DATE_DAY => 4,
FACETAPI_DATE_HOUR => 3,
FACETAPI_DATE_MINUTE => 2,
FACETAPI_DATE_SECOND => 1,
);
// Gets gap numbers for both the gap, minimum and maximum gap, checks if
// the gap is within the limit set by the $granularity parameters.
if ($gap_numbers[$granularity] < $gap_numbers[$max_granularity]) {
$granularity = $max_granularity;
}
if ($gap_numbers[$granularity] > $gap_numbers[$min_granularity]) {
$granularity = $min_granularity;
}
}
else {
$granularity = $max_granularity;
}
}
// Groups dates by the range they belong to, builds the $build array with
// the facet counts and formatted range values.
$format = search_api_facetapi_date_get_granularity_format($granularity);
foreach ($raw_values as $value => $count) {
$new_value = date($format, $value);
if (!isset($build[$new_value])) {
$build[$new_value] = array(
'#count' => $count,
);
}
elseif (!isset($active_items[$new_value])) {
$build[$new_value]['#count'] += $count;
}
// Adds parent information if not already set.
if (NULL !== $parent && $parent != $new_value) {
$build[$parent]['#item_children'][$new_value] =& $build[$new_value];
$build[$new_value]['#item_parents'][$parent] = $parent;
}
}
return $build;
}