view sites/all/modules/custom/solrsearch/solrsearch.pages.inc @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
line wrap: on
line source

<?php

/**
 * @file
 * User page callbacks for the search module.
 */

/**
 * Menu callback; presents the search form and/or search results.
 *
 * @param $module
 *   Search module to use for the search.
 * @param $keys
 *   Keywords to use for the search.
 */
function solrsearch_view($keys = '') {

  $keys = trim($keys);
  // Also try to pull search keywords out of the $_REQUEST variable to
  // support old GET format of searches for existing links.
  if (!$keys && !empty($_REQUEST['keys'])) {
    $keys = trim($_REQUEST['keys']);
  }



  // Default results output is an empty string.
  $results = array('#markup' => '');
  // Process the search form. Note that if there is $_POST data,
  // search_form_submit() will cause a redirect to search/[module path]/[keys],
  // which will get us back to this page callback. In other words, the search
  // form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle.

  if (empty($_POST['form_id']) || $_POST['form_id'] != 'solrsearch_form') {

    $conditions =  NULL;

    $conditions = solrsearch_search_conditions($keys);

    // Only search if there are keywords or non-empty conditions.
    if ($keys || !empty($conditions)) {

      // Log the search keys.
      //watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), 'search/' . $info['path'] . '/' . $keys));

      // Collect the search results.
      $results = solrsearch_data($keys, 'solrsearch_search', $conditions);

    }
  }
  // The form may be altered based on whether the search was run.

  $wrapper = array(
      '#type' => 'container',
      '#attributes' => array(
          'class' => array('tool','box'),
      ),
  );



  $wrapper['form'] = drupal_get_form('solrsearch_form', NULL, $keys);

  $build['search_form'] = $wrapper;


  $build['search_results'] = $results;


  return $build;
}

/**
 * Process variables for search-results.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $results: Search results array.
 * - $module: Module the search results came from (module implementing
 *   hook_search_info()).
 *
 * @see search-results.tpl.php
 */
function template_preprocess_solrsearch_results(&$variables) {
  $variables['search_results'] = '';
  if (!empty($variables['module'])) {
    $variables['module'] = check_plain($variables['module']);
  }




  if (user_access("manage private collections")){
    $variables['digitalobjects_items_select']=base_path()."digitalcollections/manageCurrent";
    $variables['search_results'] .='<tr><td colspan="5"><input type="submit" value="add selected to current collection">';
    $variables['search_results'] .='<input type="hidden" name="redirect" value="' . urlencode(current_path() .'?' . $_SERVER['QUERY_STRING']) . '"></td></tr>';
  }

  foreach ($variables['results'] as $result) {


    /* add checkboxes for the item */

    $variables['search_results'] .= '<tr><td colspan="5">';

    if (user_access("manage private collections")){
    $variables['search_results'] .= '<input type="checkbox" name="digitalobjects_items_select[]" value="'.$result['mpiwg-dri'].'"/>';
    }

    /* add tools for the item */
      $variables['search_results'] .= theme('digitalobjects_item_tools', array('objid' => $result['mpiwg-dri'])) . "</td></tr>";


    $variables['search_results'] .= theme('solrsearch_result', array('result' => $result, 'module' => $variables['module']));

  }
  $variables['pager'] = theme('pager', array('tags' => array('<<','<','...','>','>>'),'quantity' => 5));

  $variables['description'] = '';
  $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}


function check_array_plain($result,$field){
  if (!isset($result[$field])){
    return null;
  }

   $string = $result[$field];

    if (is_array($string)){
      return check_plain(implode($string));
    } else {
      return check_plain($string);
  }
}
/**
 * Process variables for search-result.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $result
 * - $module
 *
 * @see search-result.tpl.php
 */
function template_preprocess_solrsearch_result(&$variables) {
  global $language;

  $result = $variables['result'];

  $variables['url'] = create_url_from_dri($result['mpiwg-dri'],$result['access-type']);
  $variables['access_type'] = $result['access-type']!="free" ? "restricted" :$result['access-type'];
  $variables['title'] = check_plain($result['title']);
  $variables['author'] = check_plain($result['author']);
  $variables['date'] = check_array_plain($result,'date');
  $variables['signature'] = check_array_plain($result,'signature');
  $variables['author'] = check_plain($result['author']);
  $variables['year'] = is_array($result['year']) ? implode($result['year']) :$result['year'];
  $variables['thumburl'] = create_thumburl_from_dri($result['mpiwg-dri']);
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';

  $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
}

/**
 * As the search form collates keys from other modules hooked in via
 * hook_form_alter, the validation takes place in _submit.
 * search_form_validate() is used solely to set the 'processed_keys' form
 * value for the basic search form.
 */
function solrsearch_form_validate($form, &$form_state) {
  form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
}

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


  $keys = $form_state['values']['processed_keys'];
  if ($keys == '') {
    form_set_error('keys', t('Please enter some keywords.'));
    // Fall through to the form redirect.
  }

  $exact = $form_state['values']['exact'];
  if ($exact==0) { #nicht genaue suche dann trunkiere das wort
   $keys .="*";
  }
  $form_state['redirect'] = $form_state['action'] . '/' . $keys;
}


/**
 * Builds a search form.
 *
 * @param $action
 *   Form action. Defaults to "search/$path", where $path is the search path
 *   associated with the module in its hook_search_info(). This will be
 *   run through url().
 * @param $keys
 *   The search string entered by the user, containing keywords for the search.
 * @param $module
 *   The search module to render the form for: a module that implements
 *   hook_search_info(). If not supplied, the default search module is used.
 * @param $prompt
 *   Label for the keywords field. Defaults to t('Enter your keywords') if NULL.
 *   Supply '' to omit.
 *
 * @return
 *   A Form API array for the search form.
 */
function solrsearch_form($form, &$form_state, $action = '', $keys = '', $module = NULL, $prompt = NULL) {
  $module_info = FALSE;

  if (!$action) {
    $action = 'solrsearch';
  }
  if (!isset($prompt)) {
    $prompt = t('Enter your keywords');

  }

  if ($keys == ''){
    $keys=$prompt;
  }

  $form['#action'] = url($action);

  // Record the $action for later use in redirecting.
  $form_state['action'] = $action;
  $form['#attributes']['class'][] = 'search-form';
  $form['module'] = array('#type' => 'value', '#value' => $module);
  $form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('container-inline','searchbox')));

  $form['basic']['keys'] = array(
      '#type' => 'textfield',
      '#size' => $prompt ? 40 : 20,
      '#attributes' =>array('placeholder' => t($keys),'class' => array('text')),
      '#maxlength' => 255,
  );
  // processed_keys is used to coordinate keyword passing between other forms
  // that hook into the basic search form.
  $form['basic']['processed_keys'] = array('#type' => 'value', '#value' => '');


  $form['basic']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('>'),
      '#attributes' =>array('class' => array('submit')),

  );

  $form['basic']['exact'] = array(
      '#type' => 'checkbox',
      '#title' => 'whole word',



  );


  return $form;
}