-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviewerjs.module
420 lines (396 loc) · 14 KB
/
viewerjs.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
<?php
/**
* Created in 19/06/2014 00:07:32
* @author Daniele P. <[email protected]>
*
* @license <a href="http://www.gnu.org/licenses/gpl.html" target="_new">
* http://www.gnu.org/licenses/gpl.html</a><br /><br />
* This program is free software; you can redistribute it and/or modify<br />
* it under the terms of the GNU General Public License as published by<br />
* the Free Software Foundation; either version 2 of the License, or<br />
* (at your option) any later version<br />
* <br />
* This program is distributed in the hope that it will be useful,<br />
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
* GNU General Public License for more details:<br />
* <b>http://www.gnu.org/licenses/gpl.html</b><br />
* <br />
* You should have received a copy of the GNU General Public License<br />
* along with this program; if not, write to the Free Software<br />
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,<br />
* MA 02110-1301, USA<br />
* */
/**
* Implementation of hook_libraries_info()
* @return mixed
*/
function viewerjs_libraries_info() {
$libraries['viewerjs'] = array(
'name' => 'ViewerJs.org',
'vendor url' => 'https://github.com/Strae/Viewer.js',
'download url' => 'https://github.com/Strae/Viewer.js/archive/master.zip',
'version' => 'last'
);
return $libraries;
}
/**/
/**
* @param $phase
*
* @return array
*/
function viewerjs_requirements($phase) {
$requirements = array();
// Ensure translations do not break at install time
$t = get_t();
$requirements['viewerjs'] = array(
'title' => $t('ViewerJs'),
);
$libraries = libraries_get_libraries();
if (isset($libraries['viewerjs'])) {
if (TRUE !== file_exists($libraries['viewerjs'] . '/viewer.js')) {
$requirements['viewerjs']['value'] = $t('Not Installed');
$requirements['viewerjs']['severity'] = REQUIREMENT_ERROR;
$requirements['viewerjs']['description'] = $t('Please install the viewerjs library %url.', array('%url' => 'https://github.com/Strae/Viewer.js'));
}
else {
$requirements['viewerjs']['value'] = $t('Installed');
$requirements['viewerjs']['severity'] = REQUIREMENT_OK;
}
}
else {
$requirements['viewerjs']['value'] = $t('Not Installed');
$requirements['viewerjs']['severity'] = REQUIREMENT_ERROR;
$requirements['viewerjs']['description'] = $t('Please install the viewerjs library %url.', array('%url' => 'https://github.com/Strae/viewer.js'));
}
return $requirements;
}
/**/
/**
* Implementation of hook_field_formatter_info()
* @return array
*/
function viewerjs_field_formatter_info() {
return array(
'viewerjs' => array(
'label' => t('ViewerJs Default'),
'field types' => array('file', 'image'),
'settings' => array(
'color' => 'default',
'link_type' => 'overlay',
'overlay_size' => '1000x500',
'preview_max_size' => '1000x700',
'thumbnail' => 'yes',
'max_image_size' => 'viewerjs',
'thumbnail_size' => 'viewerjs_thumbnail',
'template' => 'index.html'
)
),
);
}
/**/
/**
* Implementation of hook_field_formatter_settings_form()
*
* @param $field
* @param $instance
* @param $view_mode
* @param $form
* @param $form_state
*/
function viewerjs_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$styles = image_styles();
$styles_options = array();
foreach ($styles AS $style_name => $options) {
$styles_options[$style_name] = $options['label'];
}
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element['link_type'] = array(
'#title' => t('Preview Type'),
'#type' => 'select',
'#default_value' => $settings['link_type'],
'#empty_option' => t('Nothing'),
'#options' => array(
'overlay' => 'Overlay',
'new_window' => 'New Window'
),
);
$element['color'] = array(
'#title' => t('Widget Color'),
'#type' => 'select',
'#default_value' => $settings['color'],
'#options' => array(
'default' => t('Default'),
'magenta' => t('Magenta'),
'green' => t('Green'),
'blue' => t('Blue'),
),
);
$element['template'] = array(
'#title' => t('ViewerJs Template file'),
'#type' => 'textfield',
'#default_value' => $settings['template'],
'#required' => TRUE,
'#description' => t('The template html file to use. Default index.html. The template MUST be in the Viewerjs library folder.')
);
$element['overlay_size'] = array(
'#title' => t('Overlay Size'),
'#type' => 'textfield',
'#default_value' => $settings['overlay_size'],
'#required' => TRUE,
'#description' => t('The overlay size in the format widthxheight, for example 400x300.<br>Used only when Overlay is set as Preview Type')
);
$element['preview_max_size'] = array(
'#title' => t('Max size of the preview'),
'#type' => 'textfield',
'#default_value' => $settings['preview_max_size'],
'#required' => TRUE,
'#description' => t('Only for images: if the image size widthXheight is bigger that this value, the preview will use "viewerjs" image style.')
);
$element['max_image_size'] = array(
'#title' => t('Image max size'),
'#type' => 'select',
'#default_value' => $settings['max_image_size'],
'#required' => TRUE,
'#options' => $styles_options,
'#description' => t('Only for images: when the image is bigger than "Max size of the preview", this image style will be used to resize it.')
);
$element['thumbnail'] = array(
'#title' => t('Show thumbnails'),
'#type' => 'select',
'#default_value' => $settings['thumbnail'],
'#options' => array(
'yes' => 'Yep.',
'no' => 'No.'
),
);
$element['thumbnail_size'] = array(
'#title' => t('Thumbnail size'),
'#type' => 'select',
'#default_value' => $settings['thumbnail_size'],
'#required' => TRUE,
'#options' => $styles_options,
'#description' => t('Only for images: the style used to create the thumbnail')
);
return $element;
}
/**/
/**
* Implementation of hook_field_formatter_settings_summary()
*
* @param $field
* @param $instance
* @param $view_mode
*
* @return string
*/
function viewerjs_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ('overlay' == $settings['link_type']) {
$output = t('Overlay size %size;', array('%size' => $settings['overlay_size']));
}
else {
$output = t('New Window;');
}
$output .= ' ' . t('using template %template;', array('%template' => $settings['template']));
$output .= ' ' . t('Max preview size: %size;', array('%size' => $settings['preview_max_size']));
$output .= ' ' . t('resized with %style image style;', array('%style' => $settings['max_image_size']));
$output .= ' ' . t('Show thumbnail: %thu;', array('%thu' => $settings['thumbnail']));
$output .= ' ' . t('resized with %style image style.', array('%style' => $settings['thumbnail_size']));
return $output;
}
/**/
/**
* Implementation of hook_field_formatter_view()
*
* @param $entity_type
* @param $entity
* @param $field
* @param $instance
* @param $langcode
* @param $items
* @param $display
*
* @return array
*/
function viewerjs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div class="viewerjs-formatter-field">',
'#suffix' => '</div>'
);
switch ($display['type']) {
case 'viewerjs':
if (!empty($items)) {
$settings = $display['settings'];
list($ow, $oh) = explode('x', $settings['overlay_size']);
global $base_url;
drupal_add_css(drupal_get_path('module', 'viewerjs') . '/css/css/fontello.css');
drupal_add_css(drupal_get_path('module', 'viewerjs') . '/css/viewerjs.css');
drupal_add_js(drupal_get_path('module', 'viewerjs') . '/js/viewerjs.js', array('scope' => 'footer'));
$viewerjs = libraries_load('viewerjs');
foreach ($items as $delta => $item) {
$splited_uri = explode('.', $item['uri']);
$extension = end($splited_uri);
if(!isset($item['type'])){
$item['type'] = $field['type'];
}
viewerjs_preview_file_alter($item, $settings);
$download_url = file_create_url($item['uri']);
$path = $base_url . '/' . $viewerjs['library path'] . '/' . $settings['template'] . '#' . $download_url;
$file = new stdClass();
foreach($item AS $k => $v){
$file->$k = $v;
}
$icon = theme_file_icon(array('file' => $file, 'icon_directory' => null));
$size = format_size($item['filesize']);
$element[$delta] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('viewerjs-field-item'),
),
'inner_wrapper' => array(
'#type' => 'container',
'#attributes' => array(
'class' => array('viewerjs-item-wrapper', $settings['color']),
),
'content' => array(
'#prefix' => '<header><span class="name">' . $icon . '<b>' . $item['filename'] . '</b></span><span class="size"><ul class="actions"><li>' . $size . '</li>',
'#suffix' => '</ul></span></header>',
)
)
);
if (isset($item['thumbnail']) AND !empty($item['thumbnail'])) {
$element[$delta]['inner_wrapper']['#attributes']['style'] = "background-image: url({$item['thumbnail']})";
}
$element[$delta]['inner_wrapper']['content']['download_' . $delta] = array(
'#prefix' => '<li>',
'#suffix' => '</li>',
'#type' => 'link',
'#href' => $download_url,
'#title' => t('Download @filename', array('@filename' => $item['filename'])),
'#weight' => 0,
'#attributes' => array(
'class' => array('download'),
'target' => '_blank'
)
);
if (in_array(strtolower($extension), viewerjs_supported_extensions())) {
if ('overlay' == $settings['link_type']) {
$element[$delta]['inner_wrapper']['content']['preview_' . $delta] = array(
'#prefix' => '<li>',
'#suffix' => '</li>',
'#type' => 'link',
'#href' => $path,
'#title' => t('Preview with Viewerjs.org'),
'#attributes' => array(
'target' => '_blank',
'title' => t('Preview with Viewerjs.org'),
'class' => array('preview', 'viewerjs-overlay'),
'data-viewerjs-overlay-width' => $ow,
'data-viewerjs-overlay-height' => $oh,
'data-viewerjs-overlay-id' => $item['fid']
),
'#weight' => 1,
);
} else {
$element[$delta]['inner_wrapper']['content']['preview_' . $delta] = array(
'#prefix' => '<li>',
'#suffix' => '</li>',
'#type' => 'link',
'#href' => $path,
'#title' => t('Preview with Viewerjs.org'),
'#attributes' => array(
'target' => '_blank',
'title' => t('Preview with Viewerjs.org'),
'class' => array('preview'),
'data-viewerjs-overlay-width' => $ow,
'data-viewerjs-overlay-height' => $oh,
'data-viewerjs-overlay-id' => $item['fid']
),
'#weight' => 1,
);
}
}
// If the thumbnail is set, add another wrapper to the dom (viewerjs-thumbnail)
if (isset($item['thumbnail']) AND !empty($item['thumbnail'])) {
$element[$delta]['#prefix'] = '<div class="viewerjs-thumbnail">';
$element[$delta]['#suffix'] = '</div>';
}
}
}
break;
}
return $element;
}
/**/
/**
* Check if we have to alter the file, adding a thumbnail or resizing the
* dimensions (for images only)
*
* @param $file
* @param $settings
*/
function viewerjs_preview_file_alter(&$file, $settings) {
if (isset($settings['thumbnail']) AND 'yes' == $settings['thumbnail']) {
viewerjs_thumbnail_file($file, $settings['thumbnail_size']);
}
if ('image' === $file['type'] AND !empty($file['image_dimensions'])) {
list($mw, $mh) = explode('x', $settings['preview_max_size']);
if ($file['image_dimensions']['width'] > $mw OR $file['image_dimensions']['height'] > $mh) {
// use viewerjs image style.
$file['uri'] = image_style_url($settings['max_image_size'], $file['uri']);
}
}
}
/**/
/**
* Create the thumbnail (image).
* For image files, simply use the style_url.
* For documents/pdf, use a icon (customizable) under viewerjs/images
*
* @param $file File array
* @param $image_style
*/
function viewerjs_thumbnail_file(&$file, $image_style) {
global $base_url;
switch ($file['type']) {
case 'image':
$file['thumbnail'] = image_style_url($image_style, $file['uri']);
break;
default:
// Get the extension
$splited_uri = explode('.', $file['uri']);
$extension = end($splited_uri);
if (file_exists(drupal_get_path('module', 'viewerjs') . '/images/' . $extension . '.png')) {
$file['thumbnail'] = $base_url .'/' . drupal_get_path('module', 'viewerjs') . '/images/' . $extension . '.png';
}
else {
$file['thumbnail'] = $base_url .'/' . drupal_get_path('module', 'viewerjs') . '/images/file.png';
}
break;
}
}
/**/
/**
* Return a list of extension supported by Viewerjs's PluginLoader
* @return array
*/
function viewerjs_supported_extensions() {
return array(
'pdf',
'odt',
'odp',
'ods',
'fodt', // documents
'jpg',
'jpeg',
'png',
'gif' //images
);
}
/**/