| 1 |
<?php
|
| 2 |
// $Id: livesearch.module,v 1.18 2008/01/20 19:49:54 kourge Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function livesearch_menu($may_cache) {
|
| 8 |
static $settings_injected;
|
| 9 |
$items = array();
|
| 10 |
$path = drupal_get_path('module', 'livesearch');
|
| 11 |
|
| 12 |
if (user_access('search content')) {
|
| 13 |
drupal_add_js($path .'/livesearch.js');
|
| 14 |
drupal_add_css($path .'/livesearch.css');
|
| 15 |
drupal_add_css(drupal_get_path('module', 'search') .'/search.css');
|
| 16 |
|
| 17 |
if (!$settings_injected) {
|
| 18 |
$search_results_location = variable_get('livesearch_search_results_location', 'after_search_box');
|
| 19 |
$settings = array('liveSearch' => array(
|
| 20 |
'targetSearchBox' => variable_get('livesearch_target_search_box', 'auto'),
|
| 21 |
'compactSearchBox' => variable_get('livesearch_compact_search_box', TRUE),
|
| 22 |
'delayDuration' => variable_get('livesearch_delay_duration', 1250),
|
| 23 |
'queryURL' => url('livesearch/node'),
|
| 24 |
'minimumWordSize' => variable_get('minimum_word_size', 3),
|
| 25 |
'useCustomElement' => ($search_results_location == 'specified_custom_element'),
|
| 26 |
'customElementId' => variable_get('livesearch_custom_search_result_element_id', ''),
|
| 27 |
'hideSnippets' => variable_get('livesearch_hide_snippets', FALSE),
|
| 28 |
'showItemInfo' => variable_get('livesearch_show_item_info', TRUE),
|
| 29 |
'scrollToResults' => variable_get('livesearch_scroll_to_search_result', TRUE),
|
| 30 |
'searchAllowed' => TRUE
|
| 31 |
));
|
| 32 |
|
| 33 |
drupal_add_js($settings, 'setting');
|
| 34 |
$settings_injected = TRUE;
|
| 35 |
}
|
| 36 |
} else {
|
| 37 |
if (!$settings_injected) {
|
| 38 |
$settings = array('liveSearch' => array('searchAllowed' => FALSE));
|
| 39 |
drupal_add_js($settings, 'setting');
|
| 40 |
$settings_injected = TRUE;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
if (!$may_cache) {
|
| 45 |
$items[] = array('path' => 'livesearch', 'title' => t('search'),
|
| 46 |
'callback' => 'livesearch_view',
|
| 47 |
'access' => user_access('search content'),
|
| 48 |
'type' => MENU_CALLBACK);
|
| 49 |
$items[] = array('path' => 'admin/settings/livesearch',
|
| 50 |
'title' => t('Live Search settings'),
|
| 51 |
'description' => t('Configure live search options.'),
|
| 52 |
'callback' => 'drupal_get_form',
|
| 53 |
'callback arguments' => array('livesearch_admin_settings'),
|
| 54 |
'access' => user_access('administer search'),
|
| 55 |
'type' => MENU_NORMAL_ITEM);
|
| 56 |
}
|
| 57 |
|
| 58 |
return $items;
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Return barebone search results.
|
| 63 |
*/
|
| 64 |
function livesearch_view() {
|
| 65 |
$keys = search_get_keys();
|
| 66 |
$type = arg(1);
|
| 67 |
|
| 68 |
// Search only if there's actually something.
|
| 69 |
if (trim($keys)){
|
| 70 |
// Log the search keys:
|
| 71 |
watchdog('search', t('%keys (@type).', array(
|
| 72 |
'%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')
|
| 73 |
)), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
|
| 74 |
|
| 75 |
// Collect the search results:
|
| 76 |
$results = search_data($keys);
|
| 77 |
|
| 78 |
if ($results) {
|
| 79 |
$results = array(
|
| 80 |
'found' => TRUE,
|
| 81 |
'results' => $results
|
| 82 |
);
|
| 83 |
}
|
| 84 |
else {
|
| 85 |
$results = array(
|
| 86 |
'found' => FALSE,
|
| 87 |
'message' => t('Your search yielded no results')
|
| 88 |
);
|
| 89 |
}
|
| 90 |
|
| 91 |
// Output the bare results.
|
| 92 |
print drupal_to_js($results);
|
| 93 |
exit;
|
| 94 |
}
|
| 95 |
return;
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Menu callback; displays the livesearch module settings page.
|
| 100 |
*/
|
| 101 |
function livesearch_admin_settings() {
|
| 102 |
$form = array();
|
| 103 |
|
| 104 |
$form['appearance'] = array(
|
| 105 |
'#type' => 'fieldset',
|
| 106 |
'#title' => t('Appearance'),
|
| 107 |
);
|
| 108 |
$form['appearance']['livesearch_target_search_box'] = array(
|
| 109 |
'#type' => 'radios',
|
| 110 |
'#title' => t('Target search box'),
|
| 111 |
'#default_value' => variable_get('livesearch_target_search_box', 'auto'),
|
| 112 |
'#options' => array(
|
| 113 |
'auto' => t('Automatically detect'),
|
| 114 |
'block' => t('Block'),
|
| 115 |
'theme' => t('Theme')
|
| 116 |
),
|
| 117 |
'#description' => t('The targeted search box will provide live search functionality. With "Automatically detect," Live Search would first try the block search box, then the theme search box, after that, other common names, and finally fail silently if none is found.')
|
| 118 |
);
|
| 119 |
$form['appearance']['livesearch_compact_search_box'] = array(
|
| 120 |
'#type' => 'checkbox',
|
| 121 |
'#title' => t('Compact search box'),
|
| 122 |
'#default_value' => variable_get('livesearch_compact_search_box', TRUE),
|
| 123 |
'#description' => t('This will place the word Search in the search box. The word Search will disappear once the box gains focus and will appear again if the box is empty when it loses focus, like Apple-style search boxes. This will also add a "compact" class to the search form. The default Live Search stylesheet hides the Search button and lengthens the search box when this option is enabled. How the form is ultimately styled in this condition is completely up to the theme applied. It is recommended that the Search block title to be hidden as well to eliminate redundancy, although the default Live Search stylesheet does not do this.')
|
| 124 |
);
|
| 125 |
|
| 126 |
$form['appearance']['search_results'] = array(
|
| 127 |
'#type' => 'fieldset',
|
| 128 |
'#title' => t('Search results')
|
| 129 |
);
|
| 130 |
$form['appearance']['search_results']['livesearch_search_results_location'] = array(
|
| 131 |
'#type' => 'radios',
|
| 132 |
'#title' => t('Location'),
|
| 133 |
'#default_value' => variable_get('livesearch_search_results_location', 'after_search_box'),
|
| 134 |
'#options' => array(
|
| 135 |
'after_search_box' => t('After search box'),
|
| 136 |
'specified_custom_element' => t('Use specified custom element')
|
| 137 |
),
|
| 138 |
'#description' => t('Specify where the search results will be injected into. By default, it will be injected in a div element with the id "live-search-results" right after the search box. You can also manually specify which element to inject the search results in by supplying its id. This will allow more theming flexibility.')
|
| 139 |
);
|
| 140 |
|
| 141 |
$form['appearance']['search_results']['livesearch_custom_search_result_element_id'] = array(
|
| 142 |
'#type' => 'textfield',
|
| 143 |
'#title' => t('Custom element id'),
|
| 144 |
'#default_value' => variable_get('livesearch_custom_search_result_element_id', ''),
|
| 145 |
'#description' => t('Specify the id of the custom element that you want the search results injected into.')
|
| 146 |
);
|
| 147 |
$form['appearance']['search_results']['livesearch_hide_snippets'] = array(
|
| 148 |
'#type' => 'checkbox',
|
| 149 |
'#title' => t('Hide snippets'),
|
| 150 |
'#default_value' => variable_get('livesearch_hide_snippets', FALSE),
|
| 151 |
'#description' => t('A snippet is an excerpt of content indicating where the keyword occurs in an item.')
|
| 152 |
);
|
| 153 |
$form['appearance']['search_results']['livesearch_show_item_info'] = array(
|
| 154 |
'#type' => 'checkbox',
|
| 155 |
'#title' => t('Show item info'),
|
| 156 |
'#default_value' => variable_get('livesearch_show_item_info', TRUE),
|
| 157 |
'#description' => t('An item info is the string of node metadata indicating, for example, node type, author, posted date, number of comments, etc.')
|
| 158 |
);
|
| 159 |
$form['appearance']['search_results']['livesearch_scroll_to_search_result'] = array(
|
| 160 |
'#type' => 'checkbox',
|
| 161 |
'#title' => t('Scroll to the top of search results'),
|
| 162 |
'#default_value' => variable_get('livesearch_scroll_to_search_result', TRUE),
|
| 163 |
'#description' => t('When a search is completed, scroll to the top of the search result. This is particularly useful when results are paged, since the user will not have to scroll back up to the top of the results.')
|
| 164 |
);
|
| 165 |
|
| 166 |
$form['performance'] = array(
|
| 167 |
'#type' => 'fieldset',
|
| 168 |
'#title' => t('Performance'),
|
| 169 |
'#collapsible'=> TRUE
|
| 170 |
);
|
| 171 |
$form['performance']['livesearch_delay_duration'] = array(
|
| 172 |
'#type' => 'textfield',
|
| 173 |
'#title' => t('Ajax request firing delay'),
|
| 174 |
'#default_value' => variable_get('livesearch_delay_duration', 1250),
|
| 175 |
'#size' => 5,
|
| 176 |
'#maxlength' => 5,
|
| 177 |
'#description' => t('An Ajax request is fired after this amount of period when the user stops typing. This duration is measured in milliseconds. This value should be low enough so that the user does not notice a significant delay, yet high enough so that a mere pause in typing or slower typing will not fire a horde of requests. The recommended value is higher than 1000 milliseconds (1 second) but lower than 2000 milliseconds (2 seconds).')
|
| 178 |
);
|
| 179 |
|
| 180 |
return system_settings_form($form);
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Validation callback for settings page.
|
| 185 |
*/
|
| 186 |
function livesearch_admin_settings_validate($form_id, $form_values) {
|
| 187 |
if (!ctype_digit($form_values['livesearch_delay_duration'])) {
|
| 188 |
form_set_error('delay_duration', t('The firing delay duration must be a number that is an integer.'));
|
| 189 |
}
|
| 190 |
}
|