diff --git a/home/wiki/apachesolr_autocomplete.module b/apachesolr_autocomplete.module index 67b2be3..820bafe 100755 --- a/home/wiki/apachesolr_autocomplete.module +++ b/apachesolr_autocomplete.module @@ -20,8 +20,6 @@ function apachesolr_autocomplete_init() { drupal_add_js(drupal_get_path('module', 'apachesolr_autocomplete') .'/apachesolr_autocomplete.js'); drupal_add_js(drupal_get_path('module', 'apachesolr_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.js'); drupal_add_css( drupal_get_path('module', 'apachesolr_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.css'); - // Specify path to autocomplete handler. - drupal_add_js(array('apachesolr_autocomplete' => array('path' => url('apachesolr_autocomplete'))), 'setting'); } } @@ -31,7 +29,7 @@ function apachesolr_autocomplete_init() { function apachesolr_autocomplete_form_search_form_alter(&$form, $form_state) { if ($form['module']['#value'] == 'apachesolr_search' || $form['module']['#value'] == 'apachesolr_multisitesearch') { $element = &$form['basic']['keys']; - apachesolr_autocomplete_do_alter($element); + apachesolr_autocomplete_do_alter($element, $form); } } @@ -40,7 +38,15 @@ function apachesolr_autocomplete_form_search_form_alter(&$form, $form_state) { */ function apachesolr_autocomplete_form_search_block_form_alter(&$form, $form_state) { $element = &$form['search_block_form']; - apachesolr_autocomplete_do_alter($element); + apachesolr_autocomplete_do_alter($element, $form); +} + +/** + * Implementation of hook_form_FORM_ID_alter(). + */ +function apachesolr_autocomplete_form_apachesolr_search_custom_page_search_form_alter(&$form, $form_state) { + $element = &$form['basic']['keys']; + apachesolr_autocomplete_do_alter($element, $form); } /** @@ -49,9 +55,23 @@ function apachesolr_autocomplete_form_search_block_form_alter(&$form, $form_stat * @param $element * The element to alter. Should be passed by reference so that original form * element will be altered. - * E.g.: apachesolr_autocomplete_do_alter(&$form['xyz']) + * @param $form + * The element's form. + * E.g.: apachesolr_autocomplete_do_alter(&$form['xyz'], $form) */ -function apachesolr_autocomplete_do_alter(&$element) { +function apachesolr_autocomplete_do_alter(&$element, $form) { + // Flag to make sure we only output the setting for the custom widget once. + // TODO: Note this only supports one widget per page. + static $custom_widget_setting_flag = FALSE; + + if (isset($form['#search_page'])) { + $search_page_id = $form['#search_page']['page_id']; + } + else { + $search_page_id = 0; + } + $autocomplete_path = 'apachesolr_autocomplete/' . $search_page_id; + if (apachesolr_autocomplete_variable_get_widget() == 'custom') { // Create elements if they do not exist. if (!isset($element['#attributes'])) { @@ -61,9 +81,14 @@ function apachesolr_autocomplete_do_alter(&$element) { $element['#attributes']['class'] = array(); } array_push($element['#attributes']['class'], 'apachesolr-autocomplete', 'unprocessed'); + // Specify path to autocomplete handler. + if (! $custom_widget_setting_flag) { + drupal_add_js(array('apachesolr_autocomplete' => array('path' => url($autocomplete_path))), 'setting'); + $custom_widget_setting_flag = TRUE; + } } else { - $element['#autocomplete_path'] = 'apachesolr_autocomplete'; + $element['#autocomplete_path'] = $autocomplete_path; } } @@ -73,8 +98,9 @@ function apachesolr_autocomplete_do_alter(&$element) { function apachesolr_autocomplete_menu() { $items = array(); - $items['apachesolr_autocomplete'] = array( + $items['apachesolr_autocomplete/%'] = array( 'page callback' => 'apachesolr_autocomplete_callback', + 'page arguments' => array(1), 'access callback' => 'user_access', 'access arguments' => array('search content'), 'type' => MENU_CALLBACK, @@ -84,19 +110,28 @@ function apachesolr_autocomplete_menu() { /** * Callback for url apachesolr_autocomplete/autocomplete. + * @param $search_page_id + * The ID of the search environment. * @param $keys * The user-entered query. */ -function apachesolr_autocomplete_callback($keys = '') { +function apachesolr_autocomplete_callback($search_page_id = NULL, $keys = '') { + if ($search_page_id && function_exists('apachesolr_search_page_load')) { + $search_page = apachesolr_search_page_load($search_page_id); + } + else { + $search_page = 0; + } + if (apachesolr_autocomplete_variable_get_widget() == 'custom') { // Keys for custom widget come from $_GET. $keys = $_GET['query']; } $suggestions = array(); - $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_word_completion($keys, 5)); + $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_word_completion($keys, 5, $search_page)); if (apachesolr_autocomplete_variable_get_suggest_keywords() || apachesolr_autocomplete_variable_get_suggest_spellcheck()) { - $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_additional_term($keys, 5)); + $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_additional_term($keys, 5, $search_page)); } $result = array(); @@ -177,17 +212,26 @@ function theme_apachesolr_autocomplete_spellcheck($variables) { * Number of facets to return. * @return array */ -function apachesolr_autocomplete_basic_params($suggestions_to_return) { - return array( - 'facet' => 'true', - 'facet.field' => array('spell'), - // We ask for $suggestions_to_return * 5 facets, because we want - // not-too-frequent terms (will be filtered below). 5 is just my best guess. - 'facet.limit' => $suggestions_to_return * 5, - 'facet.mincount' => 1, - 'start' => 0, - 'rows' => 0, - ); +function apachesolr_autocomplete_basic_params($suggestions_to_return, $search_page = NULL) { + // Include any settings provided by apachesolr_search, if defined. + if (!empty($search_page) && function_exists("apachesolr_search_conditions_default")) { + $params = apachesolr_search_conditions_default($search_page); + } + else { + $params = array(); + } + + $params['facet'] = 'true'; + $params['facet.field'] = array('spell'); + + // We ask for $suggestions_to_return * 5 facets, because we want + // not-too-frequent terms (will be filtered below). 5 is just my best guess. + $params['facet.limit'] = $suggestions_to_return * 5; + $params['facet.mincount'] = 1; + $params['start'] = 0; + $params['rows'] = 0; + + return $params; } /** @@ -198,7 +242,7 @@ function apachesolr_autocomplete_basic_params($suggestions_to_return) { * The suggested terms are returned in order of frequency (most frequent first). * */ -function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_return = 5) { +function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_return = 5, $search_page = NULL) { /** * Split $keys into two: * $first_part will contain all complete words (delimited by spaces). Can be empty. @@ -214,7 +258,7 @@ function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_ return array(); } // Ask Solr to return facets that begin with $last_part; these will be the suggestions. - $params = apachesolr_autocomplete_basic_params($suggestions_to_return); + $params = apachesolr_autocomplete_basic_params($suggestions_to_return, $search_page); $params['facet.prefix'] = $last_part; // Get array of themed suggestions. $result = apachesolr_autocomplete_suggest($first_part, $params, 'apachesolr_autocomplete_highlight', $keys, $suggestions_to_return); @@ -232,7 +276,7 @@ function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_ * learn student, learn school, learn mathematics. * The suggested terms are returned in order of frequency (most frequent first). */ -function apachesolr_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5) { +function apachesolr_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5, $search_page = NULL) { $keys = trim($keys); if ($keys == '') { return array(); @@ -290,10 +334,23 @@ function apachesolr_autocomplete_suggest($keys, $params, $theme_callback, $orig_ $keys_array = array_filter($keys_array); // Query Solr for $keys so that suggestions will always return results. - $query = apachesolr_drupal_query($keys); + // Check for apachesolr module version & issue correct call. + if (function_exists('apachesolr_search_page_load')) { + $query = apachesolr_drupal_query('apachesolr_autocomplete', $params); + } + else { + // Version for 6.x-1.x branch of ApacheSolr. + $query = apachesolr_drupal_query($keys); + } - // This hook allows modules to modify the query and params objects. - drupal_alter('apachesolr_query', $query); + // Allow other modules to modify query. + if (function_exists('apachesolr_modify_query')) { + // apachesolr-6.x-1.x compatibility. + apachesolr_modify_query($query, $params, 'apachesolr_autocomplete'); + } + else { + drupal_alter('apachesolr_query', $query); + } if (!$query) { return array(); }