diff --git a/apachesolr_multisitesearch.admin.inc b/apachesolr_multisitesearch.admin.inc index 43b5156..a7116b9 100644 --- a/apachesolr_multisitesearch.admin.inc +++ b/apachesolr_multisitesearch.admin.inc @@ -13,7 +13,6 @@ * this also reduces the clutter on the blocks admin page. */ function apachesolr_multisitesearch_settings() { - module_load_include('inc', 'apachesolr_multisitesearch', 'apachesolr_multisitesearch.index'); $form = array(); $form['#tree'] = TRUE; $form['submit_message'] = array( @@ -31,26 +30,7 @@ function apachesolr_multisitesearch_settings() { '#submit' => array('apachesolr_multisitesearch_refresh_metadata_now'), ); - // Use the metadata and a list of all the hashes in the index - // to build up checkboxes for deleting site indexes. - // This is only necessary because sometimes hashes get - // stranded in the index and deleting the index from the normal - // admin screen doesn't rectify the problem. - $metadata = variable_get('apachesolr_multisitesearch_metadata', array()); - $hashes = apachesolr_multisitesearch_get_site_hashes(); - $options = array(); - foreach ($hashes as $hash => $count) { - if ($hash == apachesolr_site_hash()) { - $options[$hash] = t('This site (!site, !count documents)', array('!site' => variable_get('site_name', 'Drupal'), '!count' => $count)); - } - elseif (!empty($metadata[$hash])) { - $options[$hash] = $metadata[$hash]['site'] . ' ' . t('(!count documents)', array('!count' => $count)); - } - else { - $options[$hash] = $hash . ' ' . t('(!count documents)', array('!count' => $count)); - } - } - + $options = apachesolr_multisitesearch_get_hash_options(); if (count($options) > 0) { $form['admin']['delete']['hashes'] = array( '#type' => 'checkboxes', diff --git a/apachesolr_multisitesearch.module b/apachesolr_multisitesearch.module index e6514f4..6be1544 100644 --- a/apachesolr_multisitesearch.module +++ b/apachesolr_multisitesearch.module @@ -80,12 +80,29 @@ function apachesolr_multisitesearch_map_username($facets, $options) { */ function apachesolr_multisitesearch_form_apachesolr_environment_edit_form_alter(&$form, &$form_state, $form_id) { $environment = reset($form_state['build_info']['args']); - $is_multisite = apachesolr_environment_variable_get($environment['env_id'], 'multisitesearch'); + $form['make_multisite'] = array( '#type' => 'checkbox', '#title' => t('Make this Solr search environment multisite capable'), - '#default_value' => $is_multisite, + '#default_value' => apachesolr_environment_variable_get($environment['env_id'], 'multisitesearch', FALSE), ); + + $options = apachesolr_multisitesearch_get_hash_options(); + $form['multisitesearch_hashes'] = array( + '#type' => 'select', + '#title' => t('Limit searching to the following sites:'), + '#description' => t('If no sites selected, searching will be performed across all sites.'), + '#multiple' => TRUE, + '#options' => $options, + '#default_value' => apachesolr_environment_variable_get($environment['env_id'], 'multisitesearch_hashes', array()), + '#states' => array( + 'visible' => array( + ':input[name="make_multisite"]' => array('checked' => TRUE), + ), + ), + '#access' => !empty($options), + ); + $form['actions']['save']['#submit'][] = 'apachesolr_multisitesearch_environment_edit_submit'; } @@ -95,6 +112,34 @@ function apachesolr_multisitesearch_form_apachesolr_environment_edit_form_alter( function apachesolr_multisitesearch_environment_edit_submit($form, &$form_state) { // Enable or disable multisite apachesolr_environment_variable_set($form_state['values']['env_id'], 'multisitesearch', $form_state['values']['make_multisite']); + apachesolr_environment_variable_set($form_state['values']['env_id'], 'multisitesearch_hashes', $form_state['values']['multisitesearch_hashes']); +} + +/** + * Return an array of multisite search sites keyed by hash. + */ +function apachesolr_multisitesearch_get_hash_options() { + $options = &drupal_static(__FUNCTION__); + + if (!isset($options)) { + module_load_include('inc', 'apachesolr_multisitesearch', 'apachesolr_multisitesearch.index'); + $metadata = variable_get('apachesolr_multisitesearch_metadata', array()); + $hashes = apachesolr_multisitesearch_get_site_hashes(); + $options = array(); + foreach ($hashes as $hash => $count) { + if ($hash == apachesolr_site_hash()) { + $options[$hash] = t('This site (!site, !count documents)', array('!site' => variable_get('site_name', 'Drupal'), '!count' => $count)); + } + elseif (!empty($metadata[$hash])) { + $options[$hash] = $metadata[$hash]['site'] . ' ' . t('(!count documents)', array('!count' => $count)); + } + else { + $options[$hash] = $hash . ' ' . t('(!count documents)', array('!count' => $count)); + } + } + } + + return $options; } /** @@ -173,15 +218,17 @@ function apachesolr_multisitesearch_query_bundles() { */ function apachesolr_multisitesearch_apachesolr_query_alter(DrupalSolrQueryInterface $query) { if (empty($query->multisite)) { - $env_id = $query->solr('getId'); - $multisite = apachesolr_environment_variable_get($env_id, 'multisitesearch'); // Add hash and site to our fields to retrieve $query->addParam('fl', 'hash'); $query->addParam('fl', 'site'); - if (empty($multisite)) { + $env_id = $query->solr('getId'); + if (!apachesolr_environment_variable_get($env_id, 'multisitesearch', FALSE)) { // Limit single site searchs via the site hash. $query->addFilter('hash', apachesolr_site_hash()); } + elseif ($hashes = apachesolr_environment_variable_get($env_id, 'multisitesearch_hashes', array())) { + $query->addFilter('hash', '(' . implode(' OR ', $hashes) . ')'); + } } // Get the variable which contains the query exclusion keys.