function gss_search_results in Google Site Search 7
Same name and namespace in other branches
- 6 gss.module \gss_search_results()
Search results page.
Parameters
string $query: The search keywords as entered by the user.
Return value
array An array of search results.
1 call to gss_search_results()
- gss_search_execute in ./
gss.module - Implements hook_search_execute().
File
- ./
gss.module, line 305 - GSS module file.
Code
function gss_search_results($query) {
global $language;
drupal_add_css(drupal_get_path('module', 'gss') . '/gss.css');
// Include dependencies.
module_load_include('inc', 'gss', 'includes/GoogleSiteSearch');
// Let other modules alter the $query variable.
drupal_alter('gss_search_key', $query);
// Let other modules add extra parameters.
$extra = NULL;
drupal_alter('gss_search_extraparams', $extra);
// Make query safe.
$query = htmlspecialchars($query, ENT_NOQUOTES);
// Init GoogleSiteSearch object.
$gss = new GoogleSiteSearch($query, variable_get('gss_api_key', ''), 20, $extra);
// Get current page.
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
// 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.'));
drupal_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) {
// Just to avoid warnings in the log if not set.
$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 {
$default_no_results = variable_get('gss_no_results', array(
'value' => t('No results'),
'format' => NULL,
));
$empty_results = format_string($default_no_results['value'], array(
'@query' => $query,
));
$output[] = array(
'link' => '',
'title' => '',
'snippet' => check_markup($empty_results, $default_no_results['format']),
);
}
// Get search head.
$output['head'] = $gss
->getSearchHead();
// Get pager.
$pager = $gss
->getPager();
$output['pager'] = drupal_render($pager);
return $output;
}