Bento box search with multiple targets
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

395 lines
12 KiB

<?php
/**
* @file
* Implementation of Roblib search for searching several targets.
*/
/**
* Implements hook_menu().
*/
function roblib_search_solr_site_menu() {
$items = array();
$items['admin/roblib_search/solr_site_search'] = array(
'title' => 'Roblib Solr Search Target configuration',
'description' => 'Configuration for the Roblib Solr site search target',
'page callback' => 'drupal_get_form',
'page arguments' => array('roblib_search_solr_site_config_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['roblib_search/solr_site/bestbet/%'] = array(
'title' => 'BestBet',
'page callback' => 'roblib_search_solr_site_bestbet',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib solr_site'),
);
$items['roblib_search/solr_site/reserves/%'] = array(
'title' => 'BestBet',
'page callback' => 'roblib_search_solr_site_reserves',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib solr_site'),
);
$items['roblib_search/solr_site/databases/%'] = array(
'title' => 'solr_site databases',
'page callback' => 'roblib_search_solr_site_databases',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib solr_site'),
);
$items['roblib_search/solr_site/guides/%'] = array(
'title' => 'solr_site guides',
'page callback' => 'roblib_search_solr_site_guides',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib solr_site'),
);
$items['roblib_search/solr_site/results/%'] = array(
'title' => 'solr_site guides',
'page callback' => 'roblib_search_solr_site_results',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib solr_site'),
);
return $items;
}
/**
* A form to manage the modules configuration.
*
* @param array $form
* A Drupal form array
* @param array $form_state
* A Drupal formstate array
*
* @return array
* A Drupal form array
*/
function roblib_search_solr_site_config_form($form, &$form_state) {
$form['roblib_search_solr_site_url'] = array(
'#type' => 'textfield',
'#title' => t('Solr url'),
'#default_value' => variable_get('roblib_search_solr_site_url', 'http://localhost:8983/solr'),
'#description' => t('The base Solr URL, for example http://localhost:8983/solr'),
'#required' => TRUE,
);
$form['roblib_search_solr_site_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#default_value' => variable_get('roblib_search_solr_site_num_results', '5'),
'#description' => t('The number of results to display in the Bento box'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Implements hook_permission().
*/
function roblib_search_solr_site_permission() {
return array(
'search roblib solr_site' => array(
'title' => t('Search the solr_site target'),
'description' => t('Search all Roblib solr_site target. This permission exposes the search blocks and allows you to see search results.'),
),
'administer roblib search_solr_site' => array(
'title' => t('Administer Roblib Search Evergreen'),
'description' => t('Administer settings for the Roblib solr_site search client.'),
),
);
}
/**
* Implements hook_theme().
*/
function roblib_search_solr_site_theme() {
// set path
$path = drupal_get_path('module', 'roblib_search_solr_site');
$file = 'theme.inc';
return array(
// results page
'roblib_search_solr_site' => array(
'path' => $path . '/theme',
'file' => $file,
'template' => 'roblib-search-solr-site-results',
'variables' => array('type' => NULL),
),
);
}
/**
* Implements hook_block_info().
*/
function roblib_search_solr_site_block_info() {
$blocks['roblib_search_solr_site_bestbet'] = array(
// info: The name of the block.
'info' => t('Roblib Solr Search Best bets'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
$blocks['roblib_search_solr_site_db'] = array(
// info: The name of the block.
'info' => t('Roblib Solr Search Databases Block'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
$blocks['roblib_search_solr_site_guides'] = array(
// info: The name of the block.
'info' => t('Roblib Solr Search Guides Block'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
$blocks['roblib_search_solr_site_results'] = array(
// info: The name of the block.
'info' => t('Roblib Solr Search Results Block'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
$blocks['roblib_search_solr_site_reserves'] = array(
// info: The name of the block.
'info' => t('Roblib Solr Search Reserves Block'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
return $blocks;
}
/**
* Add a div around our block content so we can theme our panels.
*
* By adding our divs we can theme ours without affecting other drupal panels.
*
* @param array $variables
* Drupal variables array
* @param array $hook
*/
function roblib_search_solr_site_preprocess_panels_pane(&$variables, $hook) {
switch ($variables['pane']->subtype) {
case 'roblib_search_solr_site-roblib_search_solr_site_bestbet':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-solr-bestbet-header">';
$variables['title_suffix'] = '</div>';
break;
case 'roblib_search_solr_site-roblib_search_solr_site_db':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-solr-db-header">';
$variables['title_suffix'] = '</div>';
break;
case 'roblib_search_solr_site-roblib_search_solr_site_guides':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-solr-guides-header">';
$variables['title_suffix'] = '</div>';
break;
case 'roblib_search_solr_site-roblib_search_solr_site_results':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-solr-results-header">';
$variables['title_suffix'] = '</div>';
break;
case 'roblib_search_solr_site-roblib_search_solr_site_reserves':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-solr-reserves-header">';
$variables['title_suffix'] = '</div>';
break;
}
}
/**
* Implements hook_block_view().
*/
function roblib_search_solr_site_block_view($delta = '') {
switch ($delta) {
case 'roblib_search_solr_site_bestbet':
$block['subject'] = t('Best Bets');
$block['content'] = theme('roblib_search_solr_site', array('type' => 'bestbet'));
break;
case 'roblib_search_solr_site_db':
$block['subject'] = t('Databases');
$block['content'] = theme('roblib_search_solr_site', array('type' => 'databases'));
break;
case 'roblib_search_solr_site_guides':
$block['subject'] = t('Guides');
$block['content'] = theme('roblib_search_solr_site', array('type' => 'guides'));
break;
case 'roblib_search_solr_site_results':
$block['subject'] = t('Library Site');
$block['content'] = theme('roblib_search_solr_site', array('type' => 'results'));
break;
case 'roblib_search_solr_site_reserves':
$block['subject'] = t('Reserves');
$block['content'] = theme('roblib_search_solr_site', array('type' => 'reserves'));
break;
}
return $block;
}
/**
* Send the query to solr but limit the results so it doesn't include certain content.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
function roblib_search_solr_site_results($query) {
print roblib_search_solr_site_get_results($query, '');
exit();
}
/**
* Send the query to solr but limit the results so it only includes content of type bestbet.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
function roblib_search_solr_site_bestbet($query) {
$query = str_replace('"', '', $query);
$json = roblib_search_solr_site_get_results("sort_label:\"$query\"", 'bundle:bestbet');
$json = roblib_search_solr_site_add_url($json);
print $json;
exit();
}
/**
* Send the query to solr but limit the results so it only includes content of type database.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
function roblib_search_solr_site_databases($query) {
print roblib_search_solr_site_get_results($query, 'bundle:database');
exit();
}
/**
* Send the query to solr but limit the results so it only includes content of type database.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
function roblib_search_solr_site_reserves($query) {
print roblib_search_solr_site_get_results($query, 'bundle:course_reserve');
exit();
}
/**
* Send the query to solr but limit the results so it only includes content of tagged with the guide keyword.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
function roblib_search_solr_site_guides($query) {
// TODO make the field and value searched configurable instead of hardcoded.
// sm_vid_SearchKeywords:guides is another field we might use.
print roblib_search_solr_site_get_results($query, 'im_field_keywords:627');
exit();
}
/**
*
* Updates the json passed in with a url directly to the target of the link field.
* This information is not in solr as it comes from a custom field.
*
*
* @param string $json
* The json string returned from the solr query.
*
* @return string
* json we got from solr with an updated url directly to the target of the link field
*/
function roblib_search_solr_site_add_url($json) {
$responses = json_decode($json);
foreach ($responses->response->docs as &$doc) {
$nid = $doc->entity_id;
// The link field is a complex field and we get the text in solr but not the actual url.
// Since best best will only every return one result it's not much overhead to load the node
// and get the value of the link field. probably less overhead then installing additional
// modules to get the url in solr.
$node = node_load($nid);
$url = $node->field_link['und'][0]['url'];
$doc->url = $url;
}
return json_encode($responses);
}
/**
* Sends a query to Solr.
*
* @param string $query
* A string to search for
* @param string $type
* A string to filter the query against field:value
*
* @return string
* Solr results as json.
*/
function roblib_search_solr_site_get_results($query = NULL, $type) {
$solr_url = variable_get('roblib_search_solr_site_url', 'http://localhost:8983/solr');
$num_results = variable_get('roblib_search_solr_site_num_results', '5');
$solr_qf = apachesolr_environment_variable_get(apachesolr_default_environment(), 'field_bias');
$qf = "";
foreach ($solr_qf as $key => $value){
if($value != "0") {
$qf .= $key . '^' . $value . ' ';
}
}
$data = array(
'q' => $query,
'fq' => $type,
'wt' => 'json',
'qf' => $qf,
'rows' => $num_results,
);
$url = url($solr_url . '/select', array('query' => $data));
$results = drupal_http_request($url);
if ($results->code != '200') {
return "";
}
return $results->data;
}
/**
* Implements hook_help().
*/
function roblib_search_solr_site_help($path, $arg) {
switch ($path) {
case 'admin/help#roblib_search_solr_site':
return t(
'<p>
provides a target for the Roblib search module
</p>'
);
}
}