/
plugin.php
182 lines (154 loc) · 5.98 KB
/
plugin.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
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
<?php
add_plugin_hook('install', 'DocsViewerPlugin::install');
add_plugin_hook('uninstall', 'DocsViewerPlugin::uninstall');
add_plugin_hook('config_form', 'DocsViewerPlugin::configForm');
add_plugin_hook('config', 'DocsViewerPlugin::config');
add_plugin_hook('admin_append_to_items_show_primary', 'DocsViewerPlugin::append');
add_plugin_hook('public_append_to_items_show', 'DocsViewerPlugin::append');
if (!defined('WEB_DOC_VIEWER_PLUGIN_DIR')) {
define('WEB_DOC_VIEWER_PLUGIN_DIR', WEB_PLUGIN . '/DocsViewer');
}
class DocsViewerPlugin
{
const API_URL = 'http://docs.google.com/viewer';
const DEFAULT_VIEWER_EMBED = 1;
const DEFAULT_VIEWER_WIDTH = 500;
const DEFAULT_VIEWER_HEIGHT = 600;
public static function install()
{
set_option('docsviewer_embed_admin', DocsViewerPlugin::DEFAULT_VIEWER_EMBED);
set_option('docsviewer_width_admin', DocsViewerPlugin::DEFAULT_VIEWER_WIDTH);
set_option('docsviewer_height_admin', DocsViewerPlugin::DEFAULT_VIEWER_HEIGHT);
set_option('docsviewer_embed_public', DocsViewerPlugin::DEFAULT_VIEWER_EMBED);
set_option('docsviewer_width_public', DocsViewerPlugin::DEFAULT_VIEWER_WIDTH);
set_option('docsviewer_height_public', DocsViewerPlugin::DEFAULT_VIEWER_HEIGHT);
}
public static function uninstall()
{
delete_option('docsviewer_width');
delete_option('docsviewer_height');
}
public static function configForm()
{
include 'config_form.php';
}
public static function config($post)
{
if (!is_numeric($post['docsviewer_width_admin']) ||
!is_numeric($post['docsviewer_height_admin']) ||
!is_numeric($post['docsviewer_width_public']) ||
!is_numeric($post['docsviewer_height_public'])) {
throw new Exception('The width and height must be numeric.');
}
set_option('docsviewer_embed_admin', (int) (boolean) $post['docsviewer_embed_admin']);
set_option('docsviewer_width_admin', $post['docsviewer_width_admin']);
set_option('docsviewer_height_admin', $post['docsviewer_height_admin']);
set_option('docsviewer_embed_public', (int) (boolean) $post['docsviewer_embed_public']);
set_option('docsviewer_width_public', $post['docsviewer_width_public']);
set_option('docsviewer_height_public', $post['docsviewer_height_public']);
}
public function supportedFormats($format = false)
{
// When $format is set it will be checked against the accepted file extensions for compatibility
// When $format is false (not set) this function will return the array of valid extensions
$supported = array(
'doc', // Microsoft Word
'docx', // Microsoft Word
'xls', // Microsoft Excel
'xlsx', // Microsoft Excel
'ppt', // Microsoft PowerPoint
'pptx', // Microsoft PowerPoint
'pdf', // Adobe Portable Document Format
'pages', // Apple Pages
'ai', // Adobe Illustrator
'psd', // Adobe Photoshop
'tiff', // Tagged Image File Format
'tif', // Tagged Image File Format
'dxf', // Autodesk AutoCad
'svg', // Scalable Vector Graphics
'eps', // PostScript
'ps', // PostScript
'ttf', // TrueType
'xps', // XML Paper Specification
'zip', // Zip Archive file
'rar', // Rar Archive file
);
if ($format !== false) {
if (in_array(strtolower($format),$supported)) {
return true;
} else {
return false;
}
}
return $supported;
}
public static function append()
{
// Embed viewer only if configured to do so.
if ((is_admin_theme() && !get_option('docsviewer_embed_admin')) ||
(!is_admin_theme() && !get_option('docsviewer_embed_public'))) {
return;
}
$docsViewer = new DocsViewerPlugin;
$docsViewer->embed();
}
public function embedOne (File $file)
{
$extension = pathinfo($file->archive_filename, PATHINFO_EXTENSION);
if (!$this->supportedFormats($extension)) {
return;
}
?>
<div>
<iframe src="<?php echo $this->_getUrl($file); ?>"
width="<?php echo is_admin_theme() ? get_option('docsviewer_width_admin') : get_option('docsviewer_width_public'); ?>"
height="<?php echo is_admin_theme() ? get_option('docsviewer_height_admin') : get_option('docsviewer_height_public'); ?>"
style="border: none;"></iframe>
</div>
<?php
echo "<div class=\"download\"><a href=\"". WEB_FILES . '/' . $file->archive_filename ."\">";
switch (strtolower($extension)) {
case 'pdf':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/pdf.png"> ';
break;
case 'doc':
case 'docx':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/word.png"> ';
break;
case 'xls':
case 'xlsx':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/excel.png"> ';
break;
case 'ppt':
case 'pptx':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/ppt.png"> ';
break;
case 'pages':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/pages.png"> ';
break;
case 'ai':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/ai.png"> ';
break;
case 'psd':
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/psd.png"> ';
break;
default:
echo '<img src="'. WEB_DOC_VIEWER_PLUGIN_DIR .'/views/public/images/generic.png"> ';
break;
}
echo $file->original_filename . '</a></div><br />';
}
public function embed()
{
foreach (__v()->item->Files as $file) {
$this->embedOne($file);
}
}
private function _getUrl(File $file)
{
require_once 'Zend/Uri.php';
$uri = Zend_Uri::factory(self::API_URL);
$uri->setQuery(array('url' => WEB_FILES . '/' . $file->archive_filename, 'embedded' => 'true'));
return $uri->getUri();
}
}