You are here

function search404_page in Search 404 5

Same name and namespace in other branches
  1. 6 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 Beware of messy code

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

File

./search404.module, line 108

Code

function search404_page() {
  $output = '<p>' . t('The page you requested was not found.') . '</p>';
  if (module_exists('search') && user_access('search content')) {
    $keys = "";
    if (variable_get('search404_use_search_engine', FALSE)) {
      $keys = search404_search_engine_query();
    }
    if (!$keys) {
      $keys = search404_get_keys();
    }
    if ($keys) {

      // TODO: watchdog?
      $results = module_invoke('node', 'search', 'search', $keys);
      if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', FALSE)) {

        // First, check to see if there is exactly 1 result
        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');

        // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()
        $_REQUEST['destination'] = 'node/' . $results[0]['node']->nid;
        drupal_goto();
      }
      elseif (isset($results) && is_array($results) && count($results) > 1 && variable_get('search404_first', 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');

        // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()
        $_REQUEST['destination'] = 'node/' . $results[0]['node']->nid;
        drupal_goto();
      }
      else {
        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);

          // EVIL HAXX!
          $oldgetq = $_GET['q'];
          $olddestination = $_REQUEST['destination'];
          unset($_REQUEST['destination']);
          $_GET['q'] = "search/node/{$keys}";
          $results = theme('search_page', $results, 'node');
          $_GET['q'] = $oldgetq;
          $_REQUEST['destination'] = $olddestination;

          // END OF EVIL HAXX!
        }
        else {
          $results = search_help('search#noresults');
        }
        $results = theme('box', t('Search results'), $results);
      }
    }

    // Construct the search form.
    $output .= drupal_get_form('search_form', NULL, $keys, 'node');
    $output .= $results;
  }
  return $output;
}