adapter.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @file
  4. * Classes used by the Facet API module.
  5. */
  6. /**
  7. * Facet API adapter for the Search API module.
  8. */
  9. class SearchApiFacetapiAdapter extends FacetapiAdapter {
  10. /**
  11. * Cached value for the current search for this searcher, if any.
  12. *
  13. * @see getCurrentSearch()
  14. *
  15. * @var array
  16. */
  17. protected $current_search;
  18. /**
  19. * The active facet fields for the current search.
  20. *
  21. * @var array
  22. */
  23. protected $fields = array();
  24. /**
  25. * Returns the path to the admin settings for a given realm.
  26. *
  27. * @param $realm_name
  28. * The name of the realm.
  29. *
  30. * @return
  31. * The path to the admin settings.
  32. */
  33. public function getPath($realm_name) {
  34. $base_path = 'admin/config/search/search_api';
  35. $index_id = $this->info['instance'];
  36. return $base_path . '/index/' . $index_id . '/facets/' . $realm_name;
  37. }
  38. /**
  39. * Overrides FacetapiAdapter::getSearchPath().
  40. */
  41. public function getSearchPath() {
  42. $search = $this->getCurrentSearch();
  43. if ($search && $search[0]->getOption('search_api_base_path')) {
  44. return $search[0]->getOption('search_api_base_path');
  45. }
  46. return $_GET['q'];
  47. }
  48. /**
  49. * Allows the backend to initialize its query object before adding the facet filters.
  50. *
  51. * @param mixed $query
  52. * The backend's native object.
  53. */
  54. public function initActiveFilters($query) {
  55. $search_id = $query->getOption('search id');
  56. $index_id = $this->info['instance'];
  57. $facets = facetapi_get_enabled_facets($this->info['name']);
  58. $this->fields = array();
  59. // We statically store the current search per facet so that we can correctly
  60. // assign it when building the facets. See the build() method in the query
  61. // type plugin classes.
  62. $active = &drupal_static('search_api_facetapi_active_facets', array());
  63. foreach ($facets as $facet) {
  64. $options = $this->getFacet($facet)->getSettings()->settings;
  65. // The 'default_true' option is a choice between "show on all but the
  66. // selected searches" (TRUE) and "show for only the selected searches".
  67. $default_true = isset($options['default_true']) ? $options['default_true'] : TRUE;
  68. // The 'facet_search_ids' option is the list of selected searches that
  69. // will either be excluded or for which the facet will exclusively be
  70. // displayed.
  71. $facet_search_ids = isset($options['facet_search_ids']) ? $options['facet_search_ids'] : array();
  72. if (array_search($search_id, $facet_search_ids) === FALSE) {
  73. $search_ids = variable_get('search_api_facets_search_ids', array());
  74. if (empty($search_ids[$index_id][$search_id])) {
  75. // Remember this search ID.
  76. $search_ids[$index_id][$search_id] = $search_id;
  77. variable_set('search_api_facets_search_ids', $search_ids);
  78. }
  79. if (!$default_true) {
  80. continue; // We are only to show facets for explicitly named search ids.
  81. }
  82. }
  83. elseif ($default_true) {
  84. continue; // The 'facet_search_ids' in the settings are to be excluded.
  85. }
  86. $active[$facet['name']] = $search_id;
  87. $this->fields[$facet['name']] = array(
  88. 'field' => $facet['field'],
  89. 'limit' => $options['hard_limit'],
  90. 'operator' => $options['operator'],
  91. 'min_count' => $options['facet_mincount'],
  92. 'missing' => $options['facet_missing'],
  93. );
  94. }
  95. }
  96. /**
  97. * Add the given facet to the query.
  98. */
  99. public function addFacet(array $facet, SearchApiQueryInterface $query) {
  100. if (isset($this->fields[$facet['name']])) {
  101. $options = &$query->getOptions();
  102. $options['search_api_facets'][$facet['name']] = $this->fields[$facet['name']];
  103. }
  104. }
  105. /**
  106. * Returns a boolean flagging whether $this->_searcher executed a search.
  107. */
  108. public function searchExecuted() {
  109. return (bool) $this->getCurrentSearch();
  110. }
  111. /**
  112. * Helper method for getting a current search for this searcher.
  113. *
  114. * @return array
  115. * The first matching current search, in the form specified by
  116. * search_api_current_search(). Or NULL, if no match was found.
  117. */
  118. public function getCurrentSearch() {
  119. if (!isset($this->current_search)) {
  120. $this->current_search = FALSE;
  121. $index_id = $this->info['instance'];
  122. // There is currently no way to configure the "current search" block to
  123. // show on a per-searcher basis as we do with the facets. Therefore we
  124. // cannot match it up to the correct "current search".
  125. // I suspect that http://drupal.org/node/593658 would help.
  126. // For now, just taking the first current search for this index. :-/
  127. foreach (search_api_current_search() as $search) {
  128. list($query, $results) = $search;
  129. if ($query->getIndex()->machine_name == $index_id) {
  130. $this->current_search = $search;
  131. }
  132. }
  133. }
  134. return $this->current_search ? $this->current_search : NULL;
  135. }
  136. /**
  137. * Returns a boolean flagging whether facets in a realm shoud be displayed.
  138. *
  139. * Useful, for example, for suppressing sidebar blocks in some cases.
  140. *
  141. * @return
  142. * A boolean flagging whether to display a given realm.
  143. */
  144. public function suppressOutput($realm_name) {
  145. // Not sure under what circumstances the output will need to be suppressed?
  146. return FALSE;
  147. }
  148. /**
  149. * Returns the search keys.
  150. */
  151. public function getSearchKeys() {
  152. $search = $this->getCurrentSearch();
  153. $keys = $search[0]->getOriginalKeys();
  154. if (is_array($keys)) {
  155. // This will happen nearly never when displaying the search keys to the
  156. // user, so go with a simple work-around.
  157. // If someone complains, we can easily add a method for printing them
  158. // properly.
  159. $keys = '[' . t('complex query') . ']';
  160. }
  161. elseif (!$keys) {
  162. // If a base path other than the current one is set, we assume that we
  163. // shouldn't report on the current search. Highly hack-y, of course.
  164. if ($search[0]->getOption('search_api_base_path', $_GET['q']) !== $_GET['q']) {
  165. return NULL;
  166. }
  167. // Work-around since Facet API won't show the "Current search" block
  168. // without keys.
  169. $keys = '[' . t('all items') . ']';
  170. }
  171. drupal_alter('search_api_facetapi_keys', $keys, $search[0]);
  172. return $keys;
  173. }
  174. /**
  175. * Returns the number of total results found for the current search.
  176. */
  177. public function getResultCount() {
  178. $search = $this->getCurrentSearch();
  179. // Each search is an array with the query as the first element and the results
  180. // array as the second.
  181. if (isset($search[1])) {
  182. return $search[1]['result count'];
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Allows for backend specific overrides to the settings form.
  188. */
  189. public function settingsForm(&$form, &$form_state) {
  190. $facet = $form['#facetapi']['facet'];
  191. $realm = $form['#facetapi']['realm'];
  192. $facet_settings = $this->getFacet($facet)->getSettings();
  193. $options = $facet_settings->settings;
  194. $search_ids = variable_get('search_api_facets_search_ids', array());
  195. $search_ids = isset($search_ids[$this->info['instance']]) ? $search_ids[$this->info['instance']] : array();
  196. if (count($search_ids) > 1) {
  197. $form['global']['default_true'] = array(
  198. '#type' => 'select',
  199. '#title' => t('Display for searches'),
  200. '#options' => array(
  201. TRUE => t('For all except the selected'),
  202. FALSE => t('Only for the selected'),
  203. ),
  204. '#default_value' => isset($options['default_true']) ? $options['default_true'] : TRUE,
  205. );
  206. $form['global']['facet_search_ids'] = array(
  207. '#type' => 'select',
  208. '#title' => t('Search IDs'),
  209. '#options' => $search_ids,
  210. '#size' => min(4, count($search_ids)),
  211. '#multiple' => TRUE,
  212. '#default_value' => isset($options['facet_search_ids']) ? $options['facet_search_ids'] : array(),
  213. );
  214. }
  215. else {
  216. $form['global']['default_true'] = array(
  217. '#type' => 'value',
  218. '#value' => TRUE,
  219. );
  220. $form['global']['facet_search_ids'] = array(
  221. '#type' => 'value',
  222. '#value' => array(),
  223. );
  224. }
  225. }
  226. }