annotate sites/all/modules/custom/solrsearch_autocomplete/solrsearch_autocomplete.module @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
015d06b10d37 initial
dwinter
parents:
diff changeset
1 <?php
015d06b10d37 initial
dwinter
parents:
diff changeset
2
015d06b10d37 initial
dwinter
parents:
diff changeset
3 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
4 * @file
015d06b10d37 initial
dwinter
parents:
diff changeset
5 * Alters search forms to suggest terms using Apache Solr using AJAX.
015d06b10d37 initial
dwinter
parents:
diff changeset
6 * Thanks to:
015d06b10d37 initial
dwinter
parents:
diff changeset
7 * robertDouglass who contributed some of the code.
015d06b10d37 initial
dwinter
parents:
diff changeset
8 * sch4lly for contributing to D7 version
015d06b10d37 initial
dwinter
parents:
diff changeset
9 */
015d06b10d37 initial
dwinter
parents:
diff changeset
10
015d06b10d37 initial
dwinter
parents:
diff changeset
11 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
12 * Implementation of hook_init().
015d06b10d37 initial
dwinter
parents:
diff changeset
13 */
015d06b10d37 initial
dwinter
parents:
diff changeset
14 function solrsearch_autocomplete_init() {
015d06b10d37 initial
dwinter
parents:
diff changeset
15
015d06b10d37 initial
dwinter
parents:
diff changeset
16 drupal_add_css( drupal_get_path('module', 'solrsearch_autocomplete') . '/solrsearch_autocomplete.css');
015d06b10d37 initial
dwinter
parents:
diff changeset
17
015d06b10d37 initial
dwinter
parents:
diff changeset
18 // If using custom JS widget, include files and settings.
015d06b10d37 initial
dwinter
parents:
diff changeset
19 if (solrsearch_autocomplete_variable_get_widget() == 'custom') {
015d06b10d37 initial
dwinter
parents:
diff changeset
20 // Add custom autocomplete files
015d06b10d37 initial
dwinter
parents:
diff changeset
21 drupal_add_js(drupal_get_path('module', 'solrsearch_autocomplete') .'/solrsearch_autocomplete.js');
015d06b10d37 initial
dwinter
parents:
diff changeset
22 drupal_add_js(drupal_get_path('module', 'solrsearch_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.js');
015d06b10d37 initial
dwinter
parents:
diff changeset
23 drupal_add_css( drupal_get_path('module', 'solrsearch_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.css');
015d06b10d37 initial
dwinter
parents:
diff changeset
24 // Specify path to autocomplete handler.
015d06b10d37 initial
dwinter
parents:
diff changeset
25 drupal_add_js(array('solrsearch_autocomplete' => array('path' => url('solrsearch_autocomplete'))), 'setting');
015d06b10d37 initial
dwinter
parents:
diff changeset
26 }
015d06b10d37 initial
dwinter
parents:
diff changeset
27 }
015d06b10d37 initial
dwinter
parents:
diff changeset
28
015d06b10d37 initial
dwinter
parents:
diff changeset
29 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
30 * Implementation of hook_form_FORM_ID_alter().
015d06b10d37 initial
dwinter
parents:
diff changeset
31 */
015d06b10d37 initial
dwinter
parents:
diff changeset
32 function solrsearch_autocomplete_form_search_form_alter(&$form, $form_state) {
015d06b10d37 initial
dwinter
parents:
diff changeset
33
015d06b10d37 initial
dwinter
parents:
diff changeset
34 if ($form['module']['#value'] == 'solrsearch_search' || $form['module']['#value'] == 'solrsearch_multisitesearch') {
015d06b10d37 initial
dwinter
parents:
diff changeset
35 $element = &$form['basic']['keys'];
015d06b10d37 initial
dwinter
parents:
diff changeset
36 solrsearch_autocomplete_do_alter($element);
015d06b10d37 initial
dwinter
parents:
diff changeset
37 }
015d06b10d37 initial
dwinter
parents:
diff changeset
38 }
015d06b10d37 initial
dwinter
parents:
diff changeset
39
015d06b10d37 initial
dwinter
parents:
diff changeset
40 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
41 * Implementation of hook_form_FORM_ID_alter().
015d06b10d37 initial
dwinter
parents:
diff changeset
42 */
015d06b10d37 initial
dwinter
parents:
diff changeset
43 function solrsearch_autocomplete_form_solrsearch_search_block_form_alter(&$form, $form_state) {
015d06b10d37 initial
dwinter
parents:
diff changeset
44
015d06b10d37 initial
dwinter
parents:
diff changeset
45 $element = &$form['solrsearch_search_block_form'];
015d06b10d37 initial
dwinter
parents:
diff changeset
46 solrsearch_autocomplete_do_alter($element,"title_s,title,author");
015d06b10d37 initial
dwinter
parents:
diff changeset
47 }
015d06b10d37 initial
dwinter
parents:
diff changeset
48
015d06b10d37 initial
dwinter
parents:
diff changeset
49 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
50 * Implementation of hook_form_FORM_ID_alter().
015d06b10d37 initial
dwinter
parents:
diff changeset
51 */
015d06b10d37 initial
dwinter
parents:
diff changeset
52 function solrsearch_autocomplete_form_solrsearch_search_author_block_form_alter(&$form, $form_state) {
015d06b10d37 initial
dwinter
parents:
diff changeset
53
015d06b10d37 initial
dwinter
parents:
diff changeset
54 $element = &$form['solrsearch_search_author_block_form'];
015d06b10d37 initial
dwinter
parents:
diff changeset
55
015d06b10d37 initial
dwinter
parents:
diff changeset
56 solrsearch_autocomplete_do_alter($element,"author");
015d06b10d37 initial
dwinter
parents:
diff changeset
57 }
015d06b10d37 initial
dwinter
parents:
diff changeset
58
015d06b10d37 initial
dwinter
parents:
diff changeset
59 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
60 * Implementation of hook_form_FORM_ID_alter().
015d06b10d37 initial
dwinter
parents:
diff changeset
61 */
015d06b10d37 initial
dwinter
parents:
diff changeset
62 function solrsearch_autocomplete_form_solrsearch_search_title_block_form_alter(&$form, $form_state) {
015d06b10d37 initial
dwinter
parents:
diff changeset
63
015d06b10d37 initial
dwinter
parents:
diff changeset
64 $element = &$form['solrsearch_search_title_block_form'];
015d06b10d37 initial
dwinter
parents:
diff changeset
65
015d06b10d37 initial
dwinter
parents:
diff changeset
66 solrsearch_autocomplete_do_alter($element,"title_s,title");
015d06b10d37 initial
dwinter
parents:
diff changeset
67 }
015d06b10d37 initial
dwinter
parents:
diff changeset
68 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
69 * Helper function to do the actual altering of search forms.
015d06b10d37 initial
dwinter
parents:
diff changeset
70 *
015d06b10d37 initial
dwinter
parents:
diff changeset
71 * @param $element
015d06b10d37 initial
dwinter
parents:
diff changeset
72 * The element to alter. Should be passed by reference so that original form
015d06b10d37 initial
dwinter
parents:
diff changeset
73 * element will be altered.
015d06b10d37 initial
dwinter
parents:
diff changeset
74 * E.g.: solrsearch_autocomplete_do_alter(&$form['xyz'])
015d06b10d37 initial
dwinter
parents:
diff changeset
75 */
015d06b10d37 initial
dwinter
parents:
diff changeset
76 function solrsearch_autocomplete_do_alter(&$element,$field_name) {
015d06b10d37 initial
dwinter
parents:
diff changeset
77
015d06b10d37 initial
dwinter
parents:
diff changeset
78 if (solrsearch_autocomplete_variable_get_widget() == 'custom') {
015d06b10d37 initial
dwinter
parents:
diff changeset
79 // Create elements if they do not exist.
015d06b10d37 initial
dwinter
parents:
diff changeset
80 if (!isset($element['#attributes'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
81 $element['#attributes'] = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
82 }
015d06b10d37 initial
dwinter
parents:
diff changeset
83 if (!isset($element['#attributes']['class'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
84 $element['#attributes']['class'] = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
85 }
015d06b10d37 initial
dwinter
parents:
diff changeset
86 array_push($element['#attributes']['class'], 'solrsearch-autocomplete', 'unprocessed','solrsearch-autocomplete.field.'.$field_name);
015d06b10d37 initial
dwinter
parents:
diff changeset
87
015d06b10d37 initial
dwinter
parents:
diff changeset
88 }
015d06b10d37 initial
dwinter
parents:
diff changeset
89 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
90 $element['#autocomplete_path'] = 'solrsearch_autocomplete';
015d06b10d37 initial
dwinter
parents:
diff changeset
91 }
015d06b10d37 initial
dwinter
parents:
diff changeset
92 }
015d06b10d37 initial
dwinter
parents:
diff changeset
93
015d06b10d37 initial
dwinter
parents:
diff changeset
94 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
95 * Implementation of hook_menu().
015d06b10d37 initial
dwinter
parents:
diff changeset
96 */
015d06b10d37 initial
dwinter
parents:
diff changeset
97 function solrsearch_autocomplete_menu() {
015d06b10d37 initial
dwinter
parents:
diff changeset
98 $items = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
99
015d06b10d37 initial
dwinter
parents:
diff changeset
100 $items['solrsearch_autocomplete'] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
101 'page callback' => 'solrsearch_autocomplete_callback',
015d06b10d37 initial
dwinter
parents:
diff changeset
102 'access callback' => 'user_access',
015d06b10d37 initial
dwinter
parents:
diff changeset
103 'access arguments' => array('search content'),
015d06b10d37 initial
dwinter
parents:
diff changeset
104 'type' => MENU_CALLBACK,
015d06b10d37 initial
dwinter
parents:
diff changeset
105 );
015d06b10d37 initial
dwinter
parents:
diff changeset
106 return $items;
015d06b10d37 initial
dwinter
parents:
diff changeset
107 }
015d06b10d37 initial
dwinter
parents:
diff changeset
108
015d06b10d37 initial
dwinter
parents:
diff changeset
109 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
110 * Callback for url solrsearch_autocomplete/autocomplete.
015d06b10d37 initial
dwinter
parents:
diff changeset
111 * @param $keys
015d06b10d37 initial
dwinter
parents:
diff changeset
112 * The user-entered query.
015d06b10d37 initial
dwinter
parents:
diff changeset
113 */
015d06b10d37 initial
dwinter
parents:
diff changeset
114 function solrsearch_autocomplete_callback($keys = '', $field_name='') {
015d06b10d37 initial
dwinter
parents:
diff changeset
115
015d06b10d37 initial
dwinter
parents:
diff changeset
116
015d06b10d37 initial
dwinter
parents:
diff changeset
117 if (solrsearch_autocomplete_variable_get_widget() == 'custom') {
015d06b10d37 initial
dwinter
parents:
diff changeset
118 // Keys for custom widget come from $_GET.
015d06b10d37 initial
dwinter
parents:
diff changeset
119 $keys = $_GET['query'];
015d06b10d37 initial
dwinter
parents:
diff changeset
120 $field_names =$_GET['fieldName'];
015d06b10d37 initial
dwinter
parents:
diff changeset
121 }
015d06b10d37 initial
dwinter
parents:
diff changeset
122
015d06b10d37 initial
dwinter
parents:
diff changeset
123 /*
015d06b10d37 initial
dwinter
parents:
diff changeset
124 $res = split("\:",$keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
125
015d06b10d37 initial
dwinter
parents:
diff changeset
126 if (count($res) == 2){
015d06b10d37 initial
dwinter
parents:
diff changeset
127 $keys=$res[1];
015d06b10d37 initial
dwinter
parents:
diff changeset
128 $field_name=$res[0];
015d06b10d37 initial
dwinter
parents:
diff changeset
129 } else {
015d06b10d37 initial
dwinter
parents:
diff changeset
130 $keys=$res[0];
015d06b10d37 initial
dwinter
parents:
diff changeset
131 $field_name="";
015d06b10d37 initial
dwinter
parents:
diff changeset
132 }
015d06b10d37 initial
dwinter
parents:
diff changeset
133 */
015d06b10d37 initial
dwinter
parents:
diff changeset
134
015d06b10d37 initial
dwinter
parents:
diff changeset
135 $suggestions = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
136
015d06b10d37 initial
dwinter
parents:
diff changeset
137 foreach (explode(",",$field_names) as $field_name) {
015d06b10d37 initial
dwinter
parents:
diff changeset
138
015d06b10d37 initial
dwinter
parents:
diff changeset
139 $suggestions = array_merge($suggestions, solrsearch_autocomplete_suggest_word_completion($keys, 5,$field_name));
015d06b10d37 initial
dwinter
parents:
diff changeset
140 if (solrsearch_autocomplete_variable_get_suggest_keywords() || solrsearch_autocomplete_variable_get_suggest_spellcheck()) {
015d06b10d37 initial
dwinter
parents:
diff changeset
141 $suggestions = array_merge($suggestions, solrsearch_autocomplete_suggest_additional_term($keys, 5,$field_name));
015d06b10d37 initial
dwinter
parents:
diff changeset
142
015d06b10d37 initial
dwinter
parents:
diff changeset
143 }
015d06b10d37 initial
dwinter
parents:
diff changeset
144 }
015d06b10d37 initial
dwinter
parents:
diff changeset
145 $result = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
146 if (solrsearch_autocomplete_variable_get_widget() == 'custom') {
015d06b10d37 initial
dwinter
parents:
diff changeset
147 // Place suggestions into new array for returning as JSON.
015d06b10d37 initial
dwinter
parents:
diff changeset
148 foreach ($suggestions as $key => $display) {
015d06b10d37 initial
dwinter
parents:
diff changeset
149 $result[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
150 "key" => substr($key,1),
015d06b10d37 initial
dwinter
parents:
diff changeset
151 "display" => $display
015d06b10d37 initial
dwinter
parents:
diff changeset
152 );
015d06b10d37 initial
dwinter
parents:
diff changeset
153 }
015d06b10d37 initial
dwinter
parents:
diff changeset
154 }
015d06b10d37 initial
dwinter
parents:
diff changeset
155 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
156 foreach ($suggestions as $key => $display) {
015d06b10d37 initial
dwinter
parents:
diff changeset
157 $result[substr($key,1)] =$display;
015d06b10d37 initial
dwinter
parents:
diff changeset
158 }
015d06b10d37 initial
dwinter
parents:
diff changeset
159 }
015d06b10d37 initial
dwinter
parents:
diff changeset
160 drupal_json_output($result);
015d06b10d37 initial
dwinter
parents:
diff changeset
161 exit();
015d06b10d37 initial
dwinter
parents:
diff changeset
162 }
015d06b10d37 initial
dwinter
parents:
diff changeset
163
015d06b10d37 initial
dwinter
parents:
diff changeset
164 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
165 * Implementation of hook_theme().
015d06b10d37 initial
dwinter
parents:
diff changeset
166 */
015d06b10d37 initial
dwinter
parents:
diff changeset
167 function solrsearch_autocomplete_theme() {
015d06b10d37 initial
dwinter
parents:
diff changeset
168 return array(
015d06b10d37 initial
dwinter
parents:
diff changeset
169 'solrsearch_autocomplete_highlight' => array(
015d06b10d37 initial
dwinter
parents:
diff changeset
170 'file' => 'solrsearch_autocomplete.module',
015d06b10d37 initial
dwinter
parents:
diff changeset
171 'arguments' => array(
015d06b10d37 initial
dwinter
parents:
diff changeset
172 'keys' => NULL,
015d06b10d37 initial
dwinter
parents:
diff changeset
173 'suggestion' => NULL,
015d06b10d37 initial
dwinter
parents:
diff changeset
174 'count' => NULL,
015d06b10d37 initial
dwinter
parents:
diff changeset
175 ),
015d06b10d37 initial
dwinter
parents:
diff changeset
176 ),
015d06b10d37 initial
dwinter
parents:
diff changeset
177 'solrsearch_autocomplete_spellcheck' => array(
015d06b10d37 initial
dwinter
parents:
diff changeset
178 'file' => 'solrsearch_autocomplete.module',
015d06b10d37 initial
dwinter
parents:
diff changeset
179 'arguments' => array(
015d06b10d37 initial
dwinter
parents:
diff changeset
180 'suggestion' => NULL,
015d06b10d37 initial
dwinter
parents:
diff changeset
181 ),
015d06b10d37 initial
dwinter
parents:
diff changeset
182 ),
015d06b10d37 initial
dwinter
parents:
diff changeset
183 );
015d06b10d37 initial
dwinter
parents:
diff changeset
184 }
015d06b10d37 initial
dwinter
parents:
diff changeset
185
015d06b10d37 initial
dwinter
parents:
diff changeset
186 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
187 * Themes each returned suggestion.
015d06b10d37 initial
dwinter
parents:
diff changeset
188 */
015d06b10d37 initial
dwinter
parents:
diff changeset
189 function theme_solrsearch_autocomplete_highlight($variables) {
015d06b10d37 initial
dwinter
parents:
diff changeset
190 static $first = true;
015d06b10d37 initial
dwinter
parents:
diff changeset
191 $html = '';
015d06b10d37 initial
dwinter
parents:
diff changeset
192 $html .= '<div class="solrsearch_autocomplete suggestion">';
015d06b10d37 initial
dwinter
parents:
diff changeset
193 $html .= '<strong>' . drupal_substr($variables['suggestion'], 0, strlen($variables['keys'])) . '</strong>' . drupal_substr($variables['suggestion'], strlen($variables['keys']));
015d06b10d37 initial
dwinter
parents:
diff changeset
194 $html .= '</div>';
015d06b10d37 initial
dwinter
parents:
diff changeset
195 if ($variables['count'] && $variables['show_counts']) {
015d06b10d37 initial
dwinter
parents:
diff changeset
196 if ($first) {
015d06b10d37 initial
dwinter
parents:
diff changeset
197 $html .= "<div class='solrsearch_autocomplete message' style='float:right'>";
015d06b10d37 initial
dwinter
parents:
diff changeset
198 $html .= t('!count results', array('!count' => $variables['count']));
015d06b10d37 initial
dwinter
parents:
diff changeset
199 $html .= "</div><br style='clear:both'>";
015d06b10d37 initial
dwinter
parents:
diff changeset
200 $first = false;
015d06b10d37 initial
dwinter
parents:
diff changeset
201 } else {
015d06b10d37 initial
dwinter
parents:
diff changeset
202 $html .= "<div class='solrsearch_autocomplete message count'>" . $variables['count'] . "</div><br style='clear:both'>";
015d06b10d37 initial
dwinter
parents:
diff changeset
203 }
015d06b10d37 initial
dwinter
parents:
diff changeset
204 }
015d06b10d37 initial
dwinter
parents:
diff changeset
205 return $html;
015d06b10d37 initial
dwinter
parents:
diff changeset
206 }
015d06b10d37 initial
dwinter
parents:
diff changeset
207
015d06b10d37 initial
dwinter
parents:
diff changeset
208 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
209 * Themes the spellchecker's suggestion.
015d06b10d37 initial
dwinter
parents:
diff changeset
210 */
015d06b10d37 initial
dwinter
parents:
diff changeset
211 function theme_solrsearch_autocomplete_spellcheck($variables) {
015d06b10d37 initial
dwinter
parents:
diff changeset
212 return '<span class="solrsearch_autocomplete message">' . t('Did you mean') .':</span> ' . $variables['suggestion'];
015d06b10d37 initial
dwinter
parents:
diff changeset
213 }
015d06b10d37 initial
dwinter
parents:
diff changeset
214
015d06b10d37 initial
dwinter
parents:
diff changeset
215 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
216 * Return the basic set of parameters for the Solr query.
015d06b10d37 initial
dwinter
parents:
diff changeset
217 *
015d06b10d37 initial
dwinter
parents:
diff changeset
218 * @param $suggestions_to_return
015d06b10d37 initial
dwinter
parents:
diff changeset
219 * Number of facets to return.
015d06b10d37 initial
dwinter
parents:
diff changeset
220 * @return array
015d06b10d37 initial
dwinter
parents:
diff changeset
221 */
015d06b10d37 initial
dwinter
parents:
diff changeset
222 function solrsearch_autocomplete_basic_params($suggestions_to_return,$facet_name) {
015d06b10d37 initial
dwinter
parents:
diff changeset
223 return array(
015d06b10d37 initial
dwinter
parents:
diff changeset
224 'facet' => 'true',
015d06b10d37 initial
dwinter
parents:
diff changeset
225 'facet.field' => array($facet_name),
015d06b10d37 initial
dwinter
parents:
diff changeset
226 // We ask for $suggestions_to_return * 5 facets, because we want
015d06b10d37 initial
dwinter
parents:
diff changeset
227 // not-too-frequent terms (will be filtered below). 5 is just my best guess.
015d06b10d37 initial
dwinter
parents:
diff changeset
228 'facet.limit' => $suggestions_to_return * 5,
015d06b10d37 initial
dwinter
parents:
diff changeset
229 'facet.mincount' => 1,
015d06b10d37 initial
dwinter
parents:
diff changeset
230 'start' => 0,
015d06b10d37 initial
dwinter
parents:
diff changeset
231 'rows' => 0,
015d06b10d37 initial
dwinter
parents:
diff changeset
232 );
015d06b10d37 initial
dwinter
parents:
diff changeset
233 }
015d06b10d37 initial
dwinter
parents:
diff changeset
234
015d06b10d37 initial
dwinter
parents:
diff changeset
235 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
236 * Helper function that suggests ways to complete partial words.
015d06b10d37 initial
dwinter
parents:
diff changeset
237 *
015d06b10d37 initial
dwinter
parents:
diff changeset
238 * For example, if $keys = "learn", this might return suggestions like:
015d06b10d37 initial
dwinter
parents:
diff changeset
239 * learn, learning, learner, learnability.
015d06b10d37 initial
dwinter
parents:
diff changeset
240 * The suggested terms are returned in order of frequency (most frequent first).
015d06b10d37 initial
dwinter
parents:
diff changeset
241 *
015d06b10d37 initial
dwinter
parents:
diff changeset
242 */
015d06b10d37 initial
dwinter
parents:
diff changeset
243 function solrsearch_autocomplete_suggest_word_completion($keys, $suggestions_to_return = 5, $facet_name="author") {
015d06b10d37 initial
dwinter
parents:
diff changeset
244 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
245 * Split $keys into two:
015d06b10d37 initial
dwinter
parents:
diff changeset
246 * $first_part will contain all complete words (delimited by spaces). Can be empty.
015d06b10d37 initial
dwinter
parents:
diff changeset
247 * $last_part is the (assumed incomplete) last word. If this is empty, don't suggest.
015d06b10d37 initial
dwinter
parents:
diff changeset
248 * Example:
015d06b10d37 initial
dwinter
parents:
diff changeset
249 * $keys = "learning dis" : $first_part = "learning", $last_part = "dis"
015d06b10d37 initial
dwinter
parents:
diff changeset
250 */
015d06b10d37 initial
dwinter
parents:
diff changeset
251 preg_match('/^(:?(.* |))([^ ]+)$/', $keys, $matches);
015d06b10d37 initial
dwinter
parents:
diff changeset
252 $first_part = @$matches[2];
015d06b10d37 initial
dwinter
parents:
diff changeset
253 // Make sure $last_part contains meaningful characters
015d06b10d37 initial
dwinter
parents:
diff changeset
254 $last_part = preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', @$matches[3]);
015d06b10d37 initial
dwinter
parents:
diff changeset
255 if ($last_part == '') {
015d06b10d37 initial
dwinter
parents:
diff changeset
256 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
257 }
015d06b10d37 initial
dwinter
parents:
diff changeset
258 // Ask Solr to return facets that begin with $last_part; these will be the suggestions.
015d06b10d37 initial
dwinter
parents:
diff changeset
259 $params = solrsearch_autocomplete_basic_params($suggestions_to_return,$facet_name);
015d06b10d37 initial
dwinter
parents:
diff changeset
260 $params['facet.prefix'] = $last_part;
015d06b10d37 initial
dwinter
parents:
diff changeset
261 // Get array of themed suggestions.
015d06b10d37 initial
dwinter
parents:
diff changeset
262 $result = solrsearch_autocomplete_suggest($first_part, $params, 'solrsearch_autocomplete_highlight', $keys, $suggestions_to_return);
015d06b10d37 initial
dwinter
parents:
diff changeset
263 if ($result && $result['suggestions']) {
015d06b10d37 initial
dwinter
parents:
diff changeset
264 return $result['suggestions'];
015d06b10d37 initial
dwinter
parents:
diff changeset
265 } else {
015d06b10d37 initial
dwinter
parents:
diff changeset
266 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
267 }
015d06b10d37 initial
dwinter
parents:
diff changeset
268 }
015d06b10d37 initial
dwinter
parents:
diff changeset
269
015d06b10d37 initial
dwinter
parents:
diff changeset
270 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
271 * Helper function that suggests additional terms to search for.
015d06b10d37 initial
dwinter
parents:
diff changeset
272 *
015d06b10d37 initial
dwinter
parents:
diff changeset
273 * For example, if $keys = "learn", this might return suggestions like:
015d06b10d37 initial
dwinter
parents:
diff changeset
274 * learn student, learn school, learn mathematics.
015d06b10d37 initial
dwinter
parents:
diff changeset
275 * The suggested terms are returned in order of frequency (most frequent first).
015d06b10d37 initial
dwinter
parents:
diff changeset
276 */
015d06b10d37 initial
dwinter
parents:
diff changeset
277 function solrsearch_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5, $facet_name="author") {
015d06b10d37 initial
dwinter
parents:
diff changeset
278 $keys = trim($keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
279 $keys = check_plain($keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
280 if ($keys == '') {
015d06b10d37 initial
dwinter
parents:
diff changeset
281 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
282 }
015d06b10d37 initial
dwinter
parents:
diff changeset
283 // Return no suggestions when $keys consists of only word delimiters
015d06b10d37 initial
dwinter
parents:
diff changeset
284 if (drupal_strlen(preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', $keys)) < 1) {
015d06b10d37 initial
dwinter
parents:
diff changeset
285 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
286 }
015d06b10d37 initial
dwinter
parents:
diff changeset
287
015d06b10d37 initial
dwinter
parents:
diff changeset
288 // Ask Solr to return facets from the 'spell' field to use as suggestions.
015d06b10d37 initial
dwinter
parents:
diff changeset
289 $params = solrsearch_autocomplete_basic_params($suggestions_to_return,$facet_name);
015d06b10d37 initial
dwinter
parents:
diff changeset
290
015d06b10d37 initial
dwinter
parents:
diff changeset
291 // Initialize arrays
015d06b10d37 initial
dwinter
parents:
diff changeset
292 $suggestions = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
293 $replacements = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
294
015d06b10d37 initial
dwinter
parents:
diff changeset
295 // Get array of themed suggestions.
015d06b10d37 initial
dwinter
parents:
diff changeset
296 $result = solrsearch_autocomplete_suggest($keys, $params, 'solrsearch_autocomplete_highlight', $keys, $suggestions_to_return);
015d06b10d37 initial
dwinter
parents:
diff changeset
297 if ($result && solrsearch_autocomplete_variable_get_suggest_keywords()) {
015d06b10d37 initial
dwinter
parents:
diff changeset
298 if (isset($result['suggestions']) && sizeof($result['suggestions'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
299 $suggestions = array_merge($suggestions, $result['suggestions']);
015d06b10d37 initial
dwinter
parents:
diff changeset
300 }
015d06b10d37 initial
dwinter
parents:
diff changeset
301 }
015d06b10d37 initial
dwinter
parents:
diff changeset
302
015d06b10d37 initial
dwinter
parents:
diff changeset
303 // Suggest using the spellchecker
015d06b10d37 initial
dwinter
parents:
diff changeset
304 if (solrsearch_autocomplete_variable_get_suggest_spellcheck()) {
015d06b10d37 initial
dwinter
parents:
diff changeset
305 if (isset($result['response']->spellcheck) && isset($result['response']->spellcheck->suggestions)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
306 $spellcheck_suggestions = get_object_vars($result['response']->spellcheck->suggestions);
015d06b10d37 initial
dwinter
parents:
diff changeset
307 foreach($spellcheck_suggestions as $word => $value) {
015d06b10d37 initial
dwinter
parents:
diff changeset
308 $replacements[$word] = $value->suggestion[0];
015d06b10d37 initial
dwinter
parents:
diff changeset
309 }
015d06b10d37 initial
dwinter
parents:
diff changeset
310 if (count($replacements)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
311 $new_keywords = strtr($keys, $replacements);
015d06b10d37 initial
dwinter
parents:
diff changeset
312 if ($new_keywords != $keys) {
015d06b10d37 initial
dwinter
parents:
diff changeset
313 // Place spellchecker suggestion before others
015d06b10d37 initial
dwinter
parents:
diff changeset
314 $suggestions = array_merge(array('*' . $new_keywords => theme('solrsearch_autocomplete_spellcheck', array('suggestion' => $new_keywords))), $suggestions);
015d06b10d37 initial
dwinter
parents:
diff changeset
315 }
015d06b10d37 initial
dwinter
parents:
diff changeset
316 }
015d06b10d37 initial
dwinter
parents:
diff changeset
317 }
015d06b10d37 initial
dwinter
parents:
diff changeset
318 }
015d06b10d37 initial
dwinter
parents:
diff changeset
319
015d06b10d37 initial
dwinter
parents:
diff changeset
320 return $suggestions;
015d06b10d37 initial
dwinter
parents:
diff changeset
321 }
015d06b10d37 initial
dwinter
parents:
diff changeset
322
015d06b10d37 initial
dwinter
parents:
diff changeset
323
015d06b10d37 initial
dwinter
parents:
diff changeset
324 function solrsearch_autocomplete_suggest($keys, $params, $theme_callback, $orig_keys, $suggestions_to_return = 5) {
015d06b10d37 initial
dwinter
parents:
diff changeset
325 $matches = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
326 $suggestions = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
327 $keys = trim($keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
328 $show_counts = solrsearch_autocomplete_variable_get_counts();
015d06b10d37 initial
dwinter
parents:
diff changeset
329
015d06b10d37 initial
dwinter
parents:
diff changeset
330 // We need the keys array to make sure we don't suggest words that are already
015d06b10d37 initial
dwinter
parents:
diff changeset
331 // in the search terms.
015d06b10d37 initial
dwinter
parents:
diff changeset
332 $keys_array = explode(' ', $keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
333 $keys_array = array_filter($keys_array);
015d06b10d37 initial
dwinter
parents:
diff changeset
334
015d06b10d37 initial
dwinter
parents:
diff changeset
335 // Query Solr for $keys so that suggestions will always return results.
015d06b10d37 initial
dwinter
parents:
diff changeset
336 $query = solrsearch_drupal_query($keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
337
015d06b10d37 initial
dwinter
parents:
diff changeset
338 // This hook allows modules to modify the query and params objects.
015d06b10d37 initial
dwinter
parents:
diff changeset
339 drupal_alter('solrsearch_query', $query);
015d06b10d37 initial
dwinter
parents:
diff changeset
340 if (!$query) {
015d06b10d37 initial
dwinter
parents:
diff changeset
341 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
342 }
015d06b10d37 initial
dwinter
parents:
diff changeset
343 solrsearch_search_add_spellcheck_params($query);
015d06b10d37 initial
dwinter
parents:
diff changeset
344 foreach ($params as $param => $paramValue) {
015d06b10d37 initial
dwinter
parents:
diff changeset
345 $query->addParam($param, $paramValue);
015d06b10d37 initial
dwinter
parents:
diff changeset
346 }
015d06b10d37 initial
dwinter
parents:
diff changeset
347 solrsearch_search_add_boost_params($query);
015d06b10d37 initial
dwinter
parents:
diff changeset
348
015d06b10d37 initial
dwinter
parents:
diff changeset
349 // Query Solr
015d06b10d37 initial
dwinter
parents:
diff changeset
350 $response = $query->search($keys);
015d06b10d37 initial
dwinter
parents:
diff changeset
351 // Loop through requested fields and get suggestions.
015d06b10d37 initial
dwinter
parents:
diff changeset
352 foreach ($params['facet.field'] as $field) {
015d06b10d37 initial
dwinter
parents:
diff changeset
353 foreach ($response->facet_counts->facet_fields->{$field} as $terms => $count) {
015d06b10d37 initial
dwinter
parents:
diff changeset
354
015d06b10d37 initial
dwinter
parents:
diff changeset
355
015d06b10d37 initial
dwinter
parents:
diff changeset
356
015d06b10d37 initial
dwinter
parents:
diff changeset
357 $terms = preg_replace('/[_-]+/', ' ', $terms);
015d06b10d37 initial
dwinter
parents:
diff changeset
358 $term=$terms;
015d06b10d37 initial
dwinter
parents:
diff changeset
359 if ($term) {
015d06b10d37 initial
dwinter
parents:
diff changeset
360
015d06b10d37 initial
dwinter
parents:
diff changeset
361 if (isset($matches[$term])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
362 $matches[$term] += $count;
015d06b10d37 initial
dwinter
parents:
diff changeset
363 }
015d06b10d37 initial
dwinter
parents:
diff changeset
364 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
365 $matches[$term] = $count;
015d06b10d37 initial
dwinter
parents:
diff changeset
366 }
015d06b10d37 initial
dwinter
parents:
diff changeset
367 }
015d06b10d37 initial
dwinter
parents:
diff changeset
368
015d06b10d37 initial
dwinter
parents:
diff changeset
369 }
015d06b10d37 initial
dwinter
parents:
diff changeset
370 }
015d06b10d37 initial
dwinter
parents:
diff changeset
371
015d06b10d37 initial
dwinter
parents:
diff changeset
372 if (sizeof($matches) > 0) {
015d06b10d37 initial
dwinter
parents:
diff changeset
373 // Eliminate suggestions that are stopwords or are already in the query.
015d06b10d37 initial
dwinter
parents:
diff changeset
374 $matches_clone = $matches;
015d06b10d37 initial
dwinter
parents:
diff changeset
375 $stopwords = solrsearch_autocomplete_get_stopwords();
015d06b10d37 initial
dwinter
parents:
diff changeset
376 foreach ($matches_clone as $term => $count) {
015d06b10d37 initial
dwinter
parents:
diff changeset
377 if ((strlen($term) > 3) && !in_array($term, $stopwords) && !array_search($term, $keys_array)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
378 // Longer strings get higher ratings.
015d06b10d37 initial
dwinter
parents:
diff changeset
379 #$matches_clone[$term] += strlen($term);
015d06b10d37 initial
dwinter
parents:
diff changeset
380 }
015d06b10d37 initial
dwinter
parents:
diff changeset
381 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
382 unset($matches_clone[$term]);
015d06b10d37 initial
dwinter
parents:
diff changeset
383 unset($matches[$term]);
015d06b10d37 initial
dwinter
parents:
diff changeset
384 }
015d06b10d37 initial
dwinter
parents:
diff changeset
385 }
015d06b10d37 initial
dwinter
parents:
diff changeset
386
015d06b10d37 initial
dwinter
parents:
diff changeset
387 // Don't suggest terms that are too frequent (in >90% of results).
015d06b10d37 initial
dwinter
parents:
diff changeset
388 $max_occurence = $response->response->numFound * 0.90;
015d06b10d37 initial
dwinter
parents:
diff changeset
389 foreach ($matches_clone as $match => $count) {
015d06b10d37 initial
dwinter
parents:
diff changeset
390 if ($count > $max_occurence) {
015d06b10d37 initial
dwinter
parents:
diff changeset
391 unset($matches_clone[$match]);
015d06b10d37 initial
dwinter
parents:
diff changeset
392 }
015d06b10d37 initial
dwinter
parents:
diff changeset
393 }
015d06b10d37 initial
dwinter
parents:
diff changeset
394
015d06b10d37 initial
dwinter
parents:
diff changeset
395 // The $count in this array is actually a score. We want the highest ones first.
015d06b10d37 initial
dwinter
parents:
diff changeset
396 arsort($matches_clone);
015d06b10d37 initial
dwinter
parents:
diff changeset
397
015d06b10d37 initial
dwinter
parents:
diff changeset
398 // Shorten the array to the right ones.
015d06b10d37 initial
dwinter
parents:
diff changeset
399 $matches_clone = array_slice($matches_clone, 0, $suggestions_to_return, TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
400
015d06b10d37 initial
dwinter
parents:
diff changeset
401 // Add current search as suggestion if results > 0
015d06b10d37 initial
dwinter
parents:
diff changeset
402 if ($response->response->numFound > 0 && $keys != '') {
015d06b10d37 initial
dwinter
parents:
diff changeset
403 // Add * to array element key to force into a string, else PHP will
015d06b10d37 initial
dwinter
parents:
diff changeset
404 // renumber keys that look like numbers on the returned array.
015d06b10d37 initial
dwinter
parents:
diff changeset
405 $suggestions['*' . $keys] = theme('solrsearch_autocomplete_highlight', array('keys' => $keys, 'suggestion' => $keys, 'count' => $response->response->numFound, 'show_counts' => $show_counts));
015d06b10d37 initial
dwinter
parents:
diff changeset
406 }
015d06b10d37 initial
dwinter
parents:
diff changeset
407
015d06b10d37 initial
dwinter
parents:
diff changeset
408 // Build suggestions using returned facets
015d06b10d37 initial
dwinter
parents:
diff changeset
409 foreach ($matches_clone as $match => $count) {
015d06b10d37 initial
dwinter
parents:
diff changeset
410 if ($keys != $match) {
015d06b10d37 initial
dwinter
parents:
diff changeset
411 $suggestion = trim($keys . ' ' . $match);
015d06b10d37 initial
dwinter
parents:
diff changeset
412 // On cases where there are more than 3 keywords, omit displaying
015d06b10d37 initial
dwinter
parents:
diff changeset
413 // the count because of the mm settings in solrconfig.xml
015d06b10d37 initial
dwinter
parents:
diff changeset
414 if (substr_count($suggestion, ' ') >= 2) {
015d06b10d37 initial
dwinter
parents:
diff changeset
415 $count = 0;
015d06b10d37 initial
dwinter
parents:
diff changeset
416 }
015d06b10d37 initial
dwinter
parents:
diff changeset
417 if ($suggestion != '') {
015d06b10d37 initial
dwinter
parents:
diff changeset
418 // Add * to array element key to force into a string, else PHP will
015d06b10d37 initial
dwinter
parents:
diff changeset
419 // renumber keys that look like numbers on the returned array.
015d06b10d37 initial
dwinter
parents:
diff changeset
420 $suggestions['*' . $suggestion] = theme('solrsearch_autocomplete_highlight', array('keys' => $orig_keys, 'suggestion' => $suggestion, 'count' => $count, 'show_counts' => $show_counts));
015d06b10d37 initial
dwinter
parents:
diff changeset
421 }
015d06b10d37 initial
dwinter
parents:
diff changeset
422 }
015d06b10d37 initial
dwinter
parents:
diff changeset
423 }
015d06b10d37 initial
dwinter
parents:
diff changeset
424 }
015d06b10d37 initial
dwinter
parents:
diff changeset
425
015d06b10d37 initial
dwinter
parents:
diff changeset
426 return array(
015d06b10d37 initial
dwinter
parents:
diff changeset
427 'suggestions' => $suggestions,
015d06b10d37 initial
dwinter
parents:
diff changeset
428 'response' => &$response
015d06b10d37 initial
dwinter
parents:
diff changeset
429 );
015d06b10d37 initial
dwinter
parents:
diff changeset
430 }
015d06b10d37 initial
dwinter
parents:
diff changeset
431
015d06b10d37 initial
dwinter
parents:
diff changeset
432 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
433 * Gets the current stopwords list configured in Solr.
015d06b10d37 initial
dwinter
parents:
diff changeset
434 */
015d06b10d37 initial
dwinter
parents:
diff changeset
435 function solrsearch_autocomplete_get_stopwords() {
015d06b10d37 initial
dwinter
parents:
diff changeset
436 static $words = array(), $flag = false;
015d06b10d37 initial
dwinter
parents:
diff changeset
437 if ($flag) {
015d06b10d37 initial
dwinter
parents:
diff changeset
438 return $words;
015d06b10d37 initial
dwinter
parents:
diff changeset
439 }
015d06b10d37 initial
dwinter
parents:
diff changeset
440 $stopwords_url = "/admin/file/?file=stopwords.txt";
015d06b10d37 initial
dwinter
parents:
diff changeset
441 $host = variable_get('solrsearch_host', 'localhost');
015d06b10d37 initial
dwinter
parents:
diff changeset
442 $port = variable_get('solrsearch_port', 8983);
015d06b10d37 initial
dwinter
parents:
diff changeset
443 $path = variable_get('solrsearch_path', '/solr');
015d06b10d37 initial
dwinter
parents:
diff changeset
444 $url = "http://{$host}:{$port}{$path}{$stopwords_url}";
015d06b10d37 initial
dwinter
parents:
diff changeset
445 $result = drupal_http_request($url);
015d06b10d37 initial
dwinter
parents:
diff changeset
446 if ($result->code != 200) {
015d06b10d37 initial
dwinter
parents:
diff changeset
447 return array();
015d06b10d37 initial
dwinter
parents:
diff changeset
448 }
015d06b10d37 initial
dwinter
parents:
diff changeset
449 $words = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
450 foreach (explode("\n", $result->data) as $line) {
015d06b10d37 initial
dwinter
parents:
diff changeset
451 if (drupal_substr($line, 0, 1) == "#") {
015d06b10d37 initial
dwinter
parents:
diff changeset
452 continue;
015d06b10d37 initial
dwinter
parents:
diff changeset
453 }
015d06b10d37 initial
dwinter
parents:
diff changeset
454 if ($word = trim($line)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
455 $words[] = $word;
015d06b10d37 initial
dwinter
parents:
diff changeset
456 }
015d06b10d37 initial
dwinter
parents:
diff changeset
457 }
015d06b10d37 initial
dwinter
parents:
diff changeset
458 $flag = true;
015d06b10d37 initial
dwinter
parents:
diff changeset
459 return $words;
015d06b10d37 initial
dwinter
parents:
diff changeset
460 }
015d06b10d37 initial
dwinter
parents:
diff changeset
461
015d06b10d37 initial
dwinter
parents:
diff changeset
462 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
463 * Wrapper around variable_get() for variable solrsearch_autocomplete_widget.
015d06b10d37 initial
dwinter
parents:
diff changeset
464 */
015d06b10d37 initial
dwinter
parents:
diff changeset
465 function solrsearch_autocomplete_variable_get_widget() {
015d06b10d37 initial
dwinter
parents:
diff changeset
466 return variable_get('solrsearch_autocomplete_widget', 'custom');
015d06b10d37 initial
dwinter
parents:
diff changeset
467 }
015d06b10d37 initial
dwinter
parents:
diff changeset
468
015d06b10d37 initial
dwinter
parents:
diff changeset
469 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
470 * Wrapper around variable_get() for variable solrsearch_autocomplete_suggest_keywords.
015d06b10d37 initial
dwinter
parents:
diff changeset
471 */
015d06b10d37 initial
dwinter
parents:
diff changeset
472 function solrsearch_autocomplete_variable_get_suggest_keywords() {
015d06b10d37 initial
dwinter
parents:
diff changeset
473 return variable_get('solrsearch_autocomplete_suggest_keywords', 1);
015d06b10d37 initial
dwinter
parents:
diff changeset
474 }
015d06b10d37 initial
dwinter
parents:
diff changeset
475
015d06b10d37 initial
dwinter
parents:
diff changeset
476 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
477 * Wrapper around variable_get() for variable solrsearch_autocomplete_suggest_spellcheck.
015d06b10d37 initial
dwinter
parents:
diff changeset
478 */
015d06b10d37 initial
dwinter
parents:
diff changeset
479 function solrsearch_autocomplete_variable_get_suggest_spellcheck() {
015d06b10d37 initial
dwinter
parents:
diff changeset
480 return variable_get('solrsearch_autocomplete_suggest_spellcheck', 1);
015d06b10d37 initial
dwinter
parents:
diff changeset
481 }
015d06b10d37 initial
dwinter
parents:
diff changeset
482
015d06b10d37 initial
dwinter
parents:
diff changeset
483 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
484 * Wrapper around variable_get() for variable solrsearch_autocomplete_counts.
015d06b10d37 initial
dwinter
parents:
diff changeset
485 */
015d06b10d37 initial
dwinter
parents:
diff changeset
486 function solrsearch_autocomplete_variable_get_counts() {
015d06b10d37 initial
dwinter
parents:
diff changeset
487 return variable_get('solrsearch_autocomplete_counts', TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
488 }
015d06b10d37 initial
dwinter
parents:
diff changeset
489
015d06b10d37 initial
dwinter
parents:
diff changeset
490 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
491 * Alter the solrsearch.module "advanced settings" form.
015d06b10d37 initial
dwinter
parents:
diff changeset
492 */
015d06b10d37 initial
dwinter
parents:
diff changeset
493 function solrsearch_autocomplete_form_solrsearch_settings_alter(&$form, $form_state) {
015d06b10d37 initial
dwinter
parents:
diff changeset
494 $form['advanced']['solrsearch_autocomplete_widget'] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
495 '#type' => 'radios',
015d06b10d37 initial
dwinter
parents:
diff changeset
496 '#title' => t('Autocomplete widget to use'),
015d06b10d37 initial
dwinter
parents:
diff changeset
497 '#description' => t('The custom widget provides instant search upon selection, whereas the Drupal widget needs the user to hit Enter or click on the Search button. If you are having problems, try switching to the default Drupal autocomplete widget.'),
015d06b10d37 initial
dwinter
parents:
diff changeset
498 '#options' => array('custom' => t('Custom autocomplete widget'), 'drupal' => t('Drupal core autocomplete widget')),
015d06b10d37 initial
dwinter
parents:
diff changeset
499 '#default_value' => solrsearch_autocomplete_variable_get_widget(),
015d06b10d37 initial
dwinter
parents:
diff changeset
500 );
015d06b10d37 initial
dwinter
parents:
diff changeset
501 $form['advanced']['solrsearch_autocomplete_suggest_keywords'] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
502 '#type' => 'checkbox',
015d06b10d37 initial
dwinter
parents:
diff changeset
503 '#title' => t('Enable additional keyword suggestions on the autocomplete widget'),
015d06b10d37 initial
dwinter
parents:
diff changeset
504 '#description' => t('Suggest words to add to the currently typed-in words. E.g.: typing "blue" might suggest "blue bike" or "blue shirt".'),
015d06b10d37 initial
dwinter
parents:
diff changeset
505 '#default_value' => solrsearch_autocomplete_variable_get_suggest_keywords(),
015d06b10d37 initial
dwinter
parents:
diff changeset
506 );
015d06b10d37 initial
dwinter
parents:
diff changeset
507 $form['advanced']['solrsearch_autocomplete_suggest_spellcheck'] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
508 '#type' => 'checkbox',
015d06b10d37 initial
dwinter
parents:
diff changeset
509 '#title' => t('Enable spellchecker suggestions on the autocomplete widget'),
015d06b10d37 initial
dwinter
parents:
diff changeset
510 '#description' => t('Suggest corrections to the currently typed-in words. E.g.: typing "rec" or "redd" might suggest "red".'),
015d06b10d37 initial
dwinter
parents:
diff changeset
511 '#default_value' => solrsearch_autocomplete_variable_get_suggest_spellcheck(),
015d06b10d37 initial
dwinter
parents:
diff changeset
512 );
015d06b10d37 initial
dwinter
parents:
diff changeset
513 $form['advanced']['solrsearch_autocomplete_counts'] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
514 '#type' => 'checkbox',
015d06b10d37 initial
dwinter
parents:
diff changeset
515 '#title' => t('Enable counts in autocomplete widget suggestions'),
015d06b10d37 initial
dwinter
parents:
diff changeset
516 '#description' => t('WARNING: Counts shown alongside suggestions might be lower than the actual result count due to stemming and minimum match (mm) settings in solrconfig.xml.'),
015d06b10d37 initial
dwinter
parents:
diff changeset
517 '#default_value' => solrsearch_autocomplete_variable_get_counts(),
015d06b10d37 initial
dwinter
parents:
diff changeset
518 );
015d06b10d37 initial
dwinter
parents:
diff changeset
519 }