adapter.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // Only act on queries from the right index.
  58. if ($index_id != $query->getIndex()->machine_name) {
  59. return;
  60. }
  61. $facets = facetapi_get_enabled_facets($this->info['name']);
  62. $this->fields = array();
  63. // We statically store the current search per facet so that we can correctly
  64. // assign it when building the facets. See the build() method in the query
  65. // type plugin classes.
  66. $active = &drupal_static('search_api_facetapi_active_facets', array());
  67. foreach ($facets as $facet) {
  68. $options = $this->getFacet($facet)->getSettings()->settings;
  69. // The 'default_true' option is a choice between "show on all but the
  70. // selected searches" (TRUE) and "show for only the selected searches".
  71. $default_true = isset($options['default_true']) ? $options['default_true'] : TRUE;
  72. // The 'facet_search_ids' option is the list of selected searches that
  73. // will either be excluded or for which the facet will exclusively be
  74. // displayed.
  75. $facet_search_ids = isset($options['facet_search_ids']) ? $options['facet_search_ids'] : array();
  76. // Remember this search ID, if necessary.
  77. $this->rememberSearchId($index_id, $search_id);
  78. if (array_search($search_id, $facet_search_ids) === FALSE) {
  79. if (!$default_true) {
  80. // We are only to show facets for explicitly named search ids.
  81. continue;
  82. }
  83. }
  84. elseif ($default_true) {
  85. // The 'facet_search_ids' in the settings are to be excluded.
  86. continue;
  87. }
  88. $facet_key = $facet['name'] . '@' . $this->getSearcher();
  89. $active[$facet_key] = $search_id;
  90. $this->fields[$facet['name']] = array(
  91. 'field' => $facet['field'],
  92. 'limit' => $options['hard_limit'],
  93. 'operator' => $options['operator'],
  94. 'min_count' => $options['facet_mincount'],
  95. 'missing' => $options['facet_missing'],
  96. );
  97. }
  98. }
  99. /**
  100. * Adds a search ID to the list of known searches for an index.
  101. *
  102. * @param string $index_id
  103. * The machine name of the search index.
  104. * @param string $search_id
  105. * The identifier of the executed search.
  106. */
  107. protected function rememberSearchId($index_id, $search_id) {
  108. $search_ids = variable_get('search_api_facets_search_ids', array());
  109. if (empty($search_ids[$index_id][$search_id])) {
  110. $search_ids[$index_id][$search_id] = $search_id;
  111. asort($search_ids[$index_id]);
  112. variable_set('search_api_facets_search_ids', $search_ids);
  113. }
  114. }
  115. /**
  116. * Add the given facet to the query.
  117. */
  118. public function addFacet(array $facet, SearchApiQueryInterface $query) {
  119. if (isset($this->fields[$facet['name']])) {
  120. $options = &$query->getOptions();
  121. $facet_info = $this->fields[$facet['name']];
  122. if (!empty($facet['query_options'])) {
  123. // Let facet-specific query options override the set options.
  124. $facet_info = $facet['query_options'] + $facet_info;
  125. }
  126. $options['search_api_facets'][$facet['name']] = $facet_info;
  127. }
  128. }
  129. /**
  130. * Returns a boolean flagging whether $this->_searcher executed a search.
  131. */
  132. public function searchExecuted() {
  133. return (bool) $this->getCurrentSearch();
  134. }
  135. /**
  136. * Helper method for getting a current search for this searcher.
  137. *
  138. * @return array
  139. * The first matching current search, in the form specified by
  140. * search_api_current_search(). Or NULL, if no match was found.
  141. */
  142. public function getCurrentSearch() {
  143. // Even if this fails once, there might be a search query later in the page
  144. // request. We therefore don't store anything in $this->current_search in
  145. // case of failure, but just try again if the method is called again.
  146. if (!isset($this->current_search)) {
  147. $index_id = $this->info['instance'];
  148. // There is currently no way to configure the "current search" block to
  149. // show on a per-searcher basis as we do with the facets. Therefore we
  150. // cannot match it up to the correct "current search".
  151. // I suspect that http://drupal.org/node/593658 would help.
  152. // For now, just taking the first current search for this index. :-/
  153. foreach (search_api_current_search() as $search) {
  154. list($query) = $search;
  155. if ($query->getIndex()->machine_name == $index_id) {
  156. $this->current_search = $search;
  157. }
  158. }
  159. }
  160. return $this->current_search;
  161. }
  162. /**
  163. * Returns a boolean flagging whether facets in a realm shoud be displayed.
  164. *
  165. * Useful, for example, for suppressing sidebar blocks in some cases.
  166. *
  167. * @return
  168. * A boolean flagging whether to display a given realm.
  169. */
  170. public function suppressOutput($realm_name) {
  171. // Not sure under what circumstances the output will need to be suppressed?
  172. return FALSE;
  173. }
  174. /**
  175. * Returns the search keys.
  176. */
  177. public function getSearchKeys() {
  178. $search = $this->getCurrentSearch();
  179. // If the search is empty then there's no reason to continue.
  180. if (!$search) {
  181. return NULL;
  182. }
  183. $keys = $search[0]->getOriginalKeys();
  184. if (is_array($keys)) {
  185. // This will happen nearly never when displaying the search keys to the
  186. // user, so go with a simple work-around.
  187. // If someone complains, we can easily add a method for printing them
  188. // properly.
  189. $keys = '[' . t('complex query') . ']';
  190. }
  191. drupal_alter('search_api_facetapi_keys', $keys, $search[0]);
  192. return $keys;
  193. }
  194. /**
  195. * Returns the number of total results found for the current search.
  196. */
  197. public function getResultCount() {
  198. $search = $this->getCurrentSearch();
  199. // Each search is an array with the query as the first element and the results
  200. // array as the second.
  201. if (isset($search[1])) {
  202. return $search[1]['result count'];
  203. }
  204. return 0;
  205. }
  206. /**
  207. * Allows for backend specific overrides to the settings form.
  208. */
  209. public function settingsForm(&$form, &$form_state) {
  210. $facet = $form['#facetapi']['facet'];
  211. $facet_settings = $this->getFacet($facet)->getSettings();
  212. $options = $facet_settings->settings;
  213. $search_ids = variable_get('search_api_facets_search_ids', array());
  214. $search_ids = isset($search_ids[$this->info['instance']]) ? $search_ids[$this->info['instance']] : array();
  215. if (count($search_ids) > 1) {
  216. $form['global']['default_true'] = array(
  217. '#type' => 'select',
  218. '#title' => t('Display for searches'),
  219. '#prefix' => '<div class="facetapi-global-setting">',
  220. '#options' => array(
  221. TRUE => t('For all except the selected'),
  222. FALSE => t('Only for the selected'),
  223. ),
  224. '#default_value' => isset($options['default_true']) ? $options['default_true'] : TRUE,
  225. );
  226. $form['global']['facet_search_ids'] = array(
  227. '#type' => 'select',
  228. '#title' => t('Search IDs'),
  229. '#suffix' => '</div>',
  230. '#options' => $search_ids,
  231. '#size' => min(4, count($search_ids)),
  232. '#multiple' => TRUE,
  233. '#default_value' => isset($options['facet_search_ids']) ? $options['facet_search_ids'] : array(),
  234. );
  235. }
  236. else {
  237. $form['global']['default_true'] = array(
  238. '#type' => 'value',
  239. '#value' => TRUE,
  240. );
  241. $form['global']['facet_search_ids'] = array(
  242. '#type' => 'value',
  243. '#value' => array(),
  244. );
  245. }
  246. // Add a granularity option to date query types.
  247. if (isset($facet['query type']) && $facet['query type'] == 'date') {
  248. $granularity_options = array(
  249. FACETAPI_DATE_YEAR => t('Years'),
  250. FACETAPI_DATE_MONTH => t('Months'),
  251. FACETAPI_DATE_DAY => t('Days'),
  252. FACETAPI_DATE_HOUR => t('Hours'),
  253. FACETAPI_DATE_MINUTE => t('Minutes'),
  254. FACETAPI_DATE_SECOND => t('Seconds'),
  255. );
  256. $form['global']['date_granularity'] = array(
  257. '#type' => 'select',
  258. '#title' => t('Granularity'),
  259. '#description' => t('Determine the maximum drill-down level'),
  260. '#prefix' => '<div class="facetapi-global-setting">',
  261. '#suffix' => '</div>',
  262. '#options' => $granularity_options,
  263. '#default_value' => isset($options['date_granularity']) ? $options['date_granularity'] : FACETAPI_DATE_MINUTE,
  264. );
  265. // Date facets don't support the "OR" operator (for now).
  266. $form['global']['operator']['#access'] = FALSE;
  267. $default_value = FACETAPI_DATE_YEAR;
  268. if (isset($options['date_granularity_min'])) {
  269. $default_value = $options['date_granularity_min'];
  270. }
  271. $form['global']['date_granularity_min'] = array(
  272. '#type' => 'select',
  273. '#title' => t('Minimum granularity'),
  274. '#description' => t('Determine the minimum drill-down level to start at'),
  275. '#prefix' => '<div class="facetapi-global-setting">',
  276. '#suffix' => '</div>',
  277. '#options' => $granularity_options,
  278. '#default_value' => $default_value,
  279. );
  280. }
  281. // Add an "Exclude" option for terms.
  282. if (!empty($facet['query types']) && in_array('term', $facet['query types'])) {
  283. $form['global']['operator']['#weight'] = -2;
  284. unset($form['global']['operator']['#suffix']);
  285. $form['global']['exclude'] = array(
  286. '#type' => 'checkbox',
  287. '#title' => t('Exclude'),
  288. '#description' => t('Make the search exclude selected facets, instead of restricting it to them.'),
  289. '#suffix' => '</div>',
  290. '#weight' => -1,
  291. '#default_value' => !empty($options['exclude']),
  292. );
  293. }
  294. }
  295. }