-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeJSPlugin.php
585 lines (528 loc) · 19.1 KB
/
ThreeJSPlugin.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?php
/**
* ThreeViewer is used to view 3d models in Omeka
*
*/
/**
*
* ThreeViewer plugin
* @package Omeka\Plugins\ThreeViewer
*/
// TODO Need to find a way to delete the associated file when the item is updated
require_once dirname(__FILE__) . '/helpers/ThreeViewerFunctions.php';
$appRoot = getcwd();
define('THREE_VIEWER_ROOT', dirname(__FILE__));
define('THREE_BUNDLE_STATIC_JS_DIR', THREE_VIEWER_ROOT . '/views/shared/js/ThreeJSPlugin/build/static/js');
define('THREE_BUNDLE_STATIC_CSS_DIR', THREE_VIEWER_ROOT . '/views/shared/js/ThreeJSPlugin/build/static/css');
define('THREE_BUNDLE_STATIC_MEDIA_DIR', THREE_VIEWER_ROOT . '/views/shared/js/ThreeJSPlugin/build/static/media');
define('THREE_BUNDLE_WORKER_DIR', THREE_VIEWER_ROOT . '/views/shared/js/ThreeJSPlugin/build');
define('THREE_BUNDLE_WORKER_URL', 'plugins/' . basename(__DIR__) . '/views/shared/js/ThreeJSPlugin/build/');
define('THREE_BUNDLE_STATIC_JS_URL', 'plugins/' . basename(__DIR__) . '/views/shared/js/ThreeJSPlugin/build/static/js/');
define('THREE_FALLBACK_IMG_URL', 'plugins/' . basename(__DIR__) . '/views/shared/common/images/fallback.png');
define('THREE_BUNDLE_STATIC_MEDIA_URL', 'plugins/' . basename(__DIR__) . '/views/shared/js/ThreeJSPlugin/build/static/media/');
define('THREE_BUNDLE_STATIC_CSS', 'js/ThreeJSPlugin/build/static/css');
const THREE_BUNDLE_FONT_TYPES = [
'eot' => 'embedded-opentype',
'woff2' => 'woff2',
'woff' => 'woff',
'ttf' => 'truetype',
'svg' => 'svg'
];
class ThreeJSPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_SKYBOX_ITEM_TYPE_NAME = 'Skybox';
protected $_SKYBOX_ELEMENT_INNER_GRADIENT = 'Skybox Radial Gradient Inner Color';
protected $_SKYBOX_ELEMENT_OUTER_GRADIENT = 'Skybox Radial Gradient Outer Color';
protected $_hooks = array(
'install',
'uninstall',
'initialize',
'config_form',
'config',
'items_browse_sql',
'public_items_show',
'define_routes',
'define_acl',
'admin_head',
'before_save_item',
'before_delete_item',
'upgrade',
);
protected $_addedMimeTypes = array(
'application/json',
'application/javascript',
);
protected $_addedFileExtensions = array(
'json',
'js'
);
protected $_filters = array(
'public_navigation_main',
'admin_items_form_tabs',
'api_resources',
'file_ingest_validators',
);
protected $_formOptions = array(
'fileOptions' => array(
'three-file-input' => array(
'label' => 'Upload a file in ThreeJS format',
'type' => 'file',
'id' => 'three-file-input',
'model_prop' => 'three_file_id',
'value' => NULL,
),
),
'imageUploadOptions' => array(
'three-image-input' => array(
'label' => 'Upload a thumbnail',
'type' => 'file',
'id' => 'three-image-input',
'model_prop' => 'three_thumbnail_id',
'value' => NULL,
),
),
'toolOptions' => array(
'measurement' => array(
'label' => 'Enable Measurement Tools',
'id' => 'three-measurement-input',
'type' => 'checkbox',
'model_prop' => 'enable_measurement',
'value' => NULL,
),
'materials' => array(
'label' => 'Enable Materials Tools',
'id' => 'three-materials-input',
'type' => 'checkbox',
'model_prop' => 'enable_materials',
'value' => NULL,
),
'lights' => array(
'label' => 'Enable Light Tools',
'id' => 'three-lights-input',
'type' => 'checkbox',
'model_prop' => 'enable_lights',
'value' => NULL,
),
'shaders' => array(
'label' => 'Enable Shader Tools',
'id' => 'three-shaders-input',
'model_prop' => 'enable_shaders',
'type' => 'checkbox',
'value' => NULL,
),
),
'viewerOptions' => array(
'skybox' => array(
'label' => 'Select Skybox',
'type' => 'select',
'id' => 'three-skybox-input',
'model_prop' => 'skybox_id',
'options' => array(array('value' => -1, 'label' => 'None')),
'value' => NULL,
),
'units' => array(
'label' => 'Model Units',
'type' => 'select',
'id' => 'three-units-input',
'model_prop' => 'model_units',
'options' => array(
array(
'value' => 'mm',
'label' => 'mm',
),
array(
'value' => 'cm',
'label' => 'cm',
),
array(
'value' => 'in',
'label' => 'in',
),
array(
'value' => 'ft',
'label' => 'ft',
)
),
'value' => NULL,
)
)
);
public function hookInstall()
{
$db = $this->_db;
$initViewers = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}three_js_viewers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(20) COLLATE utf8_unicode_ci NOT NULL,
`three_file_id` int(20) COLLATE utf8_unicode_ci NOT NULL,
`three_thumbnail_id` int(20) COLLATE utf8_unicode_ci,
`model_units` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`skybox_id` int(20) COLLATE utf8_unicode_ci,
`enable_measurement` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`enable_shaders` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`enable_materials` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`enable_lights` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`needs_delete` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`viewer_settings` LONGTEXT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($initViewers);
$this->_installOptions();
$this->_createSkyboxType();
// Loading fonts dynamically now so we don't need this
// $this->_patchMediaAssets();
}
public function hookUpgrade($info)
{
$db = $this->_db;
if ($info["old_version"] === "0.0.1") {
$updateViewerTable = "
ALTER TABLE `{$db->prefix}three_js_viewers` ADD COLUMN `three_thumbnail_id` int(20) COLLATE utf8_unicode_ci, ADD COLUMN `viewer_settings` LONGTEXT
";
$db->query($updateViewerTable);
} else if ($info["old_version"] < "0.0.3") {
$updateViewerTable = "
ALTER TABLE `{$db->prefix}three_js_viewers` ADD COLUMN `viewer_settings` LONGTEXT
";
$db->query($updateViewerTable);
}
// in case js build was upgraded
// $this->_patchMediaAssets();
}
public function hookUninstall()
{
$this->_removeThreeFiles();
$this->_deleteSkyboxType();
$this->_uninstallOptions();
$db = $this->_db;
$dropViewers = "DROP TABLE IF EXISTS `{$db->prefix}three_js_viewers`";
$db->query($dropViewers);
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
get_view()->addHelperPath(dirname(__FILE__) . '/views/helpers', 'ThreeJS_View_Helper');
}
public function hookDefineAcl($args)
{
$acl = $args['acl']; // get the Zend_Acl
$acl->addResource('ThreeJS_Viewers');
$acl->allow(null, 'ThreeJS_Viewers', 'show');
}
public function hookDefineRoutes($args)
{
$router = $args['router'];
$threeRoute = new Zend_Controller_Router_Route('three/*',
array('module' => 'three-js', 'controller' => 'three', 'action' => 'show')
);
$threeBrowseRoute = new Zend_Controller_Router_Route('three-browse',
array('module' => 'three-js', 'controller' => 'three', 'action' => 'browse')
);
$threeFullScreenRoute = new Zend_Controller_Router_Route('three-embed/*',
array('module' => 'three-js', 'controller' => 'three', 'action' => 'fullscreen')
);
$router->addRoute('three', $threeRoute);
$router->addRoute('three-browse', $threeBrowseRoute);
$router->addRoute('three-embed', $threeFullScreenRoute);
return $router;
}
public function hookAdminHead()
{
queue_js_file('three-plugin-admin', 'js/ThreeJSPlugin');
queue_css_file('admin-style');
}
public function hookConfigForm()
{
echo get_view()->partial('plugin/threejs-config-form.php');
}
public function hookConfig()
{
set_option('threejs_browse_description', $_POST['threejs_browse_description']);
}
public function hookItemsBrowseSql($args)
{
$params = $args['params'];
if (!is_admin_theme()) {
// filter skyboxes out from all searching and browsing on the public side
$select = $args['select'];
$db = get_db();
$skyboxTypeId = $this->_getSkyboxItemTypeId();
$select->where("item_type_id != ? OR item_type_id IS NULL", $skyboxTypeId);
}
}
// TODO put option in here for embedding as well
public function hookPublicItemsShow($data)
{
$item = $data['item'];
$viewer = item_has_viewer($item);
if ($viewer) {
$this->addJS($viewer);
}
}
protected function addJS($viewer)
{
$url = url("/three/models/{$viewer->id}?embed=true");
// TODO not working with viewer - viewer needs element-set class
$js =
"<script>
try {
var elementSets = document.getElementsByClassName('element-set');
var elementSet = [...elementSets].slice(-1);
var element = document.createElement('div');
var elementText = document.createElement('div');
var elementHeader = document.createElement('h3');
var elementLink = document.createElement('a');
elementLink.className = 'three-view-link';
element.className = 'element';
elementText.className = 'element-text';
element.id = 'three-view-metadata';
elementHeader.innerText = '3D Model';
elementLink.text = 'View in new tab';
elementLink.href = '{$url}';
elementLink.target = '_blank';
elementText.appendChild(elementLink);
element.appendChild(elementHeader);
element.appendChild(elementText);
elementSet[0].appendChild(element);
} catch(error) {
console.warn(error);
}
</script>
";
echo $js;
}
public function hookBeforeSaveItem($args)
{
$item = $args['record'];
if ($item->id) {
$viewer = item_has_viewer($item);
if ($viewer) {
if ($viewer->needs_delete) {
$viewerRecord = get_record_by_id('ThreeJSViewer', $viewer->id);
$fileRecord = get_record_by_id('File', $viewerRecord->three_file_id);
$viewerRecord->delete();
$toDelete = array($fileRecord->id);
if (array_key_exists('delete_files', $args['post'])) {
$args['post']['delete_files'] = array_merge($args['post']['delete_files'], $toDelete);
} else {
$args['post']['delete_files'] = $toDelete;
}
} else {
// Need to check here, the files arent getting added to the array
$toDelete = array();
foreach($item->getFiles() as $fileRecord) {
if (checkExtension($fileRecord->getExtension())) {
if ($fileRecord->id !== $viewer->three_file_id) {
array_push($toDelete, $fileRecord->id);
}
}
}
if (array_key_exists('delete_files', $args['post'])) {
$deleteArray = $args['post']['delete_files'];
$args['post']['delete_files'] = array_merge($deleteArray, $toDelete);
} else {
$args['post']['delete_files'] = $toDelete;
}
}
}
}
}
public function hookBeforeDeleteItem($args)
{
$item = $args['record'];
$viewer = item_has_viewer($item);
if ($viewer) {
$viewerRecord = get_record_by_id('ThreeJSViewer', $viewer->id);
$viewerRecord->delete();
}
}
public function filterPublicNavigationMain($nav)
{
$nav[] = array(
'label' => __('Browse Meshes'),
'uri' => url('three-browse'),
);
return $nav;
}
public function filterAdminItemsFormTabs($tabs, $args)
{
$skyboxOptions = $this->_formOptions['viewerOptions']['skybox']['options'];
$this->_formOptions['viewerOptions']['skybox']['options'] = array_merge($skyboxOptions, get_skybox_options($this->_getSkyboxItemTypeId()));
$item = $args['item'];
if ($item->added) {
$viewer = item_has_viewer($item);
if ($viewer) {
$tabs['ThreeJSViewer'] = $this->_getMainForm($item, 'edit', $this->hydrateOptions($viewer));
} else {
$tabs['ThreeJSViewer'] = $this->_getMainForm($item, 'add', $this->_formOptions);
}
} else {
$tabs['ThreeJSViewer'] = '<h4>A ThreeJS Viewer requires an item id. Please add your item to add a viewer </h4>';
}
return $tabs;
}
public function filterApiResources($apiResources)
{
$apiResources['threejs_viewers'] = array(
'record_type' => 'ThreeJSViewer',
'actions' => array(
'get',
'index',
'post',
'put',
'delete'
),
);
return $apiResources;
}
public function filterFileIngestValidators($validators)
{
// Keeps all the old validators and allows for JS / JSON to be uploaded
$defaultExtensionWhitelist = get_option('file_extension_whitelist');
$defaultMimeTypeWhitelist = get_option('file_mime_type_whitelist');
$defaultMimeTypeWhitelist .= ',';
$defaultExtensionWhitelist .= ',';
$defaultExtensionWhitelist .= implode(',', $this->_addedFileExtensions);
$defaultMimeTypeWhitelist .= implode(',', $this->_addedMimeTypes);
unset($validators['MIME type whitelist']);
unset($validators['extension whitelist']);
$validators['ThreeJSPlugin_MimeType_Validators'] = new Omeka_Validate_File_MimeType($defaultMimeTypeWhitelist);
$validators['ThreeJSPlugin_Extension_Validators'] = new Omeka_Validate_File_Extension($defaultExtensionWhitelist);
return $validators;
}
protected function hydrateOptions($viewer)
{
$viewer = (array) $viewer;
$newOptions = $this->_formOptions;
foreach($newOptions as $key => $group) {
$newOptions[$key] = array_map(function($option) use (&$viewer) {
$prop = $option['model_prop'];
if ($prop === 'three_file_id' || $prop === 'three_thumbnail_id') {
$file = get_record_by_id('File', $viewer[$prop]);
if ($file) {
$option['label'] = 'Current attached file is ' . $file->original_filename;
}
} else {
$option['value'] = $viewer[$prop];
}
return $option;
}, $group);
}
$newOptions['viewerId'] = $viewer['id'];
$newOptions['threeFileId'] = $viewer['three_file_id'];
return $newOptions;
}
protected function _getMainForm($item, $formType, $options)
{
if ($formType == 'add') {
require_once THREE_VIEWER_ROOT . '/forms/AddThreeViewerForm.php';
$form = new AddThreeViewerForm($item, get_view(), $options);
return $form->render();
} else {
require_once THREE_VIEWER_ROOT . '/forms/EditThreeViewerForm.php';
$form = new EditThreeViewerForm($item, get_view(), $options);
return $form->render();
}
}
protected function _createSkyboxType()
{
$skyboxType = new ItemType();
$skyboxType->name = 'Skybox';
$skyboxType->description = 'An item type to be used specifically with the ThreeJS Plugin.
By assigning an item this type, you will be able to use equirectangular images as 360 panoramic
backgrounds for your viewers. If you choose not to attach an image, you can add rgb values for
a linear gradient skybox. Currently only works with a single equirectangular (spherical) image.';
$skyboxType->save();
$elementSet = $skyboxType->getItemTypeElementSet();
$radialGradientInner = new Element();
$radialGradientInner->name = $this->_SKYBOX_ELEMENT_INNER_GRADIENT;
$radialGradientInner->description = "Either the hex value (#ffffff) or rgb value (rgb(255, 255, 255))
for the inner color of the skybox's radial gradient background.";
$radialGradientInner->element_set_id = $elementSet->id;
$radialGradientInner->save();
$radialGradientOuter = new Element();
$radialGradientOuter->name = $this->_SKYBOX_ELEMENT_OUTER_GRADIENT;
$radialGradientOuter->description = "Either the hex value (#ffffff) or rgb value (rgb(255, 255, 255))
for the outer color of the skybox's radial gradient background.";
$radialGradientOuter->element_set_id = $elementSet->id;
$radialGradientOuter->save();
$elementIds = $this->_getSkyboxElementIds();
$skyboxType->addElementById($elementIds['innerGradientId']);
$skyboxType->addElementById($elementIds['outerGradientId']);
$skyboxType->save();
}
protected function _deleteSkyboxType()
{
$skyboxType = get_record_by_id('ItemType', $this->_getSkyboxItemTypeId());
// Also need to delete all skyboxes
$db = get_db();
$res = $db->getTable('Item')->findBy(array('item_type_id' => $skyboxType->id));
if (sizeof($res) > 0) {
foreach($res as $skybox) {
$skybox->delete();
}
}
$skyboxType->delete();
$elements = $this->_getSkyboxElementIds();
if (sizeof($elements) > 0) {
$inner = get_record_by_id('Element', $elements['innerGradientId']);
$inner->delete();
$outer = get_record_by_id('Element', $elements['outerGradientId']);
$outer->delete();
}
}
protected function _removeThreeFiles()
{
$items = viewer_items();
foreach($items as $item) {
$files = $item->getFiles();
foreach($files as $file) {
$ext = checkExtension($file->getExtension());
if ($ext) {
$file->delete();
}
}
}
}
protected function _getSkyboxItemTypeId()
{
$db = get_db();
$skyboxItemType = $db->getTable('ItemType')->findByName($this->_SKYBOX_ITEM_TYPE_NAME);
if ($skyboxItemType) {
return $skyboxItemType->id;
} else {
return NULL;
}
}
protected function _getSkyboxElementIds()
{
$elements = [];
$db = get_db();
$inner = $db->getTable('Element')->findByElementSetNameAndElementName('Item Type Metadata', $this->_SKYBOX_ELEMENT_INNER_GRADIENT);
$outer = $db->getTable('Element')->findByElementSetNameAndElementName('Item Type Metadata', $this->_SKYBOX_ELEMENT_OUTER_GRADIENT);
if ($inner) {
$elements['innerGradientId'] = $inner->id;
}
if ($outer) {
$elements['outerGradientId'] = $outer->id;
}
return $elements;
}
protected function _patchMediaAssets()
{
// file must be writable by www-data
$css = load_react_css(TRUE);
$cssString = file_get_contents($css);
$res = str_replace('url(/static/media/', 'url(' . public_url(THREE_BUNDLE_STATIC_MEDIA_URL), $cssString);
file_put_contents($css, $res);
}
protected function _patchWorkers()
{
$bundle = load_js_bundle(TRUE);
foreach($bundle as $jsFile) {
$jsString = file_get_contents($jsFile);
$res = preg_replace('/Worker\((.*)\+/g', 'Worker(' . THREE_BUNDLE_WORER_URL . '+', $jsString);
file_put_contents($jsFile, $res);
}
}
}