view solrsearch.pages.inc @ 0:a2b4f67e73dc default tip

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Mon, 08 Jun 2015 10:21:54 +0200
parents
children
line wrap: on
line source

<?php

/**
 * @file
 * User page callbacks for the search module.
 */
/**
 * Menu callback;saves search in current collection
 *
 * @param $module
 *   Search module to use for the search.
 * @param $keys
 *   Keywords to use for the search.
 */
function solrsearch_save($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']);
  }


  $conditions =  NULL;

  $conditions = solrsearch_search_conditions($keys);
  $conditions['rows']=500; //TODO make this configurable

  // 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 = module_invoke('solrsearch_search', 'search_execute', $keys, $conditions);


  }
  //dpm($keys);
  //dpm($conditions);
  //dpm($results);

  foreach($results as $result){
    $data['digitalobjects_items_select'][]=$result['mpiwg-dri'];
  }

  digitalobjects_digitalcollectionsManage($data);
  return "TEST";
}







/**
 * 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>';


    $sw = explode("/",current_path());
    $key= $sw[sizeof($sw)-1];

    $variables['search_results'] .='<tr><td colspan="5"><a href="../solrsearchsave/' . $key  .'?' . $_SERVER['QUERY_STRING']. '">Save all (max 500) to the current collection</a>';
  }

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




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

    /* add checkboxes for the item */
    $variables['search_results'] .= '<div class="tools">';

    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'])) . "</div></div>";



  }
  $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) {
  

  $result = $variables['result'];
  //dpm($variables);
  $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,'IM_call-number');
  $variables['doc_type'] = check_array_plain($result,'doc-type');
  $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'];

  $idstring = str_replace("INST:", "", $result['archive-path']);
  $path = create_library_path($idstring);

  $variables['librarySearchPath'] = $path; //archive- path enthaelt die ID im Inst. Katalog

  $variables['collectionPath'] = $GLOBALS['base_url'] . str_replace("ECHOCOLL:", "/node/", $result['archive-path']);
  $variables['content'] = $result['content'];

  $variables['external_url']=$result['url'][0];
  $variables['external_image']=$result['image'][0];
  $variables['provider']=$result['provider'][0];
  $variables['dataSource']=$result['dataSource'];


}



/**
 * 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;
}