-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathautocomplete.inc
79 lines (67 loc) · 2.39 KB
/
autocomplete.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
<?php
/**
* @file
* Php you suck so badly, the array_filter function should just take extra parameters like array_walk...
*/
class AutoCompleteFilterChem {
protected $string;
function __construct($string) {
$this->string = strtolower($string);
}
function __invoke($content_model) {
return (preg_match("/^{$this->string}/i", $content_model) !== 0) ? TRUE : FALSE;
}
}
/**
* Autocomplete researchers from a solr query..
*
* @param string $string The string of characters to match with the results
* @return string List of results in JSON format
*/
function islandora_content_model_forms_autocomplete_researchers($string = '') {
$content_models = islandora_chem_sp_get_values('rels.hasModel:researcher_CM', 'dc.title');
// @todo find an alternative solution to array filter because of earlier versions 5.2
$output = array_filter($content_models, new AutoCompleteFilterChem($string));
$output[$string] = $string;
// var_dump($output);
return drupal_json($output);
}
/**
* Autocomplete institutions..
*
* @param string $string The string of characters to match with the results
* @return string List of results in JSON format
*/
function islandora_content_model_forms_autocomplete_institutions($string = '') {
$content_models = islandora_chem_sp_get_values('rels.hasModel:researcher_CM', 'mads.organization');
// @todo find an alternative solution to array filter because of earlier versions 5.2
if ($content_models == NULL) {
drupal_set_message('Solr is not responding!', 'error');
break;
}
else {
$output = array_filter($content_models, new AutoCompleteFilterChem($string));
$output[$string] = $string;
return drupal_json($output);
}
}
/**
*
* @param type $query Criteria to search for as a solr query
* @param type $results The field that you want returned
* @return type An array of results with the key and element being the same
*/
function islandora_chem_sp_get_values($query, $results) {
$solr_url = variable_get('fedora_solr_search_url', 'http://localhost:8080/solr');
$query = file_get_contents($solr_url . '/select?q=' . $query . '&fl=' . $results);
$result = new SimpleXMLElement($query);
$researchers = $result->result;
// var_dump($researchers);
$list = array();
foreach ($researchers->children() as $researcher) {
$value = (string)$researcher->arr[0]->str;
$list[$value] = $value;
}
// var_dump($list);
return $list;
}