View source
<?php
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;
}
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',
),
);
}
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);
}
function gss_search_results($query) {
global $language;
module_load_include('inc', 'gss', 'includes/GoogleSiteSearch');
$query = check_plain($query);
$gss = new GoogleSiteSearch($query, variable_get('gss_api_key', ''));
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$results = null;
$gss
->SetPageSize(variable_get('gss_page_size', 20));
$gss
->SetPagerSize(variable_get('gss_pager_size', 20));
$default_lang = variable_get('gss_default_language', FALSE);
if ($default_lang) {
$gss
->SetLanguage($language->language);
}
else {
$gss
->SetLanguage(variable_get('gss_language', ''));
}
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;
}
$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;
}
if (!empty($results)) {
$output = array();
foreach ($results as $result) {
$result['thumbnail_url'] = isset($result['thumbnail_url']) ? $result['thumbnail_url'] : '';
$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'),
);
}
$output['head'] = $gss
->GetSearchHead();
$output['pager'] = $gss
->GetPager();
return $output;
}
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';
}
}
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;
}
}
}
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,
);
}
}
function gss_search_page($results) {
return theme('gss_results', $results);
}
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;
}