1<?php
2/**
3 * @file
4 * Provide views data and handlers for search.module
5 */
6
7/**
8 * @defgroup views_search_module search.module handlers
9 *
10 * Includes the tables 'search_index'
11 * @{
12 */
13
14/**
15 * Implementation of hook_views_data()
16 */
17function search_views_data() {
18  // Basic table information.
19
20  // Define the base group of this table. Fields that don't
21  // have a group defined will go into this field by default.
22  $data['search_index']['table']['group']  = t('Search');
23
24  // For other base tables, explain how we join
25  $data['search_index']['table']['join'] = array(
26    'node' => array(
27      'left_field' => 'nid',
28      'field' => 'sid',
29    ),
30  );
31
32  $data['search_total']['table']['join'] = array(
33    'node' => array(
34      'left_table' => 'search_index',
35      'left_field' => 'word',
36      'field' => 'word',
37    ),
38    'users' => array(
39      'left_table' => 'search_index',
40      'left_field' => 'word',
41      'field' => 'word',
42    )
43  );
44
45  $data['search_dataset']['table']['join'] = array(
46    'node' => array(
47      'left_table' => 'search_index',
48      'left_field' => 'sid',
49      'field' => 'sid',
50      'extra' => 'search_index.type = search_dataset.type',
51      'type' => 'INNER',
52    ),
53    'users' => array(
54      'left_table' => 'search_index',
55      'left_field' => 'sid',
56      'field' => 'sid',
57      'extra' => 'search_index.type = search_dataset.type',
58      'type' => 'INNER',
59    ),
60  );
61
62  // ----------------------------------------------------------------
63  // Fields
64
65  // score
66  $data['search_index']['score'] = array(
67    'title' => t('Score'),
68    'help' => t('The score of the search item. This will not be used if the search filter is not also present.'),
69    'field' => array(
70      'handler' => 'views_handler_field_search_score',
71      'click sortable' => TRUE,
72      'float' => TRUE,
73    ),
74    // Information for sorting on a search score.
75    'sort' => array(
76      'handler' => 'views_handler_sort_search_score',
77    ),
78  );
79
80  // Search node links: forward links.
81  $data['search_node_links_from']['table']['group'] = t('Search');
82  $data['search_node_links_from']['table']['join'] = array(
83    'node' => array(
84      'arguments' => array('search_node_links', 'node', 'nid', 'nid', NULL, 'INNER'),
85    ),
86  );
87  $data['search_node_links_from']['sid'] = array(
88    'title' => t('Links from'),
89    'help' => t('Other nodes that are linked from the node.'),
90    'argument' => array(
91      'handler' => 'views_handler_argument_node_nid',
92    ),
93    'filter' => array(
94      'handler' => 'views_handler_filter_equality',
95    ),
96  );
97
98  // Search node links: backlinks.
99  $data['search_node_links_to']['table']['group'] = t('Search');
100  $data['search_node_links_to']['table']['join'] = array(
101    'node' => array(
102      'arguments' => array('search_node_links', 'node', 'nid', 'sid', NULL, 'INNER'),
103    ),
104  );
105  $data['search_node_links_to']['nid'] = array(
106    'title' => t('Links to'),
107    'help' => t('Other nodes that link to the node.'),
108    'argument' => array(
109      'handler' => 'views_handler_argument_node_nid',
110    ),
111    'filter' => array(
112      'handler' => 'views_handler_filter_equality',
113    ),
114  );
115
116  // search filter
117  $data['search_index']['keys'] = array(
118    'title' => t('Search Terms'), // The item it appears as on the UI,
119    'help' => t('The terms to search for.'), // The help that appears on the UI,
120    // Information for searching terms using the full search syntax
121    'filter' => array(
122      'handler' => 'views_handler_filter_search',
123    ),
124  );
125
126  return $data;
127}
128
129/**
130 * Implementation of hook_views_handlers() to register all of the basic handlers
131 * views uses.
132 */
133function search_views_handlers() {
134  return array(
135    'info' => array(
136      'path' => drupal_get_path('module', 'views') . '/modules/search',
137    ),
138    'handlers' => array(
139      'views_handler_field_search_score' => array(
140        'parent' => 'views_handler_field_numeric',
141      ),
142      'views_handler_sort_search_score' => array(
143        'parent' => 'views_handler_sort',
144      ),
145      'views_handler_filter_search' => array(
146        'parent' => 'views_handler_filter',
147      ),
148    ),
149  );
150}
151
152/**
153 * Implementation of hook_views_plugins
154 */
155function search_views_plugins() {
156  return;
157  // DISABLED. This currently doesn't work.
158  return array(
159    'module' => 'views', // This just tells our themes are elsewhere.
160    'row' => array(
161      'search' => array(
162        'title' => t('Search'),
163        'help' => t('Display the results with standard search view.'),
164        'handler' => 'views_plugin_row_search_view',
165        'path' => drupal_get_path('module', 'views') . '/modules/search', // not necessary for most modules
166        'theme' => 'views_view_row_search',
167        'base' => array('node'), // only works with 'node' as base.
168        'type' => 'normal',
169      ),
170    ),
171  );
172}
173
174/**
175 * Template helper for theme_views_view_row_search
176 */
177function template_preprocess_views_view_row_search(&$vars) {
178  $vars['node'] = ''; // make sure var is defined.
179  $nid = $vars['row']->nid;
180  if (!is_numeric($nid)) {
181    return;
182  }
183
184  $node = node_load($nid);
185
186  if (empty($node)) {
187    return;
188  }
189
190  // Build the node body.
191  $node = node_build_content($node, FALSE, FALSE);
192  $node->body = drupal_render($node->content);
193
194  // Fetch comments for snippet
195  $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
196
197  // Fetch terms for snippet
198  $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');
199
200  $vars['url'] = url('node/' . $nid);
201  $vars['title'] = check_plain($node->title);
202
203  $info = array();
204  $info['type'] = node_get_types('name', $node);
205  $info['user'] = theme('username', $node);
206  $info['date'] = format_date($node->changed, 'small');
207  $extra = node_invoke_nodeapi($node, 'search result');
208  if (isset($extra) && is_array($extra)) {
209    $info = array_merge($info, $extra);
210  }
211  $vars['info_split'] = $info;
212  $vars['info'] = implode(' - ', $info);
213
214  $vars['node'] = $node;
215  // @todo: get score from ???
216//$vars['score'] = $item->score;
217  $vars['snippet'] = search_excerpt($vars['view']->value, $node->body);
218}
219
220
221/**
222 * @}
223 */
224