apachesolr_search.pages.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Provides the page callback for user defined search pages.
  5. */
  6. /**
  7. * Returns search results on user defined search pages.
  8. */
  9. function apachesolr_search_custom_page($page_id, $keys = '', $path_replacer = NULL) {
  10. $search_page = apachesolr_search_page_load($page_id);
  11. if (empty($search_page)) {
  12. drupal_set_message(t('This search page cannot be found'), 'error');
  13. return drupal_not_found();
  14. }
  15. // Activate the page context, if the module is enabled.
  16. if ((module_exists('context')) && ($plugin = context_get_plugin('condition', 'apachesolr_page'))) {
  17. $plugin->execute($search_page);
  18. }
  19. // Add our replacement value in the conditions array
  20. if (!empty($path_replacer)) {
  21. $search_page['settings']['apachesolr_search_path_replacer'] = $path_replacer;
  22. }
  23. // Replace dynamic path with current path
  24. $search_page['search_path'] = str_replace('%', $path_replacer, $search_page['search_path']);
  25. // Retrieve the conditions that apply to this page
  26. $conditions = apachesolr_search_conditions_default($search_page);
  27. // Process our keys so they are clean
  28. $keys = rawurldecode($keys);
  29. // Initiate our build array
  30. $build = array();
  31. // Add a custom search form if required
  32. if (!empty($search_page['settings']['apachesolr_search_search_box'])) {
  33. // Adds the search form to the page.
  34. $build['search_form'] = drupal_get_form('apachesolr_search_custom_page_search_form', $search_page, $keys);
  35. }
  36. // Retrieve the results of the search
  37. $results = apachesolr_search_search_results($keys, $conditions, $search_page);
  38. // Build our page and allow modification.
  39. $build_results = apachesolr_search_search_page_custom($results, $search_page, $build);
  40. return $build_results;
  41. }
  42. /**
  43. * Search for placed on user defined search pages.
  44. */
  45. function apachesolr_search_custom_page_search_form($form, &$form_state, $search_page, $keys = '') {
  46. // Loads the core Search CSS file, use the core search module's classes.
  47. drupal_add_css(drupal_get_path('module', 'search') . '/search.css');
  48. $form = array();
  49. $form['#id'] = 'search-form';
  50. $form['#attributes']['class'][] = 'search-form';
  51. $form['#search_page'] = $search_page;
  52. $form['basic'] = array(
  53. '#type' => 'container',
  54. '#attributes' => array('class' => array('container-inline')),
  55. );
  56. $form['basic']['keys'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Enter terms'),
  59. '#default_value' => $keys,
  60. '#size' => 20,
  61. '#maxlength' => 255,
  62. );
  63. $form['basic']['submit'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Search'),
  66. );
  67. $form['basic']['get'] = array(
  68. '#type' => 'hidden',
  69. '#default_value' => json_encode(array_diff_key($_GET, array('q' => 1, 'page' => 1, 'solrsort' => 1, 'retain-filters' => 1))),
  70. );
  71. $fq = NULL;
  72. if (apachesolr_has_searched($search_page['env_id'])) {
  73. $query = apachesolr_current_query($search_page['env_id']);
  74. // We use the presence of filter query params as a flag for the retain filters checkbox.
  75. $fq = $query->getParam('fq');
  76. }
  77. if ($fq || isset($form_state['input']['retain-filters'])) {
  78. $form['basic']['retain-filters'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Retain current filters'),
  81. '#default_value' => (int) !empty($_GET['retain-filters']),
  82. );
  83. }
  84. return $form;
  85. }
  86. /**
  87. * Processes apachesolr_search_custom_page_search_form submissions.
  88. */
  89. function apachesolr_search_custom_page_search_form_submit(&$form, &$form_state) {
  90. $search_page = $form['#search_page'];
  91. $redirect = $search_page['search_path'];
  92. // Also encode slashes so we don't get akward situations when obtaining the
  93. // search key. We can't use drupal_encode_path because for "aestetic" reasons
  94. // they don't encode slashes...
  95. $redirect_value = rawurlencode($form_state['values']['keys']);
  96. if (strlen($form_state['values']['keys'])) {
  97. $redirect .= '/' . $redirect_value;
  98. }
  99. $get = array();
  100. if (isset($form_state['values']['get'])) {
  101. $get = json_decode($form_state['values']['get'], TRUE);
  102. }
  103. if (!empty($form_state['values']['retain-filters'])) {
  104. // Add our saved values
  105. $get['retain-filters'] = '1';
  106. }
  107. else {
  108. // Remove all filters
  109. if (!empty($search_page['settings']['apachesolr_search_allow_user_input'])) {
  110. unset($get['fq']);
  111. }
  112. if (module_exists('facetapi')) {
  113. unset($get['f']);
  114. }
  115. }
  116. // Add the query values into the redirect.
  117. $form_state['redirect'] = array($redirect, array('query' => $get));
  118. }