You are here

function search_autocomplete_init in Search Autocomplete 6.2

Same name and namespace in other branches
  1. 6.4 search_autocomplete.module \search_autocomplete_init()
  2. 7.2 search_autocomplete.module \search_autocomplete_init()
  3. 7.3 search_autocomplete.module \search_autocomplete_init()

HOOK OF INIT: add autocomplete.js on everypage

File

./search_autocomplete.module, line 76

Code

function search_autocomplete_init() {
  global $base_url;
  global $language;

  // 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
      $results = db_query('SELECT * FROM {search_autocomplete_forms} WHERE enabled=1');

      // build the setting array to transfert to JS
      while ($match = db_fetch_array($results)) {
        $form_id = 'form' . $match['fid'];
        $params = array(
          ':lang_code' => $language->language,
          ':lang_prefix' => $language->prefix,
          ':lang_domain' => $language->domain,
        );
        search_autocomplete_replaceArguments($match['selector'], $params);
        drupal_add_js(array(
          'search_autocomplete' => array(
            $form_id => array(
              'selector' => $match['selector'],
              'minChars' => $match['min_char'],
              'max_sug' => $match['max_sug'],
              'url' => url('search_autocomplete/' . $match['fid']) . '/autocomplete',
              'fid' => $match['fid'],
            ),
          ),
        ), 'setting');
      }

      // If there is some results: need to include the css and js....
      if ($results) {
        drupal_add_css(drupal_get_path('module', 'search_autocomplete') . '/css/jquery.autocomplete.css');
        drupal_add_js(drupal_get_path('module', 'search_autocomplete') . '/js/jquery.autocomplete.js');
      }
    }
  }
}