-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_cse.module
executable file
·336 lines (299 loc) · 8.73 KB
/
google_cse.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* @file
* Display a Google Custom Search Engine (CSE) on your site.
*/
/**
* Implements hook_search_info().
*/
function google_cse_search_info() {
return array(
'title' => google_cse_results_tab(),
'path' => 'google',
'conditions_callback' => 'google_cse_conditions_callback',
);
}
/**
* Search conditions callback.
*/
function google_cse_conditions_callback($keys) {
$conditions = array();
return $conditions;
}
/**
* Implements hook_search_execute().
*/
function google_cse_search_execute($keys = NULL, $conditions = NULL) {
if (variable_get('google_cse_use_adv', 0)) {
// Firstly, load the needed modules.
module_load_include('inc', 'google_cse', 'google_cse_adv/google_cse_adv');
// And get the google results.
$response = google_cse_adv_service($keys);
$results = google_cse_adv_response_results($response[0], $keys, $conditions);
// Allow other modules to alter the keys.
drupal_alter('google_cse_searched_keys', $keys);
return $results;
}
}
/**
* Builds a query array based on Google CSE settings.
*/
function google_cse_build_query($keys, $sitesearch = NULL, $here = TRUE) {
return array(
'query' => $keys,
'cx' => variable_get('google_cse_cx', ''),
'cof' => $here ? variable_get('google_cse_cof_here', 'FORID:11') : variable_get('google_cse_cof_google', 'FORID:0'),
'sitesearch' => isset($sitesearch) ? $sitesearch : google_cse_sitesearch_default(),
) + google_cse_advanced_settings();
}
/**
* Implements hook_search_page().
*/
function google_cse_search_page($results) {
if (!variable_get('google_cse_use_adv', 0)) {
$output['#theme'] = 'google_cse_results';
return $output;
}
else {
if ($results) {
$current_page = isset($_GET['page']) ? $_GET['page'] : 0;
$from = $current_page * 10 + 1;
$to = $current_page * 10 + 10;
$total = $GLOBALS['pager_total_items'][0];
if ($to > $total) {
$to = $total;
}
$number_results = "Results $from to $to of $total matches";
$output['prefix']['#markup'] = $number_results . '<ol class="search-results">';
foreach ($results as $entry) {
$output[] = array(
'#theme' => 'search_result',
'#result' => $entry,
'#module' => 'google_cse',
);
}
// Important, add the pager.
$output['suffix']['#markup'] = '</ol>' . theme('pager');
}
else {
// No results found.
$output['search_results'] = array('#markup' => theme('google_cse_search_noresults'));
}
}
return $output;
}
/**
* Implements hook_search_admin().
*/
function google_cse_search_admin() {
module_load_include('admin.inc', 'google_cse');
return google_cse_admin_settings();
}
/**
* Implements hook_search_access().
*/
function google_cse_search_access() {
return user_access('search Google CSE');
}
/**
* Implements hook_theme().
*/
function google_cse_theme($existing, $type, $theme, $path) {
return array(
'google_cse_results' => array(
'variables' => array('form' => FALSE, 'path' => $path),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_results',
),
'google_cse_results_gadget' => array(
'variables' => array(),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_results_gadget',
),
'google_cse_adv_results' => array(
'variables' => array('form' => FALSE, 'path' => $path),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_adv/templates/google_cse_adv_results',
),
// Shows a message when the search does not return any result.
'google_cse_search_noresults' => array(
'variables' => array(),
),
);
}
/**
* Implements hook_block_info().
*/
function google_cse_block_info() {
return array(
'google_cse' => array(
'info' => t('Google CSE'),
),
);
}
/**
* Implements hook_block_view().
*/
function google_cse_block_view($delta = '') {
if (user_access('search Google CSE')) {
switch ($delta) {
case 'google_cse':
return array(
'subject' => t('Search'),
'content' => array('#theme' => 'google_cse_results', '#form' => TRUE),
);
}
}
}
/**
* Return the Google CSE tab title, either a setting or a translation.
*/
function google_cse_results_tab() {
return ($var = variable_get('google_cse_results_tab', '')) ? $var : t('Google');
}
/**
* Implements hook_perm().
*/
function google_cse_permission() {
return array(
'search Google CSE' => array(
'title' => t('Use Google CSE'),
),
);
}
/**
* Returns an array of any advanced settings which have been set.
*/
function google_cse_advanced_settings() {
global $language;
$settings = array();
foreach (array('cr', 'gl', 'hl', 'ie', 'lr', 'oe', 'safe') as $parameter) {
if ($setting = variable_get("google_cse_$parameter", '')) {
$settings[$parameter] = $setting;
}
}
if (variable_get('google_cse_locale_hl', '')) {
$settings['hl'] = $language->language;
}
if (variable_get('google_cse_locale_lr', '')) {
$settings['lr'] = 'lang_' . $language->language;
}
return $settings;
}
/**
* Get the relevant language to use for the search.
*
* @return string
* The language.
*/
function google_cse_language() {
global $language;
return variable_get('google_cse_locale_hl', '') ? $language->language : variable_get('google_cse_hl', '');
}
/**
* Implements hook_init().
*/
function google_cse_init() {
drupal_add_js(array(
'googleCSE' => array(
'cx' => variable_get('google_cse_cx', ''),
'language' => google_cse_language(),
'resultsWidth' => intval(variable_get('google_cse_results_width', 600)),
'domain' => variable_get('google_cse_domain', 'www.google.com'),
),
), 'setting');
}
/**
* Returns SiteSearch options form item.
*/
function google_cse_sitesearch_form(&$form) {
if ($options = google_cse_sitesearch_options()) {
$form['sitesearch'] = array(
'#type' => variable_get('google_cse_sitesearch_form', 'radios'),
'#options' => $options,
'#default_value' => google_cse_sitesearch_default(),
);
if ($type == 'select' && isset($form['sa'])) {
$form['sa']['#weight'] = 10;
}
}
}
/**
* Returns SiteSearch options.
*/
function google_cse_sitesearch_options() {
static $options;
if (!isset($options)) {
$options = array();
if ($sites = preg_split('/[\n\r]+/', variable_get('google_cse_sitesearch', ''), -1, PREG_SPLIT_NO_EMPTY)) {
$options[''] = ($var = variable_get('google_cse_sitesearch_option', '')) ? $var : t('Search the web');
foreach ($sites as $site) {
$site = preg_split('/[\s]+/', trim($site), 2, PREG_SPLIT_NO_EMPTY);
// Select options will be HTML-escaped.
// Radio options will be XSS-filtered.
$options[$site[0]] = isset($site[1]) ? $site[1] : t('Search %sitesearch', array('%sitesearch' => $site[0]));
}
}
}
return $options;
}
/**
* Returns SiteSearch default value.
*/
function google_cse_sitesearch_default() {
$options = google_cse_sitesearch_options();
if (isset($_GET['sitesearch']) && isset($options[$_GET['sitesearch']])) {
return $_GET['sitesearch'];
}
elseif (variable_get('google_cse_sitesearch_default', 0)) {
// Return the key of the second element in the array.
return key(array_slice($options, 1, 1));
}
return '';
}
/**
* Adds custom submit handler for search form.
*/
function google_cse_form_search_form_alter(&$form, &$form_state, $form_id) {
if ($form['module']['#value'] == 'google_cse') {
if (variable_get('google_cse_results_gadget', 1)) {
$form['basic']['op']['#suffix'] = theme('google_cse_results_gadget');
}
google_cse_sitesearch_form($form);
$form['#attributes']['class'][] = 'google-cse';
}
}
/**
* Adds custom submit handler for search block form.
*/
function google_cse_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$info = search_get_default_module_info();
if ($info['module'] == 'google_cse') {
google_cse_sitesearch_form($form);
$form['#attributes']['class'][] = 'google-cse';
}
}
/**
* Implements hook_proxy_settings_info().
*/
function google_cse_proxy_settings_info() {
return array(
'google_cse_adv' => array(
'name' => 'Google Custom Search Engine',
),
);
}
/**
* Brief message to display when no results match the query.
*
* @see search_help()
*/
function theme_google_cse_search_noresults() {
return t('<h2>Sorry there were no results matching your enquiry.</h2>
<ul>
<li>Check the spelling of your keywords</li>
<li>Try a more specific enquiry (e.g. <em>"Penny Black"</em> instead of <em>"Stamps"</em>): "blue drop"</em></li>
<li>Be explicit (e.g. <em>"Second class stamp"</em> instead of <em>"Stamp"</em>)</li>
<li>Include spaces between keywords</li>
</ul>');
}