-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathislandora_blocks.module
317 lines (281 loc) · 9.74 KB
/
islandora_blocks.module
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
<?php
/**
* @file
* Hooks, callbacks, and helper functions for the Islandora Blocks module.
*/
/* Hooks. */
/**
* Implements hook_theme().
*/
function islandora_blocks_theme($existing, $type, $theme, $path) {
return array(
'islandora_blocks_datastreams' => array(
'template' => 'templates/islandora-blocks-datastreams',
'variables' => array('datastreams' => array()),
),
);
}
/**
* Implements hook_block_info().
*/
function islandora_blocks_block_info() {
$blocks['metadata'] = array(
'info' => t('Object Metadata'),
);
$blocks['collections'] = array(
'info' => t('Object Collections'),
);
$blocks['datastreams'] = array(
'info' => t('Object Datastreams'),
);
if (module_exists('csl')) {
$blocks['citation'] = array(
'info' => t('Object Citation'),
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function islandora_blocks_block_view($delta = '') {
// If there is no object there are no blocks.
$object = menu_get_object('islandora_object', 2);
if (!$object) {
return FALSE;
}
// We should be on the "view" callback.
$menu_item = menu_get_item();
if ($menu_item['path'] != 'islandora/object/%/view' && $menu_item['path'] != 'islandora/object/%') {
return FALSE;
}
$block = array();
switch ($delta) {
case 'metadata':
$form = drupal_get_form('islandora_blocks_metadata_form', $object);
$block['subject'] = t('Metadata');
$block['content'] = drupal_render($form);
break;
case 'collections':
$block['subject'] = t('Parent Collections');
$block['content'] = array(
'#theme' => 'item_list',
'#type' => 'ul',
'#items' => array(),
);
$parents = $object->relationships->get(NULL, 'isMemberOfCollection');
if (count($parents) == 0) {
return FALSE;
}
$return = FALSE;
foreach ($parents as $parent) {
try {
$obj = islandora_object_load($parent['object']['value']);
if ($obj) {
$return = TRUE;
$block['content']['#items'][] = l($obj->label, 'islandora/object/' . $obj->id);
}
}
catch (Exception $e) {
// No-op.
}
}
// Couldn't load any parents, return nothing.
if (!$return) {
return FALSE;
}
break;
case 'datastreams':
foreach ($object as $ds) {
try {
module_load_include('inc', 'islandora', 'includes/datastream');
$pid = $object->id;
$id = $ds->id;
$label = $ds->label;
$download_path = islandora_datastream_get_url($ds, 'download');
$datastreams[$id]['id'] = $id;
$datastreams[$id]['label'] = $label;
$datastreams[$id]['label_link'] = islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $ds) ?
l($label, $download_path) :
$label;
$datastreams[$id]['download_url'] = $download_path;
$datastreams[$id]['mimetype'] = $ds->mimetype;
$datastreams[$id]['size'] = islandora_datastream_get_human_readable_size($ds);
$datastreams[$id]['created_date'] = $ds->createdDate->format("Y-m-d");
$datastreams[$id]['class'] = drupal_strtolower(preg_replace('/[^A-Za-z0-9]/', '-', $id));
$block['subject'] = t('Datastreams');
$block['content'] = array(
'#theme' => 'islandora_blocks_datastreams',
'#datastreams' => $datastreams,
);
}
catch (RepositoryException $e) {
// No-op.
}
}
break;
case 'citation':
// To provide a citation we need a MODS record.
if (!isset($object['MODS'])) {
return FALSE;
}
$block['subject'] = t('Citation');
$block['content'] = citeproc_bibliography_from_mods(citeproc_style(variable_get('islandora_blocks_citation_style')), $object['MODS']->content);
break;
}
return $block;
}
/**
* Implements hook_block_configure().
*/
function islandora_blocks_block_configure($delta = '') {
$form = array();
if ($delta == 'citation') {
module_load_include('inc', 'csl', 'includes/csl');
$form['islandora_blocks_citation_style'] = array(
'#type' => 'select',
'#title' => t('Citation Style to Use'),
'#default_value' => variable_get('islandora_blocks_citation_style'),
'#options' => drupal_map_assoc(CSL::GetNames()),
'#element_validate' => array('islandora_blocks_configure_validate'),
);
}
elseif ($delta === 'metadata') {
// Allow the block administrator to choose which metadata sets are displayed
// in the metadata block.
$form['islandora_blocks_metadata_sets_display'] = array(
'#type' => 'checkboxes',
'#title' => t('Metadata sets to display'),
'#description' => t('Choose the sets of metadata to display. Only one dataset is displayed at a time; but if you choose more than one, a control will be shown to let the viewer toggle which set of metadata is currently shown.'),
'#options' => _islandora_blocks_available_metadata_sets(),
'#default_value' => variable_get('islandora_blocks_metadata_sets_display', array('dc_metadata' => 'dc_metadata')),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function islandora_blocks_block_save($delta = '', $edit = array()) {
if ($delta === 'metadata') {
// Allow the block administrator to choose which metadata sets are displayed
// in the metadata block.
variable_set('islandora_blocks_metadata_sets_display', $edit['islandora_blocks_metadata_sets_display']);
}
}
/* Form API callbacks. */
/**
* Element validation callback: validate citation styles.
*
* @param array $form
* A form array.
* @param array $form_state
* A form state array.
*
* @see islandora_blocks_block_configure()
*/
function islandora_blocks_configure_validate($form, &$form_state) {
variable_set('islandora_blocks_citation_style', $form_state['values']['islandora_blocks_citation_style']);
}
/**
* Form callback: Form constructor for a display of metadata.
*
* @param array $form
* The form object.
* @param array &$form_state
* The form state object. Must contain the Fedora object that we want to
* display the metadata form as the first item in the array at
* $form_state['build_info']['args'].
*
* @return array
* A form array for metadata.
*
* @ingroup forms
*/
function islandora_blocks_metadata_form($form, &$form_state) {
$available_metadata_sets = _islandora_blocks_available_metadata_sets();
$configured_metadata_sets = variable_get('islandora_blocks_metadata_sets_display', array('dc_metadata' => 'dc_metadata'));
$to_display = array_intersect_key($available_metadata_sets, $configured_metadata_sets);
// The Fedora object should be the first build argument.
$object = reset($form_state['build_info']['args']);
// If we have determined that the dependencies to display Solr metadata are
// available, and the block administrator wants to display Solr metadata in
// this block, display the container.
if (array_key_exists('solr_metadata', $to_display)) {
// Find the fields we should query and display.
module_load_include('inc', 'islandora_solr_metadata', 'includes/db');
$associations = islandora_solr_metadata_get_associations_by_cmodels($object->models);
// Render the object's Solr metadata.
$form['solr_metadata'] = array(
'#type' => 'container',
'metadata' => array(
'#theme' => 'islandora_solr_metadata_display',
'#islandora_object' => $object,
'#print' => TRUE,
'#associations' => $associations,
),
'#states' => array(
'visible' => array(
':input[name="metadata_toggle"]' => array('value' => 'solr_metadata'),
),
),
);
}
// If we have determined that the dependencies to display DC metadata are
// available, and the block administrator wants to display DC metadata in this
// block, display the container.
if (array_key_exists('dc_metadata', $to_display)) {
// Render the object's DC metadata.
$form['dc_metadata'] = array(
'#type' => 'container',
'metadata' => array(
'#theme' => 'islandora_dublin_core_display',
'#islandora_object' => $object,
'#print' => TRUE,
),
'#states' => array(
'visible' => array(
':input[name="metadata_toggle"]' => array('value' => 'dc_metadata'),
),
),
);
}
// Add a form control to toggle the visible metadata.
if (count($to_display) > 1) {
$metadata_toggle_default = (array_key_exists('solr_metadata', $available_metadata_sets)) ? 'solr_metadata' : 'dc_metadata';
$form['metadata_toggle'] = array(
'#type' => 'radios',
'#title' => t('Metadata to display'),
'#options' => $to_display,
'#default_value' => $metadata_toggle_default,
'#weight' => -1,
);
}
return $form;
}
/* Helper functions. */
/**
* Return a list of available metadata sets.
*
* @return array
* An associative array of zero or more metadata sets:
* - solr_metadata: Indexed (Solr) Metadata
* - dc_metadata: Basic (Dublin Core) Metadata
*/
function _islandora_blocks_available_metadata_sets() {
// Cache metadata sets per request.
$available_metadata_sets = &drupal_static(__FUNCTION__);
if (!isset($available_metadata_sets)) {
$available_metadata_sets = array();
// If the Islandora Solr Metadata module is installed, we can display
// Indexed Solr metadata.
if (module_exists('islandora_solr_metadata')) {
$available_metadata_sets['solr_metadata'] = t('Indexed (Solr) Metadata');
}
// If the Islandora module is available, we can display DC metadata.
if (module_exists('islandora')) {
$available_metadata_sets['dc_metadata'] = t('Basic (Dublin Core) Metadata');
}
}
return $available_metadata_sets;
}