public function FacetapiLuceneapiAdapter::fetchDate in Facet API 6
Fetches data from facets that filter results by date ranges.
File
- contrib/
facetapi_luceneapi/ facetapi_luceneapi.adapter.inc, line 48 - Classes used by the Facet API module.
Class
- FacetapiLuceneapiAdapter
- Facet API adapter for Search Lucene API modules.
Code
public function fetchDate(array $facet) {
$sql = "\n SELECT MIN(term) as minimum, MAX(term) as maximum\n FROM {" . $this
->getSearcher() . "_termfreqs}\n WHERE field = '%s'\n ";
// Calculates the minimum and maximum values.
$range = array();
if ($result = db_query($sql, array(
$facet['field'],
))) {
if ($record = db_fetch_object($result)) {
$range = array(
$record->minimum,
$record->maximum,
);
}
}
// Gets all values in the resultset.
$raw_values = array();
foreach (facetapi_luceneapi_range_matches_get($this->_index, $range[0], $range[1], TRUE, array(
$facet['field'],
)) as $key => $term) {
if ($count = facetapi_luceneapi_terms_count($this->_index, $this->_docs, array(
$term,
))) {
$raw_values[$term->text] = $count;
}
}
// Sorts by timestamp if there are values, otherwise return an empty array.
if (!empty($raw_values)) {
$facet_items = array();
ksort($raw_values);
}
else {
return array();
}
// Gets the active date facets, starts to builds the "parent - child"
// relationships.
$parent = NULL;
$gap = NULL;
foreach ($this
->getActiveFacets($facet['field alias']) as $value => $active_facet) {
// Strips field from value, adds to $facet_items array.
$facet_items[$value] = array(
'count' => count($this->_docs),
);
// Gets next "gap" increment, mintue being the lowest be can go.
$gap = facetapi_next_date_gap_get(facetapi_date_gap_get($active_facet['start'], $active_facet['end']), FACETAPI_DATE_MINUTE);
// 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) {
$facet_items[$parent]['children'][$value] =& $facet_items[$value];
$facet_items[$value]['parents'][$parent] = $parent;
}
// Stores the last value iterated over.
$parent = $value;
}
// Mind the gap! Calculates gap from min and max timestamps.
$timestamps = array_keys($raw_values);
if (NULL === $parent) {
if (count($raw_values) > 1) {
$gap = facetapi_timestamp_gap_get(min($timestamps), max($timestamps));
}
else {
$gap = FACETAPI_DATE_HOUR;
}
}
// Converts all timestamps to dates in ISO 8601 format.
$dates = array_combine($timestamps, $timestamps);
array_walk($dates, 'facetapi_luceneapi_timestamp_convert', $gap);
// Treat each date as the range start and next data as the range end.
$range_end = array();
$previous = NULL;
foreach (array_unique($dates) as $date) {
if (NULL !== $previous) {
$range_end[$previous] = facetapi_next_date_increment_get($previous, $gap);
}
$previous = $date;
}
$range_end[$previous] = facetapi_next_date_increment_get($previous, $gap);
// Groups dates by the range they belong to, builds the $facet_items array
// with the facet counts and formatted range values.
foreach ($raw_values as $value => $count) {
$new_value = '[' . $dates[$value] . ' TO ' . $range_end[$dates[$value]] . ']';
if (!isset($facet_items[$new_value])) {
$facet_items[$new_value] = array(
'count' => $count,
);
}
else {
$facet_items[$new_value]['count'] += $count;
}
// Adds parent information if not already set.
if (NULL !== $parent && !isset($facet_items[$new_value]['parents'])) {
$facet_items[$parent]['children'][$new_value] =& $facet_items[$new_value];
$facet_items[$new_value]['parents'][$parent] = $parent;
}
}
// Returns the dates.
return $facet_items;
}