google_cse.module in Google Custom Search Engine 5
Same filename and directory in other branches
Display a Google Custom Search Engine on your site.
File
google_cse.moduleView source
<?php
/**
* @file
* Display a Google Custom Search Engine on your site.
*/
/**
* Implementation of hook_menu().
*/
function google_cse_menu($maycache) {
$items = array();
if ($maycache) {
if (variable_get('google_cse_results_display', 'here') != 'google') {
$items[] = array(
'path' => 'search/google',
'access' => user_access('search Google CSE'),
'title' => t('Google'),
'description' => t('Google Custom Search Engine'),
'callback' => 'google_cse_results',
'type' => MENU_LOCAL_TASK,
);
}
$items[] = array(
'path' => 'admin/settings/google_cse',
'access' => user_access('administer site configuration'),
'title' => t('Google CSE'),
'description' => t('Configure the Google Custom Search Engine.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'google_cse_admin_settings',
),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Admin settings page for the CSE.
*/
function google_cse_admin_settings() {
$form = array();
$form['google_cse_cx'] = array(
'#title' => t('Google Custom Search Engine ID'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_cx', ''),
'#description' => t('Enter your alphanumeric <a target="_blank" href="http://www.google.com/coop/manage/cse/">Google Custom Search Engine ID</a> (click on control panel and then click on code; the only part you need is the <em>cx</em> value).'),
);
$form['google_cse_results_display'] = array(
'#title' => t('Display search results'),
'#type' => 'radios',
'#default_value' => variable_get('google_cse_results_display', 'here'),
'#options' => array(
'here' => t('On this site (requires JavaScript)'),
'google' => t('On Google'),
),
'#description' => t('Search results can be displayed on this site, using JavaScript, or on Google, which does not require JavaScript.'),
);
$form['google_cse_results_title'] = array(
'#title' => t('Search results page title'),
'#type' => 'textfield',
'#maxlength' => 50,
'#size' => 60,
'#description' => t('Enter the desired title of the page where the search results will be displayed. This has no effect if you do not display results on your own site.'),
'#default_value' => variable_get('google_cse_results_title', t('Search')),
);
$form['google_cse_results_prefix'] = array(
'#title' => t('Search results prefix text'),
'#type' => 'textarea',
'#cols' => 50,
'#rows' => 4,
'#description' => t('Enter text to appear on the search page before the search form.'),
'#default_value' => variable_get('google_cse_results_prefix', ''),
);
$form['google_cse_results_suffix'] = array(
'#title' => t('Search results suffix text'),
'#type' => 'textarea',
'#cols' => 50,
'#rows' => 4,
'#description' => t('Enter text to appear on the search page after the search form and results.'),
'#default_value' => variable_get('google_cse_results_suffix', ''),
);
$form['google_cse_searchbox_width'] = array(
'#title' => t('Searchbox width'),
'#type' => 'textfield',
'#maxlength' => 4,
'#size' => 6,
'#description' => t('Enter the desired width, in characters, of the searchbox when displayed in a block.'),
'#default_value' => variable_get('google_cse_searchbox_width', 15),
);
$form['google_cse_results_searchbox_width'] = array(
'#title' => t('Search results searchbox width'),
'#type' => 'textfield',
'#maxlength' => 4,
'#size' => 6,
'#description' => t('Enter the desired width, in characters, of the searchbox when displayed on the results page.'),
'#default_value' => variable_get('google_cse_results_searchbox_width', 40),
);
$form['google_cse_results_width'] = array(
'#title' => t('Search results frame width'),
'#type' => 'textfield',
'#maxlength' => 4,
'#size' => 6,
'#description' => t('Enter the desired width, in pixels, of the search frame.'),
'#default_value' => variable_get('google_cse_results_width', 600),
);
$form['google_cse_results_gadget'] = array(
'#title' => t('Search results "Add to Google" Google Gadget'),
'#type' => 'checkbox',
'#default_value' => variable_get('google_cse_results_gadget', 1),
'#description' => t('If enabled, an "Add to Google" button will be displayed above the search results.'),
);
$form['google_cse_domain'] = array(
'#title' => t('Search domain'),
'#type' => 'textfield',
'#maxlength' => 64,
'#description' => t('Enter the Google domain to use for search results, e.g. <em>www.google.com</em>.'),
'#default_value' => variable_get('google_cse_domain', 'www.google.com'),
);
$form['advanced'] = array(
'#title' => t('Advanced settings'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['google_cse_cr'] = array(
'#title' => t('Country restriction'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_cr', ''),
'#description' => t('Enter a 9-letter country code, e.g. <em>countryNZ</em>, and optional boolean operators, to restrict search results to documents (not) originating in particular countries. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#crsp"><em>cr</em> parameter</a>.'),
);
$form['advanced']['google_cse_gl'] = array(
'#title' => t('Country boost'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_gl', ''),
'#description' => t('Enter a 2-letter country code, e.g. <em>uk</em>, to boost documents written in a particular country. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#glsp"><em>gl</em> parameter</a>.'),
);
$form['advanced']['google_cse_hl'] = array(
'#title' => t('Interface language'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_hl', ''),
'#description' => t('Enter a supported 2- or 5-character language code, e.g. <em>fr</em>, to set the language of the user interface. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#hlsp"><em>hl</em> parameter</a>.'),
);
$form['advanced']['google_cse_locale_hl'] = array(
'#title' => t('Set interface language dynamically'),
'#type' => 'checkbox',
'#default_value' => variable_get('google_cse_locale_hl', ''),
'#description' => t('The language restriction can be set dynamically if the locale module is enabled. Note the locale language code must match one of the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#interfaceLanguages">supported language codes</a>.'),
);
$form['advanced']['google_cse_ie'] = array(
'#title' => t('Input encoding'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_ie', ''),
'#description' => t('The default <em>utf8</em> is recommended. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#iesp"><em>ie</em> parameter</a>.'),
);
$form['advanced']['google_cse_lr'] = array(
'#title' => t('Language restriction'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_lr', ''),
'#description' => t('Enter a supported 7- or 10-character language code, e.g. <em>lang_en</em>, and optional boolean operators, to restrict search results to documents (not) written in particular languages. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#lrsp"><em>lr</em> parameter</a>.'),
);
$form['advanced']['google_cse_locale_lr'] = array(
'#title' => t('Set language restriction dynamically'),
'#type' => 'checkbox',
'#default_value' => variable_get('google_cse_locale_lr', ''),
'#description' => t('The language restriction can be set dynamically if the locale module is enabled. Note the locale language code must match one of the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#languageCollections">supported language codes</a>.'),
);
$form['advanced']['google_cse_oe'] = array(
'#title' => t('Output encoding'),
'#type' => 'textfield',
'#default_value' => variable_get('google_cse_oe', ''),
'#description' => t('The default <em>utf8</em> is recommended. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#oesp"><em>oe</em> parameter</a>.'),
);
$form['advanced']['google_cse_safe'] = array(
'#title' => t('SafeSearch filter'),
'#type' => 'select',
'#options' => array(
'' => '',
'off' => t('Off'),
'medium' => t('Medium'),
'high' => t('High'),
),
'#default_value' => variable_get('google_cse_safe', ''),
'#description' => t('SafeSearch filters search results for adult content. See the <a target="_blank" href="http://www.google.com/coop/docs/cse/resultsxml.html#safesp"><em>safe</em> parameter</a>.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_block().
*/
function google_cse_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks = array();
$blocks[] = array(
'info' => t('Google CSE'),
);
return $blocks;
}
else {
if ($op == 'view' && user_access('search Google CSE')) {
return array(
'subject' => t('Search'),
'content' => drupal_get_form('google_cse_searchbox_form'),
);
}
}
}
/**
* Implementation of hook_forms();
*/
function google_cse_forms() {
$forms = array();
$forms['google_cse_searchbox_form'] = array(
'callback' => 'google_cse_searchbox_formbuilder',
'callback arguments' => array(
'searchbox',
),
);
$forms['google_cse_results_searchbox_form'] = array(
'callback' => 'google_cse_searchbox_formbuilder',
'callback arguments' => array(
'results_searchbox',
),
);
return $forms;
}
/**
* Form builder for the searchbox forms.
*/
function google_cse_searchbox_formbuilder($form_id) {
global $locale;
$form = array();
// The default form.
if (variable_get('google_cse_results_display', 'here') == 'here') {
$form['#action'] = url('search/google');
$q = 'query';
// parameter on Drupal site
$cof = 'FORID:11';
}
else {
$form['#action'] = 'http://www.google.com/cse';
$q = 'q';
// parameter on Google
$cof = 'FORID:0';
}
$form['#method'] = 'get';
$form['cx'] = array(
'#type' => 'hidden',
'#value' => variable_get('google_cse_cx', ''),
);
$form['cof'] = array(
'#type' => 'hidden',
'#value' => $cof,
);
$form[$q] = array(
'#type' => 'textfield',
'#default_value' => $_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,
);
}
drupal_add_css(drupal_get_path('module', 'google_cse') . '/google_cse.css');
drupal_add_js(array(
'googleCSE' => array(
'locale' => array(
$locale,
),
),
), 'setting', 'header');
drupal_add_js(drupal_get_path('module', 'google_cse') . '/google_cse.js', 'module', 'footer');
// And the small differences between both.
switch ($form_id) {
case 'searchbox':
$form[$q]['#size'] = intval(variable_get('google_cse_searchbox_width', 15));
$form[$q]['#attributes']['title'] = t('Enter the terms you wish to search for.');
break;
case 'results_searchbox':
$form[$q]['#size'] = intval(variable_get('google_cse_results_searchbox_width', 40));
$form[$q]['#title'] = t('Enter your keywords');
$form['sa']['#suffix'] = google_cse_results_gadget();
break;
}
return $form;
}
/**
* Returns an array of any advanced settings which have been set.
*/
function google_cse_advanced_settings() {
global $locale;
$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'] = $locale;
}
if (variable_get('google_cse_locale_lr', '')) {
$settings['lr'] = 'lang_' . $locale;
}
return $settings;
}
/**
* Display an Add-to-Google button.
*/
function google_cse_results_gadget() {
if (variable_get('google_cse_results_gadget', 1) && ($id = explode(':', variable_get('google_cse_cx', '')))) {
$output = '<div id="google-cse-results-gadget">';
$output .= '<a href="http://fusion.google.com/add?moduleurl=http%3A%2F%2Fwww.google.com%2Fcoop/api/' . drupal_urlencode($id[0]) . '/cse/' . drupal_urlencode($id[1]) . '/gadget">';
$output .= '<img src="http://buttons.googlesyndication.com/fusion/add.gif" width="104" height="17" border="0" alt="Add to Google" />';
$output .= '</a></div>';
return $output;
}
else {
return '';
}
}
/**
* Render the search page and custom title.
*/
function google_cse_results() {
drupal_set_title(check_plain(variable_get('google_cse_results_title', t('Search'))));
print theme('page', theme('google_cse_results'));
}
/**
* The search results page can be themed/customized.
*/
function theme_google_cse_results() {
$query = array(
'q' => $_GET['query'],
'cx' => variable_get('google_cse_cx', ''),
'cof' => 'FORID:0',
) + google_cse_advanced_settings();
$output = filter_xss_admin(variable_get('google_cse_results_prefix', ''));
$output .= drupal_get_form('google_cse_results_searchbox_form');
$output .= '<div id="google-cse-results">';
$output .= '<noscript>';
$output .= t('!google, or enable JavaScript to view them here.', array(
'!google' => l(t('View the results at Google'), 'http://www.google.com/cse', NULL, drupal_query_string_encode($query)),
));
$output .= '</noscript></div>
<script type="text/javascript">
//<![CDATA[
var googleSearchIframeName = "google-cse-results";
var googleSearchFormName = "google-cse-results-searchbox-form";
var googleSearchFrameWidth = ' . variable_get('google_cse_results_width', 600) . ';
var googleSearchFrameborder = 0;
var googleSearchDomain = "' . variable_get('google_cse_domain', 'google.com') . '";
var googleSearchPath = "/cse";
var googleSearchQueryString = "query";
//]]>
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>';
$output .= filter_xss_admin(variable_get('google_cse_results_suffix', ''));
return $output;
}
/**
* Implementation of hook_perm().
*/
function google_cse_perm() {
return array(
'search Google CSE',
);
}
Functions
Name | Description |
---|---|
google_cse_admin_settings | Admin settings page for the CSE. |
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_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_gadget | Display an Add-to-Google button. |
google_cse_searchbox_formbuilder | Form builder for the searchbox forms. |
theme_google_cse_results | The search results page can be themed/customized. |