google_cse.module in Google Custom Search Engine 7.2
Same filename and directory in other branches
Display a Google Custom Search Engine (CSE) on your site.
File
google_cse.moduleView source
<?php
/**
* @file
* Display a Google Custom Search Engine (CSE) on your site.
*/
/**
* Implements hook_help().
*/
function google_cse_help($path, $arg) {
switch ($path) {
case 'admin/help#google_cse':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Google Custom Search Engine (CSE) is an embedded search engine that can be used to search any set of one or more sites. No Google API key is required. Read more at <a href="http://www.google.com/cse/" target="blank">http://www.google.com/cse/</a>.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<p>' . t("After installing this module, activate Google CSE at <a href=\"/admin/config/search/settings\" target=\"blank\">admin/config/search/settings</a>, optionally setting it as the default search module, and configure it by entering Google's unique ID for your CSE. Once you have granted permission for one or more roles to search the Google CSE, the search page can be found at search/google, and a separate self-contained search block can also be enabled.") . '</p>';
$output .= '<h3>' . t('Blocks') . '</h3>';
$output .= '<p>' . t('The include Google CSE block can optionally be enabled at <a href="/admin/structure/block" target="blank">admin/structure/block</a>. The "Google CSE" block provides a search box and also displays the search results. After entering search terms, the user will be returned to the same page (via GET request) and the results will be displayed. Do not allow this Google CSE block to appear on the search/google page, as the search results will fail to display.') . '</p>';
$output .= '<h3>' . t('SiteSearch') . '</h3>';
$output .= '<p>' . t('In addition to the CSE functionality, SiteSearch on one or more domains or URL paths can optionally be configured. Radio buttons allow users to search on either the SiteSearch option(s) or the CSE, and searches can default to either option.') . '</p>';
$output .= '<h3>' . t('Advanced Settings') . '</h3>';
$output .= '<p>' . t("The collapsed advanced settings on the settings page provide various customizations such as country and language preferences. For example, with the Locale module enabled, the Google CSE user interface language can be selected dynamically based on the current user's language.") . '</p>';
return $output;
}
}
/**
* Implements hook_search_info().
*/
function google_cse_search_info() {
return array(
'title' => google_cse_results_tab(),
'path' => 'google',
'conditions_callback' => 'google_cse_conditions_callback',
);
}
/**
* Search conditions callback.
*/
function google_cse_conditions_callback($keys) {
$conditions = array();
return $conditions;
}
/**
* Implements hook_search_execute().
*/
function google_cse_search_execute($keys = NULL, $conditions = NULL) {
// Pass search terms to JS so they can be sent in google_cse_results.js
drupal_add_js(array(
'googleCSE' => array(
'keys' => $keys,
),
), 'setting');
if (variable_get('google_cse_use_adv', 0)) {
// Firstly, load the needed modules.
module_load_include('inc', 'google_cse', 'google_cse_adv/google_cse_adv');
// And get the google results.
$response = google_cse_adv_service($keys);
$results = google_cse_adv_response_results($response[0], $keys, $conditions);
// Allow other modules to alter the keys.
drupal_alter('google_cse_searched_keys', $keys);
// Allow other modules to alter the results.
drupal_alter('google_cse_searched_results', $results);
return $results;
}
}
/**
* Builds a query array based on Google CSE settings.
*/
function google_cse_build_query($keys, $sitesearch = NULL, $here = TRUE) {
if (!variable_get('google_cse_use_adv', 0)) {
return array(
'q' => $keys,
'cx' => variable_get('google_cse_cx', ''),
);
}
return array(
'q' => $keys,
'cx' => variable_get('google_cse_cx', ''),
'cof' => $here ? variable_get('google_cse_cof_here', 'FORID:11') : variable_get('google_cse_cof_google', 'FORID:0'),
'sitesearch' => isset($sitesearch) ? $sitesearch : google_cse_sitesearch_default(),
) + google_cse_advanced_settings();
}
/**
* Implements hook_search_page().
*/
function google_cse_search_page($results) {
if (!variable_get('google_cse_use_adv', 0)) {
$output['#theme'] = 'google_cse_results';
return $output;
}
if (!$results) {
// No results found.
$output['search_results'] = array(
'#markup' => theme('google_cse_search_noresults'),
);
return $output;
}
$current_page = 0;
if (!empty($_GET['page'])) {
$current_page = $_GET['page'];
}
$number_results = t('Results @from to @to of @total matches.', array(
'@from' => $current_page * 10 + 1,
'@to' => $current_page * 10 + 10,
'@total' => $GLOBALS['pager_total_items'][0],
));
$output['prefix']['#markup'] = $number_results . '<ol class="search-results">';
foreach ($results as $entry) {
$output[] = array(
'#theme' => 'search_result',
'#result' => $entry,
'#module' => 'google_cse',
);
}
// Important, add the pager.
$output['suffix']['#markup'] = '</ol>' . theme('pager');
return $output;
}
/**
* Implements hook_search_admin().
*/
function google_cse_search_admin() {
module_load_include('admin.inc', 'google_cse');
return google_cse_admin_settings();
}
/**
* Implements hook_search_access().
*/
function google_cse_search_access() {
return user_access('search Google CSE');
}
/**
* Implements hook_theme().
*/
function google_cse_theme($existing, $type, $theme, $path) {
return array(
'google_cse_results' => array(
'variables' => array(
'form' => FALSE,
'path' => $path,
),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_results',
),
'google_cse_adv_results' => array(
'variables' => array(
'form' => FALSE,
'path' => $path,
),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_adv/templates/google_cse_adv_results',
),
// Shows a message when the search does not return any result.
'google_cse_search_noresults' => array(
'variables' => array(),
),
);
}
/**
* Implements hook_block_info().
*/
function google_cse_block_info() {
return array(
'google_cse' => array(
'info' => t('Google CSE'),
),
);
}
/**
* Implements hook_block_view().
*/
function google_cse_block_view($delta = '') {
if (user_access('search Google CSE')) {
switch ($delta) {
case 'google_cse':
return array(
'subject' => t('Search'),
'content' => array(
'#theme' => 'google_cse_results',
'#form' => TRUE,
),
);
}
}
}
/**
* 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');
}
/**
* Implements hook_perm().
*/
function google_cse_permission() {
return array(
'search Google CSE' => array(
'title' => t('Use Google CSE search'),
),
);
}
/**
* 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;
}
/**
* Get the relevant language to use for the search.
*
* @return string
* The language.
*/
function google_cse_language() {
global $language;
return variable_get('google_cse_locale_hl', '') ? $language->language : variable_get('google_cse_hl', '');
}
/**
* Implements hook_init().
*/
function google_cse_init() {
drupal_add_js(array(
'googleCSE' => array(
'cx' => variable_get('google_cse_cx', ''),
'language' => google_cse_language(),
'resultsWidth' => intval(variable_get('google_cse_results_width', 600)),
'domain' => variable_get('google_cse_domain', 'www.google.com'),
'showWaterMark' => variable_get('google_cse_show_watermark', TRUE),
),
), 'setting');
}
/**
* Returns SiteSearch options form item.
*/
function google_cse_sitesearch_form(&$form) {
if ($options = google_cse_sitesearch_options()) {
$form['sitesearch'] = array(
'#type' => variable_get('google_cse_sitesearch_form', 'radios'),
'#options' => $options,
'#default_value' => google_cse_sitesearch_default(),
);
if ($form['sitesearch']['#type'] == 'select' && isset($form['sa'])) {
$form['sa']['#weight'] = 10;
}
}
}
/**
* Returns SiteSearch options.
*/
function google_cse_sitesearch_options() {
static $options;
if (!isset($options)) {
$options = array();
if ($sites = preg_split('/[\\n\\r]+/', variable_get('google_cse_sitesearch', ''), -1, PREG_SPLIT_NO_EMPTY)) {
$options[''] = ($var = variable_get('google_cse_sitesearch_option', '')) ? $var : t('Search the web');
foreach ($sites as $site) {
$site = preg_split('/[\\s]+/', trim($site), 2, PREG_SPLIT_NO_EMPTY);
// Select options will be HTML-escaped.
// Radio options will be XSS-filtered.
$options[$site[0]] = isset($site[1]) ? $site[1] : t('Search "@sitesearch"', array(
'@sitesearch' => $site[0],
));
}
}
}
return $options;
}
/**
* Returns SiteSearch default value.
*/
function google_cse_sitesearch_default() {
$options = google_cse_sitesearch_options();
if (isset($_GET['sitesearch']) && isset($options[$_GET['sitesearch']])) {
return $_GET['sitesearch'];
}
elseif (variable_get('google_cse_sitesearch_default', 0)) {
// Return the key of the second element in the array.
return key(array_slice($options, 1, 1));
}
return '';
}
/**
* Adds custom submit handler for search form.
*/
function google_cse_form_search_form_alter(&$form, &$form_state, $form_id) {
if ($form['module']['#value'] == 'google_cse') {
google_cse_sitesearch_form($form);
$form['#attributes']['class'][] = 'google-cse';
}
}
/**
* Adds custom submit handler for search block form.
*/
function google_cse_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$info = search_get_default_module_info();
if ($info['module'] == 'google_cse') {
google_cse_sitesearch_form($form);
$form['#attributes']['class'][] = 'google-cse';
}
}
/**
* Implements hook_proxy_settings_info().
*/
function google_cse_proxy_settings_info() {
return array(
'google_cse_adv' => array(
'name' => 'Google Custom Search Engine',
),
);
}
/**
* Brief message to display when no results match the query.
*
* @see search_help()
*/
function theme_google_cse_search_noresults() {
return t('<h2>Sorry there were no results matching your enquiry.</h2>
<ul>
<li>Check the spelling of your keywords</li>
<li>Try a more specific enquiry (e.g. <em>"Penny Black"</em> instead of <em>"Stamps"</em>): "blue drop"</em></li>
<li>Be explicit (e.g. <em>"Second class stamp"</em> instead of <em>"Stamp"</em>)</li>
<li>Include spaces between keywords</li>
</ul>');
}
Functions
Name | Description |
---|---|
google_cse_advanced_settings | Returns an array of any advanced settings which have been set. |
google_cse_block_info | Implements hook_block_info(). |
google_cse_block_view | Implements hook_block_view(). |
google_cse_build_query | Builds a query array based on Google CSE settings. |
google_cse_conditions_callback | Search conditions callback. |
google_cse_form_search_block_form_alter | Adds custom submit handler for search block form. |
google_cse_form_search_form_alter | Adds custom submit handler for search form. |
google_cse_help | Implements hook_help(). |
google_cse_init | Implements hook_init(). |
google_cse_language | Get the relevant language to use for the search. |
google_cse_permission | Implements hook_perm(). |
google_cse_proxy_settings_info | Implements hook_proxy_settings_info(). |
google_cse_results_tab | Return the Google CSE tab title, either a setting or a translation. |
google_cse_search_access | Implements hook_search_access(). |
google_cse_search_admin | Implements hook_search_admin(). |
google_cse_search_execute | Implements hook_search_execute(). |
google_cse_search_info | Implements hook_search_info(). |
google_cse_search_page | Implements hook_search_page(). |
google_cse_sitesearch_default | Returns SiteSearch default value. |
google_cse_sitesearch_form | Returns SiteSearch options form item. |
google_cse_sitesearch_options | Returns SiteSearch options. |
google_cse_theme | Implements hook_theme(). |
theme_google_cse_search_noresults | Brief message to display when no results match the query. |