Repository navigation
Expand file tree
/
Copy pathShariffPlugin.php
More file actions
293 lines (247 loc) · 9.09 KB
/
Copy pathShariffPlugin.php
File metadata and controls
293 lines (247 loc) · 9.09 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
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
<?php
/**
* @file plugins/generic/shariff/ShariffPlugin.inc.php
*
* Copyright (c) 2023 Universitätsbibliothek Freie Universität Berlin
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class ShariffPlugin
* @ingroup plugins_generic_shariff
*
* @brief Shariff plugin class
*/
namespace APP\plugins\generic\shariff;
define('SHARIFF_VERSION', '3.3.1');
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\facades\Locale;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\RedirectAction;
class ShariffPlugin extends GenericPlugin {
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.shariff.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.shariff.description');
}
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
$request = $this->getRequest();
$context = $request->getContext();
if ($context) {
Hook::add('Template::Settings::website::appearance', array($this, 'callbackAppearanceTab')); //to enable display of plugin settings tab
Hook::add('Schema::get::context', array($this, 'addToSchema')); // to add Shariff avriables to context schema
Hook::add('Templates::Common::Footer::PageFooter', array($this, 'addShariffButtons'));
Hook::add('Templates::Article::Details', array($this, 'addShariffButtons'));
Hook::add('Templates::Catalog::Book::Details', array($this, 'addShariffButtons'));
Hook::add('Templates::Preprint::Details', array($this, 'addShariffButtons'));
Hook::add('TemplateManager::display', function($hookName, $args) {
// This needs to be done within the display hook, because OJS only loads our context varaiables after the plugin is registered
// and we need them to decide whether to load other resources
$templateMgr = $args[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$baseUrl = $request->getBaseUrl();
if ($context->getData('shariffEnableWCAG')===NULL?true:(bool)$context->getData('shariffEnableWCAG')) {
$wcagCssUrl = $baseUrl .'/' . $this->getPluginPath() .'/css/wcag-themes.css';
$templateMgr->addStyleSheet('shariffPluginStylesWCAGCSS', $wcagCssUrl);
}
});
// Load this plugin as a block plugin as well (for sidebar)
$shariffBlockPlugin = new ShariffBlockPlugin($this->getName(), $this->getPluginPath());
PluginRegistry::register(
'blocks',
$shariffBlockPlugin,
$this->getPluginPath()
);
// load standard resources
$baseUrl = $request->getBaseUrl();
$templateMgr = TemplateManager::getManager($request);
$jsUrl = $baseUrl .'/'. $this->getPluginPath().'/shariff-'.SHARIFF_VERSION.'/shariff.complete.js';
$templateMgr->addJavaScript('shariffPluginJavascriptShariff', $jsUrl);
$shariffCssUrl = $baseUrl .'/' . $this->getPluginPath() . '/shariff-'.SHARIFF_VERSION.'/shariff.complete.css';
$templateMgr->addStyleSheet('shariffPluginStylesShariff', $shariffCssUrl);
$cssUrl = $baseUrl .'/' . $this->getPluginPath() . '/css/shariff.css';
$templateMgr->addStyleSheet('shariffPluginStylesCSS', $cssUrl);
}
}
return $success;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb): array
{
$actions = parent::getActions($request, $verb);
if (!$this->getEnabled()) {
return $actions;
}
$dispatcher = $request->getDispatcher();
array_unshift($actions, new LinkAction('settings', new RedirectAction($dispatcher->url(
$request,
Application::ROUTE_PAGE,
null,
'management',
'settings',
['website'],
[''],
'shariffPlugin'
)), __('manager.plugins.settings')));
return $actions;
}
public function addToSchema($hookName, $params) {
$schema =& $params[0];
$schema->properties->{"shariffServicesSelected"} = (object) [
'type' => 'array',
'apiSummary' => true,
'validation' => ['nullable'],
'items' => (object) ['type' => 'string']
];
$schema->properties->{"shariffThemeSelected"} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
$schema->properties->{"shariffPositionSelected"} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
$schema->properties->{"shariffOrientationSelected"} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
$schema->properties->{"shariffEnableWCAG"} = (object) [
'type' => 'boolean',
'apiSummary' => true,
'validation' => ['nullable'],
];
$schema->properties->{"shariffShowBlockHeading"} = (object) [
'type' => 'boolean',
'apiSummary' => true,
'validation' => ['nullable'],
];
$schema->properties->{"shariffPublicationSharingLink"} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
return false;
}
function callbackAppearanceTab($hookName, $args) {
# prepare data
$templateMgr =& $args[1];
$output =& $args[2];
$request =& Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();
# url to handle form dialog (we add our vars to the context schema)
$contextApiUrl = $dispatcher->url(
$request,
Application::ROUTE_API,
$context->getPath(),
'contexts/' . $context->getId()
);
// instantinate settings form
$shariffSettingsForm = new ShariffSettingsForm($contextApiUrl, $context->getSupportedLocaleNames(), $context);
// setup template
$templateMgr->setConstants([
'FORM_SHARIFF_SETTINGS',
]);
$state = $templateMgr->getTemplateVars('state');
$state['components'][FORM_SHARIFF_SETTINGS] = $shariffSettingsForm->getConfig();
$templateMgr->assign('state', $state);
$output .= $templateMgr->display($this->getTemplateResource('settingsForm.tpl'));
return false;
}
/**
* Get the name of the settings file to be installed on new context
* creation.
* @return string
*/
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
/**
* @copydoc Plugin::getTemplatePath()
*/
function getTemplatePath($inCore = false) {
return parent::getTemplatePath($inCore);
}
/**
* Hook callback: Handle requests.
* @param $hookName string The name of the hook being invoked
* @param $args array The parameters to the invoked hook
* @return bool
*/
function addShariffButtons($hookName, $args) {
$request = $this->getRequest();
$context = $request->getContext();
// display the buttons depending on the selected position
switch ($context->getData('shariffPositionSelected')) {
case 'footer':
if ($hookName != 'Templates::Common::Footer::PageFooter') return false;
break;
case 'submission':
if (($hookName != 'Templates::Article::Details') &&
($hookName != 'Templates::Catalog::Book::Details') &&
($hookName != 'Templates::Preprint::Details')) return false;
break;
default:
return false;
}
// create the template
$template =& $args[1];
$output =& $args[2];
// services
$selectedServices = $context->getData('shariffServicesSelected');
$preparedServices = array_map(function($arrayElement){return $arrayElement;}, $selectedServices);
$dataServicesString = implode(",", $preparedServices);
if ($dataServicesString != "") {
// theme
$selectedTheme = $context->getData('shariffThemeSelected');
// orientation
$selectedOrientation = $context->getData('shariffOrientationSelected');
// get language from system
$locale = Locale::getLocale();
$publicationSharingLink = $context->getData('shariffPublicationSharingLink');
if ($publicationSharingLink == 'doiUrl') {
$publication = $template->getTemplateVars('currentPublication');
if ($publication && $publication->getData('doiObject')) {
$doiUrl = $publication->getData('doiObject')->getResolvingUrl();
}
}
// urls
$baseUrl = $request->getBaseUrl();
$requestedUrl = $doiUrl ?: $request->getCompleteUrl();
$backendUrl = $baseUrl .'/'. 'shariff-backend';
// assign variables to the templates
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('dataServicesString', $dataServicesString);
$templateMgr->assign('selectedTheme', $selectedTheme);
$templateMgr->assign('shariffPostionSelected', $context->getData('shariffPositionSelected'));
$templateMgr->assign('selectedOrientation', $selectedOrientation);
$templateMgr->assign('backendUrl', $backendUrl);
$templateMgr->assign('iso1Lang', $locale);
$templateMgr->assign('requestedUrl', $requestedUrl);
$templateMgr->assign('showBlockHeading', $context->getData('shariffShowBlockHeading'));
$output .= $templateMgr->fetch($this->getTemplateResource('shariff.tpl'));
}
return false;
}
}
?>