-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbento_box_search.module
243 lines (207 loc) · 6.97 KB
/
bento_box_search.module
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* @file
* Hook implementations for Bento Box Search Module
*
* This module creates a search block allowing users to search multiple systems.
* It also creates a custom content type that stores information aobut the systems
* to be queried.
*
* The results page queries each system enabled using an ajax request,
* displaying the top results as they are received in their own box using html divs.
*/
require_once drupal_get_path('module', 'bento_box_search') . '/includes/html_parser.inc';
require_once drupal_get_path('module', 'bento_box_search') . '/includes/profile_extractor.inc';
//TODO: Make this configurable by user
define('MAX_RESULT_COUNT', 5);
define('ZERO_RESULT_MESSAGE', '<p>Your search returned 0 results.</p>');
/**
* Implements hook_block_info(). It lets Drupal know what blocks we want to create.
*/
function bento_box_search_block_info() {
$blocks = array();
$blocks['bento_box_search_block'] = array(
'info' => t('A search box with options for systems to be searched'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['bento_profile_block'] = array(
'info' => t('A block that displays a search related profile.'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view(). It returns the content for the blocks we created using
* hook_block_info().
*/
function bento_box_search_block_view($block_name = '') {
// Check which block is being requested
if ($block_name == 'bento_box_search_block') {
$block = array(
'content' => theme('bento_box_search_form'), //use hook_theme provided by this module.
);
return $block;
}
if ($block_name == 'bento_profile_block') {
$block = array(
'content' => theme('bento_profile_block'), //use hook_theme provided by this module.
);
return $block;
}
}
/**
* Implements hook_theme().
*/
function bento_box_search_theme() {
return array(
'bento_box_search_form' => array(
'path' => drupal_get_path('module', 'bento_box_search').'/theme',
'template' => 'bento-box-search-block', //see bento-box-search-block.tpl.php
),
'bento_box_search_results' => array(
'path' => drupal_get_path('module', 'bento_box_search').'/theme',
'template' => 'bento-box-search-results-page', //see bento-box-search-results-page.tpl.php
),
'bento_profile_block' => array(
'path' => drupal_get_path('module', 'bento_box_search').'/theme',
'template' => 'bento-profile-block', //see bento-box-search-results-page.tpl.php
),
);
}
/**
* Preprocess variables to be passed to the search block template assigned above in hook_theme().
*/
function template_preprocess_bento_box_search_form(&$variables) {
$search_box = drupal_render (drupal_get_form('bento_search_form'));
$variables['search_box'] = $search_box;
}
/**
* Preprocess variables to be passed to the profile block template assigned above in hook_theme().
*/
function template_preprocess_bento_profile_block(&$variables) {
$profile = 'No profile found.';
$search_sources = _search_source_list();
foreach ($search_sources as $search_source) {
$node = node_load($search_source->nid);
$extract_profile_field = $node->extract_profile;
$extract_profile = $extract_profile_field['und'][0]['value'];
if($extract_profile) {
$profile = _extract_profile($search_source->nid, $_SESSION['bento_search']['search_input']);
}
}
$variables['profile'] = $profile;
}
/**
* Preprocess variables to be passed to the results page template assigned above in hook_theme().
*/
function template_preprocess_bento_box_search_results(&$variables) {
drupal_add_js(drupal_get_path('module', 'bento_box_search') . '/js/bento_box_search.js');
$variables['search_input'] = $_SESSION['bento_search']['search_input'];
$result = _search_source_list();
$count = 0;
foreach ($result as $search_source) {
$variables['search_sources'][] = $search_source;
//replace the keywords from the search into the search url
$bento_query_url = $search_source->nid . "|" . $variables['search_input'];
drupal_add_js(array('bento_box_search' =>
array($search_source->nid => $bento_query_url)), 'setting');
$count++;
}
$variables['ss_count'] = $count;
$variables['ss_half_round'] = round($count/2);
}
/**
* Implements hook_form() for Bento Box Search Box in block.
*/
function bento_search_form($form, &$form_state) {
$form['bento_search_input'] = array(
'#type' => 'textfield',
'#required' => TRUE,
);
$form['bento_submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
/**
* Implements hook_form_submit(). Tells Drupal what to do once the search
* keywords have been successfully submitted.
*/
function bento_search_form_submit($form, &$form_state) {
$_SESSION['bento_search']['search_input'] = $form_state['values']['bento_search_input'];
$form_state['redirect'] = 'bento_search_results/' . $form_state['values']['bento_search_input'];
}
/**
* Implements hook_menu(). Creates the path for the results page and
* the individual query results for each system.
*/
function bento_box_search_menu() {
$menu['bento_search_results'] = array (
'title' => 'Search Results',
'page callback' => 'bento_search_results_page',
'access callback' => TRUE,
);
$menu['bento_search_query/%/%'] = array (
'page callback' => 'bento_search_query_result',
'page arguments' => array(1,2),
'access callback' => TRUE,
);
return $menu;
}
/**
* Callback function for search results page.
*/
function bento_search_results_page() {
$content = theme('bento_box_search_results');
return $content;
}
/**
* Callback function for system query.
*/
function bento_search_query_result($nid, $keywords) {
$results_html = _process_system_query($nid, $keywords);
print $results_html;
drupal_exit();
}
/**
* Done with code related to search block. Now we deal with the Content Type
* we create on install for the systems we want to search.
*/
/**
* Implements hook_node_info(). Provides detail for new Content Type.
*/
function bento_box_search_node_info() {
return array (
'search_source' => array(
'name' => t('Search Source'),
'base' => 'search_source',
'description' => t('Systems to be queried by Bento Box Search'),
'has_title' => TRUE,
'title_label' => t('Search Source Name'),
)
);
}
/**
* Implements hook_form() for custom content type.
*/
function search_source_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Returns a list of search sources.
*
* @return
* A result set object containing the list of search sources.
*/
function _search_source_list() {
// Use Database API to retrieve all Search Source Nodes.
$query = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created', 'uid'))
->condition('type', 'search_source')
->condition('status', 1)
->orderBy('created', 'DESC')
->addTag('node_access')
->execute();
return $query;
}