-
Notifications
You must be signed in to change notification settings - Fork 5
/
PdfEmbedPlugin.php
116 lines (99 loc) · 3.32 KB
/
PdfEmbedPlugin.php
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
<?php
/**
* @package PdfEmbed
* @copyright Copyright 2014, John Flatness
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 or any later version
*/
class PdfEmbedPlugin extends Omeka_Plugin_AbstractPlugin
{
const OPTION_NAME = 'pdf_embed_settings';
private static $types = array('application/pdf', 'application/x-pdf');
private static $exts = array('pdf');
private static $defaultSettings = array(
'height' => 500,
'pdf_embed_type' => 'object'
);
private static $settings = array();
protected $_hooks = array('initialize',
'config', 'config_form', 'install', 'uninstall'
);
public function hookInstall()
{
self::_setSettings(self::$defaultSettings);
}
public function hookUninstall()
{
delete_option(self::OPTION_NAME);
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
add_file_display_callback(
array(
'mimeTypes' => self::$types,
'fileExtensions' => self::$exts
),
'PdfEmbedPlugin::embedPdf',
self::_getSettings()
);
}
public function hookConfigForm()
{
$settings = self::_getSettings();
include 'config-form.php';
}
public function hookConfig()
{
$settings['height'] = (int) $_POST['height'];
$settings['pdf_embed_type'] = $_POST['pdf_embed_type'];
self::_setSettings($settings);
}
public static function embedPdf($file, $options)
{
switch ($options['pdf_embed_type']) {
case 'pdf_js':
return self::embedPdfJs($file, $options);
case 'object':
default:
return self::embedPdfObject($file, $options);
}
}
public static function embedPdfObject($file, $options)
{
$height = (int) $options['height'];
$pdfPath = $file->getWebPath('original');
$attrs['data'] = $pdfPath;
$attrs['type'] = 'application/pdf';
$attrs['style'] = "width: 100%; height: {$height}px";
$attrString = tag_attributes($attrs);
$fallback = '<a href="' . html_escape($pdfPath) . '">' . metadata($file, 'display_title') . '</a>';
return "<object {$attrString}>{$fallback}</object>";
}
public static function embedPdfJs($file, $options)
{
$height = (int) $options['height'];
$pdfJsViewer = web_path_to('pdf-embed-js/web/viewer.html');
$hash = (($lang = get_html_lang()) == 'en-US')
? ''
: '#locale=' . rawurlencode($lang);
$attrs['src'] = $pdfJsViewer
. '?file=' . rawurlencode($file->getWebPath('original'))
. $hash;
$attrs['style'] = "width: 100%; height: {$height}px";
$attrs['title'] = $file->original_filename;
$attrString = tag_attributes($attrs);
return "<iframe {$attrString}></iframe>";
}
public static function _getSettings()
{
if (!self::$settings) {
$settings = json_decode(get_option(self::OPTION_NAME), true);
self::$settings = $settings ? $settings : array();
}
return self::$settings;
}
public static function _setSettings($settings)
{
set_option(self::OPTION_NAME, json_encode($settings));
}
}