-
Notifications
You must be signed in to change notification settings - Fork 6
/
SearchForm.inc
302 lines (281 loc) · 10 KB
/
SearchForm.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
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
<?php
/**
* @file
*
* Contains the hooks for rendering and processing the Quick Search form.
*/
/**
* Builds a drupal form for launching a quick search.
*
* @param array $form_state
*
* @return array
*/
function scholar_search_form(array &$form_state) {
drupal_add_js(drupal_get_path('module', 'scholar') . '/js/Ahah.js');
drupal_add_css(drupal_get_path('module', 'scholar') . '/css/Search.css');
$solr_location = variable_get('islandora_solr_search_block_url', 'localhost:8080/solr');
$num_terms = 1;
if (isset($form_state['values'])) {
$values = $form_state['values'];
$num_terms = isset($form_state['num_terms']) ? $form_state['num_terms'] : $num_terms;
}
elseif (isset($_SESSION['scholar']['search']['values'])) {
/**
* Handles the changing of URL from the initial search page to the actual search page.
*/
$values = $_SESSION['scholar']['search']['values'];
$num_terms = count($values['terms']);
unset($_SESSION['scholar']['search']['values']); // Forget this for the next page visit.
}
$terms = array(
'#prefix' => '<div id="scholar-search-terms">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
for ($i = 0; $i < $num_terms; $i++) {
$term = array(
'#tree' => TRUE,
'#prefix' => '<div>',
'#suffix' => '</div>',
'field' => array(
'#title' => t('Field'),
'#type' => 'select',
'#default_value' => isset($values['terms'][$i]['field']) ? $values['terms'][$i]['field'] : 'mods.title',
'#options' => array(
'mods.abstract' => t('Abstract'),
// 'mods.roleTerm' => t('Author Identifier'),
'mods.fullname' => t('Author'),
'mods.title' => t('Title'),
'mods.isbn' => t('ISBN'),
'mods.issn' => t('ISSN'),
'mods.journal' => t('Full Journal Title'),
'type' => t('Publication Type'),
'mods.doi' => t('DOI'),
'mods.subject' => t('Keywords'),
'mods.dateIssued' => t('Year published'),
'pdf.text' => t('Full text'),
)
),
'search' => array(
'#title' => t('Search'),
'#type' => 'textfield',
'#default_value' => isset($values['terms'][$i]['search']) ? $values['terms'][$i]['search'] : NULL,
),
'hidden_submit' => array(// Used for when the user presses enter on the search field.
'#type' => 'submit',
'#value' => 'Search',
'#attributes' => array('style' => 'visibility:hidden;position:fixed;top:-1000px;right:-1000px;')
),
'submit' => array(
'#type' => 'submit',
'#value' => 'Search',
'#button' => 'search',
),
'add' => array(
'#type' => 'button',
'#prefix' => '<p>',
'#suffix' => '</p>',
'#value' => 'Add Field',
'#ahah' => array(
'path' => MENU_SCHOLAR_ADVANCED_SEARCH_ADD_FIELD,
'wrapper' => 'scholar-search-terms',
'method' => 'replace',
'effect' => 'fade'
)
),
);
if ($i > 0) {
$term['remove'] = array(
'#type' => 'button',
'#field' => $i,
'#value' => 'Remove Field',
'#name' => 'remove-field-' . $i,
'#ahah' => array(
'path' => MENU_SCHOLAR_ADVANCED_SEARCH_REMOVE_FIELD,
'wrapper' => 'scholar-search-terms',
'method' => 'replace',
'effect' => 'fade'
)
);
}
$terms[] = $term;
}
$researchers = array();
$result = file_get_contents('http://' . $solr_location . '/select/?q=rels.hasModel:researcherCModel&fl=mads.family,mads.given&rows=10000');
$xml = new SimpleXMLElement($result);
$array = $xml->xpath('//doc');
foreach ($array as $arr) {
$family = $arr->xpath('./arr[@name="mads.family"]/str');
$given = $arr->xpath('./arr[@name="mads.given"]/str');
$researchers[(string) $given[0] . ' ' . (string) $family[0]] = (string) $family[0] . ', ' . (string) $given[0];
}
asort($researchers);
return array(
'terms' => $terms,
'controls' => array(
'#type' => 'markup',
'#prefix' => '<div class="scholar-search-controls">',
'#suffix' => '</div>',
'sort' => array(
'#title' => t('Sort By'),
'#type' => 'select',
'#default_value' => isset($values['sort']) ? $values['sort'] : NULL,
'#options' => array(
NULL => t('Relevance'),
'mods.title' => t('Title'),
'mods.date' => t('Date Added'),
),
),
'order' => array(
'#title' => t('Order'),
'#type' => 'select',
'#default_value' => isset($values['order']) ? $values['order'] : 'asc',
'#options' => array(
'asc' => t('Ascending'),
'desc' => t('Descending'),
),
),
// 'submit' => array(
// '#type' => 'submit',
// '#value' => 'Search',
// '#button' => 'search',
// )
),
);
}
/**
* Submit hook for the quick search form.
*
* @param array $form
* @param array $form_state
*/
function scholar_search_form_submit(array $form, array &$form_state) {
$button = $form_state['clicked_button']['#button'];
$order = $form_state['values']['order'];
if ($button == 'search') {
$_SESSION['scholar']['search']['values'] = $form_state['values'];
$_SESSION['scholar']['search']['sort'] = $form_state['values']['sort'];
$_SESSION['scholar']['search']['order'] = $form_state['values']['order'];
$query = scholar_search_form_build_query($form_state);
if ($form_state['values']['terms']['field'] == 'mods.subject') {
$form_state['redirect'] = 'islandora/solr/search/' . urlencode($query) . '/-/dismax'; // Redirect to the search.
}
else {
$form_state['redirect'] = 'islandora/solr/search/' . urlencode($query) . '/'; // Redirect to the search.
}
}
else {
$query = $form_state['values']['author'];
$form_state['redirect'] = 'islandora/solr/search/mods.author:' . urlencode($query) . '/';
}
}
/**
*
* @param array $form_state
*/
function scholar_search_form_build_query(array &$form_state) {
$query = 'dc.title:*';
$statements = array();
foreach ($form_state['values']['terms'] as $term) {
$field = $term['field'];
$search = trim($term['search']);
if (!empty($search)) {
$statements[] = "$field:$search";
}
}
$query = !empty($statements) ? implode(' AND ', $statements) : '*'; // Empty return all results.
return $query;
}
/**
*
* @param array $form_state
*/
function scholar_search_form_store_sort_in_session(array &$form_state) {
$sort = $form_state['values']['sort'];
unset($_SESSION['scholar']['search']);
if (!empty($sort)) {
$order = $form_state['values']['order'];
$_SESSION['scholar']['search']['sort'] = $sort . ' ' . $order;
}
}
function scholar_search_advanced_add_field() {
module_load_include('inc', 'php_lib', 'Ahah');
list($form_id, $form_build_id, $form, $form_state) = Ahah::getFormInfo();
$form = Ahah::processForm($form_id, $form, $form_state);
$form_state['num_terms'] = count(element_children($form['terms'])) + 1;
$form = Ahah::rebuildForm($form_id, $form_build_id, $form, $form_state);
Ahah::respond($form['terms']);
}
function scholar_search_advanced_remove_field() {
module_load_include('inc', 'xml_form_elements', 'Ahah');
list($form_id, $form_build_id, $form, $form_state) = Ahah::getFormInfo();
$form = Ahah::processForm($form_id, $form, $form_state);
$field = $form_state['clicked_button']['#field'];
array_splice($form_state['values']['terms'], $field, 1);
$form_state['num_terms'] = count($form_state['values']['terms']);
$form = Ahah::rebuildForm($form_id, $form_build_id, $form, $form_state);
Ahah::respond($form['terms']);
}
function browse_by_department() {
module_load_include('inc', 'Fedora_Repository', 'api/fedora_utils');
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrQueryProcessor');
$filename = drupal_get_path('module', 'scholar') . '/inclusionList.txt';
$solr_location = variable_get('islandora_solr_search_block_url', 'localhost:8080/solr');
$inclusionlist = file_get_contents($filename);
$inclusionlist = fix_encoding($inclusionlist);
$inclusionlist = explode(",", $inclusionlist);
$ingnore_roles_less_than = 0;
$result = file_get_contents('http://' . $solr_location . '/select/?q=rels.hasModel:researcherCModel&fl=mads.topic&rows=500');
$xml = new SimpleXMLElement($result);
$depts = array_unique($xml->xpath('//doc/arr/str'));
$output = '<ul>';
foreach ($depts as $dept) {
if (array_search($dept, $inclusionlist)) {
$output .= "<li><a href=islandora/solr/search/mads.topic:$dept>" . $dept . '</a></li>';
}
}
// $output = $result;
// $result = db_query('SELECT role.rid, role.name FROM {role} where role.rid > %d order by role.name', 0);
// $output .= '<ul>';
// while ($role = db_fetch_object($result)) {
//
// if ($role->rid > $ingnore_roles_less_than && in_array($role->name, $inclusionlist)) {
// $urlportion = urlencode($role->name);
// $name = $role->name;
// if ('Home Economics' == $name) {
// $name = "Family and Nutritional Sciences";
// }
// else
// if ('Anatomy Physiology' == $name) {
// $name = "Biomedical Sciences";
// }
// else
// if ('Womens Studies' == $name) {
// $name = "Women's Studies";
// }
// else
// if ('Path Micro' == $name) {
// $name = "Pathology and Microbiology";
// }
// else
// if ('Soc Anth' == $name) {
// $name = "Sociology and Anthropology";
// }
// $output .= "<li><a href=islandora/solr/search/$urlportion>" . $name . '</a></li>';
// }
// }
$output .= '</ul>';
return $output;
}
/**
* @todo Put solr server variable in here.
* @param type $researcher
*/
function scholar_rss_feed($researcher) {
$solr_location = variable_get('islandora_solr_search_block_url', 'localhost:8080/solr');
$query_url = 'http://' . $solr_location . '/select?q=mods.username:' . $researcher . '&sort=mods.dateIssued+desc&wt=xslt&tr=example_rss.xsl';
$result = drupal_http_request($query_url);
drupal_set_header('Content-Type: text/xml; charset=utf-8');
print $result->data;
}