search_api_autocomplete.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 'config form' => 'search_api_autocomplete_views_config_form',
  32. );
  33. }
  34. return $types;
  35. }
  36. /**
  37. * Implements hook_menu().
  38. */
  39. function search_api_autocomplete_menu() {
  40. // Autocompletion path
  41. $items['search_api_autocomplete/%search_api_autocomplete_search'] = array(
  42. 'title' => 'Search API autocomplete',
  43. 'page callback' => 'search_api_autocomplete_autocomplete',
  44. 'page arguments' => array(1),
  45. 'access callback' => 'search_api_autocomplete_access',
  46. 'access arguments' => array(1),
  47. 'type' => MENU_CALLBACK,
  48. 'file' => 'search_api_autocomplete.pages.inc',
  49. );
  50. // Admin UI
  51. $items['admin/config/search/search_api/index/%search_api_index/autocomplete'] = array(
  52. 'title' => 'Autocomplete',
  53. 'description' => 'Add autocompletion to searches for this index.',
  54. 'page callback' => 'drupal_get_form',
  55. 'page arguments' => array('search_api_autocomplete_admin_overview', 5),
  56. 'access arguments' => array('administer search_api'),
  57. 'weight' => -1,
  58. 'type' => MENU_LOCAL_TASK,
  59. 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
  60. 'file' => 'search_api_autocomplete.admin.inc',
  61. );
  62. $items['admin/config/search/search_api/index/%/autocomplete/%search_api_autocomplete_search/edit'] = array(
  63. 'title' => 'Edit autocompletion settings',
  64. 'description' => 'Edit the autocompletion settings of a search.',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('search_api_autocomplete_admin_search_edit', 7),
  67. 'access arguments' => array('administer search_api'),
  68. 'file' => 'search_api_autocomplete.admin.inc',
  69. );
  70. $items['admin/config/search/search_api/index/%/autocomplete/%search_api_autocomplete_search/delete'] = array(
  71. 'title' => 'Delete autocompletion settings',
  72. 'description' => 'Delete the autocompletion settings of a search.',
  73. 'page callback' => 'drupal_get_form',
  74. 'page arguments' => array('search_api_autocomplete_admin_search_delete', 7),
  75. 'access arguments' => array('administer search_api'),
  76. 'file' => 'search_api_autocomplete.admin.inc',
  77. );
  78. return $items;
  79. }
  80. /**
  81. * Implements hook_theme().
  82. */
  83. function search_api_autocomplete_theme() {
  84. $themes['search_api_autocomplete_suggestion'] = array(
  85. 'variables' => array(
  86. 'prefix' => NULL,
  87. 'suggestion_prefix' => '',
  88. 'user_input' => '',
  89. 'suggestion_suffix' => '',
  90. 'results' => NULL,
  91. ),
  92. 'file' => 'search_api_autocomplete.pages.inc',
  93. );
  94. return $themes;
  95. }
  96. /**
  97. * Implements hook_entity_info().
  98. */
  99. function search_api_autocomplete_entity_info() {
  100. $info['search_api_autocomplete_search'] = array(
  101. 'label' => t('Autocomplete search'),
  102. 'controller class' => 'EntityAPIControllerExportable',
  103. 'entity class' => 'SearchApiAutocompleteSearch',
  104. 'base table' => 'search_api_autocomplete_search',
  105. 'uri callback' => 'search_api_autocomplete_search_url',
  106. 'module' => 'search_api_autocomplete',
  107. 'exportable' => TRUE,
  108. 'entity keys' => array(
  109. 'id' => 'id',
  110. 'name' => 'machine_name',
  111. 'label' => 'name',
  112. ),
  113. );
  114. return $info;
  115. }
  116. /**
  117. * Implements hook_permission().
  118. */
  119. function search_api_autocomplete_permission() {
  120. $perms = array();
  121. foreach (search_api_autocomplete_search_load_multiple() as $id => $search) {
  122. $perms['use search_api_autocomplete for ' . $id] = array(
  123. 'title' => t('Use autocomplete for the %search search', array('%search' => $search->name)),
  124. );
  125. }
  126. return $perms;
  127. }
  128. /**
  129. * Access callback for search autocompletion.
  130. *
  131. * @param $search
  132. * The autocomplete search which is being accessed.
  133. * @param $account
  134. * (optional) The account to check, if not given use currently logged in user.
  135. *
  136. * @return
  137. * TRUE, if the search is enabled, supports autocompletion and the user has
  138. * the necessary permission. FALSE otherwise.
  139. */
  140. function search_api_autocomplete_access(SearchApiAutocompleteSearch $search, $account = NULL) {
  141. return $search->enabled && user_access('use search_api_autocomplete for ' . $search->machine_name, $account) && $search->supportsAutocompletion();
  142. }
  143. /**
  144. * Implements hook_search_api_index_delete().
  145. */
  146. function search_api_autocomplete_search_api_index_delete(SearchApiIndex $index) {
  147. if (!$index->hasStatus(ENTITY_IN_CODE)) {
  148. $ids = db_query('SELECT id FROM {search_api_autocomplete_search} WHERE index_id = :id',
  149. array(':id' => $index->machine_name))->fetchCol();
  150. if ($ids) {
  151. entity_delete_multiple('search_api_autocomplete_search', $ids);
  152. }
  153. }
  154. }
  155. /**
  156. * Get information about all search types, or a specific one.
  157. *
  158. * @param $type
  159. * (optional) The name of a type.
  160. *
  161. * @return
  162. * If $type was not given, an array containing information about all search
  163. * types. Otherwise, either information about the specified type, or NULL if
  164. * the type is not known.
  165. *
  166. * @see hook_search_api_autocomplete_types()
  167. */
  168. function search_api_autocomplete_get_types($type = NULL) {
  169. $types = &drupal_static(__FUNCTION__);
  170. if (!isset($types)) {
  171. $types = module_invoke_all('search_api_autocomplete_types');
  172. }
  173. if (isset($type)) {
  174. return isset($types[$type]) ? $types[$type] : NULL;
  175. }
  176. return $types;
  177. }
  178. /**
  179. * Loads an autocomplete search entity.
  180. *
  181. * @param $id
  182. * Either the ID or machine name of an autocomplete search.
  183. * @param $reset
  184. * Whether to reset the internal cache.
  185. *
  186. * @return SearchApiAutocompleteSearch
  187. * The specified autocomplete search entity, or FALSE if it doesn't exist.
  188. */
  189. function search_api_autocomplete_search_load($id, $reset = FALSE) {
  190. $ret = search_api_autocomplete_search_load_multiple(array($id), array(), $reset);
  191. return $ret ? reset($ret) : FALSE;
  192. }
  193. /**
  194. * Loads autocomplete search entities.
  195. *
  196. * @param $ids
  197. * An array of IDs or machine names, or FALSE to load all searches.
  198. * @param $conditions
  199. * An associative array of conditions on the {search_api_autocomplete_search}
  200. * table.
  201. * @param $reset
  202. * Whether to reset the internal cache.
  203. *
  204. * @return array
  205. * An array of all autocomplete search entities that meet the criteria.
  206. *
  207. * @see entity_load()
  208. */
  209. function search_api_autocomplete_search_load_multiple($ids = FALSE, array $conditions = array(), $reset = FALSE) {
  210. return entity_load_multiple_by_name('search_api_autocomplete_search', $ids, $conditions, $reset);
  211. }