search_api_page.admin.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * Displays an overview of all defined search pages.
  4. */
  5. function search_api_page_admin_overview() {
  6. $base_path = drupal_get_path('module', 'search_api') . '/';
  7. drupal_add_css($base_path . 'search_api.admin.css');
  8. $header = array(t('Status'), t('Configuration'), t('Name'), t('Path'), t('Index'), t('Operations'));
  9. $rows = array();
  10. $t_enabled['data'] = array(
  11. '#theme' => 'image',
  12. '#path' => $base_path . 'enabled.png',
  13. '#alt' => t('enabled'),
  14. '#title' => t('enabled'),
  15. );
  16. $t_enabled['class'] = array('search-api-status');
  17. $t_disabled['data'] = array(
  18. '#theme' => 'image',
  19. '#path' => $base_path . 'disabled.png',
  20. '#alt' => t('disabled'),
  21. '#title' => t('disabled'),
  22. );
  23. $t_disabled['class'] = array('search-api-status');
  24. $t_enable = t('enable');
  25. $t_disable = t('disable');
  26. $t_edit = t('edit');
  27. $t_delete = t('delete');
  28. $pre = 'admin/config/search/search_api/page/';
  29. $pre_index = 'admin/config/search/search_api/index/';
  30. $enable = '/enable';
  31. $disable = '/disable';
  32. $edit = '/edit';
  33. $delete = '/delete';
  34. foreach (search_api_page_load_multiple() as $page) {
  35. $url = $pre . $page->machine_name;
  36. $index = search_api_index_load($page->index_id);
  37. $rows[] = array(
  38. $page->enabled ? $t_enabled : $t_disabled,
  39. theme('entity_status', array('status' => $page->status)),
  40. l($page->name, $page->path),
  41. l($page->path, $page->path),
  42. l($index->name, $pre_index . $index->machine_name),
  43. l($t_edit, $url . $edit),
  44. );
  45. }
  46. return array(
  47. '#theme' => 'table',
  48. '#header' => $header,
  49. '#rows' => $rows,
  50. '#empty' => t('There are no search pages defined yet.'),
  51. );
  52. }
  53. /**
  54. * Displays a form for adding a search page.
  55. */
  56. function search_api_page_admin_add(array $form, array &$form_state) {
  57. $form = array();
  58. if (empty($form_state['step_one'])) {
  59. $indexes = search_api_index_load_multiple(FALSE);
  60. if (!$indexes) {
  61. drupal_set_message(t('There are no searches indexes which can be searched. Please <a href="@url">create an index</a> first.', array('@url' => url('admin/config/search/search_api/add_index'))), 'warning');
  62. return array();
  63. }
  64. $index_options = array();
  65. foreach ($indexes as $index) {
  66. if ($index->enabled) {
  67. $index_options[$index->machine_name] = $index->name;
  68. }
  69. }
  70. foreach ($indexes as $index) {
  71. if (!$index->enabled) {
  72. $index_options[$index->machine_name] = $index->name . ' (' . t('disabled') . ')';
  73. }
  74. }
  75. $form['name'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Search name'),
  78. '#maxlength' => 50,
  79. '#required' => TRUE,
  80. );
  81. $form['machine_name'] = array(
  82. '#type' => 'machine_name',
  83. '#maxlength' => 51,
  84. '#machine_name' => array(
  85. 'exists' => 'search_api_index_load',
  86. ),
  87. );
  88. $form['index_id'] = array(
  89. '#type' => 'select',
  90. '#title' => t('Index'),
  91. '#description' => t('Select the index this page should search. This cannot be changed later.'),
  92. '#options' => $index_options,
  93. '#required' => TRUE,
  94. );
  95. $form['enabled'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Enabled'),
  98. '#description' => t('This will only take effect if the selected index is also enabled.'),
  99. '#default_value' => TRUE,
  100. );
  101. $form['description'] = array(
  102. '#type' => 'textarea',
  103. '#title' => t('Search description'),
  104. );
  105. $form['path'] = array(
  106. '#type' => 'textfield',
  107. '#title' => t('Path'),
  108. '#description' => t('Set the path under which the search page will be accessible, when enabled.'),
  109. '#maxlength' => 50,
  110. '#required' => TRUE,
  111. );
  112. $form['submit'] = array(
  113. '#type' => 'submit',
  114. '#value' => t('Create page'),
  115. );
  116. return $form;
  117. }
  118. $index = search_api_index_load($form_state['step_one']['index_id']);
  119. if ($index->enabled) {
  120. $modes = array();
  121. foreach ($index->query()->parseModes() as $mode => $info) {
  122. $modes[$mode] = $info['name'];
  123. }
  124. }
  125. else {
  126. $modes = array();
  127. $modes['direct'] = t('Direct query');
  128. $modes['single'] = t('Single term');
  129. $modes['terms'] = t('Multiple terms');
  130. }
  131. $form['mode'] = array(
  132. '#type' => 'select',
  133. '#title' => t('Query type'),
  134. '#description' => t('Select how the query will be parsed.'),
  135. '#options' => $modes,
  136. '#default_value' => 'terms',
  137. );
  138. $fields = array();
  139. $index_fields = $index->getFields();
  140. foreach ($index->getFulltextFields() as $name) {
  141. $fields[$name] = $index_fields[$name]['name'];
  142. }
  143. if (count($fields) > 1) {
  144. $form['fields'] = array(
  145. '#type' => 'select',
  146. '#title' => t('Searched fields'),
  147. '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
  148. '#options' => $fields,
  149. '#size' => min(4, count($fields)),
  150. '#multiple' => TRUE,
  151. );
  152. }
  153. else {
  154. $form['fields'] = array(
  155. '#type' => 'value',
  156. '#value' => array(),
  157. );
  158. }
  159. $form['per_page'] = array(
  160. '#type' => 'select',
  161. '#title' => t('Results per page'),
  162. '#description' => t('Select how many items will be displayed on one page of the search result.'),
  163. '#options' => drupal_map_assoc(array(5, 10, 20, 30, 40, 50, 60, 80, 100)),
  164. '#default_value' => 10,
  165. );
  166. $form['get_per_page'] = array(
  167. '#type' => 'checkbox',
  168. '#title' => t('Allow GET override'),
  169. '#description' => t('Allow the „Results per page“ setting to be overridden from the URL, using the "per_page" GET parameter.<br />' .
  170. 'Example: http://example.com/search_results?per_page=7'),
  171. '#default_value' => TRUE,
  172. );
  173. $view_modes = array(
  174. 'search_api_page_result' => t('Themed as search results'),
  175. );
  176. // For entities, we also add all entity view modes.
  177. if ($entity_info = entity_get_info($index->item_type)) {
  178. foreach ($entity_info['view modes'] as $mode => $mode_info) {
  179. $view_modes[$mode] = $mode_info['label'];
  180. }
  181. }
  182. if (count($view_modes) > 1) {
  183. $form['view_mode'] = array(
  184. '#type' => 'select',
  185. '#title' => t('View mode'),
  186. '#options' => $view_modes,
  187. '#description' => t('Select how search results will be displayed.'),
  188. '#size' => 1,
  189. '#default_value' => 'search_api_page_result',
  190. );
  191. }
  192. else {
  193. $form['view_mode'] = array(
  194. '#type' => 'value',
  195. '#value' => reset($view_modes),
  196. );
  197. }
  198. if (module_exists('search_api_spellcheck') && ($server = $index->server()) && $server->supportsFeature('search_api_spellcheck')) {
  199. $form['search_api_spellcheck'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Enable spell check'),
  202. '#description' => t('Display "Did you mean … ?" above search results.'),
  203. '#default_value' => TRUE,
  204. );
  205. }
  206. $form['submit'] = array(
  207. '#type' => 'submit',
  208. '#value' => t('Create page'),
  209. );
  210. return $form;
  211. }
  212. /**
  213. * Validation callback for search_api_page_admin_add().
  214. */
  215. function search_api_page_admin_add_validate(array $form, array &$form_state) {
  216. if (empty($form_state['step_one'])) {
  217. $form_state['values']['path'] = drupal_strtolower(trim($form_state['values']['path']));
  218. if (search_api_page_load_multiple(FALSE, array('path' => $form_state['values']['path']))) {
  219. form_set_error('path', t('The entered path is already in use. Please enter a unique path.'));
  220. }
  221. }
  222. }
  223. /**
  224. * Submit callback for search_api_page_admin_add().
  225. */
  226. function search_api_page_admin_add_submit(array $form, array &$form_state) {
  227. form_state_values_clean($form_state);
  228. if (empty($form_state['step_one'])) {
  229. $form_state['step_one'] = $form_state['values'];
  230. $form_state['rebuild'] = TRUE;
  231. return;
  232. }
  233. $values = $form_state['step_one'];
  234. $values['options'] = $form_state['values'];
  235. search_api_page_insert($values);
  236. drupal_set_message(t('The search page was successfully created.'));
  237. $form_state['redirect'] = 'admin/config/search/search_api/page';
  238. }
  239. /**
  240. * Displays a form for editing or deleting a search page.
  241. */
  242. function search_api_page_admin_edit(array $form, array &$form_state, Entity $page) {
  243. $index = search_api_index_load($page->index_id);
  244. $form_state['page'] = $page;
  245. $form['name'] = array(
  246. '#type' => 'textfield',
  247. '#title' => t('Search name'),
  248. '#maxlength' => 50,
  249. '#required' => TRUE,
  250. '#default_value' => $page->name,
  251. );
  252. $form['enabled'] = array(
  253. '#type' => 'checkbox',
  254. '#title' => t('Enabled'),
  255. '#description' => t('This will only take effect if the selected index is also enabled.'),
  256. '#default_value' => $page->enabled,
  257. '#disabled' => !$index->enabled,
  258. );
  259. $form['description'] = array(
  260. '#type' => 'textarea',
  261. '#title' => t('Search description'),
  262. '#default_value' => $page->description,
  263. );
  264. $form['index'] = array(
  265. '#type' => 'item',
  266. '#title' => t('Index'),
  267. '#description' => l($index->name, 'admin/config/search/search_api/index/' . $index->machine_name),
  268. );
  269. $form['path'] = array(
  270. '#type' => 'textfield',
  271. '#title' => t('Path'),
  272. '#description' => t('Set the path under which the search page will be accessible, when enabled.'),
  273. '#maxlength' => 50,
  274. '#default_value' => $page->path,
  275. );
  276. if ($index->enabled) {
  277. $modes = array();
  278. foreach ($index->query()->parseModes() as $mode => $info) {
  279. $modes[$mode] = $info['name'];
  280. }
  281. }
  282. else {
  283. $modes = array();
  284. $modes['direct'] = array(
  285. 'name' => t('Direct query'),
  286. 'description' => t("Don't parse the query, just hand it to the search server unaltered. " .
  287. "Might fail if the query contains syntax errors in regard to the specific server's query syntax."),
  288. );
  289. $modes['single'] = array(
  290. 'name' => t('Single term'),
  291. 'description' => t('The query is interpreted as a single keyword, maybe containing spaces or special characters.'),
  292. );
  293. $modes['terms'] = array(
  294. 'name' => t('Multiple terms'),
  295. 'description' => t('The query is interpreted as multiple keywords seperated by spaces. ' .
  296. 'Keywords containing spaces may be "quoted". Quoted keywords must still be seperated by spaces.'),
  297. );
  298. }
  299. $form['options']['#tree'] = TRUE;
  300. $form['options']['mode'] = array(
  301. '#type' => 'select',
  302. '#title' => t('Query type'),
  303. '#description' => t('Select how the query will be parsed.'),
  304. '#options' => $modes,
  305. '#default_value' => $page->options['mode'],
  306. );
  307. $fields = array();
  308. $index_fields = $index->getFields();
  309. foreach ($index->getFulltextFields() as $name) {
  310. $fields[$name] = $index_fields[$name]['name'];
  311. }
  312. if (count($fields) > 1) {
  313. $form['options']['fields'] = array(
  314. '#type' => 'select',
  315. '#title' => t('Searched fields'),
  316. '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
  317. '#options' => $fields,
  318. '#size' => min(4, count($fields)),
  319. '#multiple' => TRUE,
  320. '#default_value' => $page->options['fields'],
  321. );
  322. }
  323. else {
  324. $form['options']['fields'] = array(
  325. '#type' => 'value',
  326. '#value' => array(),
  327. );
  328. }
  329. $form['options']['per_page'] = array(
  330. '#type' => 'select',
  331. '#title' => t('Results per page'),
  332. '#description' => t('Select how many items will be displayed on one page of the search result.'),
  333. '#options' => drupal_map_assoc(array(5, 10, 20, 30, 40, 50, 60, 80, 100)),
  334. '#default_value' => $page->options['per_page'],
  335. );
  336. $form['options']['get_per_page'] = array(
  337. '#type' => 'checkbox',
  338. '#title' => t('Allow GET override'),
  339. '#description' => t('Allow the „Results per page“ setting to be overridden from the URL, using the "per_page" GET parameter.<br />' .
  340. 'Example: <code>http://example.com/search_results?per_page=7</code>'),
  341. '#default_value' => !empty($page->options['get_per_page']),
  342. );
  343. $view_modes = array(
  344. 'search_api_page_result' => t('Themed as search results'),
  345. );
  346. // For entities, we also add all entity view modes.
  347. if ($entity_info = entity_get_info($index->item_type)) {
  348. foreach ($entity_info['view modes'] as $mode => $mode_info) {
  349. $view_modes[$mode] = $mode_info['label'];
  350. }
  351. }
  352. if (count($view_modes) > 1) {
  353. $form['options']['view_mode'] = array(
  354. '#type' => 'select',
  355. '#title' => t('View mode'),
  356. '#options' => $view_modes,
  357. '#description' => t('Select how search results will be displayed.'),
  358. '#size' => 1,
  359. '#default_value' => isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result',
  360. );
  361. }
  362. else {
  363. $form['options']['view_mode'] = array(
  364. '#type' => 'value',
  365. '#value' => reset($view_modes),
  366. );
  367. }
  368. if (module_exists('search_api_spellcheck') && ($server = $index->server()) && $server->supportsFeature('search_api_spellcheck')) {
  369. $form['options']['search_api_spellcheck'] = array(
  370. '#type' => 'checkbox',
  371. '#title' => t('Enable spell check'),
  372. '#description' => t('Display "Did you mean … ?" above search results.'),
  373. '#default_value' => !empty($page->options['search_api_spellcheck']),
  374. );
  375. }
  376. $form['actions']['#type'] = 'actions';
  377. $form['actions']['submit'] = array(
  378. '#type' => 'submit',
  379. '#value' => t('Save changes'),
  380. );
  381. if ($page->hasStatus(ENTITY_OVERRIDDEN)) {
  382. $form['actions']['revert'] = array(
  383. '#type' => 'fieldset',
  384. '#title' => t('Revert search page'),
  385. '#description' => t('This will revert all settings on this search page back to the defaults. This action cannot be undone.'),
  386. '#collapsible' => TRUE,
  387. '#collapsed' => TRUE,
  388. 'revert' => array(
  389. '#type' => 'submit',
  390. '#value' => t('Revert search page'),
  391. ),
  392. );
  393. }
  394. elseif ($page->hasStatus(ENTITY_CUSTOM)) {
  395. $form['actions']['delete'] = array(
  396. '#type' => 'fieldset',
  397. '#title' => t('Delete search page'),
  398. '#description' => t('This will delete the search page along with all of its settings.'),
  399. '#collapsible' => TRUE,
  400. '#collapsed' => TRUE,
  401. 'delete' => array(
  402. '#type' => 'submit',
  403. '#value' => t('Delete search page'),
  404. ),
  405. );
  406. }
  407. return $form;
  408. }
  409. /**
  410. * Validation callback for search_api_page_admin_edit().
  411. */
  412. function search_api_page_admin_edit_validate(array $form, array &$form_state) {
  413. if ($form_state['values']['op'] == t('Save changes')) {
  414. $form_state['values']['path'] = drupal_strtolower(trim($form_state['values']['path']));
  415. $pages = search_api_page_load_multiple(FALSE, array('path' => $form_state['values']['path']));
  416. if (count($pages) > 1 || (($page = array_shift($pages)) && $page->machine_name != $form_state['page']->machine_name)) {
  417. form_set_error('path', t('The entered path is already in use. Please enter a unique path.'));
  418. }
  419. }
  420. }
  421. /**
  422. * Submit callback for search_api_page_admin_edit().
  423. */
  424. function search_api_page_admin_edit_submit(array $form, array &$form_state) {
  425. $op = $form_state['values']['op'];
  426. form_state_values_clean($form_state);
  427. $form_state['redirect'] = 'admin/config/search/search_api/page';
  428. if ($op == t('Delete search page') || $op == t('Revert search page')) {
  429. $form_state['page']->delete();
  430. if ($op == t('Revert search page')) {
  431. drupal_set_message(t('The search page was successfully reverted.'));
  432. }
  433. else {
  434. drupal_set_message(t('The search page was successfully deleted.'));
  435. }
  436. return;
  437. }
  438. search_api_page_edit($form_state['page']->machine_name, $form_state['values']);
  439. drupal_set_message(t('The changes were successfully saved.'));
  440. }