You are here

function search404_page in Search 404 6

Same name and namespace in other branches
  1. 5 search404.module \search404_page()
  2. 7 search404.page.inc \search404_page()

Main search function. Started with: http://drupal.org/node/12668 Updated to be more similar to search_view

1 string reference to 'search404_page'
search404_menu in ./search404.module
Implementation of hook_menu().

File

./search404.module, line 115

Code

function search404_page() {
  $output = '';
  drupal_set_title(variable_get('search404_page_title', t('Page not found')));
  if (module_exists('search') && (user_access('search content') || user_access('search by page'))) {
    $keys = "";
    $results = "";

    // If apachesolr_search or luceneapi or xapian is installed use them instead default node search.
    $type_search = module_exists('apachesolr_search') ? 'apachesolr_search' : (module_exists('luceneapi_node') ? 'luceneapi_node' : (module_exists('xapian') ? 'xapian' : 'node'));
    if (variable_get('search404_use_search_engine', FALSE)) {
      $keys = search404_search_engine_query();
    }
    if (!$keys) {
      $keys = search404_get_keys();
    }

    // Get throttle status
    $throttle = module_invoke('throttle', 'status');

    // If search keys are present and site is not throttled and automatic searching is not disabled.
    if ($keys && !$throttle && !variable_get('search404_skip_auto_search', FALSE)) {
      if (module_exists('search_by_page') && variable_get('search404_do_search_by_page', FALSE)) {

        // TODO - Perhaps move the message to the search404 goto?
        if (!variable_get('search404_disable_error_message', FALSE)) {
          drupal_set_message(t('The page you requested does not exist. For your convenience, a search was performed using the query %keys.', array(
            '%keys' => check_plain($keys),
          )), 'error');
        }
        search404_goto('search_pages/' . $keys);
      }
      elseif (module_exists('fuzzysearch') && user_access('fuzzysearch content') && variable_get('search404_do_fuzzysearch', FALSE)) {
        if (!variable_get('search404_disable_error_message', FALSE)) {
          drupal_set_message(t('The page you requested does not exist. For your convenience, a search was performed using the query %keys.', array(
            '%keys' => check_plain($keys),
          )), 'error');
        }
        search404_goto(variable_get('fuzzysearch_path_name', 'fuzzysearch/results') . '/' . $keys);
      }
      elseif (module_exists('google') && user_access('search Google CSE') && variable_get('search404_do_google_cse', FALSE)) {
        if (!variable_get('search404_disable_error_message', FALSE)) {
          drupal_set_message(t('The page you requested does not exist. For your convenience, a google search was performed using the query %keys.', array(
            '%keys' => check_plain($keys),
          )), 'error');
        }
        search404_goto('search/google/' . $keys);
      }
      elseif (module_exists('google_cse_adv') && user_access('search content') && variable_get('search404_do_google_cse_adv', FALSE)) {
        if (!variable_get('search404_disable_error_message', FALSE)) {
          drupal_set_message(t('The page you requested does not exist. For your convenience, a google search was performed using the query %keys.', array(
            '%keys' => check_plain($keys),
          )), 'error');
        }
        search404_goto('search/google_cse_adv/' . $keys);
      }
      elseif (variable_get('search404_do_custom_search', FALSE)) {
        if (!variable_get('search404_disable_error_message', FALSE)) {
          drupal_set_message(t('The page you requested does not exist. For your convenience, a search was performed using the query %keys.', array(
            '%keys' => check_plain($keys),
          )), 'error');
        }
        $custom_search_path = variable_get('search404_custom_search_path', 'search/@keys');
        $custom_search_path = str_replace('@keys', $keys, $custom_search_path);
        search404_goto($custom_search_path);
      }
      else {

        // Called for apache solr, lucene, xapian and core search.
        $results = module_invoke($type_search, 'search', 'search', $keys);

        // First, check to see if there is exactly 1 result and if jump to first
        // is selected then jump to it.
        if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', FALSE)) {
          if (!variable_get('search404_disable_error_message', FALSE)) {
            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array(
              '%keys' => check_plain($keys),
            )), 'status');
          }
          search404_goto('node/' . $results[0]['node']->nid);
        }
        elseif (isset($results) && is_array($results) && count($results) >= 1 && variable_get('search404_first', FALSE)) {
          if (!variable_get('search404_disable_error_message', FALSE)) {
            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array(
              '%keys' => check_plain($keys),
            )), 'status');
          }
          search404_goto('node/' . $results[0]['node']->nid);
        }
        else {
          if (!variable_get('search404_disable_error_message', FALSE)) {
            drupal_set_message(t('The page you requested does not exist. For your convenience, a search was performed using the query %keys.', array(
              '%keys' => check_plain($keys),
            )), 'error');
          }
          if (isset($results) && is_array($results) && count($results) > 0) {
            drupal_add_css(drupal_get_path('module', 'search') . '/search.css', 'module', 'all', FALSE);

            // Hack to get search results themed by manipulating $_GET['q']!
            $oldgetq = $_GET['q'];
            $olddestination = $_REQUEST['destination'];
            unset($_REQUEST['destination']);
            $_GET['q'] = "search/{$type_search}/{$keys}";
            $results = theme('search_results', $results, 'node');
            $_GET['q'] = $oldgetq;
            $_REQUEST['destination'] = $olddestination;

            // End of Hack
          }
          else {

            // If no results are found and Lucene Did You Mean is present, then redirect to Lucene DYM
            if (module_exists('luceneapi_dym') && ($suggestions = luceneapi_dym_suggestions_get($keys))) {
              $lucene_dym_path = preg_replace('/^.*href="\\/search\\/luceneapi_node\\/([^"]*)".*$/i', '$1', $suggestions);
              search404_goto($lucene_dym_path);
            }

            // If we are searching with apache solr, use the no results text
            // from the apachesolr_search module (#1127412).
            if (module_exists('apachesolr_search')) {

              // Some versions of apachesolr_search has this as a function
              // instead of a theme function (#1279466).
              if (function_exists('apachesolr_search_noresults')) {
                $results = apachesolr_search_noresults();
              }
              else {
                $results = theme('apachesolr_search_noresults');
              }
            }
            else {
              $results = search_help('search#noresults', drupal_help_arg());
            }
          }
          $results = theme('box', t('Search results'), $results);
        }
      }
    }
    $search_suggestions = '';

    // If Lucent DYM is present then add Lucene "Did You Mean" suggestions
    if (module_exists('luceneapi_dym') && ($suggestions = luceneapi_dym_suggestions_get($keys))) {
      $search_suggestions = theme('box', t('Did you mean'), $suggestions);
    }

    // Construct the search form.
    if (module_exists('fuzzysearch') && variable_get('search404_do_fuzzysearch', FALSE)) {
      $search_form = drupal_get_form('fuzzysearch_form', $keys);
    }
    else {
      $search_form = drupal_get_form('search_form', NULL, $keys, $type_search);
    }
    $output = variable_get('search404_page_text', '') . $search_form . $search_suggestions . $results;
  }

  // This was done to display sidebars left and right of page,
  // if the option is set from Search404 settings Page.
  // However this will cause the program flow to not return
  // to the drupal_not_found() and back to index.php.
  if (variable_get('search404_block_show', FALSE)) {
    print theme('page', $output);
    drupal_page_footer();
    unset($output);
    exit(0);
  }
  else {
    return $output;
  }
}