search_api_autocomplete.module 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Adds autocomplete capabilities for Search API searches.
  5. */
  6. // Include the files with the module-specific implementations.
  7. require_once('search_api_autocomplete.search_api_page.inc');
  8. require_once('search_api_autocomplete.search_api_views.inc');
  9. /**
  10. * Implements hook_search_api_autocomplete_types().
  11. *
  12. * Adds search types for search pages and views. The actual implementations lie
  13. * in the above include files.
  14. */
  15. function search_api_autocomplete_search_api_autocomplete_types() {
  16. $types = array();
  17. if (module_exists('search_api_page')) {
  18. $types['search_api_page'] = array(
  19. 'name' => t('Search pages'),
  20. 'description' => t('Searches provided by the <em>Search pages</em> module.'),
  21. 'list searches' => 'search_api_autocomplete_pages_searches',
  22. 'create query' => 'search_api_autocomplete_pages_query',
  23. );
  24. }
  25. if (module_exists('search_api_views')) {
  26. $types['search_api_views'] = array(
  27. 'name' => t('Search views'),
  28. 'description' => t('Searches provided by the <em>Search views</em> module.'),
  29. 'list searches' => 'search_api_autocomplete_views_searches',
  30. 'create query' => 'search_api_autocomplete_views_query',
  31. );
  32. }
  33. return $types;
  34. }
  35. /**
  36. * Implements hook_menu().
  37. */
  38. function search_api_autocomplete_menu() {
  39. // Autocompletion path
  40. $items['search_api_autocomplete/%search_api_autocomplete_search'] = array(
  41. 'title' => 'Search API autocomplete',
  42. 'page callback' => 'search_api_autocomplete_autocomplete',
  43. 'page arguments' => array(1),
  44. 'access arguments' => array('use search_api_autocomplete'),
  45. 'type' => MENU_CALLBACK,
  46. 'file' => 'search_api_autocomplete.pages.inc',
  47. );
  48. // Admin UI
  49. $items['admin/config/search/search_api/index/%search_api_index/autocomplete'] = array(
  50. 'title' => 'Autocomplete',
  51. 'description' => 'Add autocompletion to searches for this index.',
  52. 'page callback' => 'drupal_get_form',
  53. 'page arguments' => array('search_api_autocomplete_admin_overview', 5),
  54. 'access arguments' => array('administer search_api'),
  55. 'weight' => -1,
  56. 'type' => MENU_LOCAL_TASK,
  57. 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
  58. 'file' => 'search_api_autocomplete.admin.inc',
  59. );
  60. $items['admin/config/search/search_api/index/%/autocomplete/%search_api_autocomplete_search/edit'] = array(
  61. 'title' => 'Edit autocompletion settings',
  62. 'description' => 'Edit the autocompletion settings of a search.',
  63. 'page callback' => 'drupal_get_form',
  64. 'page arguments' => array('search_api_autocomplete_admin_search_edit', 7),
  65. 'access arguments' => array('administer search_api'),
  66. 'file' => 'search_api_autocomplete.admin.inc',
  67. );
  68. $items['admin/config/search/search_api/index/%/autocomplete/%search_api_autocomplete_search/delete'] = array(
  69. 'title' => 'Delete autocompletion settings',
  70. 'description' => 'Delete the autocompletion settings of a search.',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array('search_api_autocomplete_admin_search_delete', 7),
  73. 'access arguments' => array('administer search_api'),
  74. 'file' => 'search_api_autocomplete.admin.inc',
  75. );
  76. return $items;
  77. }
  78. /**
  79. * Implements hook_theme().
  80. */
  81. function search_api_autocomplete_theme() {
  82. $themes['search_api_autocomplete_suggestion'] = array(
  83. 'variables' => array(
  84. 'prefix' => NULL,
  85. 'suggestion_prefix' => '',
  86. 'user_input' => '',
  87. 'suggestion_suffix' => '',
  88. 'results' => NULL,
  89. ),
  90. 'file' => 'search_api_autocomplete.pages.inc',
  91. );
  92. return $themes;
  93. }
  94. /**
  95. * Implements hook_entity_info().
  96. */
  97. function search_api_autocomplete_entity_info() {
  98. $info['search_api_autocomplete_search'] = array(
  99. 'label' => t('Autocomplete search'),
  100. 'controller class' => 'EntityAPIControllerExportable',
  101. 'entity class' => 'SearchApiAutocompleteSearch',
  102. 'base table' => 'search_api_autocomplete_search',
  103. 'uri callback' => 'search_api_autocomplete_search_url',
  104. 'module' => 'search_api_autocomplete',
  105. 'exportable' => TRUE,
  106. 'entity keys' => array(
  107. 'id' => 'id',
  108. 'name' => 'machine_name',
  109. 'label' => 'name',
  110. ),
  111. );
  112. return $info;
  113. }
  114. /**
  115. * Implements hook_permission().
  116. */
  117. function search_api_autocomplete_permission() {
  118. return array(
  119. 'use search_api_autocomplete' => array(
  120. 'title' => t('Use search autocomplete'),
  121. ),
  122. );
  123. }
  124. /**
  125. * Implements hook_search_api_index_delete().
  126. */
  127. function search_api_autocomplete_search_api_index_delete(SearchApiIndex $index) {
  128. if (!$index->hasStatus(ENTITY_IN_CODE)) {
  129. $ids = db_query('SELECT id FROM {search_api_autocomplete_search} WHERE index_id = :id',
  130. array(':id' => $index->machine_name))->fetchCol();
  131. if ($ids) {
  132. entity_delete_multiple('search_api_autocomplete_search', $ids);
  133. }
  134. }
  135. }
  136. /**
  137. * Get information about all search types, or a specific one.
  138. *
  139. * @param $type
  140. * (optional) The name of a type.
  141. *
  142. * @return
  143. * If $type was not given, an array containing information about all search
  144. * types. Otherwise, either information about the specified type, or NULL if
  145. * the type is not known.
  146. *
  147. * @see hook_search_api_autocomplete_types()
  148. */
  149. function search_api_autocomplete_get_types($type = NULL) {
  150. $types = &drupal_static(__FUNCTION__);
  151. if (!isset($types)) {
  152. $types = module_invoke_all('search_api_autocomplete_types');
  153. }
  154. if (isset($type)) {
  155. return isset($types[$type]) ? $types[$type] : NULL;
  156. }
  157. return $types;
  158. }
  159. /**
  160. * Loads an autocomplete search entity.
  161. *
  162. * @param $id
  163. * Either the ID or machine name of an autocomplete search.
  164. * @param $reset
  165. * Whether to reset the internal cache.
  166. *
  167. * @return SearchApiAutocompleteSearch
  168. * The specified autocomplete search entity, or FALSE if it doesn't exist.
  169. */
  170. function search_api_autocomplete_search_load($id, $reset = FALSE) {
  171. $ret = search_api_autocomplete_search_load_multiple(array($id), array(), $reset);
  172. return $ret ? reset($ret) : FALSE;
  173. }
  174. /**
  175. * Loads autocomplete search entities.
  176. *
  177. * @param $ids
  178. * An array of IDs or machine names, or FALSE to load all searches.
  179. * @param $conditions
  180. * An associative array of conditions on the {search_api_autocomplete_search}
  181. * table.
  182. * @param $reset
  183. * Whether to reset the internal cache.
  184. *
  185. * @return array
  186. * An array of all autocomplete search entities that meet the criteria.
  187. *
  188. * @see entity_load()
  189. */
  190. function search_api_autocomplete_search_load_multiple($ids = FALSE, array $conditions = array(), $reset = FALSE) {
  191. return entity_load_multiple_by_name('search_api_autocomplete_search', $ids, $conditions, $reset);
  192. }