'google_appliance',
'callback' => 'google_appliance_search',
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'admin/settings/google_appliance',
'title' => t('Google Appliance Settings'),
'description' => t('Configuration for the Google Appliance search'),
'callback' => 'drupal_get_form',
'callback arguments' => array('google_appliance_admin_settings'),
'access' => user_access('administer search'),
'type' => MENU_NORMAL_ITEM,
);
} else {
_google_appliance_add_meta_tags();
}
return $items;
}
/**
* Implementation of hook_block().
*/
function google_appliance_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks['recommended_links']['info'] = t("Recommended Links");
$blocks['recommended_links']['title'] = t('Recommended Links');
$blocks['recommended_links']['pages'] = '*search/google_appliance*';
$blocks['recommended_links']['visibility'] = 1;
return $blocks;
break;
case 'view':
switch ($delta) {
case 'recommended_links':
if ($result =& google_appliance_static_response_cache()) {
$matches = $result->getKeyMatches();
if (!$matches) {
return;
}
$links = array();
foreach ($matches as $link => $title) {
$links[] = l($title,$link);
}
if (count($links)) {
$block['content'] = theme('item_list',$links);
} else {
return false;
}
}
break;
}
return $block;
break;
}
}
function google_appliance_admin_settings() {
$form = array();
// initial required config fields
$form["config_init"] = array(
"#title" => t("Initial Configuration"),
"#type" => "fieldset",
);
$form["config_init"]["google_appliance_name"] = array(
"#type" => "textfield",
"#size" => 30,
"#title" => t("Search Name"),
"#description" => t('The name of this search, to appear as sub-navigation on the search page.'),
"#default_value" => variable_get('google_appliance_name', 'Google Appliance'),
"#required" => true,
);
$form["config_init"]["google_appliance_host_name"] = array(
"#type" => "textfield",
"#size" => 50,
"#title" => t("Host Name"),
"#description" => t('Your Google Search Appliance host name or IP address (with http:// or https://), which were assigned when the appliance was set up.
You do not need to include "/search" at the end, or a trailing slash, but you should include a port number if needed.
Example: http://mygooglebox.com'),
"#default_value" => variable_get('google_appliance_host_name', ''),
"#required" => true,
);
$form["config_init"]["google_appliance_collection"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Collection"),
"#description" => t('The name of the collection of indexed content to search.'),
"#default_value" => variable_get('google_appliance_collection', ''),
"#required" => true,
);
$form["config_init"]["google_appliance_client"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Client"),
"#description" => t('The name of a valid front-end, defined when you set up the appliance.'),
"#default_value" => variable_get('google_appliance_client', ''),
"#required" => true,
);
$form["config_init"]["google_appliance_cache_timeout"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Cache Timeout"),
"#description" => t('If you wish to use caching of results (to reduce load on mini, enter a timeout here'),
"#default_value" => variable_get('google_appliance_cache_timeout', ''),
);
$form["config_init"]["google_debug"] = array(
"#type" => "textfield",
"#size" => 20,
"#title" => t("Debug Level"),
"#description" => t('1 = watchdog, 2 = dpr(needs devel module), 3 = more dpr\'s'),
"#default_value" => variable_get('google_debug', ''),
);
$form["config_init"]["google_appliance_limit_per_page"] = array(
"#type" => "textfield",
"#size" => 5,
"#title" => t("Number of results per page"),
"#description" => t('If you enter 0, it will return the max allowed by the appliance (100)'),
"#default_value" => variable_get('google_appliance_limit_per_page', 10),
);
// error message config
$form["config_messages"] = array(
"#title" => t("Error Messages"),
"#type" => "fieldset",
"#collapsible" => true,
);
$form["config_messages"]["google_appliance_errorcode_1"] = array(
"#title" => t("No results found"),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => true,
"#description" => t('If there are no results for the search criteria.'),
"#default_value" => variable_get('google_appliance_errorcode_1', 'No results were found that matched your criteria. Please try broadening your search.'),
);
$form["config_messages"]["google_appliance_errorcode_2"] = array(
"#title" => t("More than 1,000 results"),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => true,
"#description" => t('If there are more than 1,000 results for the search criteria.'),
"#default_value" => variable_get('google_appliance_errorcode_2', 'Sorry, but our search does not return more than 1,000 records, please refine your criteria.'),
);
$form["config_messages"]["google_appliance_errorcode_neg_99"] = array(
"#title" => t("Cannot perform search"),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => true,
"#description" => t('If the search cannot perform due to a query error.'),
"#default_value" => variable_get('google_appliance_errorcode_neg_99', 'We apologize, but your search cannot be completed at this time, please try again later.'),
);
$form["config_messages"]["google_appliance_errorcode_neg_100"] = array(
"#title" => t("Cannot connect to Google Appliance"),
"#type" => "textfield",
"#size" => 100,
"#maxlength" => 255,
"#required" => true,
"#description" => t('If the search cannot connect to the Google Appliance server.'),
"#default_value" => variable_get('google_appliance_errorcode_neg_100','We apologize, but the connection to our search engine appears to be down at the moment, please try again later.'),
);
// optional metadata configuration
/*
$form["config_metadata"] = array(
"#title" => t("Metadata Configuration"),
"#type" => "fieldset",
"#collapsible" => true,
);
*/
// last but not least, submit
$form["submit"] = array(
"#type" => "submit",
"#value" => t("Save Settings"),
);
return $form;
}
/**
* Validation function, though it's actually getting overridden by the #required fields...
*
*/
function google_appliance_admin_settings_validate($form_id, $form) {
if (empty($form['google_appliance_host_name'])) {
form_set_error('google_appliance_host_name', t('Please enter your host name or IP address.'));
}
if (empty($form['google_appliance_collection'])) {
form_set_error('google_appliance_collection', t('Please enter the name of the collection you want to search.'));
}
if (empty($form['google_appliance_client'])) {
form_set_error('google_appliance_client', t('Please enter name of the client frontend you are searching.'));
}
if (empty($form['google_appliance_name'])) {
form_set_error('google_appliance_name', t('Please enter the name of this search, to appear as sub-navigation on the search page.'));
}
}
/**
* Submits the admin settings form and saves all the variables.
*/
function google_appliance_admin_settings_submit($form_id, $form) {
variable_set('google_appliance_host_name', check_plain($form['google_appliance_host_name']));
variable_set('google_appliance_collection', check_plain($form['google_appliance_collection']));
variable_set('google_appliance_client', check_plain($form['google_appliance_client']));
variable_set('google_appliance_name', check_plain($form['google_appliance_name']));
variable_set('google_appliance_cache_timeout', check_plain($form['google_appliance_cache_timeout']));
variable_set('google_debug', check_plain($form['google_debug']));
variable_set('google_appliance_limit_per_page', check_plain($form['google_appliance_limit_per_page']));
// don't run check_plain on these because they can have HTML
variable_set('google_appliance_errorcode_1', $form['google_appliance_errorcode_1']);
variable_set('google_appliance_errorcode_2', $form['google_appliance_errorcode_2']);
variable_set('google_appliance_errorcode_neg_99', $form['google_appliance_errorcode_neg_99']);
variable_set('google_appliance_errorcode_neg_100', $form['google_appliance_errorcode_neg_100']);
drupal_set_message(t('Your settings have been saved.'));
}
/**
* Invokes the google_appliance_appconfig hook to add tags to nodes
* for indexing metadata by the google crawler.
*
*/
function _google_appliance_add_meta_tags() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
theme('add_google_appliance_meta_tags', $node);
}
}
/**
* Implementation of hook_search()
*
* @param string $op
* Operation - name, reset, search, status
* @param string $keys
* Keyword string sent to the search
* @return
* Array of search results (each is an assoc. array) that can be fed to a theme function
*/
function google_appliance_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t(variable_get('google_appliance_name', "Google Appliance"));
break;
case 'search':
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';
// Convert comma-separated $page to an array, used by other functions.
$pager_page_array = explode(',', $page);
// $element indicates which of the pagers active this pager is working from. $limit indicates how many per page.
$element = 0;
$limit = variable_get('google_appliance_limit_per_page',10);
$dir = drupal_get_path('module', 'google_appliance');
include_once $dir . '/DrupalGoogleMini.php';
$google_debug = variable_get('google_debug',0);
if ($google_debug >= 2 ){
$gm = new DrupalGoogleMini(true,'dpr');
} elseif ($google_debug == 1) {
$gm = new DrupalGoogleMini(true);
} else {
$gm = new DrupalGoogleMini(false);
}
/**
* If you have many searches for the same content
* You can use this setting to keep the GSA from getting hit too often
*
*/
if ($cache = variable_get('google_appliance_cache_timeout',0) ) {
cache_clear_all(null,'cache_google');
$gm->cache = true;
}
// initialize search object
try {
$gm->setOutputEncoding('utf8');
$gm->setInputEncoding('utf8');
$gm->setMetaDataRequested('*');
// get configuration from settings page
$_tmp_host = variable_get('google_appliance_host_name', false);
if (!$_tmp_host) {
drupal_set_message(t('No host name has been configured for the search appliance. Please enter it on the Google Appliance settings page', array("@admin-url" => url("admin/settings/search/google_appliance"))), 'error');
return false;
}
$gm->baseUrl = $_tmp_host . "/search";
$gm->collection = variable_get('google_appliance_collection', '');
$gm->setQueryPart('client',variable_get('google_appliance_client', ''));
$gm->setPageAndResultsPerPage($page,$limit);
// set search parameters
$gm->setKeywords($keys);
if (module_exists('i18n')) {
if ($lang = i18n_get_lang()) {
$gm->setLanguageFilter(array($lang));
}
}
}
catch (GoogleMiniCriteriaException $e) {
$code = $e->getCode();
if ($message = variable_get('google_appliance_errorcode_' . $code,'')) {
$user_message = $message;
} else {
$user_message = GoogleMiniException::getUserMessage($code);
}
$error_message = $e->getMessage();
if ($code > 0) {
$output .= "