Mercurial > hg > MPIWG-drupal-modules
comparison sites/all/modules/custom/solrconnect/plugins/facetapi/adapter.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 * @file | |
5 * Classes used by the Facet API module. | |
6 */ | |
7 | |
8 /** | |
9 * Facet API adapter for the Apache Solr Search Integration module. | |
10 */ | |
11 class ApacheSolrFacetapiAdapter extends FacetapiAdapter { | |
12 | |
13 /** | |
14 * Returns the path to the admin settings for a given realm. | |
15 * | |
16 * @param string $realm_name | |
17 * The name of the realm. | |
18 * | |
19 * @return string | |
20 * The path to the admin settings. | |
21 */ | |
22 public function getPath($realm_name) { | |
23 $path = 'admin/config/search/apachesolr/settings'; | |
24 // $adapter will be an instance of class FacetapiAdapter | |
25 if ($adapter = menu_get_object('facetapi_adapter', 4)) { | |
26 // Get the environment ID from the machine name of the searcher. | |
27 $env_id = ltrim(strstr($adapter->getSearcher(), '@'), '@'); | |
28 $path .= '/' . $env_id . '/facets'; | |
29 // Add the realm name to the path if it is not the first one in the list. | |
30 if (key(facetapi_get_realm_info()) != $realm_name) { | |
31 $path .= '/' . $realm_name; | |
32 } | |
33 } | |
34 return $path; | |
35 } | |
36 | |
37 /** | |
38 * Allows the backend to initialize its query object before adding the facet | |
39 * filters. | |
40 * | |
41 * @param mixed $query | |
42 * The backend's native object. | |
43 */ | |
44 function initActiveFilters($query) { | |
45 $enabled_facets = facetapi_get_enabled_facets($this->info['name']); | |
46 if ($enabled_facets) { | |
47 $query->addParam('facet', 'true'); | |
48 $query->addParam('facet.sort', 'count'); | |
49 $query->addParam('facet.mincount', '1'); | |
50 } | |
51 } | |
52 | |
53 /** | |
54 * Returns a boolean flagging whether $this->_searcher executed a search. | |
55 */ | |
56 public function searchExecuted() { | |
57 // Initial check - has ANY solr query run in our environment. | |
58 $env_id = $this->info['instance']; | |
59 $this_has_searched = apachesolr_has_searched($env_id); | |
60 // Secondary check - do we have results for this searcher? | |
61 $this_has_searched = $this_has_searched && apachesolr_static_response_cache($this->getSearcher()); | |
62 return $this_has_searched; | |
63 } | |
64 | |
65 /** | |
66 * Suppress output of the realm | |
67 * | |
68 * @param string $realm_name | |
69 * | |
70 * @return bool $flag | |
71 * Returns if it was suppressed or not | |
72 */ | |
73 public function suppressOutput($realm_name) { | |
74 $flag = FALSE; | |
75 if ($realm_name == 'block') { | |
76 $env_id = $this->info['instance']; | |
77 $flag = apachesolr_suppress_blocks($env_id); | |
78 } | |
79 return $flag || !$this->searchExecuted(); | |
80 } | |
81 | |
82 /** | |
83 * Returns the search keys. | |
84 * | |
85 * @return string | |
86 */ | |
87 public function getSearchKeys() { | |
88 if (NULL === $this->keys) { | |
89 $env_id = $this->info['instance']; | |
90 if ($query = apachesolr_current_query($env_id)) { | |
91 return $query->getParam('q'); | |
92 } | |
93 } | |
94 else { | |
95 return $this->keys; | |
96 } | |
97 return FALSE; | |
98 } | |
99 | |
100 /** | |
101 * Returns the search path. | |
102 * | |
103 * @return string | |
104 * A string containing the search path. | |
105 * | |
106 * @todo D8 should provide an API function for this. | |
107 */ | |
108 public function getSearchPath() { | |
109 $env_id = $this->info['instance']; | |
110 $query = apachesolr_current_query($env_id); | |
111 if (!$query || (NULL === $this->searchPath && NULL === $query->getPath())) { | |
112 if ($path = module_invoke($this->info['module'] . '_search', 'search_info')) { | |
113 $this->searchPath = 'search/' . $path['path']; | |
114 if (!isset($_GET['keys']) && ($keys = $this->getSearchKeys())) { | |
115 $this->searchPath .= '/' . $keys; | |
116 } | |
117 } | |
118 } | |
119 if (!$query || NULL === $query->getPath()) { | |
120 return $this->searchPath; | |
121 } | |
122 else { | |
123 return $query->getPath(); | |
124 } | |
125 | |
126 } | |
127 | |
128 /** | |
129 * Returns the number of total results found for the current search. | |
130 * | |
131 * @return bool|int | |
132 * Number of results or false if no search response was found | |
133 */ | |
134 public function getResultCount() { | |
135 $response = apachesolr_static_response_cache($this->getSearcher()); | |
136 if ($response) { | |
137 return $response->response->numFound; | |
138 } | |
139 return FALSE; | |
140 } | |
141 | |
142 /** | |
143 * Allows for backend specific overrides to the settings form. | |
144 * | |
145 * @param array $form | |
146 * @param array $form_state | |
147 */ | |
148 public function settingsForm(&$form, &$form_state) { | |
149 $form['#validate'][] = 'apachesolr_facet_form_validate'; | |
150 } | |
151 } |