function search_autocomplete_init in Search Autocomplete 7.3
Same name and namespace in other branches
- 6.4 search_autocomplete.module \search_autocomplete_init()
- 6.2 search_autocomplete.module \search_autocomplete_init()
- 7.2 search_autocomplete.module \search_autocomplete_init()
Implements hook_init(). Add autocomplete.js on everypage.
File
- ./
search_autocomplete.module, line 23
Code
function search_autocomplete_init() {
// checkout if user have authorization to access the autocompleted form
if (user_access('use Search Autocomplete')) {
// init:
$settings = array();
// checkout if the db exists (it should)
if (db_table_exists('search_autocomplete_forms')) {
// get every form to autocomplete
$result = db_query('SELECT * FROM {search_autocomplete_forms} WHERE enabled=1');
// build the setting array to transfert to JS
foreach ($result as $match) {
$form_id = 'form' . $match->fid;
$input_data = explode("\n", $match->data_static);
$data_static = array();
for ($i = 0; $i < count($input_data); $i++) {
$cut = strripos($input_data[$i], '=>');
$object = array();
if ($cut > 0) {
$object['label'] = trim(substr($input_data[$i], 0, $cut));
$object['value'] = trim(substr($input_data[$i], 0, $cut));
$object['link'] = trim(substr($input_data[$i], $cut + 2, strlen($input_data[$i])));
}
else {
$object['label'] = trim($input_data[$i]);
$object['value'] = trim($input_data[$i]);
}
$data_static[] = $object;
}
$theme_id = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $match->theme);
$data_source = $match->data_callback;
if ($match->data_source == 1 && !url_is_external($match->data_callback)) {
$data_source = urldecode(url($match->data_callback, array(
'absolute' => TRUE,
)));
}
drupal_add_js(array(
'search_autocomplete' => array(
$form_id => array(
'selector' => $match->selector,
'minChars' => $match->min_char,
'max_sug' => $match->max_sug,
'no_results' => $match->no_results,
'type' => $match->data_source,
'datas' => $match->data_source > 1 ? $data_static : $data_source,
'fid' => $match->fid,
'theme' => str_replace(' ', '-', strtolower($theme_id)),
// get the css filename with '-' instead of ' ', lower case and no '.css'
'auto_submit' => $match->auto_submit,
'auto_redirect' => $match->auto_redirect,
),
),
), 'setting');
drupal_add_css(drupal_get_path('module', 'search_autocomplete') . '/css/' . $match->theme);
}
// If there is some results: need to include the css and js....
if ($result) {
drupal_add_library('system', 'ui');
drupal_add_library('system', 'ui.widget');
drupal_add_library('system', 'ui.position');
drupal_add_library('system', 'ui.autocomplete');
drupal_add_js(drupal_get_path('module', 'search_autocomplete') . '/js/jquery.autocomplete.js');
}
}
}
if (isset($_GET['sort_order'])) {
$_GET['sort_order'] = drupal_strtoupper($_GET['sort_order']);
}
}