forked from ojsde/shariff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShariffPlugin.inc.php
226 lines (192 loc) · 6.92 KB
/
ShariffPlugin.inc.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
<?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
*/
use PKP\core\JSONMessage;
use APP\notification\NotificationManager;
use APP\i18n\AppLocale;
import('lib.pkp.classes.plugins.GenericPlugin');
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) {
$contextId = $context->getId();
HookRegistry::register('Template::Settings::website::appearance', array($this, 'callbackAppearanceTab')); //to enable display of plugin settings tab
HookRegistry::register('Schema::get::context', array($this, 'addToSchema')); // to add Shariff avriables to context schema
HookRegistry::register('Templates::Common::Footer::PageFooter', array($this, 'addShariffButtons'));
HookRegistry::register('Templates::Article::Details', array($this, 'addShariffButtons'));
HookRegistry::register('Templates::Catalog::Book::Details', array($this, 'addShariffButtons'));
HookRegistry::register('Templates::Preprint::Details', array($this, 'addShariffButtons'));
// Load this plugin as a block plugin as well (for sidebar)
$this->import('ShariffBlockPlugin');
$shariffBlockPlugin = new ShariffBlockPlugin($this->getName(), $this->getPluginPath());
PluginRegistry::register(
'blocks',
$shariffBlockPlugin,
$this->getPluginPath()
);
}
}
return $success;
}
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'],
];
return false;
}
function callbackAppearanceTab($hookName, $args) {
# prepare data
$templateMgr =& $args[1];
$output =& $args[2];
$request =& Registry::get('request');
$context = $request->getContext();
$contextId = $context->getId();
$dispatcher = $request->getDispatcher();
# url to handle form dialog (we add our vars to the context schema)
$contextApiUrl = $dispatcher->url(
$request,
ROUTE_API,
$context->getPath(),
'contexts/' . $context->getId()
);
$contextUrl = $request->getRouter()->url($request, $context->getPath());
// instantinate settings form
$this->import('ShariffSettingsForm');
$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); // In OJS 3.3 $templateMgr->setState doesn't seem to update template vars anymore
$output .= $templateMgr->display($this->getTemplateResource('settingsForm.tpl'));
// Permit other plugins to continue interacting with this hook
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) . 'templates/';
}
/**
* 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 in 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);
// theme
$selectedTheme = $context->getData('shariffThemeSelected');
// orientation
$selectedOrientation = $context->getData('shariffOrientationSelected');
// get language from system
$locale = AppLocale::getLocale();
// javascript, css and backend url
$requestedUrl = $request->getCompleteUrl();
$request = new PKPRequest();
$baseUrl = $request->getBaseUrl();
$jsUrl = $baseUrl .'/'. $this->getPluginPath().'/shariff-3.2.1/shariff.complete.js';
$cssUrl = $baseUrl .'/' . $this->getPluginPath() . '/' . 'shariff-3.2.1/shariff.complete.css';
$backendUrl = $baseUrl .'/'. 'shariff-backend';
$selectedPositon = $context->getData('shariffPositionSelected');
if ($selectedPositon == 'footer') {
$divWrapper = '<div class="pkp_structure_footer_wrapper"><div class="pkp_structure_footer">';
} elseif ($selectedPositon == 'submission') {
$divWrapper = '<div class="shariffblock"><div>';
}
$output .= '
<link rel="stylesheet" type="text/css" href="'.$cssUrl.'">'.$divWrapper.'
<div class="shariff item" data-lang="'. str_split($locale, 2)[0] .'"
data-services="['.$dataServicesString.']"
data-mail-url="mailto:"
data-mail-body={url}
data-backend-url="'.$backendUrl.'"
data-theme="'.$selectedTheme.'"
data-orientation="'.$selectedOrientation.'"
data-url="'. $requestedUrl .'">
</div>
</div>
</div>
<script src="'.$jsUrl.'"></script>';
return false;
}
}
?>