You are here

google_cse.module in Google Custom Search Engine 6

Display a Google Custom Search Engine on your site.

File

google_cse.module
View source
<?php

/**
 * @file
 * Display a Google Custom Search Engine on your site.
 */

/**
 * Implementation of hook_menu().
 */
function google_cse_menu() {
  if (!module_exists('google') && variable_get('google_cse_results_display', 'here') != 'google') {
    $items['search/google'] = array(
      'access arguments' => array(
        'search Google CSE',
      ),
      'title callback' => 'google_cse_results_tab',
      'description' => 'Google Custom Search Engine',
      'page callback' => 'google_cse_results',
      'type' => MENU_LOCAL_TASK,
    );
  }
  $items['admin/settings/google_cse'] = array(
    'access arguments' => array(
      'administer site configuration',
    ),
    'title' => 'Google CSE',
    'description' => 'Configure the Google Custom Search Engine.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'google_cse_admin_settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'google_cse.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function google_cse_theme() {
  return array(
    'google_cse_results' => array(
      'arguments' => array(
        'form' => TRUE,
        'self' => FALSE,
      ),
      'file' => 'google_cse.theme.inc',
      'template' => 'google_cse_results',
    ),
    'google_cse_results_gadget' => array(
      'arguments' => array(),
      'file' => 'google_cse.theme.inc',
      'template' => 'google_cse_results_gadget',
    ),
  );
}

/**
 * Implementation of hook_block().
 */
function google_cse_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    return array(
      0 => array(
        'info' => t('Google CSE'),
        'cache' => BLOCK_NO_CACHE,
      ),
      1 => array(
        'info' => t('Google CSE results'),
        'cache' => BLOCK_NO_CACHE,
      ),
    );
  }
  elseif ($op == 'view' && user_access('search Google CSE')) {
    switch ($delta) {
      case 0:
        return array(
          'subject' => t('Search'),
          'content' => drupal_get_form('google_cse_searchbox_form'),
        );
      case 1:
        return array(
          'subject' => t('Search'),
          'content' => theme('google_cse_results', TRUE, TRUE),
        );
    }
  }
}

/**
 * Implementation of hook_forms();
 */
function google_cse_forms() {
  $forms = array();
  $forms['google_cse_searchbox_form'] = array(
    'callback' => 'google_cse_searchbox_form_builder',
    'callback arguments' => array(
      'google_cse_searchbox_form',
    ),
  );
  $forms['google_cse_results_searchbox_form'] = array(
    'callback' => 'google_cse_searchbox_form_builder',
    'callback arguments' => array(
      'google_cse_results_searchbox_form',
    ),
  );
  return $forms;
}

/**
 * Form builder for the searchbox forms.
 */
