comparison sites/all/modules/custom/solrconnect/plugins/facetapi/query_type_geo.inc @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:015d06b10d37
1 <?php
2
3 /**
4 * Plugin for "apachesolr_geo" query types.
5 */
6 class ApacheSolrFacetapiGeo extends FacetapiQueryType implements FacetapiQueryTypeInterface {
7 // Center point is Denver.
8 public $center_point = '39.7391667,-104.9841667';
9 public $facet_options = '0.5,0.1,0.01';
10 public $default_radius = 100;
11
12 /**
13 * Returns the query type associated with the plugin.
14 *
15 * @return string
16 * The query type.
17 */
18 static public function getType() {
19 return 'geo';
20 }
21
22 /**
23 * Adds the filter to the query object.
24 *
25 * @param DrupalSolrQueryInterface $query
26 * An object containing the query in the backend's native API.
27 */
28 public function execute($query) {
29 // Retrieve settings of the facet.
30 // We should be able to get all constants as facet options.
31 $settings = $this->adapter->getFacet($this->facet)->getSettings();
32
33 $facet_distances = explode(',', $this->facet_options);
34
35 $active_items = $this->adapter->getActiveItems($this->facet);
36
37 if (empty($active_items)) {
38 $distance = $this->default_radius;
39 }
40 else {
41 $active_item = array_pop($active_items);
42 $distance = substr($active_item['value'], 1);
43 // Add current selected distance to have possibility to unselect it.
44 $facet_distances[] = 1;
45 }
46
47 // Search center point.
48 $query->addParam('pt', $this->center_point);
49
50 // Set location field name.
51 $query->addParam('sfield', $this->facet['field']);
52 $query->addParam('fq', '{!geofilt sfield=' . $this->facet['field'] . '}');
53
54 // Set search radius.
55 $query->addParam('d', $distance);
56
57 // Set facets.
58 foreach ($facet_distances as $facet_option) {
59 $facet_distance = $distance * $facet_option;
60 $query->addParam('facet.query', '{!geofilt d=' . $facet_distance . ' key=d' . $facet_distance . '}');
61 }
62 }
63
64 /**
65 * Initializes the facet's build array.
66 *
67 * @return array
68 * The initialized render array.
69 */
70 public function build() {
71 $build = array();
72 if ($response = apachesolr_static_response_cache($this->adapter->getSearcher())) {
73 if (isset($response->facet_counts->facet_queries)) {
74 foreach ($response->facet_counts->facet_queries as $value => $count) {
75 // Skip zero results values.
76 if ($count > 0) {
77 $build[$value] = array('#count' => $count);
78 }
79 }
80 }
81 }
82 return $build;
83 }
84 }