search_api_autocomplete.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. 'fields' => array(),
  68. );
  69. $types[$type]['searches'][$id] = entity_create('search_api_autocomplete_search', $search);
  70. }
  71. }
  72. }
  73. foreach ($searches as $id => $search) {
  74. $search->unavailable = TRUE;
  75. $type = isset($types[$search->type]) ? $search->type : '';
  76. $types[$type]['searches'][$id] = $search;
  77. $show_status |= $search->hasStatus(ENTITY_IN_CODE);
  78. }
  79. $base_path = 'admin/config/search/search_api/index/' . $index_id . '/autocomplete/';
  80. foreach ($types as $type => $info) {
  81. if (empty($info['searches'])) {
  82. continue;
  83. }
  84. if (!$type) {
  85. $info += array(
  86. 'name' => t('Unavailable search types'),
  87. 'description' => t('The modules providing these searches were disabled or uninstalled. ' .
  88. "If you won't use them anymore, you can delete their settings."),
  89. );
  90. }
  91. $form[$type] = array(
  92. '#type' => 'fieldset',
  93. '#title' => $info['name'],
  94. );
  95. if (!empty($info['description'])) {
  96. $form[$type]['#description'] = '<p>' . $info['description'] . '</p>';
  97. }
  98. $form[$type]['searches']['#theme'] = 'tableselect';
  99. $form[$type]['searches']['#header'] = array();
  100. if ($show_status) {
  101. $form[$type]['searches']['#header']['status'] = t('Status');
  102. }
  103. $form[$type]['searches']['#header'] += array(
  104. 'name' => t('Name'),
  105. 'operations' => t('Operations'),
  106. );
  107. $form[$type]['searches']['#empty'] = '';
  108. $form[$type]['searches']['#js_select'] = TRUE;
  109. foreach ($info['searches'] as $id => $search) {
  110. $form[$type]['searches'][$id] = array(
  111. '#type' => 'checkbox',
  112. '#default_value' => $search->enabled,
  113. '#parents' => array('searches', $id),
  114. );
  115. if (!empty($search->unavailabe)) {
  116. $form[$type]['searches'][$id]['#default_value'] = FALSE;
  117. $form[$type]['searches'][$id]['#disabled'] = TRUE;
  118. }
  119. $form_state['searches'][$id] = $search;
  120. $options = &$form[$type]['searches']['#options'][$id];
  121. if ($show_status) {
  122. $options['status'] = isset($search->status) ? theme('entity_status', array('status' => $search->status)) : '';;
  123. }
  124. $options['name'] = $search->name;
  125. $items = array();
  126. if (empty($search->unavailabe) && !empty($search->id)) {
  127. $items[] = l(t('edit'), $base_path . $id . '/edit');
  128. }
  129. if (!empty($search->status) && ($search->hasStatus(ENTITY_CUSTOM))) {
  130. $title = $search->hasStatus(ENTITY_IN_CODE) ? t('revert') : t('delete');
  131. $items[] = l($title, $base_path . $id . '/delete');
  132. }
  133. if ($items) {
  134. $variables = array(
  135. 'items' => $items,
  136. 'attributes' => array('class' => array('inline')),
  137. );
  138. $options['operations'] = theme('item_list', $variables);
  139. }
  140. else {
  141. $options['operations'] = '';
  142. }
  143. unset($options);
  144. }
  145. }
  146. if (empty($form)) {
  147. $form['message']['#markup'] = '<p>' . t('There are currently no searches known for this index.') . '</p>';
  148. }
  149. else {
  150. $form['submit'] = array(
  151. '#type' => 'submit',
  152. '#value' => t('Save'),
  153. );
  154. }
  155. return $form;
  156. }
  157. /**
  158. * Submit callback for search_api_autocomplete_admin_overview().
  159. *
  160. * @see search_api_autocomplete_admin_overview()
  161. */
  162. function search_api_autocomplete_admin_overview_submit(array $form, array &$form_state) {
  163. $msg = t('The settings have been saved.');
  164. foreach ($form_state['values']['searches'] as $id => $enabled) {
  165. $search = $form_state['searches'][$id];
  166. if ($search->enabled != $enabled) {
  167. $change = TRUE;
  168. if (!empty($search->is_new)) {
  169. $options['query']['destination'] = $_GET['q'];
  170. $options['fragment'] = 'module-search_api_autocomplete';
  171. $vars['@perm_url'] = url('admin/people/permissions', $options);
  172. $msg = t('The settings have been saved. Please remember to set the <a href="@perm_url">permissions</a> for the newly enabled searches.', $vars);
  173. }
  174. $search->enabled = $enabled;
  175. $search->save();
  176. }
  177. }
  178. drupal_set_message(empty($change) ? t('No values were changed.') : $msg);
  179. }
  180. /**
  181. * Submit callback for search_api_autocomplete_admin_overview(), when all
  182. * settings for the index should be deleted.
  183. *
  184. * @see search_api_autocomplete_admin_overview()
  185. */
  186. function search_api_autocomplete_admin_overview_submit_delete(array $form, array &$form_state) {
  187. $index = $form_state['index'];
  188. $ids = array_keys(search_api_autocomplete_search_load_multiple(FALSE, array('index_id' => $index->machine_name)));
  189. if ($ids) {
  190. entity_delete_multiple('search_api_autocomplete_search', $ids);
  191. drupal_set_message(t('All autocompletion settings stored for this index were deleted.'));
  192. }
  193. else {
  194. drupal_set_message(t('There were no settings to delete.'), 'warning');
  195. }
  196. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $index->machine_name;
  197. }
  198. /**
  199. * Form for editing the autocompletion settings for a search.
  200. *
  201. * @param SearchApiAutocompleteSearch $search
  202. * The search whose settings should be edited.
  203. *
  204. * @see search_api_autocomplete_admin_search_edit_validate()
  205. * @see search_api_autocomplete_admin_search_edit_submit()
  206. * @ingroup forms
  207. */
  208. function search_api_autocomplete_admin_search_edit(array $form, array &$form_state, SearchApiAutocompleteSearch $search) {
  209. drupal_set_title(t('Edit %search', array('%search' => $search->name)), PASS_THROUGH);
  210. $form_state['search'] = $search;
  211. $form_state['type'] = $type = search_api_autocomplete_get_types($search->type);
  212. if (!$type) {
  213. drupal_set_message(t('No information about the type of this search was found.'), 'error');
  214. return array();
  215. }
  216. $form['#tree'] = TRUE;
  217. $form['enabled'] = array(
  218. '#type' => 'checkbox',
  219. '#title' => t('Enabled'),
  220. '#default_value' => $search->enabled,
  221. );
  222. $form['options']['results'] = array(
  223. '#type' => 'checkbox',
  224. '#title' => t('Display result count estimates'),
  225. '#description' => t('Display the estimated number of result for each suggestion. ' .
  226. 'This option might not have an effect for some servers or types of suggestion.'),
  227. '#default_value' => !empty($search->options['results']),
  228. );
  229. // Add a list of fields to include for autocomplete searches.
  230. $fields = $search->index()->getFields();
  231. $fulltext_fields = $search->index()->getFulltextFields();
  232. $options = array();
  233. foreach ($fulltext_fields as $field) {
  234. $options[$field] = $fields[$field]['name'];
  235. }
  236. $form['options']['fields'] = array(
  237. '#type' => 'checkboxes',
  238. '#title' => t('Fields to use'),
  239. '#description' => t('Select the fields which should be searched for matches when looking for autocompletion suggestions. Leave blank to use the same fields as the underlying search.'),
  240. '#options' => $options,
  241. '#default_value' => empty($search->options['fields']) ? array() : drupal_map_assoc($search->options['fields']),
  242. '#attributes' => array('class' => array('search-api-checkboxes-list')),
  243. );
  244. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  245. if (!empty($type['config form']) && function_exists($type['config form']) && is_array($custom_form = $type['config form']($custom_form, $form_state, $search))) {
  246. $form['options']['custom'] = $custom_form;
  247. }
  248. $form['submit'] = array(
  249. '#type' => 'submit',
  250. '#value' => t('Save'),
  251. );
  252. return $form;
  253. }
  254. /**
  255. * Validate callback for search_api_autocomplete_admin_search_edit().
  256. *
  257. * @see search_api_autocomplete_admin_search_edit()
  258. * @see search_api_autocomplete_admin_search_edit_submit()
  259. */
  260. function search_api_autocomplete_admin_search_edit_validate(array $form, array &$form_state) {
  261. if (empty($form_state['type']['config form'])) {
  262. return;
  263. }
  264. $f = $form_state['type']['config form'] . '_validate';
  265. if (function_exists($f)) {
  266. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  267. $f($form['options']['custom'], $form_state, $form_state['values']['options']['custom']);
  268. }
  269. }
  270. /**
  271. * Submit callback for search_api_autocomplete_admin_search_edit().
  272. *
  273. * @see search_api_autocomplete_admin_search_edit()
  274. * @see search_api_autocomplete_admin_search_edit_validate()
  275. */
  276. function search_api_autocomplete_admin_search_edit_submit(array $form, array &$form_state) {
  277. $values = &$form_state['values'];
  278. if (!empty($form_state['type']['config form'])) {
  279. $f = $form_state['type']['config form'] . '_submit';
  280. if (function_exists($f)) {
  281. $custom_form = empty($form['options']['custom']) ? array() : $form['options']['custom'];
  282. $f($form['options']['custom'], $form_state, $values['options']['custom']);
  283. }
  284. }
  285. $search = $form_state['search'];
  286. $search->enabled = $values['enabled'];
  287. $search->options['results'] = $values['options']['results'];
  288. $search->options['fields'] = array_keys(array_filter($values['options']['fields']));
  289. if (isset($values['options']['custom'])) {
  290. // Take care of custom options that aren't changed in the config form.
  291. $old = empty($search->options['custom']) ? array() : $search->options['custom'];
  292. $search->options['custom'] = $values['options']['custom'] + $old;
  293. }
  294. $search->save();
  295. drupal_set_message(t('The autocompletion settings for the search have been saved.'));
  296. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $search->index_id . '/autocomplete';
  297. }
  298. /**
  299. * Form for deleting the autocompletion settings of a search.
  300. *
  301. * @param SearchApiAutocompleteSearch $search
  302. * The search whose settings should be deleted.
  303. *
  304. * @see search_api_autocomplete_admin_search_delete_submit()
  305. * @ingroup forms
  306. */
  307. function search_api_autocomplete_admin_search_delete(array $form, array &$form_state, SearchApiAutocompleteSearch $search) {
  308. $form_state['search'] = $search;
  309. return confirm_form(
  310. $form,
  311. t('Do you really want to delete the autocompletion settings for search %search?', array('%search' => $search->name)),
  312. 'admin/config/search/search_api/index/' . $search->index_id . '/autocomplete'
  313. );
  314. }
  315. /**
  316. * Submit callback for search_api_autocomplete_admin_search_delete().
  317. *
  318. * @see search_api_autocomplete_admin_search_delete()
  319. */
  320. function search_api_autocomplete_admin_search_delete_submit(array $form, array &$form_state) {
  321. $form_state['search']->delete();
  322. drupal_set_message(t('The autocompletion settings for the search have been deleted.'));
  323. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $form_state['search']->index_id . '/autocomplete';
  324. }