forked from Islandora/islandora_solution_pack_book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislandora_book.module
475 lines (448 loc) · 18.1 KB
/
islandora_book.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
<?php
/**
* @file
* Defines all the hooks this module implements.
*/
/**
* Implements hook_menu().
*/
function islandora_book_menu() {
return array(
'admin/islandora/solution_pack_config/book' => array(
'title' => 'Book Solution Pack',
'description' => 'Select viewers and configure ingest behavior.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_book_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
'islandora/object/%islandora_object/pages' => array(
'title' => 'Pages',
'type' => MENU_LOCAL_TASK,
'page callback' => 'islandora_book_pages_menu',
'page arguments' => array(2),
'access callback' => 'islandora_user_access',
'access arguments' => array(
2, array(ISLANDORA_VIEW_OBJECTS), array('islandora:bookCModel'),
),
),
'islandora/object/%islandora_object/manage/book' => array(
'title' => 'Book',
'type' => MENU_LOCAL_TASK,
'page callback' => 'islandora_paged_content_manage_pages_menu',
'page arguments' => array(2),
'access callback' => 'islandora_paged_content_manage_pages_access_callback',
'access arguments' => array(2, array('islandora:bookCModel')),
'file path' => drupal_get_path('module', 'islandora_paged_content'),
'file' => 'includes/manage_pages.inc',
),
'islandora/object/%islandora_object/manage/book/ingest' => array(
'title' => 'Add Page',
'page callback' => 'islandora_book_ingest_page',
'page arguments' => array(2),
'type' => MENU_LOCAL_ACTION,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_INGEST, 2),
'file' => 'includes/manage_book.inc',
),
'islandora/object/%islandora_object/manage/book/ingest_zipped' => array(
'title' => 'Add Zipped Pages',
'page callback' => 'islandora_paged_content_ingest_zipped_pages',
'page arguments' => array(2,
array('tif', 'tiff', 'jpg', 'jpeg', 'jp2'),
'islandora:pageCModel',
variable_get('islandora_book_ingest_derivatives', array('ocr')),
),
'type' => MENU_LOCAL_ACTION,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_INGEST, 2),
'file path' => drupal_get_path('module', 'islandora_paged_content'),
'file' => 'includes/manage_pages.inc',
),
'islandora/object/%islandora_object/manage/book_page' => array(
'title' => 'Page',
'type' => MENU_LOCAL_TASK,
'page callback' => 'islandora_paged_content_manage_page_menu',
'page arguments' => array(2),
'access callback' => 'islandora_paged_content_manage_page_access_callback',
'access arguments' => array(2, array('islandora:pageCModel')),
'file path' => drupal_get_path('module', 'islandora_paged_content'),
'file' => 'includes/manage_page.inc',
),
);
}
/**
* Implements hook_theme().
*/
function islandora_book_theme() {
return array(
'islandora_book_book' => array(
'variables' => array('object' => NULL),
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-book-book',
),
'islandora_book_page' => array(
'arguments' => array('object' => NULL),
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-book-page',
),
'islandora_book_page_img_print' => array(
'template' => 'theme/islandora-book-page-img-print',
'variables' => array('islandora_content' => NULL),
),
);
}
/**
* Implements hook_islandora_xml_form_builder_form_associations().
*/
function islandora_book_islandora_xml_form_builder_form_associations() {
return array(
'islandora_book_mods_form' => array(
'content_model' => 'islandora:bookCModel',
'form_name' => 'Islandora Book MODS Form',
'dsid' => 'MODS',
'title_field' => array('titleInfo', 'title'),
'transform' => 'mods_to_dc.xsl',
'template' => FALSE,
),
);
}
/**
* Implements hook_islandora_xml_form_builder_forms().
*/
function islandora_book_islandora_xml_form_builder_forms() {
$module_path = drupal_get_path('module', 'islandora_book');
return array('Islandora Book MODS Form' => array('form_file' => "$module_path/data/forms/book_form_mods.xml"));
}
/**
* Implements hook_islandora_required_objects().
*/
function islandora_book_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'islandora_book');
$datastreams_path = "$module_path/data/datastreams";
// Page Content Model.
$page_content_model = $connection->repository->constructObject('islandora:pageCModel');
$page_content_model->owner = 'fedoraAdmin';
$page_content_model->label = 'Islandora Page Content Model';
$page_content_model->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL Datastream.
$datastream = $page_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'M');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'application/xml';
$paged_content_path = drupal_get_path('module', 'islandora_paged_content');
$datastream->setContentFromFile("$paged_content_path/xml/islandora_pageCModel_ds_composite_model.xml", FALSE);
$page_content_model->ingestDatastream($datastream);
// Book Content Model.
$book_content_model = $connection->repository->constructObject('islandora:bookCModel');
$book_content_model->owner = 'fedoraAdmin';
$book_content_model->label = 'Islandora Internet Archive Book Content Model';
$book_content_model->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL Datastream.
$datastream = $book_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'M');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'application/xml';
$datastream->setContentFromFile("$datastreams_path/islandora_bookCModel_ds_composite_model.xml", FALSE);
$book_content_model->ingestDatastream($datastream);
// Book Collection.
$book_collection = $connection->repository->constructObject('islandora:bookCollection');
$book_collection->owner = 'fedoraAdmin';
$book_collection->label = 'Book Collection';
$book_collection->models = 'islandora:collectionCModel';
$book_collection->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:root');
// Collection Policy Datastream.
$datastream = $book_collection->constructDatastream('COLLECTION_POLICY', 'M');
$datastream->label = 'COLLECTION_POLICY';
$datastream->mimetype = 'application/xml';
$datastream->setContentFromFile("$datastreams_path/islandora_book_collection_policy.xml", FALSE);
$book_collection->ingestDatastream($datastream);
// TN Datastream.
$datastream = $book_collection->constructDatastream('TN', 'M');
$datastream->label = 'TN';
$datastream->mimetype = 'image/png';
$datastream->setContentFromFile("$module_path/images/folder.png", FALSE);
$book_collection->ingestDatastream($datastream);
return array(
'islandora_book' => array(
'title' => 'Islandora book',
'objects' => array(
$page_content_model,
$book_content_model,
$book_collection,
),
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_book_islandora_bookcmodel_islandora_view_object($object) {
$output = theme('islandora_book_book', array('object' => $object));
return array('islandora_book' => $output);
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_book_islandora_pagecmodel_islandora_view_object($object, $page_number, $page_size) {
$output = theme('islandora_book_page', array('object' => $object));
return array('islandora_book' => $output);
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*
* Replaces the url for the search result to be the book's url, not the page.
* The page is added as a fragment at the end of the book url.
*/
function islandora_book_islandora_pageCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
// Grab the names of the appropriate solr fields from the db.
$parent_book_field_name = variable_get('islandora_book_parent_book_solr_field', 'RELS_EXT_isMemberOf_uri_ms');
$page_number_field_name = variable_get('islandora_paged_content_page_number_solr_field', 'RELS_EXT_isSequenceNumber_literal_ms');
// If:
// there's an object url AND
// there's a solr doc AND
// the solr doc contains the parent book AND
// the solr doc contains the page number...
if (isset($search_results['object_url']) &&
isset($search_results['solr_doc']) &&
isset($search_results['solr_doc'][$parent_book_field_name]) &&
count($search_results['solr_doc'][$parent_book_field_name]) &&
isset($search_results['solr_doc'][$page_number_field_name]) &&
count($search_results['solr_doc'][$page_number_field_name])) {
// Replace the result url with that of the parent book and add the page
// number as a fragment.
$book_pid = preg_replace('/info\:fedora\//', '', $search_results['solr_doc'][$parent_book_field_name][0], 1);
$page_number = $search_results['solr_doc'][$page_number_field_name][0];
if (islandora_object_access(ISLANDORA_VIEW_OBJECTS, islandora_object_load($book_pid))) {
$search_results['object_url'] = "islandora/object/$book_pid";
$search_results['object_url_fragment'] = "page/$page_number/mode/1up";
// XXX: Won't handle fielded searches nicely... then again, if our
// highlighting field is not the one being search on, this makes sense?
if ($query_processor->solrDefType == 'dismax' || $query_processor->solrDefType == 'edismax') {
$search_results['object_url_fragment'] .= "/search/" . rawurlencode($query_processor->solrQuery);
}
}
}
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*
* Add page viewing fragment and search term to show all search results within
* book on page load.
*/
function islandora_book_islandora_bookCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
$view_types = array(
"1" => "1up",
"2" => "2up",
"3" => "thumb",
);
$field_search = FALSE;
$fields = preg_split('/OR|AND|NOT/', $query_processor->solrQuery);
foreach ($fields as $field) {
if (preg_match('/^(.*):\(.*\)/', $field)) {
$field_search = TRUE;
break;
}
}
if ($query_processor->solrQuery != " " && $query_processor->solrQuery && !$field_search) {
$ia_view = variable_get('islandora_internet_archive_bookreader_default_page_view', "1");
if ($query_processor->solrQuery != '*:*') {
$search_results['object_url_fragment'] = "page/1/mode/{$view_types[$ia_view]}/search/";
$search_results['object_url_fragment'] .= rawurlencode($query_processor->solrQuery);
}
}
}
/**
* Implements hook_islandora_ingest_steps().
*/
function islandora_book_islandora_pagecmodel_islandora_ingest_steps(array $form_state) {
$shared_storage = islandora_ingest_form_get_shared_storage($form_state);
if (empty($shared_storage['book'])) {
return;
}
return array(
'islandora_book_upload_pages' => array(
'weight' => 10,
'type' => 'form',
'form_id' => 'islandora_paged_content_upload_page_form',
'args' => array($shared_storage['book']),
'module' => 'islandora_paged_content',
'file' => 'includes/upload_page.form.inc',
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_ingest_steps().
*/
function islandora_book_islandora_bookCModel_islandora_ingest_steps(array $form_state) {
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
if (islandora_paged_content_can_extract_from_pdf()) {
return array(
'islandora_book_upload_pdf' => array(
'weight' => 10,
'type' => 'form',
'form_id' => 'islandora_paged_content_upload_pdf_form',
'args' => array('islandora:pageCModel'),
'module' => 'islandora_paged_content',
'file' => 'includes/upload_pdf.form.inc',
),
);
}
}
/**
* Implements hook_islandora_derivative().
*/
function islandora_book_islandora_bookcmodel_islandora_derivative() {
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
return islandora_paged_content_paged_object_derivatives('islandora_book');
}
/**
* Implements hook_islandora_derivative().
*/
function islandora_book_islandora_pagecmodel_islandora_derivative() {
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
return islandora_paged_content_page_derivatives('islandora_book');
}
/**
* Implements hook_form_islandora_object_properties_form_alter().
*/
function islandora_book_form_islandora_object_properties_form_alter(array &$form, array &$form_state) {
$object = $form_state['object'];
if (in_array('islandora:bookCModel', $object->models)) {
$form['delete']['#value'] = t('Delete Book');
}
}
/**
* Implements hook_form_islandora_delete_object_form_alter().
*/
function islandora_book_form_islandora_delete_object_form_alter(array &$form, array &$form_state) {
$object = $form_state['object'];
if (in_array('islandora:bookCModel', $object->models)) {
$form['description']['#markup'] = t('This will remove the book object and all related page objects. This action cannot be undone.');
$form['#submit'][] = 'islandora_book_islandora_delete_object_form_delete_pages_submit';
if (($key = array_search('islandora_delete_object_form_submit', $form['#submit'])) !== FALSE) {
unset($form['#submit'][$key]);
}
}
}
/**
* Delete all the page objects related to the book object being deleted.
*
* @param array $form
* The Drupal form.
* @param array $form_state
* The Drupal form state.
*/
function islandora_book_islandora_delete_object_form_delete_pages_submit(array $form, array &$form_state) {
module_load_include('inc', 'islandora_paged_content', 'includes/batch');
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
module_load_include('inc', 'islandora', 'includes/utilities');
$object = $form_state['object'];
$get_pid = function($o) {
return $o['pid'];
};
$pages = array_values(array_map($get_pid, islandora_paged_content_get_pages($object)));
$batch_delete = islandora_paged_content_delete_pages_batch($object, $pages);
$batch_delete['operations'][] = array('islandora_paged_content_delete_parent_object_operation', array($object->id));
batch_set($batch_delete);
$parents = islandora_get_parents_from_rels_ext($object);
$parent = array_pop($parents);
$form_state['redirect'] = isset($parent) ? "islandora/object/{$parent->id}" : 'islandora';
}
/**
* Renders the Pages local menu task.
*
* @param AbstractObject $object
* The book object to fetch the pages from.
*
* @return string
* The HTML repersentation of the given books pages.
*/
function islandora_book_pages_menu(AbstractObject $object) {
module_load_include('inc', 'islandora', 'includes/breadcrumb');
drupal_set_breadcrumb(islandora_get_breadcrumbs($object));
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
$pages = islandora_paged_content_get_pages($object);
return theme('islandora_objects', array('objects' => array_keys($pages)));
}
/**
* Implements hook_islandora_update_related_objects_properties().
*/
function islandora_book_islandora_bookCModel_islandora_update_related_objects_properties($object) {
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
$get_pid = function($o) {
return $o['pid'];
};
$pages = array_values(array_map($get_pid, islandora_paged_content_get_pages($object)));
return $pages;
}
/**
* Implements hook_islandora_xacml_editor_child_query().
*/
function islandora_book_islandora_bookCModel_islandora_xacml_editor_child_query(AbstractObject $object) {
$pages_query = <<<EOQ
SELECT ?object from <#ri> WHERE
{
?object <fedora-rels-ext:isMemberOf> <info:fedora/{$object->id}>
}
EOQ;
return array(
'book_pages' => array(
'type' => 'sparql',
'query' => $pages_query,
'description' => t('All pages of this book (existing and new).'),
'recurse' => TRUE,
),
);
}
/**
* Implements hook_islandora_view_print_object().
*/
function islandora_book_islandora_view_print_object($object) {
if (in_array('islandora:pageCModel', $object->models)) {
// Theme image separately.
$islandora_content = theme('image', array(
'title' => $object->label,
'path' => url("islandora/object/{$object->id}/datastream/JPG/view"),
));
return theme('islandora_book_page_img_print', array(
'islandora_content' => $islandora_content,
));
}
}
/**
* Implements hook_CMODEL_PID_DSID_islandora_datastream_ingested().
*/
function islandora_book_islandora_pagecmodel_techmd_islandora_datastream_ingested(AbstractObject $object, AbstractDatastream $datastream) {
module_load_include('module', 'islandora_fits');
$techmd = $datastream->content;
$xml = new SimpleXMLElement($techmd);
$xml->registerXPathNamespace('fits', 'http://hul.harvard.edu/ois/xml/ns/fits/fits_output');
$fits_metadata = islandora_fits_child_xpath($xml);
$unit = isset($fits_metadata['Jhove']['Sampling Frequency Unit'][0]) ? $fits_metadata['Jhove']['Sampling Frequency Unit'][0] : 'ins.';
$xcount = isset($fits_metadata['Jhove']['X Sampling Frequency'][0]) ? $fits_metadata['Jhove']['X Sampling Frequency'][0] : '600';
$ycount = isset($fits_metadata['Jhove']['Y Sampling Frequency'][0]) ? $fits_metadata['Jhove']['Y Sampling Frequency'][0] : '600';
if ($xcount < 600 || $ycount < 600) {
$message = t("Ingested image has a sampling frequency of @x by @y per @unit For best results use images of at least 600 by 600 per in.",
array(
'@x' => $xcount,
'@y' => $ycount,
'@unit' => $unit,
));
drupal_set_message($message, 'warning', FALSE);
}
}
/**
* Implements hook_islandora_paged_content_model_registry().
*/
function islandora_book_islandora_paged_content_content_model_registry() {
return array(
'islandora:bookCModel' => array(
'parents' => array(
'islandora:collectionCModel' => 'isMemberOfCollection',
),
'children' => array(
'islandora:pageCModel' => array(),
),
),
);
}