You are here

function apachesolr_search_init in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr_search.module \apachesolr_search_init()
  2. 7 apachesolr_search.module \apachesolr_search_init()

Implements hook_init().

Checks if we should run an empty facet query so the facet blocks can be displayed.

File

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

Code

function apachesolr_search_init() {

  // Useless without facetapi
  if (!module_exists('facetapi')) {
    return NULL;
  }

  // Using a simple query we will figure out if we have to execute this snippet
  // on every page or exit as fast as possible.
  $query = "SELECT count(env_id)\n    FROM {apachesolr_environment_variable}\n    WHERE name = 'apachesolr_search_show_facets'";
  $count = db_query($query)
    ->fetchField();
  if ($count == 0) {
    return NULL;
  }

  // Load the default search page, we only support facets to link to this
  // search page due to complexity and slow downs
  $search_page_id = apachesolr_search_default_search_page();
  $search_page = apachesolr_search_page_load($search_page_id);

  // Do not continue if our search page is not valid
  if (empty($search_page)) {
    return NULL;
  }
  $show_facets = apachesolr_environment_variable_get($search_page['env_id'], 'apachesolr_search_show_facets', 0);
  if ($show_facets) {

    // Converts current path to lowercase for case insensitive matching.
    $paths = array();
    $path = drupal_strtolower(drupal_get_path_alias(current_path()));

    // Use the path as the key to keep entries unique.
    $paths[$path] = $path;
    $path = drupal_strtolower(current_path());
    $paths[$path] = $path;

    // Do not continue if the current path is the default search path.
    foreach ($paths as $path) {
      if (drupal_match_path($path, $search_page['search_path'] . '*')) {
        return;
      }
    }
    $facet_pages = apachesolr_environment_variable_get($search_page['env_id'], 'apachesolr_search_facet_pages', '');

    // Iterates over each environment to check if an empty query should be run.
    if (!empty($facet_pages)) {

      // Compares path with settings, runs query if there is a match.
      $patterns = drupal_strtolower($facet_pages);
      foreach ($paths as $path) {
        if (drupal_match_path($path, $patterns)) {
          try {
            if (!empty($search_page['search_path'])) {
              $solr = apachesolr_get_solr($search_page['env_id']);

              // Initializes params for empty query.
              $params = array(
                'spellcheck' => 'false',
                'fq' => array(),
                'rows' => 1,
              );
              $context['page_id'] = $search_page_id;
              $context['search_type'] = 'apachesolr_search_show_facets';
              apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr, $context);

              // Exit the foreach loop if this has run.
              break;
            }
          } catch (Exception $e) {
            watchdog('Apache Solr', nl2br(check_plain($e
              ->getMessage())), NULL, WATCHDOG_ERROR);
          }
        }
      }
    }
  }
}