/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { $facet = $this->adapter->getFacet($this->facet); $search_ids = drupal_static('search_api_facetapi_active_facets', array()); if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) { return array(); } $search_id = $search_ids[$facet['name']]; $build = array(); $search = search_api_current_search($search_id); $results = $search[1]; if (!$results['result count']) { return array(); } // Executes query, iterates over results. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['field']])) { $values = $results['search_api_facets'][$this->facet['field']]; $build = date_facets_get_ranges(); $now = $_SERVER['REQUEST_TIME']; // Calculate values by facet. foreach ($values as $value) { $value['filter'] = str_replace('"', '', $value['filter']); $diff = $now - $value['filter']; foreach ($build as $key => $item) { if ($diff < $item['#time_interval']) { $build[$key]['#count'] += $value['count']; } } } } // Unset empty items. foreach ($build as $key => $item) { if ($item['#count'] === NULL) { unset($build[$key]); } } // Gets total number of documents matched in search. $total = $results['result count']; $keys_of_active_facets = array(); // Gets active facets, starts building hierarchy. foreach ($this->adapter->getActiveItems($this->facet) as $key => $item) { // If the item is active, the count is the result set count. $build[$key]['#count'] = $total; $keys_of_active_facets[] = $key; } // If we have active item, unset other items. $settings = $facet->getSettings()->settings; if (isset($settings['operator']) && $settings['operator'] !== FACETAPI_OPERATOR_OR) { if (!empty($keys_of_active_facets)) { foreach ($build as $key => $item) { if (!in_array($key, $keys_of_active_facets)) { unset($build[$key]); } } } } return $build; }
/** * Implements FacetapiQueryTypeInterface::build(). */ public function build() { // Get the configured date ranges for the facet, or defaults if none have // been set up. $realm = facetapi_realm_load('block'); $settings = $this->adapter->getFacetSettings($this->facet, $realm); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the facet's date ranges. $build = date_facets_get_ranges($ranges); if ($this->adapter->searchExecuted()) { $facet_global_settings = $this->adapter->getFacet($this->facet)->getSettings(); // Iterate over each date range to get a count of results for that item. foreach ($ranges as $range) { // Generate our conditions for this date range. list($start, $end) = $this->generateRange($range); // We need to clone the query so we can get a count for each of our // date ranges. $facet_query = clone $this->adapter->getFacetQueryExtender(); $query_info = $this->adapter->getQueryInfo($this->facet); $facet_query->addFacetField($query_info); // Add the appropriate joins to our query, and add our date range // conditions. foreach ($query_info['fields'] as $field_info) { $facet_query->addFacetJoin($query_info, $field_info['table_alias']); $field = $field_info['table_alias'] . '.' . $this->facet['field']; $facet_query->condition($field, $start, '>='); $facet_query->condition($field, $end, '<'); } // Executes query, iterates over results. $result = $facet_query->execute()->fetchAll(); // If the result is 0, and the mincount is set to more than 0, remove // the facet option. if (empty($result) && $facet_global_settings->settings['facet_mincount'] > 0) { unset($build[$range['machine_name']]); } // Add the facet option counts to the build array. foreach ($result as $record) { $build[$range['machine_name']]['#count'] = $record->count; } } } return $build; }
/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { return date_facets_get_ranges(); }
/** * Maps a facet item to a filter. * * @param string $range_machine_name * For example 'past_hour'. * * @return string|false * A string that can be used as a filter, false if no filter was found. */ public static function mapFacetItemToFilter($range_machine_name, $facet) { $ranges = date_facets_get_ranges($facet['name'], $facet['map options']['index id']); $filter_str = FALSE; if (isset($ranges[$range_machine_name])) { $start = self::rangeToString($ranges[$range_machine_name], 'start'); $end = self::rangeToString($ranges[$range_machine_name], 'end'); // Future. if ($ranges[$range_machine_name]['date_range_end_op'] == '+') { $filter_str = "[{$start} TO {$end}]"; } elseif ($ranges[$range_machine_name]['date_range_start_op'] == '-') { $filter_str = "[{$start} TO {$end}]"; } } return $filter_str; }
/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { // Get facet's configured date ranges, or defaults if none have been set up. $settings = $this->adapter->getFacetSettings($this->facet, facetapi_realm_load('block')); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the date ranges. $build = date_facets_get_ranges($ranges); if ($response = apachesolr_static_response_cache($this->adapter->getSearcher())) { $facet_global_settings = $this->adapter->getFacet($this->facet)->getSettings(); $values = (array) $response->facet_counts->facet_queries; // Add result counts from the facet queries added in execute(). foreach ($ranges as $range) { list($start, $end) = $this->generateRange($range); $build[$range['machine_name']]['#count'] = $values[$this->facet['field'] . ":[{$start} TO {$end}]"]; } // Unset options with fewer results than the minimum count setting. foreach ($build as $name => $item) { if ($item['#count'] < $facet_global_settings->settings['facet_mincount']) { unset($build[$name]); } } } return $build; }