function google_cse_searchbox_form_builder(&$form_state, $form_id, $self = FALSE) {
  $form = array();

  // The default form.
  if (variable_get('google_cse_results_display', 'here') == 'here') {
    $form['#action'] = url($self ? $_GET['q'] : 'search/google');
    $cof = variable_get('google_cse_cof_here', 'FORID:11');
  }
  else {
    $form['#action'] = 'http://' . variable_get('google_cse_domain', 'www.google.com') . '/cse';
    $cof = variable_get('google_cse_cof_google', 'FORID:0');
  }
  $form['#method'] = 'get';
  $form['cx'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('google_cse_cx', ''),
  );
  $form['cof'] = array(
    '#type' => 'hidden',
    '#value' => $cof,
  );
  $form['query'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($_GET['query']) ? $_GET['query'] : '',
  );
  $form['sa'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  foreach (google_cse_advanced_settings() as $parameter => $setting) {
    $form[$parameter] = array(
      '#type' => 'hidden',
      '#value' => $setting,
    );
  }

  // And the small differences between both.
  switch ($form_id) {
    case 'google_cse_searchbox_form':
      $form['query']['#size'] = intval(variable_get('google_cse_searchbox_width', 15));
      $form['query']['#attributes']['title'] = t('Enter the terms you wish to search for.');
      break;
    case 'google_cse_results_searchbox_form':
      $form['query']['#size'] = intval(variable_get('google_cse_results_searchbox_width', 40));
      $form['query']['#title'] = t('Enter your keywords');
      if (variable_get('google_cse_results_gadget', 1)) {
        $form['sa']['#suffix'] = theme('google_cse_results_gadget');
      }
      break;
  }
  google_cse_sitesearch_form($form);
  return $form;
}

/**
 * Render the search page and custom title.
 */
function google_cse_results() {
  google_cse_results_set_title();
  return theme('google_cse_results');
}

/**
 * Return the Google CSE tab title, either a setting or a translation.
 */
function google_cse_results_tab() {
  return ($var = variable_get('google_cse_results_tab', '')) ? $var : t('Google');
}

/**
 * Return the Google CSE page title, either a setting or a translation.
 */
function google_cse_results_set_title() {
  drupal_set_title(($var = variable_get('google_cse_results_title', '')) ? check_plain($var) : t('Search'));
}

/**
 * Implementation of hook_perm().
 */
function google_cse_perm() {
  return array(
    'search Google CSE',
  );
}

/**
 * Returns an array of any advanced settings which have been set.
 */
function google_cse_advanced_settings() {
  global $language;
  $settings = array();
  foreach (array(
    'cr',
    'gl',
    'hl',
    'ie',
    'lr',
    'oe',
    'safe',
  ) as $parameter) {
    if ($setting = variable_get("google_cse_{$parameter}", '')) {
      $settings[$parameter] = $setting;
    }
  }
  if (variable_get('google_cse_locale_hl', '')) {
    $settings['hl'] = $language->language;
  }
  if (variable_get('google_cse_locale_lr', '')) {
    $settings['lr'] = 'lang_' . $language->language;
  }
  return $settings;
}

/**
 * Implementation of hook_init().
 */
function google_cse_init() {
  global $language;
  drupal_add_css(drupal_get_path('module', 'google_cse') . '/google_cse.css');
  drupal_add_js(array(
    'googleCSE' => array(
      'language' => $language->language,
    ),
  ), 'setting', 'footer');
  drupal_add_js(drupal_get_path('module', 'google_cse') . '/google_cse.js', 'module', 'footer');
}

/**
 * Returns SiteSearch options form item.
 */
function google_cse_sitesearch_form(&$form) {
  if ($sites = preg_split('/[\\n\\r]+/', variable_get('google_cse_sitesearch', ''), -1, PREG_SPLIT_NO_EMPTY)) {
    $type = count($sites) > 1 ? 'select' : 'radios';
    foreach ($sites as $site) {
      $site = preg_split('/[\\s]+/', trim($site), 2, PREG_SPLIT_NO_EMPTY);
      if ($type == 'radios') {

        // Unlike select options, we need to HTML-escape radios options (both return values and display values).
        $options[check_plain($site[0])] = isset($site[1]) ? check_plain($site[1]) : t('Search %sitesearch', array(
          '%sitesearch' => $site[0],
        ));
      }
      else {

        // Select options will be HTML-escaped for us.
        $options[$site[0]] = isset($site[1]) ? $site[1] : t('Search !sitesearch', array(
          '!sitesearch' => $site[0],
        ));
      }
    }
    $form['sitesearch'] = array(
      '#type' => $type,
      '#options' => array(
        '' => ($var = variable_get('google_cse_sitesearch_option', '')) ? $type == 'radios' ? check_plain($var) : $var : t('Search the web'),
      ) + $options,
      '#default_value' => google_cse_sitesearch_default(),
    );
    if ($type == 'select' && isset($form['sa'])) {
      $form['sa']['#weight'] = 10;
    }
  }
}

/**
 * Return SiteSearch default value.
 */
function google_cse_sitesearch_default() {
  return isset($_GET['sitesearch']) ? $_GET['sitesearch'] : (variable_get('google_cse_sitesearch_default', 0) ? array_shift(preg_split('/[\\s]+/', variable_get('google_cse_sitesearch', ''))) : '');
}

Functions

Namesort descending Description
google_cse_advanced_settings Returns an array of any advanced settings which have been set.
google_cse_block Implementation of hook_block().
google_cse_forms Implementation of hook_forms();
google_cse_init Implementation of hook_init().
google_cse_menu Implementation of hook_menu().
google_cse_perm Implementation of hook_perm().
google_cse_results Render the search page and custom title.
google_cse_results_set_title Return the Google CSE page title, either a setting or a translation.
google_cse_results_tab Return the Google CSE tab title, either a setting or a translation.
google_cse_searchbox_form_builder Form builder for the searchbox forms.
google_cse_sitesearch_default Return SiteSearch default value.
google_cse_sitesearch_form Returns SiteSearch options form item.
google_cse_theme Implementation of hook_theme().