-
Notifications
You must be signed in to change notification settings - Fork 11
/
DocxToJatsPlugin.inc.php
executable file
·152 lines (133 loc) · 4.8 KB
/
DocxToJatsPlugin.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
<?php
/**
* @file plugins/generic/docxConverter/DocxToJatsPlugin.inc.php
*
* Copyright (c) 2014-2019 Simon Fraser University Library
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2.
*
* @brief main class of the DOCX to JATS XML Converter Plugin
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class DocxToJatsPlugin extends GenericPlugin {
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.docxToJats.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.docxToJats.description');
}
/**
* Register the plugin
*
* @param $category string Plugin category
* @param $path string Plugin path
* @param $mainContextId ?integer
* @return bool True on successful registration false otherwise
*/
function register($category, $path, $mainContextId = null) {
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled()) {
// Register callbacks.
HookRegistry::register('TemplateManager::fetch', array($this, 'templateFetchCallback'));
HookRegistry::register('LoadHandler', array($this, 'callbackLoadHandler'));
$this->_registerTemplateResource();
}
return true;
}
return false;
}
/**
* Get plugin URL
* @param $request PKPRequest
* @return string
*/
function getPluginUrl($request) {
return $request->getBaseUrl() . '/' . $this->getPluginPath();
}
public function callbackLoadHandler($hookName, $args) {
$page = $args[0];
$op = $args[1];
if ($page == "docxParser" && $op == "parse") {
define('HANDLER_CLASS', 'ConverterHandler');
define('CONVERTER_PLUGIN_NAME', $this->getName());
$args[2] = $this->getPluginPath() . '/' . 'DOCXConverterHandler.inc.php';
}
return false;
}
/**
* Adds additional links to submission files grid row
* @param $hookName string The name of the invoked hook
* @param $args array Hook parameters
*/
public function templateFetchCallback($hookName, $params) {
$request = $this->getRequest();
$dispatcher = $request->getDispatcher();
$templateMgr = $params[0];
$resourceName = $params[1];
if ($resourceName == 'controllers/grid/gridRow.tpl') {
/* @var $row GridRow */
$row = $templateMgr->getTemplateVars('row');
$data = $row->getData();
if (is_array($data) && (isset($data['submissionFile']))) {
$submissionFile = $data['submissionFile'];
$fileExtension = strtolower($submissionFile->getData('mimetype'));
// Ensure that the conversion is run on the appropriate workflow stage
$stageId = (int) $request->getUserVar('stageId');
$submissionId = $submissionFile->getData('submissionId');
$submission = Services::get('submission')->get($submissionId); /** @var $submission Submission */
$submissionStageId = $submission->getData('stageId');
$roles = $request->getUser()->getRoles($request->getContext()->getId());
$accessAllowed = false;
foreach ($roles as $role) {
if (in_array($role->getId(), [ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT])) {
$accessAllowed = true;
break;
}
}
if (in_array(strtolower($fileExtension), static::getSupportedMimetypes()) && // show only for files with docx extension
$accessAllowed && // only for those that have access according to the DOCXConverterHandler rules
in_array($stageId, $this->getAllowedWorkflowStages()) && // only for stage ids copyediting or higher
in_array($submissionStageId, $this->getAllowedWorkflowStages()) // only if submission has correspondent stage id
) {
$path = $dispatcher->url($request, ROUTE_PAGE, null, 'docxParser', 'parse', null,
array(
'submissionId' => $submissionId,
'submissionFileId' => $submissionFile->getId(),
'stageId' => $stageId
));
$pathRedirect = $dispatcher->url($request, ROUTE_PAGE, null, 'workflow', 'access', $submissionId);
import('lib.pkp.classes.linkAction.request.AjaxAction');
$linkAction = new LinkAction(
'parse',
new PostAndRedirectAction($path, $pathRedirect),
__('plugins.generic.docxToJats.button.parseDocx')
);
$row->addAction($linkAction);
}
}
}
}
public function getAllowedWorkflowStages() {
return [
WORKFLOW_STAGE_ID_EDITING,
WORKFLOW_STAGE_ID_PRODUCTION
];
}
/**
* @return string[] MIME type supported by the plugin for conversion
*/
public static function getSupportedMimetypes()
{
return [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
// OJS identifies Google Docs files exported in DOCX format as having this MIME type
'application/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.openxmlformats-officedocument.wordprocessingml.document'
];
}
}