You are here

function _search_by_page_do_search in Search by Page 6

Internal function: performs a search, for hook_search($op = 'search').

Parameters

$keys: Keywords we are searching for

Return value

Array of search results (see hook_search() for documentation).

1 call to _search_by_page_do_search()
search_by_page_search in ./search_by_page.module
Implementation of hook_search().

File

./search_by_page.module, line 1378
Main module file for Drupal module Search by Page.

Code

function _search_by_page_do_search($keys = NULL) {
  global $pager_page_array, $pager_total, $pager_total_items;
  global $language;
  $lang = $language->language;

  // Extract environment and clear from keys.
  $environment = search_query_extract($keys, 'environment');
  $keys = search_query_insert($keys, 'environment');
  if (!$environment) {
    $environment = variable_get('search_by_page_default_environment', 1);
  }

  // Set up query for Search module.
  $join = 'INNER JOIN {sbp_path} sp ON i.sid = sp.pid';

  // Postgres note: integers are not booleans!
  $where = '0=1';
  $args = array();
  foreach (module_implements('sbp_query_modify') as $module) {
    $stuff = module_invoke($module, 'sbp_query_modify', $environment);
    $join = $join . ' ' . $stuff['join'];
    $where = $where . ' OR (sp.from_module = \'%s\' AND ' . $stuff['where'] . ')';
    $args[] = $module;
    $args = array_merge($args, $stuff['arguments']);
  }
  $mainwhere = 'sp.environment=%d';
  $args[] = $environment;
  if ($lang) {
    $mainwhere .= " AND sp.language='%s'";
    $args[] = $lang;
  }
  $where = '((' . $where . ') AND ' . $mainwhere . ')';

  // Let Search perform the actual search
  $stuff = do_search($keys, 'search_by_page', $join, $where, $args);

  // Save global pager information, page title, etc. because rendering
  // can screw this up.
  $title = drupal_get_title();
  $tmp_parray = $pager_page_array[0];
  $tmp_ptotal = $pager_total[0];
  $tmp_itotal = $pager_total_items[0];
  $tmp_breadcrumb = drupal_get_breadcrumb();
  $tmp_trail = menu_get_active_trail();
  $tmp_menu = menu_get_item();

  // Use PHP's output buffering to prevent any output during rendering.
  // Some modules print things directly when rendering the page, which is
  // not good practice, but so it goes.
  ob_start();

  // Create array of formatted results for Search
  $results = array();
  foreach ($stuff as $item) {
    $info = _search_by_page_lookup($item->sid);

    // Figure out the URL to this page.
    $parts = search_by_page_path_parts($info->page_path);
    $args = array(
      'absolute' => TRUE,
    );
    if (isset($parts[1])) {
      $args['query'] = $parts[1];
    }

    // Special case: if this is a file, we need to use the File API to get
    // the URL. Otherwise, use the normal Drupal URL function.
    if (strpos($parts[0], file_directory_path()) !== FALSE) {

      // This is a file
      $link = file_create_url($parts[0]);
    }
    else {
      $link = url($parts[0], $args);
    }
    $res = array(
      'link' => $link,
    );

    // Merge URL with information provided by module from hook_sbp_details()
    $res2 = module_invoke($info->from_module, 'sbp_details', $info->modid, $environment, $keys);
    if (is_array($res2)) {
      $res = array_merge($res, $res2);
    }

    // Make sure we have a title
    if (!$res['title']) {
      $res['title'] = $link;
    }
    $results[] = $res;
  }

  // Reset page title and pager stuff, and clear the output buffer.
  drupal_set_title($title);
  $pager_page_array[0] = $tmp_parray;
  $pager_total[0] = $tmp_ptotal;
  $pager_total_items[0] = $tmp_itotal;
  menu_set_item(NULL, $tmp_menu);
  drupal_set_breadcrumb($tmp_breadcrumb);
  menu_set_active_trail($tmp_trail);
  ob_end_clean();
  return $results;
}