comparison plugins/facetapi/adapter.inc @ 0:a2b4f67e73dc default tip

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Mon, 08 Jun 2015 10:21:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a2b4f67e73dc
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 solrsearchFacetapiAdapter 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/solrsearch/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 = solrsearch_has_searched($env_id);
60 // Secondary check - do we have results for this searcher?
61 $this_has_searched = $this_has_searched && solrsearch_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 //dpm("suppressoutput");
76 if ($realm_name == 'block') {
77 //dpm($this->info);
78 $env_id = $this->info['instance'];
79 $flag = solrsearch_suppress_blocks($env_id);
80 //dpm($env_id);
81 //dpm($flag);
82
83 }
84 return FALSE;
85 return $flag || !$this->searchExecuted(); }
86
87 /**
88 * Returns the search keys.
89 *
90 * @return string
91 */
92 public function getSearchKeys() {
93 if (NULL === $this->keys) {
94 $env_id = $this->info['instance'];
95 if ($query = solrsearch_current_query($env_id)) {
96 return $query->getParam('q');
97 }
98 }
99 else {
100 return $this->keys;
101 }
102 return FALSE;
103 }
104
105 /**
106 * Returns the search path.
107 *
108 * @return string
109 * A string containing the search path.
110 *
111 * @todo D8 should provide an API function for this.
112 */
113 public function getSearchPath() {
114 $env_id = $this->info['instance'];
115 $query = solrsearch_current_query($env_id);
116 if (!$query || (NULL === $this->searchPath && NULL === $query->getPath())) {
117 if ($path = module_invoke($this->info['module'] . '_search', 'search_info')) {
118 $this->searchPath = 'search/' . $path['path'];
119 if (!isset($_GET['keys']) && ($keys = $this->getSearchKeys())) {
120 $this->searchPath .= '/' . $keys;
121 }
122 }
123 }
124 if (!$query || NULL === $query->getPath()) {
125 return $this->searchPath;
126 }
127 else {
128 return $query->getPath();
129 }
130
131 }
132
133 /**
134 * Returns the number of total results found for the current search.
135 *
136 * @return bool|int
137 * Number of results or false if no search response was found
138 */
139 public function getResultCount() {
140 $response = solrsearch_static_response_cache($this->getSearcher());
141 if ($response) {
142 return $response->response->numFound;
143 }
144 return FALSE;
145 }
146
147 /**
148 * Allows for backend specific overrides to the settings form.
149 *
150 * @param array $form
151 * @param array $form_state
152 */
153 public function settingsForm(&$form, &$form_state) {
154 $form['#validate'][] = 'solrsearch_facet_form_validate';
155 }
156
157
158 /**
159 * Allows the backend to add facet queries to its native query object.
160 *
161 * This method is called by the implementing module to initialize the facet
162 * display process. The following actions are taken:
163 * - FacetapiAdapter::initActiveFilters() hook is invoked.
164 * - Dependency plugins are instantiated and executed.
165 * - Query type plugins are executed.
166 *
167 * @param mixed $query
168 * The backend's native query object.
169 *
170 * @todo Should this method be deprecated in favor of one name init()? This
171 * might make the code more readable in implementing modules.
172 *
173 * @see FacetapiAdapter::initActiveFilters()
174 */
175 function addActiveFilters($query) {
176 module_load_include('inc', 'solrsearch', 'facetapi.callbacks');
177 facetapi_add_active_searcher($this->info['name']);
178
179 // Invoke initActiveFilters hook.
180 $this->initActiveFilters($query);
181
182
183 foreach ($this->getEnabledFacets() as $facet) {
184
185 $settings = $this->getFacet($facet)->getSettings();
186
187 // Instantiate and execute dependency plugins.
188 $display = TRUE;
189 foreach ($facet['dependency plugins'] as $id) {
190 $class = ctools_plugin_load_class('facetapi', 'dependencies', $id, 'handler');
191 $plugin = new $class($id, $this, $facet, $settings, $this->activeItems['facet']);
192 if (NULL !== ($return = $plugin->execute())) {
193 $display = $return;
194 }
195 }
196
197 // Store whether this facet passed its dependencies.
198 $this->dependenciesPassed[$facet['name']] = $display;
199
200 // Execute query type plugin if dependencies were met, otherwise remove
201 // the facet's active items so they don't display in the current search
202 // block or appear as active in the breadcrumb trail.
203 if ($display && $this->queryTypes[$facet['name']]) {
204 $this->queryTypes[$facet['name']]->execute($query);
205 }
206 else {
207 foreach ($this->activeItems['facet'][$facet['name']] as $item) {
208 $this->urlProcessor->removeParam($item['pos']);
209 $filter = $item['field alias'] . ':' . $item['value'];
210 unset($this->activeItems['filter'][$filter]);
211 }
212 $this->activeItems['facet'][$facet['name']] = array();
213 }
214 }
215
216 }
217 }