You are here

function custom_search_submit in Custom Search 6

Same name and namespace in other branches
  1. 7 custom_search.module \custom_search_submit()

Alter the search to respect the search modes selected.

1 string reference to 'custom_search_submit'
custom_search_form_alter in ./custom_search.module
Implementation of hook_form_alter().

File

./custom_search.module, line 325
Bring customizations to the default search box

Code

function custom_search_submit($form, &$form_state) {
  $delta = isset($form_state['values']['delta']) ? 'blocks_' . $form_state['values']['delta'] . '_' : '';
  variable_set('custom_search_delta', $delta);

  // save for later use (exclusion & refresh)
  $type = 'node';
  $keys = $form_state['values'][$form_state['values']['form_id']];
  $types = $form_state['values']['custom_search_types'];
  if (!is_array($types)) {
    $types = array(
      $types,
    );
  }
  $types = array_map('_custom_search_filter_keys', array_filter($types));
  if (module_exists('taxonomy')) {
    $terms = array();
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $voc) {
      $vterms = $form_state['values']['custom_search_vocabulary_' . $voc->vid];
      if (!is_array($vterms)) {
        $vterms = array(
          $vterms,
        );
      }
      $terms = array_merge($terms, $vterms);
    }
    $terms = array_map('_custom_search_filter_keys', array_values(array_filter($terms)));

    // if one or more -Any- is selected, delete them
    while (($index = array_search('all', $terms)) !== FALSE) {
      array_splice($terms, $index, 1);
    }
  }
  $search_types = module_implements('search');
  if (in_array(current($types), $search_types)) {
    $type = current($types);
  }
  else {
    if (trim($form_state['values']['custom_search_criteria_or']) != '') {
      $keys .= ' ' . str_replace(' ', ' OR ', trim($form_state['values']['custom_search_criteria_or']));
    }
    if (trim($form_state['values']['custom_search_criteria_negative']) != '') {
      $keys .= ' -' . str_replace(' ', ' -', trim($form_state['values']['custom_search_criteria_negative']));
    }
    if (trim($form_state['values']['custom_search_criteria_phrase']) != '') {
      $keys .= ' "' . trim($form_state['values']['custom_search_criteria_phrase']) . '"';
    }
    $original_keywords = $keys;
    if (count($types)) {

      // If a content type is selected, and it's not -Any-, search for that type.
      if (!in_array('all', $types)) {
        $keys = search_query_insert($keys, 'type', implode(',', $types));
      }
      elseif (variable_get('custom_search_' . $delta . 'any_restricts', FALSE)) {
        $restricted_types = array_keys(array_filter(variable_get('custom_search_' . $delta . 'node_types', array())));
        $keys = search_query_insert($keys, 'type', implode(',', $restricted_types));
      }
    }
    if (module_exists('taxonomy') && count($terms)) {
      $keys = search_query_insert($keys, 'category', implode(',', $terms));
    }
  }

  // Integrates other search modules
  if (module_exists('apachesolr_search') && variable_get('apachesolr_search_make_default', 0)) {
    $search_path = _custom_search_apachesolr_search(array(
      'keywords' => $original_keywords,
      'types' => $types,
      'terms' => !empty($terms) ? $terms : array(),
    ));
  }
  elseif (module_exists('google_appliance')) {
    $search_path = _custom_search_google_appliance_search(array(
      'keys' => $keys,
    ));
  }
  elseif (module_exists('luceneapi_node') && variable_get('luceneapi:default_search', 0)) {
    $search_path = _custom_search_lucenapi_search(array(
      'keywords' => $original_keywords,
      'types' => $types,
      'terms' => !empty($terms) ? $terms : array(),
    ));
  }
  else {
    $search_path = array(
      'path' => 'search/' . $type . '/' . $keys,
      'query' => '',
    );
  }

  // Build a custom path if needed
  if (isset($form_state['values']['custom_search_paths']) && $form_state['values']['custom_search_paths'] != '') {
    $custom_path = str_replace('[key]', $form_state['values'][$form_state['values']['form_id']], $form_state['values']['custom_search_paths']);
    if (strpos($form_state['values']['custom_search_paths'], '[terms]') !== FALSE) {
      $custom_path = str_replace('[terms]', count($terms) ? implode($form_state['values']['custom_search_paths_terms_separator'], $terms) : '', $custom_path);
    }
    $form_state['redirect'] = $custom_path;

    // check for external path. If not, add base path
    if (drupal_substr($custom_path, 0, 4) != 'http') {
      $form_state['redirect'] = url($form_state['redirect'], array(
        'absolute' => TRUE,
      ));
    }
  }
  else {
    $form_state['redirect'] = url($search_path['path'], array(
      'query' => $search_path['query'],
      'absolute' => TRUE,
    ));
  }
}