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