-
Notifications
You must be signed in to change notification settings - Fork 6
/
SolrResultsScholar.inc
431 lines (382 loc) · 19.4 KB
/
SolrResultsScholar.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/*
* @file
* contains methods to search solr and display results. depends on Apache_Solr_Php client.
*/
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
/**
* Extention of IslandoraSolrResults for templating purposes.
* This overrides the displayResults function to provide an alternate display type.
*/
class ScholarSolrResults extends IslandoraSolrResults {
static $facetSeparator = '~'; //used to separate facets in url
static $slashReplacement = '~slsh~'; // a pattern used to replace / in url's the slash breaks drupal clean url's
public static $SEARCH_CLASS_ADVANCED_SEARCH_NUMBER_FIELDS = 5;
function IslandoraSolrResultsTable() {
parent::__construct();
}
/**
* Outputs results in unified tabular format.
*
* This function collects all field names from all results in the
* resultset and constructs a single table with said field names in
* a unified header row, rather than outputting each result as a
* miniature horizontal table.
*
* @param $results
* A solr resultset object.
*
* @return
* html output for the resultset. Note: we currently create this
* output manually, should refactor to use drupal forms api.
*/
function displayResultsScholar($solrQueryProcessor, $title = "Search Results", $output = '') {
$apacheSolrResult = $solrQueryProcessor->solrResult;
$total = (int) $apacheSolrResult->response->numFound;
$end = min(($solrQueryProcessor->solrLimit + $solrQueryProcessor->solrStart), $total);
$title = "";
$headers = variable_get('scholar_solr_node_for_header', 'Institutional Repository Collection ~ 8');
$arr = islandora_build_substitution_list($headers);
//load a node as a header for the search results in case we have canned searches we would like to explain
$node_id = $arr[$solrQueryProcessor->solrQuery];
if (!empty($node_id)) {
$output .= '<div id="islandora_search_header">';
$node = node_load($node_id);
$output .= $node->body;
$output .= '</div>';
}
$limit_block = module_invoke('islandora_solr_search', 'block', 'view', 'limit_results');
// Check for empty resultset
if ($total === 0) {
$output = "<h2>Search Results</h2>";
$output .= "<div>Sorry, but your search returned no results.</div>";
return $output;
}
// Initialize drupal-style pager
islandora_solr_search_pager_init($total, $solrQueryProcessor->solrLimit);
// Get secondary display profiles
$secondary_display_profiles = module_invoke_all('islandora_solr_secondary_display');
// How about this for an expected return?
// return array(
// 'machine-name' = array(
// 'name' => 'Human Readable Name',
// 'module' => 'module_name',
// 'file' => 'FileName.inc',
// 'class' => 'ClassName',
// 'function' => 'function_name',
// 'description' => 'A description of the display profile',
// 'logo' => 'Some text, some <html>, an <img>, etc used to link to this output,
// );
// check if
// if the islandora_solr_search admin form has never been saved, then
// the variable 'islandora_solr_secondary_display' won't be set yet.
$secondary_array = variable_get('islandora_solr_secondary_display', array());
// count number of elements in array
$array_count = count($secondary_array);
// count number of disabled displays
$i = 0;
foreach ($secondary_array as $key => $value) {
if ($value === 0) {
$i++;
}
}
// if number of elements is the same as the number of disabled displays,
// this means that there are no displays enabled.
if ($array_count != $i) {
$output .= "<div id='islandora_solr_secondary_display_profiles'>";
foreach ($secondary_array as $name => $status) {
if ($status === $name) {
$description = $secondary_display_profiles[$name]['description'];
$logo = $secondary_display_profiles[$name]['logo'];
$attributes = array('title' => $description);
$query = array();
$query[] = drupal_query_string_encode(array('solr_profile' => $name));
$output .= l($logo, $_GET['q'], array('attributes' => $attributes, 'html' => TRUE, 'query' => count($query) ? implode('&', $query) : NULL));
}
}
$output .= "</div>";
}
//$output .= '<h2>' . $title . '</h2>';
$output .= '<div class="islandora-solr-search-controls">';
$output .= '<div class="islandora-solr-search-count-limit">' . $limit_block['content'] . '</div>';
$output .= '<div class="islandora-solr-result-count"><div id="islandora_solr_result_count">' . ($solrQueryProcessor->solrStart + 1) . ' - ' . $end . ' of ' . $total . '</div></div>';
$output .= '<div class="islandora-solr-search-pager-top">' . theme('pager', NULL, $solrQueryProcessor->solrLimit, 0, NULL, 5) . '</div>';
$output .= '</div>';
$output .= '<div class="content">';
// drupal_set_message(print_r($secondary_display_profiles,1));
/*
* Then, here, we need to build links to the current query under the secondary query display
* profile for each such profile which uses the hook.
*
* The link should basically be $theCurrentURL with &proile=<profile_name> tacked on.
*
* Also, perhaps there's no difference between primary and secondary profiles?
*/
$output .= $this->printResultsScholar($solrQueryProcessor->solrResult);
// Render Drupal pager (bottom of search results)
$output .= theme('pager', NULL, $solrQueryProcessor->solrLimit, 0, NULL, 5);
$output .= "</div>"; // class = "content"
return $output;
}
function printResultsScholar($results) {
// drupal_add_js(drupal_get_path('module', 'scholar') . '/js/searchresults.js');
module_load_include('inc', 'fedora_repository', 'CollectionClass');
module_load_include('inc', 'citeproc', 'CiteProcJSBibliography');
module_load_include('inc', 'scholar', '/citation/bibliography/BibliographyForm');
module_load_include('inc', 'scholar', '/citation/bibliography/CitationForm');
module_load_include('inc', 'fedora_repository', '/api/fedora_item');
module_load_include('inc', 'scholar', 'Coins');
module_load_include('inc', 'islandora_bibliography', 'Bibliography');
drupal_add_css(drupal_get_path('module', 'islandora_bibliography') . '/css/export.css');
drupal_add_css(drupal_get_path('module', 'scholar') . '/css/scholar.css');
static $path;
$path = drupal_get_path('module', 'scholar');
$items = array();
$type = "ol";
$title = NULL;
$recordStart = $results->response->start;
$limitResults = variable_get('islandora_solr_search_limit_result_fields', 0);
$highlights = $results->highlighting;
foreach ($highlights as $highlight) {
$fields = get_object_vars($highlight);
$keys = array_keys($fields);
if (count($keys) > 0) {
foreach ($highlight->$keys[0] as $body) {
}
}
}
global $base_url;
global $base_path;
if (empty($results)) {
return "no results";
}
$row = 1;
foreach ($results->response->docs as $doc) {
$rows = array();
$csl = variable_get('scholar_default_csl', 'default');
$department = $person = FALSE;
foreach ($doc as $field => $value) {
if ($field == 'rels.isMemberOfCollection' && $value == 'ir:people') {
$person = TRUE;
}
elseif ($field == 'rels.isMemberOfCollection' && $value == 'ir:departments') {
$department = TRUE;
}
}
$style = citeproc_style($csl);
$bibliography = new CiteProcJSBibliography($style);
$mods = islandora_bibilgraphy_form_get_mods($doc->PID);
if ($mods) {
$ref = citeproc_citation_from_mods($mods);
$bibliography->addCitation($ref);
}
$dc_attribute = 'dc.title';
$dc_title = $doc->$dc_attribute;
$dc_title = preg_replace('/\s+/', ' ', $dc_title);
$dc_title = str_replace(array('/', ':', '-', ' and ', ' or ', ' of ', ' in ', ' on ', ' to ', ' for ', ' with ', ' the ', 'The ', ' but ', ' not ', ' by '), ' ', $dc_title);
if (is_array($dc_title)) {
$dc_title = $dc_title[0];
}
if (strlen($dc_title) > 60) {
$array = explode(' ', $dc_title);
$dc_title = implode(' ', array_slice($array, 0, 9));
}
if ($mods) {
$object = new Fedora_Item($doc->PID);
$ds_array = $object->get_datastreams_list_as_array();
$pdf_check = array_key_exists('PDF', $ds_array) || array_key_exists('OBJ', $ds_array);
$check = '';
$data = new Coins($doc->PID);
if (array_key_exists('OBJ', $ds_array)) {//check status in case ds exists but is embargoed
$check = 'OBJ';
}
if (array_key_exists('PDF', $ds_array)) {
$check = 'PDF';
}
$url_frag = 'fedora/repository/';
if (module_exists('scholar_tracking')) {
$url_frag = 'download_ds/';
}
$embargoed = $data->is_embargoed($data->embargo_date);
$full_text = 'Full Text';
if (isset($data->type)) {
if ($data->type == 'PRE-PRINT') {
$full_text .= ': Pre-print';
}
else if ($data->type == 'POST-PRINT') {
$full_text .= ': Post-print';
}
}
if ($check == 'OBJ' && !$embargoed) {
$download_img = $base_url . '/' . $path . '/images/download.png';
//$pdf_result = '<div class="scholar_pdf button">' . l('Full text', $url_frag . $doc->PID . '/OBJ/OBJ', array('html' => TRUE)) . '</div>';
$pdf_result = '<div class="scholar_pdf">' . l($full_text, $url_frag . $doc->PID . '/OBJ/OBJ', array('html' => TRUE, 'attributes' => array('class' => 'button fulltext'))) . '</div>';
}
elseif ($check == 'PDF' && !$embargoed) {
$download_img = $base_url . '/' . $path . '/images/download.png';
//$pdf_result = '<div class="scholar_pdf button">' . l('Full text', $url_frag . $doc->PID . '/PDF/PDF', array('html' => TRUE)) . '</div>';
$pdf_result = '<div class="scholar_pdf">' . l($full_text, $url_frag . $doc->PID . '/PDF/PDF', array('html' => TRUE, 'attributes' => array('class' => 'button fulltext'))) . '</div>';
}
else {
if (stripos((string) $data->genre[0], 'book') !== FALSE) {
$coins_url = ' 				http://articles.library.upei.ca:7888/godot/hold_tab.cgi?hold_tab_branch=PCU&isbn=' . $data->isbn . '&date=' . $data->date . '&title=' . $data->get_godot_title()
.'&genre=' . $data->genre[0];
}
else {
$coins_url = ' 				http://articles.library.upei.ca:7888/godot/hold_tab.cgi?hold_tab_branch=PCU&issn=' . $data->issn . '&date=' . $data->date . '&volume=' . urlencode($data->volume) . '&issue=' . $data->issue
. '&spage=' . $data->start_page . '&atitle=' . urlencode($data->get_godot_title()) . '&stitle=' . urlencode($data->journal_title) . '&genre=' . $data->genre[0];
}
$pdf_result = '<div class="scholar_pdf"><A HREF="' . $coins_url . '"><img src="http://library.upei.ca/sites/all/files/sharedfolder/files/getit_upei.gif" /> </A></div>';
}
/* switch ($check) {
case 'OBJ':
$download_img = $base_url . '/' . $path . '/images/download.png';
//$pdf_result = '<div class="scholar_pdf button">' . l('Full text', $url_frag . $doc->PID . '/OBJ/OBJ', array('html' => TRUE)) . '</div>';
$pdf_result = '<div class="scholar_pdf">' . l('Full text', $url_frag . $doc->PID . '/OBJ/OBJ', array('html' => TRUE, 'attributes' => array('class' => 'button fulltext'))) . '</div>';
break;
case 'PDF':
$download_img = $base_url . '/' . $path . '/images/download.png';
//$pdf_result = '<div class="scholar_pdf button">' . l('Full text', $url_frag . $doc->PID . '/PDF/PDF', array('html' => TRUE)) . '</div>';
$pdf_result = '<div class="scholar_pdf">' . l('Full text', $url_frag . $doc->PID . '/PDF/PDF', array('html' => TRUE, 'attributes' => array('class' => 'button fulltext'))) . '</div>';
break;
default:
if (stripos((string) $data->genre[0], 'book') !== FALSE) {
$coins_url = ' 				http://articles.library.upei.ca:7888/godot/hold_tab.cgi?hold_tab_branch=PCU&isbn=' . $data->isbn . '&date=' . $data->date . '&title=' . $data->title;
}
else {
$coins_url = ' 				http://articles.library.upei.ca:7888/godot/hold_tab.cgi?hold_tab_branch=PCU&issn=' . $data->issn . '&date=' . $data->date . '&volume=' . $data->volume . '&issue=' . $data->issue . '&spage=' . $data->start_page . '&atitle=' . $data->title . '&stitle=' . $data->journal_title;
}
$pdf_result = '<div class="scholar_pdf"><A HREF="' . $coins_url . '"><img src="http://library.upei.ca/sites/all/files/sharedfolder/files/getit_upei.gif" /> </A></div>';
break;
} */
$supp_result = NULL;
$sup_embargoed = $data->is_embargoed($data->sup_embargo_date);
if (array_key_exists('ZIP', $ds_array) && !$sup_embargoed) {
//$supp_result = '<div class="scholar_pdf">' . l('Supplemental information', 'fedora/repository/' . $doc->PID . '/ZIP/' . $doc->PID . '.zip', array('html' => TRUE, 'attributes' => array('class' => 'button supplement'))) . '</div>';
$supp_result = '<div class="scholar_pdf">' . l('Supplemental information', $url_frag . $doc->PID . '/ZIP/ZIP', array('html' => TRUE, 'attributes' => array('class' => 'button supplement'))) . '</div>';
}
$abstract_snippet = 300;
if (strlen($data->abstract) > $abstract_snippet) {
$abstract = substr($data->abstract, 0, $abstract_snippet);
$abstract .= '...';
}
else {
$abstract = $data->abstract;
}
$scholar_class = Bibliography::Exists($doc->PID) ? 'my-list-remove' : 'my-list-add';
//$link_text = Bibliography::Exists($doc->PID) ? t('Remove from My List') : t('Add to My List');
//$bookmark = '<div class="scholar_search_bookmark "><a NAME = "'.$doc->PID.'" id="my-list-link-' .$doc->PID . '" class="'. $scholar_class .' button">'.$link_text.'</a></div>';
$bibliography_text = ""; //Bibliography::Exists($doc->PID) ? 'Remove from My List' : 'Add to My List';
$object_title = '<div class="scholar_search_title">' . l($data->title, 'fedora/repository/' . $doc->PID, array('html' => TRUE)) . '</div>';
$bookmark = '<div class="scholar_search_bookmark ' . $scholar_class . ' ">' . drupal_get_form('islandora_bibliography_citation_search_form', $doc->PID) . '</div><div class="scholar_search_bookmark_text">' . $bibliography_text . '</div>';
$related = '<div class="scholar_search_related">' . l('<img src="' . $base_url . '/' . $path . '/images/reload.png"/> Related content', 'islandora/solr/search/' . str_replace(' ', ' OR ', $dc_title), array('html' => TRUE)) . '</div>';
$rows[][] = $object_title . '<div class="scholar_search_citation">' . $bibliography->render() . '</div>' . '<div class="scholar_search_citation">' . $abstract . '</div>' . $bookmark . $pdf_result . $supp_result;
$row++;
}
elseif ($person) {
$image_path = $base_path . drupal_get_path('module', 'scholar') . '/images/gnome_stock_person.png';
$rows[][] = '<img src="' . $image_path . '"/>';
foreach ($doc as $field => $value) {
$person_result_fields = array('mads.fullname', 'mads.topic');
if (in_array($field, $person_result_fields)) {
if ($limitResults && empty($this->resultFieldArray[$field])) {
continue;
}
$translated_field_name = isset($this->allSubsArray[$field]) ? $this->allSubsArray[$field] : $field;
$rows[$row][] = array(
'data' => $translated_field_name,
'header' => TRUE,
);
if ($field == 'PID') {
$l = l($value, 'fedora/repository/' . htmlspecialchars($value, ENT_QUOTES, 'utf-8')) . '</BR>';
$rows[$row][] = $l;
}
elseif ($field == 'Title' && is_array($value)) {
$rows[$row][] = ucfirst(ltrim($value[0]));
}
elseif ($field == 'mads.fullname' && $value != NULL) {
$l = l($value, 'fedora/repository/' . $doc->PID) . '</BR>';
$rows[$row][] = $l;
}
elseif ($field == 'mads.topic' && is_array($value)) {
$rows[$row][] = implode(', ', array_unique($value));
}
else {
$rows[$row][] = ucfirst(ltrim($value));
}
$row++;
}
}
}
elseif ($department) {
$image_path = $base_path . drupal_get_path('module', 'scholar') . '/images/gnome_stock_dept.png';
$rows[][] = '<img src="' . $image_path . '"/>';
foreach ($doc as $field => $value) {
$person_result_fields = array('mads.identifier');
if (in_array($field, $person_result_fields)) {
if ($limitResults && empty($this->resultFieldArray[$field])) {
continue;
}
$translated_field_name = isset($this->allSubsArray[$field]) ? $this->allSubsArray[$field] : $field;
$rows[$row][] = array(
'data' => $translated_field_name,
'header' => TRUE,
);
if ($field == 'PID') {
$l = l($value, 'fedora/repository/' . htmlspecialchars($value, ENT_QUOTES, 'utf-8')) . '</BR>';
$rows[$row][] = $l;
}
elseif ($field == 'Title' && is_array($value)) {
$rows[$row][] = ucfirst(ltrim($value[0]));
}
elseif ($field == 'mads.identifier' && $value != NULL) {
$l = l($value, 'fedora/repository/' . $doc->PID) . '</BR>';
$rows[$row][] = $l;
}
else {
$rows[$row][] = ucfirst(ltrim($value));
}
$row++;
}
}
}
else {
foreach ($doc as $field => $value) {
if ($limitResults && empty($this->resultFieldArray[$field])) {
continue;
}
$translated_field_name = isset($this->allSubsArray[$field]) ? $this->allSubsArray[$field] : $field;
$rows[$row][] = array(
'data' => $translated_field_name,
'header' => TRUE,
);
if ($field == 'PID') {
$l = l($value, 'fedora/repository/' . htmlspecialchars($value, ENT_QUOTES, 'utf-8')) . '</BR>';
$rows[$row][] = $l;
}
elseif ($field == 'Title' && is_array($value)) {
$rows[$row][] = ucfirst(ltrim($value[0]));
}
else {
$rows[$row][] = ucfirst(ltrim($value));
}
$row++;
}
}
$items[] = theme_table(NULL, $rows, array('class' => 'islandora_solr_search_results_object'));
}
$output = theme_item_list($items, $title, $type, array('class' => 'stuff', 'start' => ($recordStart + 1)));
if (variable_get('islandora_solr_search_debug_mode', 0)) { // debug dump
$results_r .= "<pre>Results: " . print_r($results, TRUE) . "</pre>";
$fieldset_r = array(
'#title' => t("Raw Results"),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $results_r,
);
$output .= theme('fieldset', $fieldset_r);
}
$_SESSION['search_url'] = request_uri();
return implode('', $items);
}
}