Mercurial > hg > solrsearch
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/facetapi/adapter.inc Mon Jun 08 10:21:54 2015 +0200 @@ -0,0 +1,217 @@ +<?php + +/** + * @file + * Classes used by the Facet API module. + */ + +/** + * Facet API adapter for the Apache Solr Search Integration module. + */ +class solrsearchFacetapiAdapter extends FacetapiAdapter { + + /** + * Returns the path to the admin settings for a given realm. + * + * @param string $realm_name + * The name of the realm. + * + * @return string + * The path to the admin settings. + */ + public function getPath($realm_name) { + $path = 'admin/config/search/solrsearch/settings'; + // $adapter will be an instance of class FacetapiAdapter + if ($adapter = menu_get_object('facetapi_adapter', 4)) { + // Get the environment ID from the machine name of the searcher. + $env_id = ltrim(strstr($adapter->getSearcher(), '@'), '@'); + $path .= '/' . $env_id . '/facets'; + // Add the realm name to the path if it is not the first one in the list. + if (key(facetapi_get_realm_info()) != $realm_name) { + $path .= '/' . $realm_name; + } + } + return $path; + } + + /** + * Allows the backend to initialize its query object before adding the facet + * filters. + * + * @param mixed $query + * The backend's native object. + */ + function initActiveFilters($query) { + $enabled_facets = facetapi_get_enabled_facets($this->info['name']); + if ($enabled_facets) { + $query->addParam('facet', 'true'); + $query->addParam('facet.sort', 'count'); + $query->addParam('facet.mincount', '1'); + } + } + + /** + * Returns a boolean flagging whether $this->_searcher executed a search. + */ + public function searchExecuted() { + // Initial check - has ANY solr query run in our environment. + $env_id = $this->info['instance']; + $this_has_searched = solrsearch_has_searched($env_id); + // Secondary check - do we have results for this searcher? + $this_has_searched = $this_has_searched && solrsearch_static_response_cache($this->getSearcher()); + return $this_has_searched; + } + + /** + * Suppress output of the realm + * + * @param string $realm_name + * + * @return bool $flag + * Returns if it was suppressed or not + */ + public function suppressOutput($realm_name) { + $flag = FALSE; + //dpm("suppressoutput"); + if ($realm_name == 'block') { + //dpm($this->info); + $env_id = $this->info['instance']; + $flag = solrsearch_suppress_blocks($env_id); + //dpm($env_id); + //dpm($flag); + + } + return FALSE; + return $flag || !$this->searchExecuted(); } + + /** + * Returns the search keys. + * + * @return string + */ + public function getSearchKeys() { + if (NULL === $this->keys) { + $env_id = $this->info['instance']; + if ($query = solrsearch_current_query($env_id)) { + return $query->getParam('q'); + } + } + else { + return $this->keys; + } + return FALSE; + } + + /** + * Returns the search path. + * + * @return string + * A string containing the search path. + * + * @todo D8 should provide an API function for this. + */ + public function getSearchPath() { + $env_id = $this->info['instance']; + $query = solrsearch_current_query($env_id); + if (!$query || (NULL === $this->searchPath && NULL === $query->getPath())) { + if ($path = module_invoke($this->info['module'] . '_search', 'search_info')) { + $this->searchPath = 'search/' . $path['path']; + if (!isset($_GET['keys']) && ($keys = $this->getSearchKeys())) { + $this->searchPath .= '/' . $keys; + } + } + } + if (!$query || NULL === $query->getPath()) { + return $this->searchPath; + } + else { + return $query->getPath(); + } + + } + + /** + * Returns the number of total results found for the current search. + * + * @return bool|int + * Number of results or false if no search response was found + */ + public function getResultCount() { + $response = solrsearch_static_response_cache($this->getSearcher()); + if ($response) { + return $response->response->numFound; + } + return FALSE; + } + + /** + * Allows for backend specific overrides to the settings form. + * + * @param array $form + * @param array $form_state + */ + public function settingsForm(&$form, &$form_state) { + $form['#validate'][] = 'solrsearch_facet_form_validate'; + } + + + /** + * Allows the backend to add facet queries to its native query object. + * + * This method is called by the implementing module to initialize the facet + * display process. The following actions are taken: + * - FacetapiAdapter::initActiveFilters() hook is invoked. + * - Dependency plugins are instantiated and executed. + * - Query type plugins are executed. + * + * @param mixed $query + * The backend's native query object. + * + * @todo Should this method be deprecated in favor of one name init()? This + * might make the code more readable in implementing modules. + * + * @see FacetapiAdapter::initActiveFilters() + */ + function addActiveFilters($query) { + module_load_include('inc', 'solrsearch', 'facetapi.callbacks'); + facetapi_add_active_searcher($this->info['name']); + + // Invoke initActiveFilters hook. + $this->initActiveFilters($query); + + + foreach ($this->getEnabledFacets() as $facet) { + + $settings = $this->getFacet($facet)->getSettings(); + + // Instantiate and execute dependency plugins. + $display = TRUE; + foreach ($facet['dependency plugins'] as $id) { + $class = ctools_plugin_load_class('facetapi', 'dependencies', $id, 'handler'); + $plugin = new $class($id, $this, $facet, $settings, $this->activeItems['facet']); + if (NULL !== ($return = $plugin->execute())) { + $display = $return; + } + } + + // Store whether this facet passed its dependencies. + $this->dependenciesPassed[$facet['name']] = $display; + + // Execute query type plugin if dependencies were met, otherwise remove + // the facet's active items so they don't display in the current search + // block or appear as active in the breadcrumb trail. + if ($display && $this->queryTypes[$facet['name']]) { + $this->queryTypes[$facet['name']]->execute($query); + } + else { + foreach ($this->activeItems['facet'][$facet['name']] as $item) { + $this->urlProcessor->removeParam($item['pos']); + $filter = $item['field alias'] . ':' . $item['value']; + unset($this->activeItems['filter'][$filter]); + } + $this->activeItems['facet'][$facet['name']] = array(); + } + } + + } +}