-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmanidora.module
600 lines (540 loc) · 16.8 KB
/
manidora.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* @file
* Custom University of Manitoba hacks for Islandora.
*/
// Includes functions for blocks.
require_once dirname(__FILE__) . '/includes/blocks.inc';
/**
* API call to insert a link to a printer friendly version of something.
*
* @param string $path
* path to page we want a printer friendly version of
*/
function manidora_print_insert_link($path) {
return l(t("Printer Friendly Version"), $path);
}
/**
* Implements hook_preprocess_print().
*
* Strips out unneccessary content and adds MODS metadata below the object.
*/
function manidora_preprocess_print(&$variables) {
// Get the DOM.
$content_dom = new DOMDocument();
$content_dom->loadHTML($variables["print"]["content"]);
//
// Get just the content we want.
// We're looking for the 'content' divs.
//
$divs = $content_dom->getElementsByTagName("div");
$found = FALSE;
for ($i = 0; $i < $divs->length && !$found; $i++) {
if ($divs->item($i)->hasAttribute("class") &&
($divs->item($i)->getAttribute("class") == "islandora-basic-image-content" ||
$divs->item($i)->getAttribute("class") == "islandora-large-image-content" ||
$divs->item($i)->getAttribute("class") == "islandora-newspaper-content")
) {
// Replace original content with just the stripped out 'content' div.
$variables["print"]["content"] = $content_dom->saveHTML($divs->item($i));
$found = TRUE;
}
}
//
// Append the MODS metadata to the end of the content.
//
if ($found) {
// Hack out the PID from the URL.
$url = $variables["print"]["url"];
$explode_results = explode("/islandora/object/", $url);
if (count($explode_results) > 1) {
$pid = $explode_results[1];
}
else {
$pid = $explode_results[0];
}
$pid = urldecode($pid);
// Use the PID to get the Islandora object.
$islandora_object = islandora_object_load($pid);
// Append the MODS metadata list to the end of the content to be themed by
// the printer friendly module.
if ($islandora_object['MODS']) {
$metadata = manidora_retrieve_metadata_form($islandora_object);
$variables["print"]["content"] .= "<br/>" . drupal_render($metadata);
}
}
}
/**
* Implements hook_menu().
*/
function manidora_menu() {
$items = array();
$items['islandora/object/%islandora_object/manitoba_metadata'] = array(
'title' => 'Metadata',
'type' => MENU_LOCAL_TASK,
'page callback' => 'manidora_retrieve_metadata_form',
'page arguments' => array(2),
'access callback' => 'manidora_metadata_access',
'access arguments' => array(2),
'weight' => 1,
);
$items['islandora/object/%islandora_object/manitoba_download'] = array(
'title' => 'Download',
'type' => MENU_LOCAL_TASK,
'page callback' => 'manidora_download_table',
'page arguments' => array(2),
'access callback' => 'manidora_download_access',
'access arguments' => array(2),
'weight' => 2,
);
$items['islandora/manitoba/newspaper_search'] = array(
'title' => 'Manitoba Advanced Newspaper Search',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('manidora_newspaper_search_form'),
'access arguments' => array(FEDORA_VIEW_OBJECTS),
);
$items['admin/islandora/manidora'] = array(
'title' => 'Manidora',
'description' => 'Configure the UofM customization module.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('manidora_admin'),
'type' => MENU_NORMAL_ITEM,
);
$items['islandora/manitoba/pdf/print'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'manidora_printable_pdf',
'page arguments' => array(4),
'access arguments' => array(FEDORA_VIEW_OBJECTS),
);
return $items;
}
/**
* Implements hook_menu_alter().
*
* Do the ol' switcharooney on the menu entry for the islandora_fits
* module so that UofM can continue their tabish lifestyles.
*/
function manidora_menu_alter(&$items) {
if (module_exists('islandora_fits')) {
$fits_tab = $items['islandora/object/%islandora_object/manage/fits_metadata'];
$items['islandora/object/%islandora_object/fits_metadata'] = $fits_tab;
$items['islandora/object/%islandora_object/fits_metadata']['weight'] = 3;
$items['islandora/object/%islandora_object/fits_metadata']['access callback'] = 'manidora_technical_metadata_access';
unset($items['islandora/object/%islandora_object/manage/fits_metadata']);
}
}
/**
* Implements hook_block_view_alter().
*
* Add a link to view the content div in a colorbox.
*/
function manidora_block_view_alter(&$data, $block) {
if ($block->delta == 'compound_navigation' && !empty($data['content']['#markup'])) {
drupal_add_js(drupal_get_path('module', 'manidora') . '/js/manidora.js');
$data['content']['#markup'] .= '<a class="manidora-colorbox" href="#content">' . t('Full Screen') . '</a>';
}
}
/**
* Fetches MODS metadata as form encoded markup.
*
* @param IslandoraObject $object
* The object you wish to retrive formatted metadata from
*/
function manidora_retrieve_metadata_form($object) {
drupal_set_title(t("@label Metadata", array('@label' => $object->label)));
$output = manidora_mods_transform($object);
if ($output) {
$form = array(
'#type' => 'item',
'#markup' => $output,
);
}
else {
drupal_set_message(t('A transformation error has occured!'), 'error');
}
return $form;
}
/**
* Implements hook_access().
*/
function manidora_metadata_access($object) {
if (!in_array('islandora:collectionCModel', $object->models) && $object['MODS']) {
return TRUE;
}
return FALSE;
}
/**
* Overwrites the islandora_fits_access() function for use in Manidora.
*
* @param IslandoraObject $object
* An instansiated IslandoraObject.
*
* @return bool
* Access or not.
*/
function manidora_technical_metadata_access($object) {
$allowed_models = array('islandora:sp_basic_image', 'islandora:sp_large_image_cmodel');
if ($object[variable_get('islandora_fits_techmd_dsid', 'TECHMD')] && user_access('view technical metadata') && count(array_intersect($allowed_models, $object->models))) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Implements hook_access().
* @TODO: Expand on the restriction?
*/
function manidora_download_access($object) {
if (!in_array('islandora:collectionCModel', $object->models)) {
return TRUE;
}
return FALSE;
}
/**
* Utility function that runs an xslt on MODS metadata.
*
* The transform was straight jacked from U of M's D6 site.
*
* @param IslandoraObject $object
* The IslandoraObject whose MODS metadata you wish to transform.
*/
function manidora_mods_transform($object) {
$mods_text = $object->repository->api->a->getDatastreamDissemination($object->id, 'MODS');
if ($mods_text) {
$xslt_processor = new XSLTProcessor();
$xsl = new DOMDocument();
$file = drupal_get_path('module', 'manidora') . '/xsl/manidora_mods2html.xsl';
$xsl->load($file);
$input = new DOMDocument();
$did_load = $input->loadXML($mods_text);
if ($did_load) {
$xslt_processor->importStylesheet($xsl);
$mods_transformed = $xslt_processor->transformToXml($input);
return $mods_transformed;
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
/**
* Generates a table of all downloadable datastreams for an IslandoraObject.
*
* Used in the "Download" tab.
*
* @param IslandoraObject $islandora_object
* The object whose datastreams you wish to download.
*
* @return array
* Returns a table containing all the datastreams.
*/
function manidora_download_table($islandora_object) {
module_load_include('inc', 'islandora', 'includes/datastream');
module_load_include('inc', 'islandora', 'includes/utilities');
drupal_set_title(t("@label Downloads", array('@label' => $islandora_object->label)));
$headers = array(
'ID' => t('ID'),
'label' => t('Label'),
'mime' => t('Mime type'),
'size' => t('Size'),
'download' => t('Download'),
);
foreach ($islandora_object as $ds) {
$rows[] = array(
'ID' => l($ds->id, islandora_datastream_get_url($ds, 'view')),
'label' => $ds->label,
'mime' => $ds->mimeType,
'size' => islandora_datastream_get_human_readable_size($ds),
'download' => l(t('download'), islandora_datastream_get_url($ds, 'download')),
);
}
$table = array(
'#theme' => 'table',
'#header' => $headers,
'#rows' => $rows,
);
return $table;
}
/**
* Creates the custom search form for searching Manitoba's newspapers.
*
* @TODO: Limit searches based upon newspapers only.
*
* @param array $form
* The form to construct
*
* @param array $form_state
* Its state
*
* @return array
* The constructed form
*/
function manidora_newspaper_search_form($form, &$form_state) {
$form = array(
'all_words' => array(
'#type' => 'textfield',
'#title' => t('With all of the words'),
),
'exact_phrase' => array(
'#type' => 'textfield',
'#title' => t('With the exact phrase'),
),
'least_one' => array(
'#type' => 'textfield',
'#title' => t('With at least one of the words'),
),
'without_words' => array(
'#type' => 'textfield',
'#title' => t('Without the words'),
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Search'),
'#submit' => array('manidora_newspaper_search_form_submit'),
),
);
return $form;
}
/**
* Submit handler for the manidora_newspaper_search_form.
*
* Redirects to the islandora/solr/search menupath to execute the
* given query.
*
* @param array $form
* The form to submit
* @param array $form_state
* Its state
*/
function manidora_newspaper_search_form_submit($form, &$form_state) {
$query_string = '';
if (strlen($form_state['values']['all_words']) > 0) {
$all_words = $form_state['values']['all_words'];
$all_array = explode(' ', $all_words);
$count = 0;
foreach ($all_array as $word) {
if ($count == 0) {
$query_string .= '(' . $word;
}
else {
$query_string .= ' AND ' . $word;
}
$count++;
}
$query_string .= ')';
}
if (strlen($form_state['values']['exact_phrase']) > 0) {
$exact_phrase = $form_state['values']['exact_phrase'];
$exact_phrase = '"' . $exact_phrase . '"';
if (!strlen($query_string)) {
$query_string .= '(' . $exact_phrase . ')';
}
else {
$query_string .= ' AND (' . $exact_phrase . ')';
}
}
// "x" or field:"y" or field:"z"
if (strlen($form_state['values']['least_one']) > 0) {
$least_one = $form_state['values']['least_one'];
$least_array = explode(' ', $least_one);
$count = 0;
if (strlen($query_string)) {
$query_string .= ' AND ';
}
foreach ($least_array as $word) {
if ($count == 0) {
$query_string .= '(' . $word;
}
else {
$query_string .= ' OR ' . $word;
}
$count++;
}
$query_string .= ')';
}
// Negative of "with all of the words"
if (strlen($form_state['values']['without_words']) > 0) {
$without_words = $form_state['values']['without_words'];
$without_array = explode(' ', $without_words);
$count = 0;
if (strlen($query_string)) {
$query_string .= ' AND ';
}
foreach ($without_array as $word) {
if ($count == 0) {
$query_string .= '-(' . $word;
}
else {
$query_string .= ' OR ' . $word;
}
$count++;
}
$query_string .= ')';
}
$query_string = str_replace('/', '~slsh~', $query_string);
$query = array('type' => 'edismax');
drupal_goto('islandora/search/' . $query_string, array('query' => $query));
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*
* Links all collection results to solr results rather than directly to the
* objects.
*/
function manidora_islandora_collectionCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
// We are using this strange hackish URL to get around a bug in the solr
// module https://jira.duraspace.org/browse/ISLANDORA-840
// UofM's collection field has been hacked to not be a URI.
$search_results['object_url'] = 'islandora/search/';
$search_results['object_url_params']['f[0]'] = variable_get('manidora_collection_solr_field', 'RELS_EXT_isMemberOfCollection_uri_ms')
. ':"' . $search_results['solr_doc']['PID'] . '"';
}
/**
* Admin form definition.
*/
function manidora_admin($form, &$form_state) {
$form = array();
$form['manidora_collection_solr_field'] = array(
'#type' => 'textfield',
'#title' => t('Collection Solr Field'),
'#description' => t("Holds the collection Solr field."),
'#default_value' => variable_get('manidora_collection_solr_field', 'RELS_EXT_isMemberOfCollection_uri_ms'),
'#size' => 30,
);
return system_settings_form($form);
}
/**
* Preprocessor for newspaper_page, removes some controls.
*/
function manidora_preprocess_islandora_newspaper_page(&$variables) {
unset($variables['newspaper_controls_array']['jp2_download']);
unset($variables['newspaper_controls_array']['view']);
$variables['newspaper_controls'] = theme('item_list',
array(
'items' => $variables['newspaper_controls_array'],
NULL,
'ul',
'attributes' => array('class' => array('items', 'inline'))));
}
/**
* Constructs a printable PDF from the JPG datastream with header content.
*
* @param string $pid
* A PID representing a Fedora object.
*
* @return string
* Download link containing our created PDF.
*/
function manidora_printable_pdf($pid) {
@include_once 'sites/all/libraries/tcpdf/config/lang/eng.php';
@include_once 'sites/all/libraries/tcpdf/tcpdf.php';
// Extend the TCPDF class to create custom Header and Footer.
class MYPDF extends TCPDF {
/**
* Constructor.
*/
public function __construct($pid, $orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = TRUE, $encoding = 'UTF-8', $diskcache = FALSE, $pdfa = FALSE) {
$this->pid = $pid;
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache);
}
/**
* Page header.
*/
public function Header() {
$string = manidora_retrieve_header_text($this->pid);
if ($string) {
$this->Cell(0, 15, $string, 0, FALSE, 'C', 0, '', 0, FALSE, 'M', 'M');
}
}
}
// Create new PDF document.
$pdf = new MYPDF($pid, PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
$header_text = manidora_retrieve_header_text($pid);
if ($header_text) {
// Parse a bit for use below.
$header_parts = explode(',', $header_text);
$title = $header_parts[0];
}
else {
$title = $pid;
$header_text = $pid;
}
// Set document information.
$pdf->SetTitle($title);
$pdf->SetSubject($title);
// Set default monospaced font.
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// Set margins.
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
// Set auto page breaks.
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Set image scale factor.
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Set some language-dependent strings.
$pdf->setLanguageArray($l);
// Set font.
$pdf->SetFont('times', 'BI', 12);
// Add a page.
$pdf->AddPage();
$pdf->Image(variable_get('islandora_base_url', 'http://localhost:8080/fedora') . '/objects/' . $pid . '/datastreams/JPG/content', '0', '15', '', '', '', '', '', 2);
// Close and output PDF document.
$pdf->Output($header_text . '.pdf', 'D');
}
/**
* Constructs the text used in the header of the PDF.
*
* @param string $pid
* A PID representing a Fedora object.
*
* @return string
* Formatted string for output display.
*/
function manidora_retrieve_header_text($pid) {
$object = islandora_object_load($pid);
if (isset($object['MODS'])) {
$mods = $object['MODS']->content;
$xml = new SimpleXMLElement($mods);
$title = '';
$date = '';
$page = '';
if (isset($xml->titleInfo)) {
if (isset($xml->titleInfo->title)) {
$title = (string) $xml->titleInfo->title;
$title = preg_replace('/:(.*)/', '', $title);
}
}
if (empty($title)) {
$title = t('Unknown');
}
if (isset($xml->relatedItem)) {
if (isset($xml->relatedItem->part)) {
if (isset($xml->relatedItem->part->date)) {
$date = $xml->relatedItem->part->date;
}
if (isset($xml->relatedItem->part->extent)) {
if (isset($xml->relatedItem->part->extent->start)) {
$page = $xml->relatedItem->part->extent->start;
}
}
}
}
if (empty($date)) {
$date = t('Unknown');
}
if (empty($page)) {
$page = t('Unknown');
}
$text = $title . ', ' . $date . ' (Page ' . $page . ')';
return $text;
}
else {
return FALSE;
}
}