-
Notifications
You must be signed in to change notification settings - Fork 15
/
islandora_pdfjs.module
171 lines (158 loc) · 5.99 KB
/
islandora_pdfjs.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
<?php
/**
* @file
* Defines all the hooks this module implements.
*/
/**
* Implements hook_theme().
*/
function islandora_pdfjs_theme() {
return array(
'islandora_pdfjs' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-pdfjs',
'variables' => array(
'fedora_object' => NULL,
'dsid' => NULL,
),
),
'viewer' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/viewer',
'variables' => array(
'fedora_object' => NULL,
),
),
);
}
/**
* Implements hook_islandora_viewer_info().
*/
function islandora_pdfjs_islandora_viewer_info() {
return array(
'islandora_pdfjs' => array(
'label' => t('pdf.js Reader'),
'description' => t('Mozilla pdf.js Reader.'),
'configuration' => '',
'callback' => 'islandora_pdfjs_viewer_callback',
'model' => array(
'islandora:sp_pdf',
'islandora:bookCModel',
'islandora:pageCModel',
'islandora:newspaperIssueCModel'),
'mimetype' => array('application/pdf'),
),
);
}
/**
* Callback function for the viewer.
*
* @param array $params
* The params for the theme function 'islandora_pdfjs'.
*
* @param IslandoraAbstractObject $fedora_object
* The object that we are viewing.
*
* @return string
* HTML repersentation of the pdf.js Reader.
*/
function islandora_pdfjs_viewer_callback(array $params, $fedora_object = NULL) {
$dsid = NULL;
if (isset($params['dsid']) && !empty($params['dsid']) && isset($fedora_object[$params['dsid']]) &&
$fedora_object[$params['dsid']]->mimetype == 'application/pdf') {
$dsid = $params['dsid'];
}
else {
if (isset($fedora_object['PDF'])) {
$dsid = 'PDF';
}
elseif (isset($fedora_object['OBJ']) && $fedora_object['OBJ']->mimetype == 'application/pdf') {
$dsid = 'OBJ';
}
}
if ($dsid && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $fedora_object[$dsid])) {
return theme('islandora_pdfjs', array('fedora_object' => $fedora_object, 'dsid' => $dsid));
}
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*/
function islandora_pdfjs_islandora_bookCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
module_load_include('inc', 'islandora', 'includes/solution_packs');
$viewer_id = islandora_get_viewer_id('islandora_book_viewers');
if ($viewer_id == 'islandora_pdfjs') {
islandora_pdfjs_add_solr_search_param($search_results, $query_processor);
}
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*/
function islandora_pdfjs_islandora_sp_pdf_islandora_solr_object_result_alter(&$search_results, $query_processor) {
module_load_include('inc', 'islandora', 'includes/solution_packs');
$viewer_id = islandora_get_viewer_id('islandora_pdf_viewers');
if ($viewer_id == 'islandora_pdfjs') {
islandora_pdfjs_add_solr_search_param($search_results, $query_processor);
}
}
/**
* Implements hook_CMODEL_PID_islandora_solr_object_result_alter().
*/
function islandora_pdfjs_islandora_newspaperIssueCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
module_load_include('inc', 'islandora', 'includes/solution_packs');
$viewer_id = islandora_get_viewer_id('islandora_newspaper_issue_viewers');
if ($viewer_id == 'islandora_pdfjs') {
islandora_pdfjs_add_solr_search_param($search_results, $query_processor);
}
}
/**
* 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 parameter at the end of the book url.
*/
function islandora_pdfjs_islandora_pageCModel_islandora_solr_object_result_alter(&$search_results, $query_processor) {
module_load_include('inc', 'islandora', 'includes/solution_packs');
$viewer_id = islandora_get_viewer_id('islandora_book_page_viewers');
if ($viewer_id == 'islandora_pdfjs') {
islandora_pdfjs_add_solr_search_param($search_results, $query_processor);
}
$viewer_id = islandora_get_viewer_id('islandora_book_viewers');
if ($viewer_id == 'islandora_pdfjs') {
// 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 parameter.
$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_params']['page'] = $page_number;
}
}
}
}
/**
* Add search term to result url as a param.
*
* @see hook_CMODEL_PID_islandora_solr_object_result_alter
*/
function islandora_pdfjs_add_solr_search_param(&$search_results, $query_processor) {
if ($query_processor->solrQuery != " " &&
$query_processor->solrQuery &&
$query_processor->solrQuery != '*:*' &&
($query_processor->solrDefType == 'dismax' || $query_processor->solrDefType == 'edismax')) {
$search_results['object_url_params']['search'] = rawurlencode($query_processor->solrQuery);
}
}