forked from Daniel-KM/Omeka-plugin-OpenLayersZoom
/
OpenLayersZoomPlugin.php
418 lines (373 loc) · 12.6 KB
/
OpenLayersZoomPlugin.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
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
<?php
/**
* OpenLayers Zoom: an OpenLayers based image zoom widget.
*
* @copyright Daniel Berthereau, 2013-2015
* @copyright Peter Binkley, 2012-2013
* @copyright Matt Miller, 2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
/**
* The OpenLayers Zoom plugin.
*
* @package Omeka\Plugins\OpenLayersZoom
*/
class OpenLayersZoomPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
'initialize',
'config_form',
'config',
'admin_items_batch_edit_form',
'items_batch_edit_custom',
'public_head',
'after_save_item',
'before_delete_file',
'public_items_show',
'open_layers_zoom_display_file',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_items_form_tabs',
// Currently, it's a checkbox, so no error can be done.
// 'items_batch_edit_error',
);
/**
* @var array Options and their default values.
*/
protected $_options = array(
'openlayerszoom_tiles_dir' => '/zoom_tiles',
'openlayerszoom_tiles_web' => '/zoom_tiles',
'openlayerszoom_use_default_hook' => true,
'openlayerszoom_use_public_head' => true,
);
/**
* Installs the plugin.
*/
public function hookInstall()
{
$this->_options['openlayerszoom_tiles_dir'] = FILES_DIR . DIRECTORY_SEPARATOR . 'zoom_tiles';
// define('ZOOMTILES_WEB', 'http://ec2-75-101-192-109.compute-1.amazonaws.com/cgi-bin/iipsrv.fcgi?zoomify=/var/www/jp2samples');
$this->_options['openlayerszoom_tiles_web'] = WEB_FILES . '/zoom_tiles';
$this->_installOptions();
// Check if there is a directory in the archive for the zoom titles we
// will be making.
$tilesDir = get_option('openlayerszoom_tiles_dir');
if (!file_exists($tilesDir)) {
mkdir($tilesDir);
@chmod($tilesDir, 0755);
copy(FILES_DIR . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . 'index.html', $tilesDir . DIRECTORY_SEPARATOR . 'index.html');
@chmod($tilesDir . DIRECTORY_SEPARATOR . 'index.html', 0644);
}
}
/**
* Uninstalls the plugin.
*/
public function hookUninstall()
{
// Nuke the zoom tiles directory.
$tilesDir = get_option('openlayerszoom_tiles_dir');
$this->_rrmdir($tilesDir);
$this->_uninstallOptions();
}
/**
* Initialize the plugin.
*/
public function hookInitialize()
{
add_shortcode('zoom', array($this, 'shortcodeOpenLayersZoom'));
}
/**
* Shows plugin configuration page.
*/
public function hookConfigForm($args)
{
$view = get_view();
echo $view->partial('plugins/openlayerszoom-config-form.php');
}
/**
* Saves plugin configuration page.
*
* @param array Options set in the config form.
*/
public function hookConfig($args)
{
$post = $args['post'];
$post['openlayerszoom_tiles_dir'] = realpath(trim($post['openlayerszoom_tiles_dir']));
foreach ($this->_options as $optionKey => $optionValue) {
if (isset($post[$optionKey])) {
set_option($optionKey, $post[$optionKey]);
}
}
}
/**
* Add a partial batch edit form.
*
* @return void
*/
public function hookAdminItemsBatchEditForm($args)
{
$view = $args['view'];
echo get_view()->partial(
'forms/openlayerszoom-batch-edit.php'
);
}
/**
* Process the partial batch edit form.
*
* @return void
*/
public function hookItemsBatchEditCustom($args)
{
$item = $args['item'];
$zoomify = $args['custom']['openlayerszoom']['zoomify'];
if (!$zoomify) {
return;
}
$supportedFormats = array(
'jpeg' => 'JPEG Joint Photographic Experts Group JFIF format',
'jpg' => 'Joint Photographic Experts Group JFIF format',
'png' => 'Portable Network Graphics',
'gif' => 'Graphics Interchange Format',
'tif' => 'Tagged Image File Format',
'tiff' => 'Tagged Image File Format',
);
// Set the regular expression to match selected/supported formats.
$supportedFormatRegEx = '/\.' . implode('|', array_keys($supportedFormats)) . '$/i';
// Retrieve image files from the item.
$view = get_view();
$creator = new OpenLayersZoom_Creator();
foreach($item->Files as $file) {
if ($file->hasThumbnail()
&& preg_match($supportedFormatRegEx, $file->filename)
&& !$view->openLayersZoom()->isZoomed($file)
) {
$creator->createTiles($file->filename);
}
}
}
/**
* Add css and js in the header of the public theme.
*/
public function hookPublicHead($args)
{
if (!get_option('openlayerszoom_use_public_head')) {
return;
}
$view = $args['view'];
$request = Zend_Controller_Front::getInstance()->getRequest();
if ($request->getControllerName() == 'items'
&& $request->getActionName() == 'show'
&& $view->openLayersZoom()->zoomedFilesCount($view->item) > 0
) {
queue_css_file('ol');
queue_js_file(array(
'ol',
'OpenLayersZoom',
));
}
}
/**
* Fired once the record is saved, if there is a `open_layers_zoom_filename`
* passed in the $_POST along with save then we know that we need to zoom
* resource.
*/
public function hookAfterSaveItem($args)
{
if (!$args['post']) {
return;
}
$item = $args['record'];
$post = $args['post'];
// Loop through and see if there are any files to zoom.
// Only checked values are posted.
$filesave = false;
$view = get_view();
$creator = new OpenLayersZoom_Creator();
$files = $creator->getFilesById($item);
foreach ($post as $key => $value) {
// Key is the file id of the stored image, value is the filename.
if (strpos($key, 'open_layers_zoom_filename_') !== false) {
$file = $files[(int) substr($key, strlen('open_layers_zoom_filename_'))];
if (!$view->openLayersZoom()->isZoomed($file)) {
$creator->createTiles($value);
}
$filesaved = true;
}
elseif ((strpos($key, 'open_layers_zoom_removed_hidden_') !== false) && ($filesaved != true)) {
$creator->removeZDataDir($value);
}
}
}
/**
* Manages deletion of the folder of a file when this file is removed.
*/
public function hookBeforeDeleteFile($args)
{
$file = $args['record'];
$item = $file->getItem();
$creator = new OpenLayersZoom_Creator();
$creator->removeZDataDir($file);
}
/**
* Controls how the image will be returned.
*
* @todo Need to change this based on how non-zoomed images are to be
* presented.
*
* @param array $args
* Array containing:
* - 'file': object a file object
* - 'options'
*
* @return string
*/
public function hookPublicItemsShow($args = array())
{
if (!get_option('openlayerszoom_use_default_hook')) {
return;
}
$view = $args['view'];
$item = $args['item'];
echo $view->openLayersZoom()->zoom($item);
}
/**
* Controls how the image will be returned.
*
* @deprecated since v 2.5
* @todo To be removed in next release.
* @internal Different from public_items_show hook, because it can return
* the normal file if the file is not zoomed and it needs to be wrapped with
* <div id="openlayerszoom-images"></div>.
*
* @param array $args
* Array containing:
* - 'file': object a file object
* - 'options'
*
* @return string
*/
public function hookOpenLayersZoomDisplayFile($args = array())
{
if (!isset($args['file'])) {
return;
}
$file = $args['file'];
$view = get_view();
$html = $view->openLayersZoom()->zoom($file);
// Display normal file if nothing.
if (empty($html)) {
$options = isset($args['options']) ? $args['options'] : array();
$html = file_markup($file, $options);
}
echo $html;
}
/**
* Adds the zoom options to the images attached to the record, it inserts a
* "Zoom" tab in the admin->edit page
*
* @return array of tabs
*/
public function filterAdminItemsFormTabs($tabs, $args)
{
$item = $args['item'];
$useHtml = '<span>' . __('Only images files attached to the record can be zoomed.') . '</span>';
$zoomList = '';
$view = get_view();
foreach($item->Files as $file) {
if (strpos($file->mime_type, 'image/') === 0) {
// See if this image has been zoooomed yet.
if ($view->openLayersZoom()->isZoomed($file)) {
$isChecked = '<input type="checkbox" checked="checked" name="open_layers_zoom_filename_' . $file->id . '" id="open_layers_zoom_filename_' . $file->id . '" value="' . $file->filename . '"/>' . __('This image is zoomed.') . '</label>';
$isChecked .= '<input type="hidden" name="open_layers_zoom_removed_hidden_' . $file->id . '" id="open_layers_zoom_removed_hidden_' . $file->id . '" value="' . $file->filename . '"/>';
$title = __('Click and Save Changes to make this image un zoom-able');
$style_color = "color:green";
}
else {
$isChecked = '<input type="checkbox" name="open_layers_zoom_filename_' . $file->id . '" id="open_layers_zoom_filename_' . $file->id . '" value="' . $file->filename . '"/>' . __('Zoom this image') . '</label>';
$title = __('Click and Save Changes to make this image zoom-able');
$style_color = "color:black";
}
$useHtml .= '
<div style="float:left; margin:10px;">
<label title="' . $title . '" style="width:auto;' . $style_color . ';" for="zoomThis_' . $file->id . '">'
. file_markup($file, array('imageSize'=>'thumbnail'))
. $isChecked . '<br />
</div>';
}
}
$ttabs = array();
foreach($tabs as $key => $html) {
if ($key == 'Tags') {
$ttabs['Zoom'] = $useHtml;
}
$ttabs[$key] = $html;
}
$tabs = $ttabs;
return $tabs;
}
/**
* Shortcode to display viewer.
*
* @param array $args
* @param Omeka_View $view
* @return string
*/
public static function shortcodeOpenLayersZoom($args, $view)
{
// Check required arguments
$recordType = isset($args['record_type']) ? $args['record_type'] : 'Item';
$recordType = ucfirst(strtolower($recordType));
if (!in_array($recordType, array('Item', 'File'))) {
return;
}
// Get the specified record.
if (isset($args['record_id'])) {
$recordId = (integer) $args['record_id'];
$record = get_record_by_id($recordType, $recordId);
}
// Get the current record.
else {
$record = get_current_record(strtolower($recordType));
}
if (empty($record)) {
return;
}
$html = $view->openLayersZoom()->zoom($record);
if ($html) {
$html = '<link href="' . css_src('ol') . '" media="all" rel="stylesheet" type="text/css" >'
. js_tag('ol')
. js_tag('OpenLayersZoom')
. $html;
return $html;
}
}
/**
* Removes directories recursively.
*
* @param string $dirPath Directory name.
*
* @return boolean
*/
protected function _rrmdir($dirPath)
{
$glob = glob($dirPath);
foreach ($glob as $g) {
if (!is_dir($g)) {
unlink($g);
}
else {
$this->_rrmdir("$g/*");
rmdir($g);
}
}
return true;
}
}