adapter.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. $facet_info = $this->fields[$facet['name']];
  103. if (!empty($facet['query_options'])) {
  104. // Let facet-specific query options override the set options.
  105. $facet_info = $facet['query_options'] + $facet_info;
  106. }
  107. $options['search_api_facets'][$facet['name']] = $facet_info;
  108. }
  109. }
  110. /**
  111. * Returns a boolean flagging whether $this->_searcher executed a search.
  112. */
  113. public function searchExecuted() {
  114. return (bool) $this->getCurrentSearch();
  115. }
  116. /**
  117. * Helper method for getting a current search for this searcher.
  118. *
  119. * @return array
  120. * The first matching current search, in the form specified by
  121. * search_api_current_search(). Or NULL, if no match was found.
  122. */
  123. public function getCurrentSearch() {
  124. // Even if this fails once, there might be a search query later in the page
  125. // request. We therefore don't store anything in $this->current_search in
  126. // case of failure, but just try again if the method is called again.
  127. if (!isset($this->current_search)) {
  128. $index_id = $this->info['instance'];
  129. // There is currently no way to configure the "current search" block to
  130. // show on a per-searcher basis as we do with the facets. Therefore we
  131. // cannot match it up to the correct "current search".
  132. // I suspect that http://drupal.org/node/593658 would help.
  133. // For now, just taking the first current search for this index. :-/
  134. foreach (search_api_current_search() as $search) {
  135. list($query) = $search;
  136. if ($query->getIndex()->machine_name == $index_id) {
  137. $this->current_search = $search;
  138. }
  139. }
  140. }
  141. return $this->current_search;
  142. }
  143. /**
  144. * Returns a boolean flagging whether facets in a realm shoud be displayed.
  145. *
  146. * Useful, for example, for suppressing sidebar blocks in some cases.
  147. *
  148. * @return
  149. * A boolean flagging whether to display a given realm.
  150. */
  151. public function suppressOutput($realm_name) {
  152. // Not sure under what circumstances the output will need to be suppressed?
  153. return FALSE;
  154. }
  155. /**
  156. * Returns the search keys.
  157. */
  158. public function getSearchKeys() {
  159. $search = $this->getCurrentSearch();
  160. $keys = $search[0]->getOriginalKeys();
  161. if (is_array($keys)) {
  162. // This will happen nearly never when displaying the search keys to the
  163. // user, so go with a simple work-around.
  164. // If someone complains, we can easily add a method for printing them
  165. // properly.
  166. $keys = '[' . t('complex query') . ']';
  167. }
  168. drupal_alter('search_api_facetapi_keys', $keys, $search[0]);
  169. return $keys;
  170. }
  171. /**
  172. * Returns the number of total results found for the current search.
  173. */
  174. public function getResultCount() {
  175. $search = $this->getCurrentSearch();
  176. // Each search is an array with the query as the first element and the results
  177. // array as the second.
  178. if (isset($search[1])) {
  179. return $search[1]['result count'];
  180. }
  181. return 0;
  182. }
  183. /**
  184. * Allows for backend specific overrides to the settings form.
  185. */
  186. public function settingsForm(&$form, &$form_state) {
  187. $facet = $form['#facetapi']['facet'];
  188. $facet_settings = $this->getFacet($facet)->getSettings();
  189. $options = $facet_settings->settings;
  190. $search_ids = variable_get('search_api_facets_search_ids', array());
  191. $search_ids = isset($search_ids[$this->info['instance']]) ? $search_ids[$this->info['instance']] : array();
  192. if (count($search_ids) > 1) {
  193. $form['global']['default_true'] = array(
  194. '#type' => 'select',
  195. '#title' => t('Display for searches'),
  196. '#prefix' => '<div class="facetapi-global-setting">',
  197. '#options' => array(
  198. TRUE => t('For all except the selected'),
  199. FALSE => t('Only for the selected'),
  200. ),
  201. '#default_value' => isset($options['default_true']) ? $options['default_true'] : TRUE,
  202. );
  203. $form['global']['facet_search_ids'] = array(
  204. '#type' => 'select',
  205. '#title' => t('Search IDs'),
  206. '#suffix' => '</div>',
  207. '#options' => $search_ids,
  208. '#size' => min(4, count($search_ids)),
  209. '#multiple' => TRUE,
  210. '#default_value' => isset($options['facet_search_ids']) ? $options['facet_search_ids'] : array(),
  211. );
  212. }
  213. else {
  214. $form['global']['default_true'] = array(
  215. '#type' => 'value',
  216. '#value' => TRUE,
  217. );
  218. $form['global']['facet_search_ids'] = array(
  219. '#type' => 'value',
  220. '#value' => array(),
  221. );
  222. }
  223. // Add a granularity option to date query types.
  224. if (isset($facet['query type']) && $facet['query type'] == 'date') {
  225. $granularity_options = array(
  226. FACETAPI_DATE_YEAR => t('Years'),
  227. FACETAPI_DATE_MONTH => t('Months'),
  228. FACETAPI_DATE_DAY => t('Days'),
  229. FACETAPI_DATE_HOUR => t('Hours'),
  230. FACETAPI_DATE_MINUTE => t('Minutes'),
  231. FACETAPI_DATE_SECOND => t('Seconds'),
  232. );
  233. $form['global']['date_granularity'] = array(
  234. '#type' => 'select',
  235. '#title' => t('Granularity'),
  236. '#description' => t('Determine the maximum drill-down level'),
  237. '#prefix' => '<div class="facetapi-global-setting">',
  238. '#suffix' => '</div>',
  239. '#options' => $granularity_options,
  240. '#default_value' => isset($options['date_granularity']) ? $options['date_granularity'] : FACETAPI_DATE_MINUTE,
  241. );
  242. }
  243. // Add an "Exclude" option for terms.
  244. if(!empty($facet['query types']) && in_array('term', $facet['query types'])) {
  245. $form['global']['operator']['#weight'] = -2;
  246. unset($form['global']['operator']['#suffix']);
  247. $form['global']['exclude'] = array(
  248. '#type' => 'checkbox',
  249. '#title' => t('Exclude'),
  250. '#description' => t('Make the search exclude selected facets, instead of restricting it to them.'),
  251. '#suffix' => '</div>',
  252. '#weight' => -1,
  253. '#default_value' => !empty($options['exclude']),
  254. );
  255. }
  256. }
  257. }