You are here

apachesolr_panels.module in Apache Solr Panels 6.3

Same filename and directory in other branches
  1. 6 apachesolr_panels.module
  2. 7 apachesolr_panels.module

Integrates Apache Solr Search with Panels.

Authors: Joakim Stai (ximo) <http://drupal.org/user/88701) Kevin Bridges (cyberswat) <http://drupal.org/user/27802>

File

apachesolr_panels.module
View source
<?php

/**
 * @file
 * Integrates Apache Solr Search with Panels.
 *
 * Authors:
 *   Joakim Stai (ximo) <http://drupal.org/user/88701)
 *   Kevin Bridges (cyberswat) <http://drupal.org/user/27802>
 */

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function apachesolr_panels_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && !empty($plugin)) {
    return "plugins/{$plugin}";
  }
}

/**
 * Implementation of hook_forms().
 */
function apachesolr_panels_forms() {
  $forms['apachesolr_panels_search_form'] = array(
    'callback' => 'search_form',
  );
  return $forms;
}

/**
 * Implementation of hook_block().
 */
function apachesolr_panels_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Search form (Apache Solr Panels)');

    // Not worth caching.
    $blocks[0]['cache'] = BLOCK_NO_CACHE;
    return $blocks;
  }
  elseif ($op == 'configure') {
    $form['search_page'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to search page'),
      '#default_value' => variable_get('apachesolr_panels_search_page', ''),
    );
    return $form;
  }
  elseif ($op == 'save') {
    variable_set('apachesolr_panels_search_page', $edit['search_page']);
  }
  elseif ($op == 'view' && user_access('search content')) {
    $block['content'] = drupal_get_form('apachesolr_panels_search_block');
    $block['subject'] = t('Search');
    return $block;
  }
}

/**
 * Output a search form for the search block, based on core's search box.
 */
function apachesolr_panels_search_block(&$form_state) {
  $form = search_box($form_state, 'apachesolr_panels_search_block_form');
  $form['#submit'] = array(
    'apachesolr_panels_search_block_form_submit',
  );
  return $form;
}

/**
 * Process a block search form submission.
 */
function apachesolr_panels_search_block_form_submit($form, &$form_state) {

  // Allow core to ensure we have control of the redirect destination.
  search_box_form_submit($form, $form_state);

  // Redirect to the specified search page.
  $path = variable_get('apachesolr_panels_search_page', '');
  $form_state['redirect'] = $path . '/' . trim($form_state['values']['apachesolr_panels_search_block_form']);
}

/**
 * Execute a Solr search.
 *
 * Using hook_search() would make this module a first-class search module
 * alongside Apache Solr Search, which we don't want. Instead, we execute the
 * search directly and imitate what the core Search module does.
 *
 * @see search_data()
 */
function apachesolr_panels_search_execute($search) {

  // Store information about the search for use in other panes.
  apachesolr_panels_static_search_cache($search);
  $results = NULL;
  try {
    $solr = apachesolr_get_solr($search['env_id']);
    $results = apachesolr_search_run('apachesolr' . uniqid(), array_filter(array(
      'q' => $search['keys'],
      'fq' => $search['filters'],
      'rows' => $search['rows'],
    )), $search['sort'], $search['path'], $search['page'], $solr);
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
    apachesolr_failure(t('Solr search'), $search['keys']);
  }

  // Imitate search_data() to present the results as Apache Solr Search would.
  if (is_array($results) && count($results)) {
    return theme('search_results', $results, 'apachesolr_search', array(
      'env_id' => $search['env_id'],
    ));
  }
}

/**
 * Store/fetch information about the executed search.
 *
 * The search will be stored by the "Search result" pane and fetched by the
 * "Search form" and "Search information" panes.
 */
function apachesolr_panels_static_search_cache($search = NULL) {
  static $_search = NULL;
  if (!empty($search)) {
    $_search = array(
      'keys' => '',
      'filters' => '',
      'rows' => '',
      'env_id' => '',
      'sort' => '',
      'path' => '',
      'page' => 0,
    );
    $_search = array_merge($_search, $search);
  }
  return $_search;
}

Functions

Namesort descending Description
apachesolr_panels_block Implementation of hook_block().
apachesolr_panels_ctools_plugin_directory Implementation of hook_ctools_plugin_directory().
apachesolr_panels_forms Implementation of hook_forms().
apachesolr_panels_search_block Output a search form for the search block, based on core's search box.
apachesolr_panels_search_block_form_submit Process a block search form submission.
apachesolr_panels_search_execute Execute a Solr search.
apachesolr_panels_static_search_cache Store/fetch information about the executed search.