You are here

function custom_search_submit in Custom Search 7

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

Alter the search to respect the search modes selected.

2 string references to 'custom_search_submit'
custom_search_blocks_form in modules/custom_search_blocks/custom_search_blocks.module
Form builder; Output a search form for the additional search blocks.
custom_search_form_alter in ./custom_search.module
Implements hook_form_alter().

File

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

Code

function custom_search_submit($form, &$form_state) {

  // Save delta for later use (exclusion & refresh).
  $delta = isset($form_state['values']['delta']) ? 'blocks_' . $form_state['values']['delta'] . '_' : '';
  variable_set('custom_search_delta', $delta);
  $type = 'node';
  $keys = trim($form_state['values'][$form_state['values']['form_id']]);
  if (variable_get('custom_search_' . $delta . 'operator', 'AND') == 'OR') {
    $exploded_keys = explode('"', ' ' . $keys);

    // Make sure quotes are never the first char
    $keys_array = array();
    for ($a = 0; $a < count($exploded_keys); $a = $a + 2) {
      if ($a != 0 && strlen($exploded_keys[$a]) || $a == 0 && $exploded_keys[$a] != ' ') {
        if ($a == 0) {
          $exploded_keys[$a] = substr($exploded_keys[$a], 1);
        }
        $keys_array[] = str_replace(' ', ' OR ', $exploded_keys[$a]);
      }
      if (isset($exploded_keys[$a + 1])) {
        $keys_array[] = '"' . $exploded_keys[$a + 1] . '"';
      }
    }
    $keys = implode(' ', $keys_array);
  }
  $original_keywords = $keys;
  $types = isset($form_state['values']['custom_search_types']) ? $form_state['values']['custom_search_types'] : array();
  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) {
      if (isset($form_state['values']['custom_search_vocabulary_' . $voc->vid])) {
        $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);
    }
  }

  // Invoke other modules hooks.
  $other = array();
  foreach (module_implements('custom_search_filter') as $module) {
    $function = $module . '_custom_search_filter';
    if (function_exists($function)) {
      $other = call_user_func_array($function, array(
        &$form,
        &$form_state,
        $other,
      ));
    }
  }
  $search_types = module_implements('search_info');
  if (in_array(current($types), $search_types)) {
    $type = current($types);
    $info = module_invoke($type, 'search_info');
    $path = isset($info['path']) ? $info['path'] : $type;
  }
  else {
    $path = $type;
    if (isset($form_state['values']['custom_search_criteria_or']) && trim($form_state['values']['custom_search_criteria_or']) != '') {
      $keys .= ' ' . str_replace(' ', ' OR ', trim($form_state['values']['custom_search_criteria_or']));
    }
    if (isset($form_state['values']['custom_search_criteria_negative']) && trim($form_state['values']['custom_search_criteria_negative']) != '') {
      $keys .= ' -' . str_replace(' ', ' -', trim($form_state['values']['custom_search_criteria_negative']));
    }
    if (isset($form_state['values']['custom_search_criteria_phrase']) && 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_expression_insert($keys, 'type', implode(',', $types));
      }
      elseif (variable_get('custom_search_' . $delta . 'any_restricts', FALSE)) {
        $types = array_keys(array_filter(variable_get('custom_search_' . $delta . 'node_types', array())));
        $keys = search_expression_insert($keys, 'type', implode(',', $types));
      }
    }
    if (module_exists('taxonomy') && count($terms)) {
      $keys = search_expression_insert($keys, 'term', implode(',', $terms));
    }
    if (module_exists('custom_search_i18n')) {
      $i18n_search_language = variable_get('custom_search_i18n_search_language', 'all');
      if ($i18n_search_language == 'current') {
        $keys = search_expression_insert($keys, 'language', i18n_language()->language);
      }
      elseif ($i18n_search_language == 'current_neutral') {
        $keys = search_expression_insert($keys, 'language', i18n_language()->language . ',und');
      }
    }
  }
  $search_path = array(
    'path' => 'search/' . $path . '/' . $keys,
    'query' => array(),
  );

  // Integrates other search modules.
  if (module_exists('apachesolr_search')) {
    $fields = field_info_fields();
    $node_types = array_keys(node_type_get_types());
    foreach ($types as $key => $bundle) {

      // Remove the item from the array if it's not a content type.
      if (!in_array($bundle, $node_types)) {
        unset($types[$key]);
      }
    }
    $search_path = _custom_search_apachesolr_search(array(
      'keywords' => $original_keywords,
      'types' => $types,
      'terms' => !empty($terms) ? $terms : array(),
      'other' => !empty($other) ? $other : array(),
    ), $keys, $fields);
  }
  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(),
    ));
  }
  elseif (module_exists('search_api_page')) {
    $search_api_page = search_api_page_load(variable_get('custom_search_' . $delta . 'search_api_page', 0));
    if ($search_api_page) {
      $search_path = _custom_search_search_api_search(array(
        'keywords' => $original_keywords,
        'types' => $types,
        'terms' => !empty($terms) ? $terms : array(),
        'page' => $search_api_page,
      ));
    }
  }

  // Build a custom path if needed.
  if (isset($form_state['values']['custom_search_paths']) && $form_state['values']['custom_search_paths'] != '') {
    $key = $form_state['values'][$form_state['values']['form_id']];
    $custom_path = str_replace('[key]', $key, $form_state['values']['custom_search_paths']);
    $custom_path = str_replace('[current_path]', current_path(), $custom_path);
    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);
    }

    // Check for a query string.
    $custom_path_query_position = strpos($custom_path, '?');
    $custom_path_query = array();
    if ($custom_path_query_position !== FALSE) {
      $custom_path_query_tmp = substr($custom_path, 1 + $custom_path_query_position);
      $custom_path_query_tmp = str_replace('&amp;', '&', $custom_path_query_tmp);
      $custom_path_query_tmp = explode('&', $custom_path_query_tmp);
      foreach ($custom_path_query_tmp as $param) {
        $param_exploded = explode('=', $param);
        $custom_path_query[$param_exploded[0]] = $param_exploded[1];
      }
      $custom_path = substr($custom_path, 0, $custom_path_query_position);
    }

    // Check for external path. If not, add base path.
    if (drupal_substr($custom_path, 0, 4) != 'http') {
      $custom_path = url($custom_path, array(
        'absolute' => TRUE,
      ));
    }

    // Send the final url.
    $form_state['redirect'] = url($custom_path, array(
      'query' => $custom_path_query,
      'absolute' => TRUE,
    ));
  }
  else {
    $form_state['redirect'] = url($search_path['path'], array(
      'query' => $search_path['query'],
      'absolute' => TRUE,
    ));
  }
}