-
Notifications
You must be signed in to change notification settings - Fork 1
/
dlts_viewer.module
executable file
·460 lines (373 loc) · 11.9 KB
/
dlts_viewer.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
<?php
/**
* @file
* DLTS Viewer module.
*/
/**
* Load MongoDB helper.
*/
include_once 'inc/common.inc';
/**
* Include helpers and others.
*/
include_once 'inc/utils.inc';
/**
* Load MongoDB helper.
*/
include_once 'inc/mongodb.inc';
/**
* Include wildcard loaders.
*/
include_once 'inc/loaders.inc';
/**
* Include hook_menu and all the module routes.
*/
include_once 'inc/routes.inc';
/**
* Include types.
*/
include_once 'inc/types.inc';
/**
* Include Views hooks and Views helpers.
*/
include_once 'inc/views.inc';
/**
* Include datasource helpers.
*/
include_once 'inc/datasource.inc';
/**
* Include theme functions and hook_theme.
*/
include_once 'inc/themes.inc';
/**
* Include shutdown functions.
*/
include_once 'inc/shutdown.inc';
/**
* Implementation of hook_init().
*/
function dlts_viewer_init() {
drupal_add_http_header('Access-Control-Allow-Origin', '*');
}
/**
* Implementation of hook_theme().
* https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7
*/
function dlts_viewer_custom_theme() {
if (dlts_viewer_is_resource()) {
return 'tao';
}
if (current_path() == 'api/v1') {
return 'tao';
}
return 'rubik';
}
/**
* Implements hook_permission().
*/
function dlts_viewer_permission() {
return [
'access dlts repository' => [
'title' => t('Query repository'),
'description' => t('Query repository and search objects.'),
],
'administer fileserver' => [
'title' => t('administer fileserver'),
'description' => t('Administer the settings for the fileServer.'),
],
'dlts create object' => [
'title' => t('Use DLTS Viewer API to create object'),
'description' => t('Allow user to create object using DLTS Viewer API.'),
],
'dlts topic api' => [
'title' => t('Allow user to see Topic API'),
'description' => t('Allow user to see Topic API'),
],
'access iiif content' => [
'title' => t('Allow user to IIIF content'),
'description' => t('Allow user to IIIF content'),
],
'administer viewer' => [
'title' => t('Allow user to administer Viewer'),
'description' => t('Allow user to administer Viewer'),
],
'access supafolio api' => [
'title' => t('Access Supafolio API'),
'description' => t('Access and search Supafolio API'),
],
];
}
/**
* Implements hook_entity_info_alter().
* https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_info_alter/7
*/
function dlts_viewer_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['metadata'] = [
'label' => t('Metadata List'),
'custom settings' => TRUE,
];
}
/**
* Implements template_preprocess_html
* https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_html/7
*/
function dlts_viewer_preprocess_html(&$vars) {
$vars['analytics'] = variable_get('analytics', []);
if (isset($vars['head_title_array']['title'])) {
$vars['head_title'] = $vars['head_title_array']['title'];
}
/**
* The X-Frame-Options HTTP response header can be used to indicate whether or not a
* browser should be allowed to render a page in a <frame>, <iframe> or <object> .
* Sites can use this to avoid clickjacking attacks, by ensuring that their content is
* not embedded into other sites.
*
* The added security is only provided if the user accessing the document is using a
* browser supporting X-Frame-Options.
*
* As of Drupal 7.50, Drupal core sets X-Frame-Options by default to prevent the site
* from being embedded in an iframe on another domain. We want our sites to be
* embedded; we need to remove this header.
*
* See: https://www.drupal.org/node/2735873
* See: https://www.owasp.org/index.php/Clickjacking
* See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
*
*/
header_remove('X-Frame-Options');
// check if we are in an admin page
if (dlts_viewer_is_resource()) {
// see html.tpl.php
$vars['pane_metadata_hidden'] = '';
/** The Viewer sets language direction in the markup */
$vars['language_dir'] = dlts_viewer_viewer_language($vars);
/** we are not in admin page, default Viewer HTML template */
$vars['theme_hook_suggestions'][] = 'html__viewer';
/** Check for mobile */
if (dlts_viewer_is_mobile()) {
$vars['classes_array'][] = 'is_mobile';
// hide information panel for mobile
// see https://jira.nyu.edu/jira/browse/DLTSVIEWER-70
$vars['pane_metadata_hidden'] = 'pagemeta-hidden';
}
else {
$vars['classes_array'][] = 'not_mobile';
}
// See: https://jira.nyu.edu/jira/browse/DLTSVIEWER-81
$language_default = language_default();
$vars['resource_language_code'] = $language_default->language;
// We have access to $resource_language_code in the global context
// if the resource exist.
// See: dlts_viewer_book_dispatcher()
$resource_language_code = dlts_viewer_resource_language_code();
if ($resource_language_code) {
$vars['resource_language_code'] = $resource_language_code;
}
/**
* We use Drupal to render objects from MongoDB that don't have a record register
* in Drupal's database. We don't want Drupal to manage this records. We need a
* way to "HOOK" in different stages when the objects it's being render. In this
* case we want to allowed changes to hook_preprocess_html
*/
$call_suggestion = array_shift($vars['theme_hook_suggestions']);
if ($call_suggestion) {
$call_suggestion = str_replace('html__', '', $call_suggestion);
$function_to_call = __FUNCTION__ . '_' . $call_suggestion;
if (function_exists($function_to_call)) {
$function_to_call($vars);
}
}
}
}
/**
* Implements hook_process_HOOK
* https://api.drupal.org/api/drupal/modules%21system%21theme.api.php/function/hook_process_HOOK/7.x
*/
function dlts_viewer_process_node(&$vars) {
$function_to_call = __FUNCTION__ . '_' . $vars['node']->type;
/** Call hook_process_node_TYPE if exist */
if (function_exists($function_to_call)) {
$function_to_call($vars);
}
}
/**
* Implements template_preprocess_node().
* https://api.drupal.org/api/drupal/modules%21node%21node.module/function/template_preprocess_node/7
*/
function dlts_viewer_preprocess_node(&$vars) {
$function_to_call = __FUNCTION__ . '_' . $vars['node']->type;
/** Call hook_process_node_TYPE if exist */
if (function_exists($function_to_call)) {
$function_to_call($vars);
}
}
/**
* Implements template_preprocess_page
* https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7
*/
function dlts_viewer_preprocess_page(&$vars) {
if (dlts_viewer_is_resource()) {
$vars['language_dir'] = dlts_viewer_viewer_language($vars);
}
}
/**
* Implements template_process_page().
* https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_process_page/7
*/
function dlts_viewer_process_page(&$vars) {
if (dlts_viewer_is_resource()) {
$is_embed = dlts_viewer_is_embed();
$vars['theme_hook_suggestions'][] = 'page__viewer';
$vars['language_dir'] = dlts_viewer_viewer_language($vars);
$vars['is_embed'] = $is_embed;
// check if this is a embed request. Don't show title if is embeded
if (!$is_embed) {
$top_options = [
'is_admin' => $vars['is_admin'],
'tabs' => render($vars['tabs']),
'messages' => $vars['messages'],
'title' => $vars['title'],
'action_links' => render($vars['action_links']),
'title_prefix' => render($vars['title_prefix']),
'title_suffix' => render($vars['title_suffix']),
'help' => render($vars['page']['help']),
];
$vars['top'] = theme('dlts_viewer_top', $top_options);
}
}
}
/**
* Implements hook_stream_wrappers().
*
* hook_stream_wrappers() is Drupal's way of exposing the class that PHP will
* use to provide a new stream wrapper class. In this case, we'll expose the
* 'fileserver' scheme, e.g., "fileserver://example/example.txt"
*
* @see FileFilesServerStreamWrapper
*/
function dlts_viewer_stream_wrappers() {
return [
'fileserver' => [
'name' => t('Fileserver StreamWrapper'),
'class' => 'FileFilesServerStreamWrapper',
'description' => t('Access file from the FileServer.'),
'type' => STREAM_WRAPPERS_WRITE_VISIBLE,
],
'pdfserver' => [
'name' => t('PDFServer StreamWrapper'),
'class' => 'PDFServerStreamWrapper',
'description' => t('Access PDF file from the PDFServer.'),
'type' => STREAM_WRAPPERS_WRITE_VISIBLE,
],
];
}
function dlts_viewer_metadata_display($node) {
return node_view($node, 'metadata', $node->language);
}
/**
* Implements hook_flush_caches().
*
* Using this as a way to get certain phrases into the t() workflow, that don't
* seem to otherwise make it. To get these items to show up in the Translate
* interface, go to a different language and in the Drupal admin clear the
* Drupal cache. (ie, at /admin/config/development/performance
*/
function dlts_viewer_flush_caches() {
// Translatables
// Included for use with string extractors like potx.
// https://docs.google.com/spreadsheets/d/1LIwBMDOtuuaPqjx70CbqvygWF8MUVRmM7-gB1plE9kM/edit#gid=0
t('Author');
t('High-resolution');
t('Low-resolution');
t('High resolution');
t('Low resolution');
t('High-resolution PDF');
t('Low-resolution PDF');
t('Vol.');
t('Download');
t('Author/Contributor');
t('Binding Orientation');
t('Call Number');
t('Collection');
t('Contributor');
t('Creator');
t('Description');
t('Dimensions');
t('Editor');
t('Geographic Coordinates');
t('Geographic Subject');
t('Permanent Link');
t('Identifier');
t('ISBN');
t('Language');
t('Language Code');
t('Number');
t('OCR Text');
t('Other Version');
t('Page Count');
t('Provider');
t('PDF');
t('Date of Publication');
t('Place of Publication');
t('Publisher');
t('Read Order');
t('Representative Image');
t('Rights');
t('Scan Date');
t('Scan Order');
t('Scanning Notes');
t('Sequence Count');
t('Subject');
t('Subtitle');
t('Title');
t('Topic');
t('Volume');
t('Arabic collections online');
// المجموعات العربية على الانترنت
t('New York University Libraries');
// مكتبات جامعة نيويورك
t('Princeton University Libraries');
// مكتبات جامعة برينستون
t('Cornell University Libraries');
// مكتبات جامعة كورنيل
t('Columbia University Libraries');
// مكتبات جامعة كولومبيا
t('American University of Beirut');
// الجامعة الاميركية في بيروت
t('American University in Cairo');
// الجامعة الاميركية بالقاهرة
t('United Arab Emirates National Archives');
// الامارات العربية المتحدة - الارشيف الوطني
}
function dlts_viewer_js_alter(&$javascript) {
if (dlts_viewer_is_resource()) {
$allow = [
'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/4.1.1/openseadragon.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/axios/1.6.8/axios.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/keyboardjs/2.6.2/keyboard.min.js',
'sites/all/modules/dlts_viewer/dist/polyfill.bundle.js',
'sites/all/modules/dlts_viewer/dist/books.bundle.js',
];
foreach ($javascript as $key => $setting) {
if ((array_search($key, $allow)) === false) {
unset($javascript[$key]);
}
}
}
}
function dlts_viewer_css_alter(&$css) {
if (dlts_viewer_is_resource()) {
$allow = [
'sites/all/modules/dlts_viewer/dist/viewer.css',
];
foreach ($css as $key => $setting) {
if ((array_search($key, $allow)) === false) {
unset($css[$key]);
}
}
}
}
// Here we need to make sure we delete the record from Apache Solr
function dlts_viewer_node_delete($node) {
db_delete('dlts_viewer_resource')->condition('nid', $node->nid)->execute();
}