You are here

function _google_appliance_search_initialise in Google Search Appliance 6.2

Initialise the search object

1 call to _google_appliance_search_initialise()
google_appliance_search in ./google_appliance.module
Implementation of hook_search().

File

./google_appliance.module, line 881
Google Search Appliance (GSA) / Google Mini integration

Code

function _google_appliance_search_initialise(&$gm, $client = NULL, $collection = NULL, $keys = NULL, $options = array()) {
  try {
    $gm
      ->setOutputEncoding('utf8');
    $gm
      ->setInputEncoding('utf8');
    $gm
      ->setMetaDataRequested('*');

    // Establish the Google Appliance host name / IP address, or abort.
    $google_appliance_host = variable_get('google_appliance_host_name', FALSE);
    if (!$google_appliance_host) {
      $google_appliance_name = variable_get('google_appliance_name', GOOGLE_APPLIANCE_NAME_DEFAULT);
      drupal_set_message(t('No host name has been configured for the search appliance. Please enter it on the !admin_settings_page', array(
        '!admin_settings_page' => l($google_appliance_name . ' settings page', 'admin/settings/search/google_appliance'),
      )), 'error');
      return FALSE;
    }
    $gm->baseUrl = $google_appliance_host . "/search";
    $gm->collection = $collection ? $collection : variable_get('google_appliance_default_collection', '');
    $gm
      ->setQueryPart('client', $client ? $client : variable_get('google_appliance_default_client', ''));

    // Prevent the GSA from omitting 'similar' results.
    // This is pretty much necessary if we want our paging to be
    // accurate, as the totalResults value is incorrect otherwise.
    $gm
      ->setQueryPart('filter', 0);

    // Add any additional QueryPart options
    foreach ($options as $opt_name => $opt_value) {
      $gm
        ->setQueryPart($opt_name, $opt_value);
    }

    // Set page and results-per-page
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    $limit = variable_get('google_appliance_limit_per_page', GOOGLE_APPLIANCE_RESULTS_PER_PAGE_DEFAULT);
    $gm
      ->setPageAndResultsPerPage($page, $limit);

    // set search parameters
    $gm
      ->setKeywords($keys);
    if (module_exists('i18n')) {
      if ($lang = i18n_get_lang()) {
        $gm
          ->setLanguageFilter(array(
          $lang,
        ));
      }
    }
  } catch (GoogleMiniCriteriaException $e) {
    $code = $e
      ->getCode();
    if ($message = variable_get('google_appliance_errorcode_' . $code, '')) {
      $user_message = $message;
    }
    else {
      $user_message = GoogleMiniException::getUserMessage($code);
    }
    $error_message = $e
      ->getMessage();
    if ($code > 0) {
      $output .= "<h2>" . $user_message . "</h2>";
      return $output;
    }
    else {
      watchdog('google_appliance', $error_message);
      drupal_set_message($error_message, 'error');
    }
  }
}