forked from artesis/ting_search_autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting_search_autocomplete.pages.inc
108 lines (97 loc) · 2.92 KB
/
ting_search_autocomplete.pages.inc
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
<?php
function ting_search_autocomplete($query = NULL) {
$query = $_GET['query'];
$return = array();
$delimiter = 0;
if ($query == TRUE) {
$cache = cache_get(md5($query), 'cache_ting_search_autocomplete');
if ($cache instanceof stdClass && is_array($cache->data)) {
$return = $cache->data;
}
else {
require_once drupal_get_path('module', 'ting') . '/ting.client.inc';
$terms = array();
// Scan for search results
$scan_results = ting_do_scan($query, 'anyIndexes', 10);
if ($scan_results) {
if (is_array($scan_results->terms)) {
foreach ($scan_results->terms as $term) {
$terms['any'][strtolower($term->name)] = $term;
}
uasort($terms['any'], 'ting_search_autocomplete_term_sort');
$terms['any'] = array_reverse($terms['any']);
}
// Scan for spell suggestions
$spell_suggestions = ting_get_spell_suggestions($query, 3);
foreach ($spell_suggestions as $spell_word) {
if (strtolower($spell_word->word) == strtolower($query)) {
continue;
}
$spell_word->count = $spell_word->weight;
$spell_word->name = $spell_word->word;
$terms['spell'][$spell_word->word] = $spell_word;
}
foreach ($terms as $type => $row) {
if (is_array($row)) {
foreach ($row as $term) {
$value = ting_search_autocomplete_substr_word_bondary($term->name);
if ($type == 'spell') {
$return[] = array(
'id' => $term->name,
'value' => $value,
'label' => t('Did you mean \'@query\' ?', array('@query' => $term->name)),
);
}
else {
$return[] = array(
'id' => $term->name,
'value' => $value,
'label' => $term->name,
);
}
}
}
}
cache_set(md5($query), $return, 'cache_ting_search_autocomplete', CACHE_TEMPORARY);
}
}
}
drupal_json_output($return);
}
function ting_search_autocomplete_term_sort($t1, $t2) {
if ($t1->count > $t2->count) {
return 1;
}
elseif ($t2 > $t1->count) {
return -1;
}
else {
return 0;
}
}
function ting_search_autocomplete_substr_word_bondary($text, $max = 128) {
global $multibyte;
$strlen = drupal_strlen($text);
if ($strlen <= $max) {
return $text;
}
else {
if ($multibyte == UNICODE_MULTIBYTE) {
if (mb_strpos($text, ' ', $max) == $max + 1) {
$lastspace = $max;
}
else {
$lastspace = mb_strrpos($text, ' ', $max - $strlen);
}
}
else {
if (strpos($text, ' ', $max) == $max + 1) {
$lastspace = $max;
}
else {
$lastspace = strrpos($text, ' ', $max - $strlen);
}
}
return drupal_substr($text, 0, $lastspace);
}
}