You are here

gss.module in Google Site Search 6

Same filename and directory in other branches
  1. 8 gss.module
  2. 7 gss.module

File

gss.module
View source
<?php

// $Id$

/**
* Implementation of hook_menu().
*/
function gss_menu() {
  $items = array();
  $items['admin/settings/gss'] = array(
    'title' => 'Google Site Search',
    'description' => 'Google Site Search settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gss_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function gss_theme($existing, $type, $theme, $path) {
  return array(
    'gss_result' => array(
      'arguments' => array(
        'result' => NULL,
        'module' => NULL,
      ),
      'file' => 'gss.theme.inc',
      'template' => 'gss-result',
    ),
    'gss_results' => array(
      'arguments' => array(
        'results' => NULL,
        'module' => NULL,
      ),
      'file' => 'gss.theme.inc',
      'template' => 'gss-results',
    ),
  );
}

/**
* Module settings.
*/
function gss_settings() {
  $form = array();
  $form['main'] = array(
    '#type' => 'fieldset',
    '#title' => t('Main'),
    '#weight' => -10,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['main']['gss_enabled'] = array(
    '#title' => t('Enable Google Site Search'),
    '#type' => 'checkbox',
    '#description' => t('This will show "Google Search" or custom tab in the search page. It will also make Google Site Search to default in the site search.'),
    '#default_value' => variable_get('gss_enabled', FALSE),
  );
  $form['main']['gss_api_key'] = array(
    '#title' => t('Search engine unique ID'),
    '#type' => 'textfield',
    '#description' => t('Your Search engine unique ID. If no ID is given, the module will not work and will be disabled.'),
    '#default_value' => variable_get('gss_api_key', ''),
  );
  $form['language'] = array(
    '#type' => 'fieldset',
    '#title' => t('Language'),
    '#weight' => -9,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['language']['gss_language'] = array(
    '#title' => t('Language'),
    '#type' => 'textfield',
    '#description' => t('The language to search. Write the language code with two letters.<br />Ex: for "english", input "en"; for "Chinese (Simplified)", input "zh-CN".'),
    '#default_value' => variable_get('gss_language', FALSE),
    '#size' => 5,
    '#max_length' => 5,
  );
  $form['language']['gss_default_language'] = array(
    '#title' => t('Use current site language'),
    '#type' => 'checkbox',
    '#description' => t('This setting will override the custom language above.'),
    '#default_value' => variable_get('gss_default_language', FALSE),
  );
  $form['miscellaneous'] = array(
    '#type' => 'fieldset',
    '#title' => t('Miscellaneous'),
    '#weight' => -8,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['miscellaneous']['gss_results_tab'] = array(
    '#title' => t('Search results tab name'),
    '#type' => 'textfield',
    '#maxlength' => 50,
    '#size' => 60,
    '#description' => t('Enter a custom name of the tab where search results are displayed (defaults to %google).', array(
      '%google' => t('Google Search'),
    )),
    '#default_value' => variable_get('gss_results_tab', ''),
  );
  $form['miscellaneous']['gss_page_size'] = array(
    '#title' => t('Page size'),
    '#type' => 'textfield',
    '#description' => t('Number of results to display per page.'),
    '#default_value' => variable_get('gss_page_size', 20),
    '#size' => 5,
    '#max_length' => 5,
  );
  $form['miscellaneous']['gss_pager_size'] = array(
    '#title' => t('Pager size'),
    '#type' => 'textfield',
    '#description' => t('Number of pages to show in the pager. Imput ONLY odd numbers like 5, 7 or 9 and NOT 6, 8 or 10, for example.'),
    '#default_value' => variable_get('gss_pager_size', 19),
    '#size' => 5,
    '#max_length' => 5,
  );
  $form['miscellaneous']['gss_labels'] = array(
    '#title' => t('Show labels'),
    '#type' => 'checkbox',
    '#description' => t('Let the user filter the search result by labels. <a href="@link">Click here</a> to read more about search labels.', array(
      '@link' => url('https://developers.google.com/custom-search/docs/ref_prebuiltlabels'),
    )),
    '#default_value' => variable_get('gss_labels', TRUE),
  );
  return system_settings_form($form);
}

/**
* Search results page.
*/
function gss_search_results($query) {
  global $language;

  // Include dependencies.
  module_load_include('inc', 'gss', 'includes/GoogleSiteSearch');

  // Make query safe.
  $query = check_plain($query);

  // Init GoogleSiteSearch object.
  $gss = new GoogleSiteSearch($query, variable_get('gss_api_key', ''));

  // Get current page.
  $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

  // Init results variable.
  $results = null;

  // Set the number of results per page.
  $gss
    ->SetPageSize(variable_get('gss_page_size', 20));

  // Set the number of pages to show in the pager.
  $gss
    ->SetPagerSize(variable_get('gss_pager_size', 20));

  // Set the search language
  $default_lang = variable_get('gss_default_language', FALSE);
  if ($default_lang) {
    $gss
      ->SetLanguage($language->language);
  }
  else {
    $gss
      ->SetLanguage(variable_get('gss_language', ''));
  }

  // Perform search.
  try {
    $results = $gss
      ->GetSearchResults($page);
  } catch (Exception $e) {
    watchdog('gss', $e, NULL, WATCHDOG_ERROR);
    drupal_set_message(t('Sorry, the search function is unavailable at this time.'));
    exit;
  }

  // Get search summary.
  $total_results = $gss
    ->GetTotalResults();
  $end_pos = $gss
    ->GetCurrentPage() * $gss
    ->GetPageSize();
  $start_pos = $end_pos - $gss
    ->GetPageSize() + 1;
  if ($end_pos > $total_results) {
    $end_pos = $total_results;
  }

  // Build output.
  if (!empty($results)) {
    $output = array();

    // Loop results.
    foreach ($results as $result) {
      $result['thumbnail_url'] = isset($result['thumbnail_url']) ? $result['thumbnail_url'] : '';

      //just to avoid warnings in the log if not set
      $output[] = array(
        'link' => urldecode($result['url']),
        'title' => $result['title'],
        'thumbnail_url' => $result['thumbnail_url'],
        'snippet' => $result['description'],
      );
    }
  }
  else {
    $output[] = array(
      'link' => '',
      'title' => '',
      'snippet' => t('No results'),
    );
  }

  // Get searrch head
  $output['head'] = $gss
    ->GetSearchHead();

  // Get pager
  $output['pager'] = $gss
    ->GetPager();
  return $output;
}

/**
 * Adds custom submit handler for search form.
 */
function gss_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form' || $form_id == 'search_block_form' || $form_id == 'search_theme_form') {
    $form['#submit'][] = 'gss_search_form_submit';
    $form['#attributes']['class'] = 'gss';
  }
}

/**
 * Implementation of hook_search().
 */
function gss_search($op = 'search', $keys = NULL) {
  $check = _gss_check();
  if ($check) {
    switch ($op) {
      case 'name':
        $gss_results_tab = variable_get('gss_results_tab', FALSE) ? variable_get('gss_results_tab', '') : 'Google Search';
        return $gss_results_tab;
        break;
      case 'search':
        $page = isset($_GET['page']) ? $_GET['page'] : 0;
        return gss_search_results($keys);
        break;
    }
  }
}

/**
* Submit handler.
*/
function gss_search_form_submit($form, &$form_state) {
  $check = _gss_check();
  if ($check) {
    $form_id = $form_state['values']['form_id'];
    $keys = $form_state['values'][$form_id];
    if (!$keys) {
      $keys = $form_state['values']['processed_keys'];
    }
    $form_state['redirect'] = array(
      'search/gss/' . $keys,
    );
  }
}

/**
 * Implementation of hook_search_page().
 */
function gss_search_page($results) {
  return theme('gss_results', $results);
}

/**
 * Check if Google Site Search can run.
 */
function _gss_check() {
  $gss_enabled = variable_get('gss_enabled', FALSE);
  if (!$gss_enabled) {
    return FALSE;
  }
  $gss_api_key = variable_get('gss_api_key', FALSE);
  if (!$gss_api_key) {
    variable_set('gss_enabled', 0);
    return FALSE;
  }
  return TRUE;
}

Functions

Namesort descending Description
gss_form_alter Adds custom submit handler for search form.
gss_menu Implementation of hook_menu().
gss_search Implementation of hook_search().
gss_search_form_submit Submit handler.
gss_search_page Implementation of hook_search_page().
gss_search_results Search results page.
gss_settings Module settings.
gss_theme Implements hook_theme().
_gss_check Check if Google Site Search can run.