-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRORPlugin.php
More file actions
executable file
·203 lines (172 loc) · 6.12 KB
/
RORPlugin.php
File metadata and controls
executable file
·203 lines (172 loc) · 6.12 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
<?php
/**
* @file RORPlugin.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
* @class RorPlugin
*
* @brief Ror Plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
/**
* Class rorPlugin
*/
class RORPlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = NULL) {
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled()) {
# Author form
HookRegistry::register('authordao::getAdditionalFieldNames', array($this, 'handleAdditionalFieldNames'));
HookRegistry::register('authorform::display', array($this, 'handleFormDisplay'));
HookRegistry::register('authorform::execute', array($this, 'handleAuthorFormExecute'));
HookRegistry::register('Schema::get::author', function ($hookName, $args) {
$schema = $args[0];
$schema->properties->rorId = (object)[
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable']
];
});
# Submission
HookRegistry::register('ArticleHandler::view', array(&$this, 'submissionView'));
HookRegistry::register('TemplateManager::display', array(&$this, 'handleSubmissionDisplay'));
}
return $success;
}
function addSubmissionDisplay($hookName, $params) {
$templateMgr = $params[1];
$output =& $params[2];
$submission = $templateMgr->get_template_vars('monograph') ? $templateMgr->get_template_vars('monograph') : $templateMgr->get_template_vars('article');
}
function handleAuthorFormExecute($hookname, $args) {
$form =& $args[0];
$form->readUserVars(array('affiliation'));
$author = $form->getAuthor();
$affiliation = $form->getData('affiliation');
$publicationLocale = $form->getDefaultFormLocale();
$rorIDPattern = '/\[[\s]*https:\/\/ror\.org\/[\w|\d]*[\s]*\]/';
foreach ($affiliation as $locale => $value) {
preg_match($rorIDPattern, $value, $matches);
if (count($matches) > 0) {
$author->setData('rorId', str_replace(['[', ']'], '', $matches[0]));
$affiliation = preg_replace($rorIDPattern, '', $value);
$author->setData('affiliation', $affiliation, $locale);
} elseif (!is_null($author->getId()) && $locale == $publicationLocale) {
$currentAffiliation = $this->getCurrentAuthorAffiliation($author->getId(), $locale);
if ($currentAffiliation !== $value) {
$author->setData('rorId', null);
}
}
}
}
private function getCurrentAuthorAffiliation($formAuthorId, $locale) {
$authorDao = DAORegistry::getDAO('AuthorDAO');
$author = $authorDao->getById($formAuthorId);
return $author->getData('affiliation', $locale);
}
function handleFormDisplay($hookName, $args) {
$request = PKPApplication::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
switch ($hookName) {
case 'authorform::display':
$authorForm =& $args[0];
$author = $authorForm->getAuthor();
if ($author) {
$templateMgr->assign(
array(
'rorId' => $author->getData('rorId'),
'rorPlaceHolder' => __('plugins.generic.ror.rorPlaceHolder'),
'supportedLocales' => $request->getContext()->getSupportedFormLocales()
)
);
}
$templateMgr->registerFilter("output", array($this, 'authorFormFilter'));
break;
}
return false;
}
function submissionView($hook, $args) {
$request = $args[0];
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
"rorIdIcon" => $this->getIcon()
));
return false;
}
function getIcon() {
$pluginPath = $this->getPluginPath();
$path = Core::getBaseDir() . '/' . $pluginPath . '/assets/rorId.svg';
return file_exists($path) ? file_get_contents($path) : '';
}
function authorFormFilter($output, $templateMgr) {
if (preg_match('/<input[^>]+name="submissionId"[^>]*>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$newOutput = substr($output, 0, $offset + strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplateResource('affiliation.tpl'));
$newOutput .= substr($output, $offset + strlen($match));
$output = $newOutput;
$templateMgr->unregisterFilter('output', array($this, 'authorFormFilter'));
}
return $output;
}
function handleAdditionalFieldNames($hookName, $params) {
$fields =& $params[1];
$fields[] = 'rorId';
return false;
}
function handleSubmissionDisplay($hookName, $args) {
$templateMgr = &$args[0];
$template = $args[1];
$output =& $args[2];
$applicationName = Application::get()->getName();
if ($applicationName === 'ojs2') {
return false;
}
if (!in_array($template, ['frontend/pages/preprint.tpl', 'frontend/pages/book.tpl'])) {
return false;
}
$templateMgr->assign(array(
"rorIdIcon" => $this->getIcon()
));
$activeTheme = $templateMgr->get_template_vars('activeTheme');
if ($activeTheme->getDirName() === 'default') {
$templateMgr->registerFilter("output", array($this, 'submissionDisplayFilter'));
}
return false;
}
function submissionDisplayFilter($output, $templateMgr) {
$applicationName = Application::get()->getName();
$authorsPattern = $applicationName === 'ops'
? '/<section class="item authors">([\s\S]*?)<\/section>/'
: '/<div class="item authors">([\s\S]*?)<\/div>\s*(?=\s*<div class="item)/';
if (preg_match($authorsPattern, $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[1][0];
$offset = $matches[1][1];
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplateResource($applicationName . '_submission_authors.tpl'));
$newOutput .= substr($output, $offset + strlen($match));
$output = $newOutput;
$templateMgr->unregisterFilter('output', array($this, 'submissionDisplayFilter'));
}
return $output;
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.ror.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.ror.description');
}
}