adapter.inc 4.9 KB

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