search_api_autocomplete.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * @file
  4. * Contains page callbacks and related functions for the admin UI.
  5. */
  6. /**
  7. * Form displaying an overview over all searches available for autocompletion.
  8. *
  9. * @param SearchApiIndex $index
  10. * The index for which autocompletion searches should be configured.
  11. *
  12. * @see search_api_autocomplete_admin_overview_submit()
  13. * @see search_api_autocomplete_admin_overview_submit_delete()
  14. * @ingroup forms
  15. */
  16. function search_api_autocomplete_admin_overview(array $form, array &$form_state, SearchApiIndex $index) {
  17. $form = array();
  18. $form_state['index'] = $index;
  19. $index_id = $index->machine_name;
  20. $server = $index->server();
  21. if (!$server || !$server->supportsFeature('search_api_autocomplete')) {
  22. drupal_set_message(t("The server this index currently lies on doesn't support autocompletion. " .
  23. 'To use autocompletion, you will have to move this index to a server supporting this feature.'), 'error');
  24. if (search_api_autocomplete_search_load_multiple(FALSE, array('index_id' => $index_id))) {
  25. $form['description'] = array(
  26. '#type' => 'item',
  27. '#title' => t('Delete autocompletion settings'),
  28. '#description' => t("If you won't use autocompletion with this index anymore, you can delete all autocompletion settings associated with it. " .
  29. "This will delete all autocompletion settings on this index. Settings on other indexes won't be influenced."),
  30. );
  31. $form['button'] = array(
  32. '#type' => 'submit',
  33. '#value' => t('Delete autocompletion settings'),
  34. '#submit' => array('search_api_autocomplete_admin_overview_submit_delete'),
  35. );
  36. }
  37. return $form;
  38. }
  39. $form['#tree'] = TRUE;
  40. $types = search_api_autocomplete_get_types();
  41. $searches = search_api_autocomplete_search_load_multiple(FALSE, array('index_id' => $index_id));
  42. $show_status = FALSE;
  43. foreach ($types as $type => $info) {
  44. if (empty($info['list searches'])) {
  45. continue;
  46. }
  47. $t_searches = $info['list searches']($index);
  48. if (empty($t_searches)) {
  49. $t_searches = array();
  50. }
  51. foreach ($t_searches as $id => $search) {
  52. if (isset($searches[$id])) {
  53. $types[$type]['searches'][$id] = $searches[$id];
  54. $show_status |= $searches[$id]->hasStatus(ENTITY_IN_CODE);
  55. unset($searches[$id]);
  56. }
  57. else {
  58. $search += array(
  59. 'machine_name' => $id,
  60. 'index_id' => $index_id,
  61. 'type' => $type,
  62. 'enabled' => 0,
  63. 'options' => array(),
  64. );
  65. $search['options'] += array(
  66. 'result count' => TRUE,
  67. );
  68. $types[$type]['searches'][$id] = entity_create('search_api_autocomplete_search', $search);
  69. }
  70. }
  71. }
  72. foreach ($searches as $id => $search) {
  73. $search->unavailable = TRUE;
  74. $type = isset($types[$search->type]) ? $search->type : '';
  75. $types[$type]['searches'][$id] = $search;
  76. $show_status |= $search->hasStatus(ENTITY_IN_CODE);
  77. }
  78. $base_path = 'admin/config/search/search_api/index/' . $index_id . '/autocomplete/';
  79. foreach ($types as $type => $info) {
  80. if (empty($info['searches'])) {
  81. continue;
  82. }
  83. if (!$type) {
  84. $info += array(
  85. 'name' => t('Unavailable search types'),
  86. 'description' => t('The modules providing these searches were disabled or uninstalled. ' .
  87. "If you won't use them anymore, you can delete their settings."),
  88. );
  89. }
  90. $form[$type] = array(
  91. '#type' => 'fieldset',
  92. '#title' => $info['name'],
  93. );
  94. if (!empty($info['description'])) {
  95. $form[$type]['#description'] = '<p>' . $info['description'] . '</p>';
  96. }
  97. $form[$type]['searches']['#theme'] = 'tableselect';
  98. $form[$type]['searches']['#header'] = array();
  99. if ($show_status) {
  100. $form[$type]['searches']['#header']['status'] = t('Status');
  101. }
  102. $form[$type]['searches']['#header'] += array(
  103. 'name' => t('Name'),
  104. 'operations' => t('Operations'),
  105. );
  106. $form[$type]['searches']['#empty'] = '';
  107. $form[$type]['searches']['#js_select'] = TRUE;
  108. foreach ($info['searches'] as $id => $search) {
  109. $form[$type]['searches'][$id] = array(
  110. '#type' => 'checkbox',
  111. '#default_value' => $search->enabled,
  112. '#parents' => array('searches', $id),
  113. );
  114. if (!empty($search->unavailabe)) {
  115. $form[$type]['searches'][$id]['#default_value'] = FALSE;
  116. $form[$type]['searches'][$id]['#disabled'] = TRUE;
  117. }
  118. $form_state['searches'][$id] = $search;
  119. $options = &$form[$type]['searches']['#options'][$id];
  120. if ($show_status) {
  121. $options['status'] = isset($search->status) ? theme('entity_status', array('status' => $search->status)) : '';;
  122. }
  123. $options['name'] = $search->name;
  124. $items = array();
  125. if (empty($search->unavailabe) && !empty($search->id)) {
  126. $items[] = l(t('edit'), $base_path . $id . '/edit');
  127. }
  128. if (!empty($search->status) && ($search->hasStatus(ENTITY_CUSTOM))) {
  129. $title = $search->hasStatus(ENTITY_IN_CODE) ? t('revert') : t('delete');
  130. $items[] = l($title, $base_path . $id . '/delete');
  131. }
  132. if ($items) {
  133. $variables = array(
  134. 'items' => $items,
  135. 'attributes' => array('class' => array('inline')),
  136. );
  137. $options['operations'] = theme('item_list', $variables);
  138. }
  139. else {
  140. $options['operations'] = '';
  141. }
  142. unset($options);
  143. }
  144. }
  145. if (empty($form)) {
  146. $form['message']['#markup'] = '<p>' . t('There are currently no searches known for this index.') . '</p>';
  147. }
  148. else {
  149. $form['submit'] = array(
  150. '#type' => 'submit',
  151. '#value' => t('Save'),
  152. );
  153. }
  154. return $form;
  155. }
  156. /**
  157. * Submit callback for search_api_autocomplete_admin_overview().
  158. *
  159. * @see search_api_autocomplete_admin_overview()
  160. */
  161. function search_api_autocomplete_admin_overview_submit(array $form, array &$form_state) {
  162. foreach ($form_state['values']['searches'] as $id => $enabled) {
  163. $search = $form_state['searches'][$id];
  164. if ($search->enabled != $enabled) {
  165. $search->enabled = $enabled;
  166. $search->save();
  167. }
  168. }
  169. drupal_set_message(t('The settings have been saved.'));
  170. }
  171. /**
  172. * Submit callback for search_api_autocomplete_admin_overview(), when all
  173. * settings for the index should be deleted.
  174. *
  175. * @see search_api_autocomplete_admin_overview()
  176. */
  177. function search_api_autocomplete_admin_overview_submit_delete(array $form, array &$form_state) {
  178. $index = $form_state['index'];
  179. $ids = array_keys(search_api_autocomplete_search_load_multiple(FALSE, array('index_id' => $index->machine_name)));
  180. if ($ids) {
  181. entity_delete_multiple('search_api_autocomplete_search', $ids);
  182. drupal_set_message(t('All autocompletion settings stored for this index were deleted.'));
  183. }
  184. else {
  185. drupal_set_message(t('There were no settings to delete.'), 'warning');
  186. }
  187. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $index->machine_name;
  188. }
  189. /**
  190. * Form for editing the autocompletion settings for a search.
  191. *
  192. * @param SearchApiAutocompleteSearch $search
  193. * The search whose settings should be edited.
  194. *
  195. * @see search_api_autocomplete_admin_search_edit_validate()
  196. * @see search_api_autocomplete_admin_search_edit_submit()
  197. * @ingroup forms
  198. */
  199. function search_api_autocomplete_admin_search_edit(array $form, array &$form_state, SearchApiAutocompleteSearch $search) {
  200. drupal_set_title(t('Edit %search', array('%search' => $search->name)), PASS_THROUGH);
  201. $form_state['search'] = $search;
  202. $form_state['type'] = $type = search_api_autocomplete_get_types($search->type);
  203. if (!$type) {
  204. drupal_set_message(t('No information about the type of this search was found.'), 'error');
  205. return array();
  206. }
  207. $form['#tree'] = TRUE;
  208. $form['enabled'] = array(
  209. '#type' => 'checkbox',
  210. '#title' => t('Enabled'),
  211. '#default_value' => $search->enabled,
  212. );
  213. $form['options']['results'] = array(
  214. '#type' => 'checkbox',
  215. '#title' => t('Display result count estimates'),
  216. '#description' => t('Display the estimated number of result for each suggestion. ' .
  217. 'This option might not have an effect for some servers or types of suggestion.'),
  218. '#default_value' => !empty($search->options['results']),
  219. );
  220. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  221. if (!empty($type['config form']) && function_exists($type['config form']) && is_array($custom_form = $type['config form']($custom_form, $form_state, $search))) {
  222. $form['options']['custom'] = $custom_form;
  223. }
  224. $form['submit'] = array(
  225. '#type' => 'submit',
  226. '#value' => t('Save'),
  227. );
  228. return $form;
  229. }
  230. /**
  231. * Validate callback for search_api_autocomplete_admin_search_edit().
  232. *
  233. * @see search_api_autocomplete_admin_search_edit()
  234. * @see search_api_autocomplete_admin_search_edit_submit()
  235. */
  236. function search_api_autocomplete_admin_search_edit_validate(array $form, array &$form_state) {
  237. if (empty($form_state['type']['config form'])) {
  238. return;
  239. }
  240. $f = $form_state['type']['config form'] . '_validate';
  241. if (function_exists($f)) {
  242. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  243. $f($form['options']['custom'], $form_state, $form_state['values']['options']['custom']);
  244. }
  245. }
  246. /**
  247. * Submit callback for search_api_autocomplete_admin_search_edit().
  248. *
  249. * @see search_api_autocomplete_admin_search_edit()
  250. * @see search_api_autocomplete_admin_search_edit_validate()
  251. */
  252. function search_api_autocomplete_admin_search_edit_submit(array $form, array &$form_state) {
  253. $values = &$form_state['values'];
  254. if (!empty($form_state['type']['config form'])) {
  255. $f = $form_state['type']['config form'] . '_submit';
  256. if (function_exists($f)) {
  257. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  258. $f($form['options']['custom'], $form_state, $values['options']['custom']);
  259. }
  260. }
  261. $search = $form_state['search'];
  262. $search->enabled = $values['enabled'];
  263. $search->options['results'] = $values['options']['results'];
  264. if (isset($values['options']['custom'])) {
  265. // Take care of custom options that aren't changed in the config form.
  266. $old = empty($search->options['custom']) ? array() : $search->options['custom'];
  267. $search->options['custom'] = $values['options']['custom'] + $old;
  268. }
  269. $search->save();
  270. drupal_set_message(t('The autocompletion settings for the search have been saved.'));
  271. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $search->index_id . '/autocomplete';
  272. }
  273. /**
  274. * Form for deleting the autocompletion settings of a search.
  275. *
  276. * @param SearchApiAutocompleteSearch $search
  277. * The search whose settings should be deleted.
  278. *
  279. * @see search_api_autocomplete_admin_search_delete_submit()
  280. * @ingroup forms
  281. */
  282. function search_api_autocomplete_admin_search_delete(array $form, array &$form_state, SearchApiAutocompleteSearch $search) {
  283. $form_state['search'] = $search;
  284. return confirm_form(
  285. $form,
  286. t('Do you really want to delete the autocompletion settings for search %search?', array('%search' => $search->name)),
  287. 'admin/config/search/search_api/index/' . $search->index_id . '/autocomplete'
  288. );
  289. }
  290. /**
  291. * Submit callback for search_api_autocomplete_admin_search_delete().
  292. *
  293. * @see search_api_autocomplete_admin_search_delete()
  294. */
  295. function search_api_autocomplete_admin_search_delete_submit(array $form, array &$form_state) {
  296. $form_state['search']->delete();
  297. drupal_set_message(t('The autocompletion settings for the search have been deleted.'));
  298. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $form_state['search']->index_id . '/autocomplete';
  299. }