You are here

function apachesolr_search_view in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 5.2 apachesolr_search.module \apachesolr_search_view()
  2. 6 apachesolr_search.module \apachesolr_search_view()

Re-implementation of search_view().

1 string reference to 'apachesolr_search_view'
apachesolr_search_menu_alter in ./apachesolr_search.module
Implementation of hook_menu_alter().

File

./apachesolr_search.module, line 142
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_view($type = NULL) {
  if (empty($type)) {

    // Note: search/X can not be a default tab because it would take on the
    // path of its parent (search). It would prevent remembering keywords when
    // switching tabs. This is why we drupal_goto to it from the parent instead.
    drupal_goto('search/apachesolr_search');
  }
  $keys = trim(search_get_keys());

  // Construct the search form.  If the user submits POST data, this will
  // redirect to a GET request before the search actually runs.
  if (isset($_POST['form_id']) && $_POST['form_id'] == 'search_form') {
    $form = drupal_get_form('search_form', NULL, $keys, $type);
  }

  // We did not redirect, so run the search if needed.
  $content = '';
  $filters = '';
  if (isset($_GET['filters'])) {
    $filters = trim($_GET['filters']);
  }

  // Only perform search if there is non-whitespace search term or filters:
  if ($keys || $filters || variable_get('apachesolr_search_browse', 'browse') == 'results') {
    if (variable_get('apachesolr_logging', TRUE)) {

      // Log the search keys:
      $log = $keys;
      if ($filters) {
        $log .= 'filters=' . $filters;
      }
      watchdog('search', '%keys (@type).', array(
        '%keys' => $log,
        '@type' => t('Search'),
      ), WATCHDOG_NOTICE, l(t('results'), 'search/' . $type . '/' . $keys));
    }

    // Collect the search results. See search_data().
    $content = module_invoke($type, 'search', 'search', $keys);
    if (isset($content) && is_array($content) && count($content)) {
      if (module_hook($type, 'search_page')) {
        $output = module_invoke($type, 'search_page', $content);
      }
      else {
        $output = theme('search_results', $content, $type);
      }
      return drupal_get_form('search_form', NULL, $keys, $type) . $output;
    }

    // See search_view().
    if ($content) {
      $content = theme('box', t('Search results'), $content);
    }
    else {
      $content = theme('box', t('Your search yielded no results'), theme('apachesolr_search_noresults'));
    }
  }
  elseif ($type != 'node') {

    // Ignore $type == node. Since we override the menu path to search to point
    // to this function, we have to count on core searches coming in here, too.
    switch (variable_get('apachesolr_search_browse', 'browse')) {
      case 'browse':
        try {

          // Show search form and browse-by blocks.
          $blocks = apachesolr_search_browse('', '', '', 'search/' . $type);
          if (count($blocks) > 0) {
            $content = theme('apachesolr_browse_blocks', $blocks);
          }
        } catch (Exception $e) {
          watchdog('Apache Solr', nl2br(check_plain($e
            ->getMessage())), NULL, WATCHDOG_ERROR);
          apachesolr_failure(t('Solr search'), $keys);
        }
        break;
      case 'blocks':

        // Launch search so that hook_block() will show blocks.
        $dummy = search_data($keys, $type);
        break;
      default:
        break;
    }
  }
  if (empty($form)) {

    // The form may be altered based on the query that was run.
    $form = drupal_get_form('search_form', NULL, $keys, $type);
  }
  return theme('apachesolr_search_results_page', $form, $content);
}