public
Description: A revamped search module for Drupal, taking advantage of PostgreSQL's advanced full text search.
Homepage:
Clone URL: git://github.com/mikl/drupal-tsearch.git
drupal-tsearch / tsearch.pages.inc
100644 131 lines (116 sloc) 4.353 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// $Id: search.pages.inc,v 1.4 2007/12/06 09:51:01 goba Exp $
 
/**
 * @file
 * User page callbacks for the search module.
 */
 
/**
 * Menu callback; presents the search form and/or search results.
 */
function tsearch_view($type = 'node') {
  // Search form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle:
  // search/type/keyword+keyword
  if (!isset($_POST['form_id'])) {
    if ($type == '') {
      // Note: search/node can not be a default tab because it would take on the
      // path of its parent (search). It would prevent remembering keywords when
      // switching tabs. This is why we drupal_goto to it from the parent instead.
      drupal_goto('tsearch/node');
    }
 
    // Construct the search form.
    $output = drupal_get_form('tsearch_form', NULL, $keys, $type);;
 
    $keys = tsearch_get_keys();
    // Only perform search if there is non-whitespace search term:
    if (trim($keys)) {
      // Log the search keys:
      watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'tsearch', 'name')), WATCHDOG_NOTICE, l(t('results'), 'tsearch/'. $type .'/'. $keys));
 
      // Collect the search results:
      $results = tsearch_data($keys, $type);
 
      if (!empty($results)) {
        $rendered_results = theme('tsearch_results', $results, $type);
        $output .= theme('box', t('Search results'), $rendered_results);
      }
      else {
        $output .= theme('box', t('Your search yielded no results'), tsearch_help('search#noresults', drupal_help_arg()));
      }
    }
 
    return $output;
  }
 
  return drupal_get_form('tsearch_form', NULL, empty($keys) ? '' : $keys, $type);
}
 
/**
 * Process variables for search-results.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $results
 * - $type
 *
 * @see search-results.tpl.php
 */
function template_preprocess_tsearch_results(&$variables) {
  $variables['search_results'] = '';
  foreach ($variables['results'] as $result) {
    $variables['search_results'] .= theme('tsearch_result', $result, $variables['type']);
  }
  $variables['pager'] = theme('pager', NULL, 10, 0);
  // Provide alternate search results template.
  $variables['template_files'][] = 'tsearch-results-'. $variables['type'];
}
 
/**
 * Process variables for search-result.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $result
 * - $type
 *
 * @see search-result.tpl.php
 */
function template_preprocess_tsearch_result(&$variables) {
  $result = $variables['result'];
  $variables['url'] = check_url($result['link']);
  $variables['title'] = check_plain($result['title']);
 
  $info = array();
  if (!empty($result['type'])) {
    $info['type'] = check_plain($result['type']);
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = implode(' - ', $info);
  // Provide alternate search result template.
  $variables['template_files'][] = 'search-result-'. $variables['type'];
}
 
/**
 * As the search form collates keys from other modules hooked in via
 * hook_form_alter, the validation takes place in _submit.
 * tsearch_form_validate() is used solely to set the 'processed_keys' form
 * value for the basic search form.
 */
function tsearch_form_validate($form, &$form_state) {
  form_set_value($form['basic']['inline']['processed_keys'],
                 trim($form_state['values']['keys']), $form_state);
}
 
/**
 * Process a search form submission.
 */
function tsearch_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 drupal_goto() call.
  }
 
  $type = $form_state['values']['module'] ? $form_state['values']['module'] : 'node';
  $form_state['redirect'] = 'tsearch/'. $type .'/'. $keys;
  return;
}