-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFintoPlugin.inc.php
More file actions
147 lines (127 loc) · 3.9 KB
/
FintoPlugin.inc.php
File metadata and controls
147 lines (127 loc) · 3.9 KB
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
<?php
/**
* @file fintoPlugin.inc.php
*
* Copyright (c) 2013-2021 Simon Fraser University Library
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class fintoPlugin
* @ingroup plugins_generic_finto
* @brief finto plugin class
*
*
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class FintoPlugin extends GenericPlugin {
function getName() {
return 'fintoPlugin';
}
function getDisplayName() {
return "Finto/YSO";
}
function getDescription() {
return "Integrates YSO ontology from the Finnish Finto service to the keyword field in OJS. Supports Finnish, English and Swedish.";
}
function register($category, $path, $mainContextId = NULL) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
HookRegistry::register('Template::Workflow', array($this, 'addCustomAutosuggest'));
HookRegistry::register('TemplateManager::display',array($this, 'hideNativeAutosuggest'));
}
return $success;
}
/**
* Add custom js for backend and frontend
*/
function hideNativeAutosuggest($hookName, $params) {
$template =& $params[1];
if ($template !== 'workflow/workflow.tpl') {
return false;
}
$templateMgr =& $params[0];
$templateMgr->addStylesheet(
"FintoGridHandlerStyles",
".pkpAutosuggest .autosuggest__results-container{ position: absolute; left: -9999px; }
.ui-helper-hidden-accessible{ display: none !important; }",
[
"inline" => true,
"contexts" => "backend",
]
);
return false;
}
/**
* Add YSO autoSuggest scripts for keywords
* @param $hookName string
* @param $params array
*/
function addCustomAutosuggest($hookName, $params) {
$output =& $params[2];
$output .= $this->ysoAutosuggest('en','en');
$output .= $this->ysoAutosuggest('fi','fi');
$output .= $this->ysoAutosuggest('sv','sv');
return false;
}
/**
* Get YSO autosuggest api script for selected locale
* @param $localeField string
* @param $localeApi string
* @return $string
*/
function ysoAutosuggest($localeField, $localeApi){
return
<script>
$( function() {
$( '#metadata-keywords-control-" . $localeField . "' ).autocomplete({
source: function( request, response ) {
$.ajax( {
url: 'https://api.finto.fi/rest/v1/search?vocab=yso&lang=" . $localeApi . "',
dataType: 'json',
data: {
query: request.term + '*'
},
success:
function( data ) {
var output = data.results;
response($.map(data.results, function(item) {
return {
label: item.prefLabel + ' [' + item.uri + ']',
value: item.prefLabel + ' [' + item.uri + ']'
}
}));
}
} );
},
minLength: 2,
autoFocus: true,
select: function(event, ui){
var \$i = $('#metadata-keywords-control-" . $localeField . "');
var el = \$i.get(0);
if (!el || !ui || !ui.item || !ui.item.value) return false;
\$i.val(ui.item.value);
try {
\$i.autocomplete('close');
\$i.autocomplete('disable');
} catch (e) {}
el.dispatchEvent(new Event('input', { bubbles: true }));
var down = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13
});
Object.defineProperty(down, 'keyCode', { get: function(){ return 13; } });
Object.defineProperty(down, 'which', { get: function(){ return 13; } });
el.dispatchEvent(down);
setTimeout(function(){ try { \$i.autocomplete('enable'); } catch(e){} }, 0);
return false;
}
} );
} );
</script>";
}
}
?>