This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
forked from ajstanley/scholar
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIslandoraSolrResultsCitation.inc
286 lines (273 loc) · 9.47 KB
/
IslandoraSolrResultsCitation.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
<?php
/**
* @file
*
*/
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 IslandoraSolrResultsCitation extends IslandoraSolrResults {
/**
* Overrides parent function.
*
* @param IslandoraSolrQueryProcessor $query_processor
* @param string $title
* @param string $output
*/
function displayResults(IslandoraSolrQueryProcessor $query_processor, $title = "Search Results", $output = '') {
module_load_include('inc', 'scholar', 'SearchForm');
drupal_add_css(drupal_get_path('module', 'scholar') . '/css/Search.css');
$sort = $_SESSION['scholar']['search']['sort'];
$order = $_SESSION['scholar']['search']['order'];
if (!empty($_SESSION['scholar']['search']['sort'])) {
$query_processor->solrParams['fl'] = 'PID';
$query_processor->solrParams['sort'] = $sort . ' ' . $order;
$query_processor->executeQuery();
}
$total = (int) $query_processor->solrResult->response->numFound;
$output = '<h4>Advanced Search</h4>';
$output .= drupal_get_form('scholar_search_form');
if ($total === 0) {
$output .= "<h2>Search Results</h2>";
$output .= "<div>Sorry, but your search returned no results.</div>";
return $output;
}
islandora_solr_search_pager_init($total, $query_processor->solrLimit);
$output .= '<div class="content">';
$output .= drupal_get_form('scholar_search_results_form', $query_processor);
$output .= theme('pager', null, $query_processor->solrLimit, 0, null, 5);
$output .= "</div>";
return $output;
}
/**
* Function: printResults
*
* Description: translates a solr query result into a basic in-browser search result.
*
* @param Apache_Solr_Object $results
*/
function printResults($results) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
if (empty($results)) {
return "no results";
}
$output = '';
$record_start = $results->response->start;
$limit_results = variable_get('islandora_solr_search_limit_result_fields', 0);
module_load_include('inc', 'csl', 'CSL');
$styles = array_values(CSL::GetNames()); //Array returned has keys == values, for some reason... Whatever.
$style = citeproc_style($styles[0]);
foreach ($results->response->docs as $doc) {
$pid = $doc->PID;
$item = new Fedora_Item($pid);
if ($item->exists() && isset($item->datastreams['MODS'])) {
$output .= theme(THEME_SCHOLAR_SEARCH_ITEM, $pid, $style);
}
}
return $output;
}
}
/**
*
* @param array $variables
*/
function template_preprocess_scholar_search_item(array &$variables) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pid = $variables['pid'];
$item = new Fedora_Item($pid);
if ($item->exists() && isset($item->datastreams['MODS'])) {
$title = $item->objectProfile->objLabel;
$variables['title'] = l($title, 'fedora/repository/' . htmlspecialchars($pid, ENT_QUOTES, 'utf-8'));
$mods = $item->get_datastream_dissemination('MODS');
$mods = trim($mods);
if (!empty($mods)) {
$doc = new DOMDocument();
$doc->loadXML($mods);
$bibliography = citeproc_bibliography_from_mods($variables['style'], $doc);
$variables['citation'] = $bibliography->render();
}
}
}
function scholar_search_results_form(array &$form_state, IslandoraSolrQueryProcessor $query_processor) {
citation_exporter_include_download_redirect();
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$results = $query_processor->solrResult;
$total = (int) $results->response->numFound;
$start = (int) $query_processor->solrStart + 1;
$end = min(($query_processor->solrLimit + $query_processor->solrStart), $total);
$title = t('Search Results (%start - %end of %total)', array('%start' => $start, '%end' => $end, '%total' => $total));
$table = array(
'#header' => array(theme('table_select_header_cell'), $title),
'#theme' => 'scholar_search_results_table',
'#tree' => TRUE,
);
module_load_include('inc', 'csl', 'CSL');
$styles = CSL::GetNames();
$style = citeproc_style('APA'); //array_shift($styles));
$pids = array();
foreach ($results->response->docs as $doc) {
$pid = $doc->PID;
$result = '';
$item = new Fedora_Item($pid);
if ($item->exists() && isset($item->datastreams['MODS'])) {
$pids[] = $pid;
$result = theme(THEME_SCHOLAR_SEARCH_ITEM, $pid, $style);
$table['rows'][] = array(
'#pid' => $pid,
'result' => array('#value' => $result),
);
}
}
$checkboxes = array_fill_keys($pids, '');
$table['selections'] = array(
'#type' => 'checkboxes',
'#options' => $checkboxes,
);
$form_state['storage']['query'] = $query_processor;
return array(
'controls' => array(
'#type' => 'markup',
'#prefix' => '<div class="scholar-export-controls">',
'#suffix' => '</div>',
'export' => array(
'#value' => t('Export To File'),
'#prefix' => '<h4>',
'#suffix' => '</h4>'
),
'format' => array(
'#type' => 'select',
'#length' => 50,
'#options' => array(
'RIS' => 'RIS',
'RTF' => 'RTF',
'PDF' => 'PDF',
)
),
'style' => array(
'#type' => 'select',
'#options' => $styles
),
'export all' => array(
'#type' => 'submit',
'#action' => 'export',
'#export' => 'all',
'#value' => t('Export All Results'),
'#submit' => array('scholar_search_results_form_export')
),
'export selected' => array(
'#type' => 'submit',
'#action' => 'export',
'#export' => 'selected',
'#value' => t('Export Selected'),
'#submit' => array('scholar_search_results_form_export')
),
'save' => array(
'#value' => t('Save to Bibliography'),
'#prefix' => '<h4>',
'#suffix' => '</h4>'
),
'save all' => array(
'#type' => 'submit',
'#save' => 'all',
'#value' => t('Save All Results'),
'#submit' => array('scholar_search_results_form_save_to_bibliography')
),
'save selected' => array(
'#type' => 'submit',
'#save' => 'selected',
'#value' => t('Save Selected'),
'#submit' => array('scholar_search_results_form_save_to_bibliography')
),
),
'pager' => array(
'#value' => theme('pager', null, $query_processor->solrLimit, 0, null, 5)
),
'table' => $table,
);
}
/**
* Check to see if some thing has been selected.
*/
function scholar_search_results_form_validate(array $form, array &$form_state) {
// @todo prevent submission with more then.
}
/**
*
* @param array $form_state
* @return type
*/
function _scholar_search_get_all_pids_from_query(array &$form_state) {
$query_processor = $form_state['storage']['query'];
$previous_limit = $query_processor->solrLimit;
$query_processor->solrLimit = 100000;
$query_processor->solrParams['fl'] = 'PID';
$query_processor->executeQuery();
unset($query_processor->solrParams['fl']);
$query_processor->solrLimit = $previous_limit;
$results = $query_processor->solrResult;
$pids = array();
foreach ($results->response->docs as $doc) {
$pids[] = $doc->PID;
}
return $pids;
}
function scholar_search_results_form_save_to_bibliography(array $form, array &$form_state) {
module_load_include('inc', 'islandora_bibliography', 'Bibliography');
$action = $form_state['clicked_button']['#save'];
if ($action == 'all') {
$pids = _scholar_search_get_all_pids_from_query($form_state);
}
else {
$pids = $form_state['values']['table']['selections'];
$pids = array_filter($pids);
$pids = array_keys($pids);
}
$count = Bibliography::AddCitations($pids);
$form_state['storage'] = NULL;
drupal_set_message(t('Added %num Citation(s)', array('%num' => $count)));
}
function scholar_search_results_form_export(array $form, array &$form_state) {
$action = $form_state['clicked_button']['#export'];
if ($action == 'all') {
$pids = _scholar_search_get_all_pids_from_query($form_state);
}
else {
$selections = array_filter($form_state['values']['table']['selections']);
$pids = array_keys($selections);
}
module_load_include('inc', 'citation_exporter', 'Batch');
citation_exporter_batch_export($pids, $form_state['values']['style'], $form_state['values']['format']);
batch_process();
unset($form_state['storage']);
}
/**
* Theme's a form table for this module.
*
* @param array $element
* A Drupal Form Element.
*
* @return sting
* HTML that renders a table of settings for datastreams.
*/
function theme_scholar_search_results_table(array $element) {
$rows = array();
foreach (element_children($element['rows']) as $child) {
$setting = $element['rows'][$child];
$pid = $setting['#pid'];
$fields = array(
drupal_render($element['selections'][$pid]) // First field is a checkbox
);
foreach (element_children($setting) as $property) {
$field = $setting[$property];
$fields[] = drupal_render($field);
}
$rows[] = array(
'data' => $fields,
'class' => isset($setting['#attributes']['class']) ? $setting['#attributes']['class'] : NULL
);
}
$attributes = isset($element['#id']) ? array('id' => $element['#id']) : NULL;
return '<div class="scholar-search-results">' . theme_table($element['#header'], $rows, $attributes) . '</div>';
}