-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibdk_autocomplete.module
378 lines (326 loc) · 10.7 KB
/
bibdk_autocomplete.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* @file
* Code for Bibliotek.dk Autocomplete
*
*
*
*/
require_once 'includes/bibdk_autocomplete_service_settings_form.inc';
/**
* Implements hook_menu().
*/
function bibdk_autocomplete_menu() {
$items['autocomplete/%/%'] = array(
'title' => 'Custom search autocomplete',
'description' => 'Autocomplete callback',
'page callback' => 'bibdk_autocomplete_callback',
'page arguments' => array(1, 2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/config/search/bibdk_autocomplete'] = array(
'title' => 'Custom search autocomplete',
'description' => 'Settings for bibliotek.dk autocomplete',
'page callback' => 'drupal_get_form',
'page arguments' => array('bibdk_autocomplete_admin'),
'access arguments' => array('administer search'),
'file' => 'includes/bibdk_autocomplete.admin.inc',
);
return $items;
}
/**
* Callback function used when input is entered in a search field.
*
* @param $p_uuid
* @param $v_uuid
* @param $string
* @param bool $retry -- the server tends to respond with a error 500 however
* retrying once seems to fix it most of the times.
*/
function bibdk_autocomplete_callback($p_uuid, $v_uuid, $string = '') {
$items = array();
if (strlen($string) < 3) {
drupal_json_output($items);
drupal_exit();
}
$settings = variable_get('bibdk_autocomplete_settings', array());
$vars = variable_get('bibdk_autocomplete_var', array());
$value_vars = $vars[$p_uuid]['vars'][$v_uuid];
unset($value_vars['autocomplete']);
if (empty($value_vars['index'])) {
watchdog('bibdk_autocomplete', 'Autocomplete index not defined for value uuid ' . $v_uuid, array(), $severity = WATCHDOG_WARNING);
drupal_json_output(array());
drupal_exit();
}
$filter_array = bibdk_autocomplete_parse_filters($string);
$search_string = $filter_array['search_string'];
$operation = $path = ($value_vars['operation']) ? $value_vars['operation'] : 'suggest';
$query = array('query' => check_plain($search_string)) + $value_vars + $settings;
try {
switch ($operation) {
case 'suggest':
$result = fbi_api_suggest($query['index'], $query['query']);
break;
default:
break;
}
} catch (Exception $e) {
drupal_json_output(array());
drupal_exit();
}
switch ($operation) {
case 'suggest':
if (empty($result['data']['complexSuggest']['result'])) {
bibdk_autocomplete_callback_error("suggest", $query);
}
foreach ($result['data']['complexSuggest']['result'] as $kay => $val) {
if (strpos($val['term'], ' ')) {
// contains blank
$items['"' . bibdk_autocomplete_truncate(html_entity_decode(strip_tags($val['term'])), 64) . '"'] = $val['term'];
}
else {
$items[bibdk_autocomplete_truncate(html_entity_decode(strip_tags($val['term'])), 64)] = $val['term'];
}
}
break;
default:
watchdog('bibdk_autocomplete', 'No operation given in suggestrequest', array(), $severity = WATCHDOG_WARNING);
break;
}
// Return the result to the form in json
drupal_json_output($items);
drupal_exit();
}
/**
* For openplatform suggester (https://openplatform.dbc.dk/v3/suggest) we need
* to alter the options - it is a different api than http://opensuggestion.addi.dk/
* @param $options
*/
function bibdk_autocomplete_set_suggest_options($options) {
return array(
'q' => $options['query']['query'],
"type" => $options['query']['index'],
);
}
/**
* Log error.
*
* @param $action
* @param $options
*/
function bibdk_autocomplete_callback_error($action, $options) {
watchdog('bibdk_autocomplete', 'Autocomplete return empty set for ' . ': ' . $action . ' : ' . http_build_query($options['query']), array(), WATCHDOG_WARNING);
drupal_json_output(array());
drupal_exit();
}
function bibdk_autocomplete_parse_filters($filter_string) {
$res = array();
$search_filter = explode('::::', $filter_string);
$res['search_string'] = trim($search_filter[0], " \t\n\r\0\x0B\"");
$res['filters'] = array();
for ($i = 1; $i < count($search_filter); $i++) {
$filter = array();
$elements = explode('$$', $search_filter[$i]);
$filter['v_uuid'] = $elements[0];
$filter['p_uuid'] = $elements[1];
$filter['term'] = trim($elements[2], " \t\n\r\0\x0B\"");
$res['filters'][] = $filter;
}
return $res;
}
/**
* helper function for bibdk_autocomplete()
*/
function bibdk_autocomplete_truncate($string, $width) {
$parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
$parts_count = count($parts);
$length = 0;
$last_part = 0;
for (; $last_part < $parts_count; ++$last_part) {
$length += strlen($parts[$last_part]);
if ($length > $width) {
break;
}
}
return implode(array_slice($parts, 0, $last_part));
}
/**
* Implements hook_form_alter().
*
* @param $form
* @param $form_state
* @param $form_id
* @return mixed
*/
function bibdk_autocomplete_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'search_block_form';
// case 'search_form';
_bibdk_autocomplete_form_search_block_form_alter($form, $form_state, $form_id);
break;
}
}
/**
* Implements hook_form_FORM_ID_alter() for form search_block_form and search_form.
* - no it does not - it is called from above
* @TODO update comment
*/
function _bibdk_autocomplete_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$vars = NULL;
// check if this is https or not - we need it for the autocomplete javascript
// @see js/bibdk_autocomplete.js
// if page is behind a proxy
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$https = ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
}
else {
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
}
$settings = array(
'https' => $https,
);
drupal_add_js($settings, 'setting');
if (!empty($form['page_id']['#value'])) {
$page_path = $form['page_id']['#value'];
}
else {
if (isset($_GET['q']['page_id '])) {
$page_path = $_GET['q']['page_id '];
}
else {
$page_path = $_GET['q'];
}
}
$autocomplete_elements = variable_get('bibdk_autocomplete_var', array());
foreach ($autocomplete_elements as $key => $page) {
if ($page['page_path'] == $page_path) {
$vars = $page['vars'];
$p_uuid = $key;
}
}
if (!empty($vars)) {
foreach ($vars as $v_uuid => $values) {
if (!empty($form['advanced']) && !empty($form['advanced']['main'])) {
foreach ($form['advanced']['main'] as $element_label => $elements) {
bibdk_autocomplete_set_path($form, $elements, $element_label, $values, $p_uuid, $v_uuid, 'main');
}
}
if (!empty($form['advanced']) && !empty($form['advanced']['expand'])) {
foreach ($form['advanced']['expand'] as $element_label => $elements) {
bibdk_autocomplete_set_path($form, $elements, $element_label, $values, $p_uuid, $v_uuid, 'expand');
}
}
}
// Ensure autocomplete alters never gets loaded before autocomplete JS.
$form['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'bibdk_autocomplete') . '/js/bibdk_autocomplete.js',
'weight' => 10
);
$form['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'bibdk_autocomplete') . '/js/bibdk_autocomplete.overrides.js',
'weight' => 10,
);
}
}
/**
* helper function for hook_form_FORM_ID_alter()
*/
function bibdk_autocomplete_set_path(&$form, $elements, $element_label, $values, $p_uuid, $v_uuid, $region = 'main') {
if (!is_array($elements)) {
return;
}
foreach ($elements as $element_uuid => $element_val) {
foreach ($element_val as $element_key => $element_var) {
if (!is_array($element_var)) {
continue;
}
if (isset($element_var['#id']) && (strpos($element_var['#id'], $values['id']) === 0) && $values['autocomplete']) {
$form['advanced'][$region][$element_label][$element_uuid][$element_key]['#attributes']['data-autocomplete-function'] = $values['operation'];
$form['advanced'][$region][$element_label][$element_uuid][$element_key]['#autocomplete_path'] = 'autocomplete/' . $p_uuid . '/' . $v_uuid;
}
}
}
}
/**
* Implements hook_help().
*/
function bibdk_autocomplete_help($path, $arg) {
switch ($path) {
case 'admin/help#bibdk_autocomplete';
$file = drupal_get_path('module', 'bibdk_autocomplete') . "/help/bibdk_autocomplete.html";
return $output = file_get_contents($file);
break;
}
}
/**
* Implements hook_0_hit().
* @return array
*
*/
function bibdk_autocomplete_0_hit_search($conditions) {
$settings = variable_get('bibdk_autocomplete_settings', array());
$url = variable_get('bibdk_autocomplete_rest_url', null);
$value_vars = array();
$value_vars['index'] = 'scanterm.default';
$value_vars['facetIndex'] = 'scanphrase.default';
$url = $url . '/facetSpell';
$settings['highlight'] = ($settings['highlight']) ? 'true' : null;
$options['query'] = array('query' => check_plain($conditions)) + $value_vars + $settings;
$options['maxTime'] = 300;
$url = url($url, $options);
$curl = new MicroCURL();
$result = json_decode($curl->get(array($url)));
$curl->close();
$items = array();
if (!empty($result->suggestions)) {
foreach ($result->suggestions as $suggestion) {
$items[bibdk_autocomplete_truncate(html_entity_decode(strip_tags($suggestion->phrase)), 64)] = $suggestion->phrase;
}
}
$link_items = bibdk_make_item_list($items);
return $link_items;
}
/**
* helper function for 0_hit_search()
*/
function bibdk_make_item_list($items) {
$links = array();
// make a list of suggesttions
foreach ($items as $suggest) {
$zero_hit_link = array(
'#theme' => 'link',
'#path' => 'search/work/' . '"' . $suggest . '"',
'#weight' => 0,
'#text' => $suggest,
'#options' => array(
'attributes' => array(
'class' => array(
'bibdk-suggest-link',
),
),
'html' => false,
),
);
$links[] = drupal_render($zero_hit_link);
}
// label for suggestions
$render['label'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => t('label_did_you_mean', array(), array('context' => 'bibdk_autocomplete_suggest')),
'#attributes' => array(
'class' => array('bibdk-suggest-label'),
),
);
// render array for suggestions
$render['suggest_list'] = array(
'#theme' => 'item_list',
'#type' => 'ul',
'#attributes' => array(
'class' => array('links', 'bibdk-suggest-links'),
),
'#items' => $links,
);
return $render;
}