forked from kaschioudi/sword
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSwordHandler.php
More file actions
187 lines (168 loc) · 5.67 KB
/
SwordHandler.php
File metadata and controls
187 lines (168 loc) · 5.67 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
<?php
/**
* @file SwordHandler.php
*
* Copyright (c) 2003-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class SwordHandler
* @brief Handles request for sword plugin.
*/
namespace APP\plugins\generic\sword;
use APP\facades\Repo;
use PKP\security\authorization\ContextAccessPolicy;
use PKP\core\JSONMessage;
use PKP\plugins\PluginRegistry;
use PKP\security\Role;
use PKP\db\DAORegistry;
use PKP\security\Validation;
use APP\handler\Handler;
use APP\notification\Notification;
use APP\notification\NotificationManager;
use APP\plugins\generic\sword\classes\DepositPoint;
use APP\plugins\generic\sword\classes\DepositPointsHelper;
use APP\plugins\generic\sword\DepositPointForm;
use APP\template\TemplateManager;
class SwordHandler extends Handler {
/** @var SwordPlugin Sword plugin */
protected $_parentPlugin = null;
/**
* Constructor
*/
public function __construct() {
parent::__construct();
// set reference to markup plugin
$this->_parentPlugin = PluginRegistry::getPlugin('generic', 'swordplugin');
$this->addRoleAssignment(
[Role::ROLE_ID_MANAGER],
['performManagerOnlyDeposit']
);
$this->addRoleAssignment(
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_AUTHOR],
['index', 'depositPoints']
);
}
/**
* @copydoc PKPHandler::authorize()
*/
function authorize($request, &$args, $roleAssignments) {
$this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
return parent::authorize($request, $args, $roleAssignments);
}
/**
* Get reference to the sword plugin
* @return SwordPlugin
*/
public function getSwordPlugin() {
return $this->_parentPlugin;
}
/**
* Returns deposit point details
* @param $args array
* @param $request PKPRequest
*
* @return JSONMessage
*/
public function depositPoints($args, $request) {
$context = $request->getContext();
$depositPointId = $request->getUserVar('depositPointId');
/** @var DepositPointDAO $depositPointDao */
$depositPointDao = DAORegistry::getDAO('DepositPointDAO');
$depositPoint = $depositPointDao->getById($depositPointId, $context->getId());
if (!$depositPoint) {
return new JSONMessage(false);
}
$isManager = Validation::isAuthorized(Role::ROLE_ID_MANAGER, $context->getId());
if (!$isManager && $depositPoint->getType() != SWORD_DEPOSIT_TYPE_OPTIONAL_SELECTION) {
return new JSONMessage(false);
}
$collections = DepositPointsHelper::loadCollectionsFromServer(
$depositPoint->getSwordUrl(),
$depositPoint->getSwordUsername() ?: $request->getUserVar('username'),
$depositPoint->getSwordPassword() ?: $request->getUserVar('password'),
$depositPoint->getSwordApikey()
);
return new JSONMessage(true, [
'username' => $isManager ? $depositPoint->getSwordUsername() : null,
'password' => SWORD_PASSWORD_SLUG,
'apikey' => $isManager ? $depositPoint->getSwordApikey() : null,
'depositPoints' => $collections,
]);
}
/**
* Returns author deposit points page
* @param $args array
* @param $request PKPRequest
*
* @return JSONMessage
*/
public function index($args, $request) {
$context = $request->getContext();
$user = $request->getUser();
$submissionId = (int) array_shift($args);
$save = array_shift($args) == 'save';
$submission = Repo::submission()->get($submissionId);
if (!$submission || !$user || !$context ||
($submission->getData('contextId') != $context->getId())) {
$request->redirect(null, 'index');
}
$userCanDeposit = false;
/** @var StageAssignmentDAO $stageAssignmentDao */
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$daoResult = $stageAssignmentDao->getBySubmissionAndRoleId($submission->getId(), Role::ROLE_ID_AUTHOR);
while ($record = $daoResult->next()) {
if($user->getId() == $record->getData('userId')) {
$userCanDeposit = true;
break;
}
}
if (!$userCanDeposit) {
$request->redirect(null, 'index');
}
$swordPlugin = $this->getSwordPlugin();
$authorDepositForm = new AuthorDepositForm($swordPlugin, $context, $submission);
if ($save) {
$authorDepositForm->readInputData();
if ($authorDepositForm->validate()) {
try {
$responses = $authorDepositForm->execute($request);
$templateMgr = TemplateManager::getManager($request);
$results = [];
/** @var DepositPointDAO $depositPointDao */
$depositPointDao = DAORegistry::getDAO('DepositPointDAO');
$depositPoints = iterator_to_array($depositPointDao->getByContextId($context->getId()));
foreach ($responses as $url => $response) {
// Identify the deposit point this result relates to
$depositPoint = null;
foreach ($depositPoints as $candidateDepositPoint) {
if ($candidateDepositPoint->getSwordUrl() == $url) $depositPoint = $candidateDepositPoint;
}
// Add the result to the list
$results[] = [
'url' => $url,
'depositPoint' => $depositPoint,
'itemTitle' => $response->sac_title,
'treatment' => $response->sac_treatment,
'alternateLink' => $this->_parentPlugin->getAlternateLink($response->sac_xml),
];
}
$templateMgr->assign('results', $results);
$templateMgr->display($this->_parentPlugin->getTemplateResource('results.tpl'));
return;
} catch (\Exception $e) {
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(
$user->getId(),
Notification::NOTIFICATION_TYPE_ERROR,
['contents' => __('plugins.importexport.sword.depositFailed') . ': ' . $e->getMessage()]
);
error_log($e->getTraceAsString());
}
}
} else {
$authorDepositForm->initData();
}
$authorDepositForm->display($request);
}
}