forked from Vitaliy-1/docxConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOCXConverterHandler.inc.php
153 lines (129 loc) · 5.26 KB
/
DOCXConverterHandler.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
<?php
/**
* @file plugins/generic/docxConverter/DOCXConverterHandler.inc.php
*
* Copyright (c) 2014-2019 Simon Fraser University Library
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2.
*
* @brief handler for the grid's conversion
*/
import('classes.handler.Handler');
import('plugins.generic.docxConverter.classes.DOCXConverterDocument');
import('lib.pkp.classes.file.PrivateFileManager');
require_once __DIR__ . "/docxToJats/vendor/autoload.php";
use docx2jats\DOCXArchive;
class ConverterHandler extends Handler {
/** @copydoc PKPHandler::_isBackendPage */
var $_isBackendPage = true;
/**
* Constructor
*/
function __construct() {
parent::__construct();
$this->_plugin = PluginRegistry::getPlugin('generic', CONVERTER_PLUGIN_NAME);
$this->addRoleAssignment(
array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT),
array('parse')
);
}
/**
* @copydoc PKPHandler::authorize()
*/
function authorize($request, &$args, $roleAssignments) {
import('lib.pkp.classes.security.authorization.WorkflowStageAccessPolicy');
$this->addPolicy(new WorkflowStageAccessPolicy($request, $args, $roleAssignments, 'submissionId', (int)$request->getUserVar('stageId')));
return parent::authorize($request, $args, $roleAssignments);
}
public function parse($args, $request) {
$fileId = (int) $request->getUserVar('fileId');
$submissionFiles = Services::get('submissionFile')->getMany([
'fileIds' => [$fileId],
]);
$submissionFile = $submissionFiles->current();
$fileManager = new PrivateFileManager();
$filePath = $fileManager->getBasePath() . '/' . $submissionFile->getData('path');
$docxArchive = new DOCXArchive($filePath);
$jatsXML = new DOCXConverterDocument($docxArchive);
$submissionId = $submissionFile->getData('submissionId');
$submission = Services::get('submission')->get($submissionId);
$jatsXML->setDocumentMeta($request, $submission);
$tmpfname = tempnam(sys_get_temp_dir(), 'docxConverter');
file_put_contents($tmpfname, $jatsXML->saveXML());
$genreId = $submissionFile->getData('genreId');
// Add new JATS XML file
$submissionDir = Services::get('submissionFile')->getSubmissionDir($submission->getData('contextId'), $submissionId);
$newFileId = Services::get('file')->add(
$tmpfname,
$submissionDir . DIRECTORY_SEPARATOR . uniqid() . '.xml'
);
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
$newSubmissionFile = $submissionFileDao->newDataObject();
$newName = [];
foreach ($submissionFile->getData('name') as $localeKey => $name) {
$newName[$localeKey] = pathinfo($name)['filename'] . '.xml';
}
$newSubmissionFile->setAllData(
[
'fileId' => $newFileId,
'assocType' => $submissionFile->getData('assocType'),
'assocId' => $submissionFile->getData('assocId'),
'fileStage' => $submissionFile->getData('fileStage'),
'mimetype' => 'application/xml',
'locale' => $submissionFile->getData('locale'),
'genreId' => $genreId,
'name' => $newName,
'submissionId' => $submissionId,
]
);
$newSubmissionFile = Services::get('submissionFile')->add($newSubmissionFile, $request);
unlink($tmpfname);
$mediaData = $docxArchive->getMediaFilesContent();
if (!empty($mediaData)) {
foreach ($mediaData as $originalName => $singleData) {
$this->_attachSupplementaryFile($request, $submission, $submissionFileDao, $newSubmissionFile, $fileManager, $originalName, $singleData);
}
}
return new JSONMessage(true, array(
'submissionId' => $submissionId,
'fileId' => $newSubmissionFile->getData('fileId'),
'fileStage' => $newSubmissionFile->getData('fileStage'),
));
}
private function _attachSupplementaryFile(Request $request, Submission $submission, SubmissionFileDAO $submissionFileDao, SubmissionFile $newSubmissionFile, PrivateFileManager $fileManager, string $originalName, string $singleData) {
$tmpfnameSuppl = tempnam(sys_get_temp_dir(), 'docxConverter');
file_put_contents($tmpfnameSuppl, $singleData);
$mimeType = mime_content_type($tmpfnameSuppl);
// Determine genre
$genreDao = DAORegistry::getDAO('GenreDAO');
$genres = $genreDao->getByDependenceAndContextId(true, $request->getContext()->getId());
$supplGenreId = null;
while ($genre = $genres->next()) {
if (($mimeType == "image/png" || $mimeType == "image/jpeg") && $genre->getKey() == "IMAGE") {
$supplGenreId = $genre->getId();
}
}
if (!$supplGenreId) {
unlink($tmpfnameSuppl);
return;
}
$submissionDir = Services::get('submissionFile')->getSubmissionDir($submission->getData('contextId'), $submission->getId());
$newFileId = Services::get('file')->add(
$tmpfnameSuppl,
$submissionDir . '/' . uniqid() . '.' . $fileManager->parseFileExtension($originalName)
);
// Set file
$newSupplementaryFile = $submissionFileDao->newDataObject();
$newSupplementaryFile->setAllData([
'fileId' => $newFileId,
'assocId' => $newSubmissionFile->getId(),
'assocType' => ASSOC_TYPE_SUBMISSION_FILE,
'fileStage' => SUBMISSION_FILE_DEPENDENT,
'submissionId' => $submission->getId(),
'genreId' => $supplGenreId,
'name' => array_fill_keys(array_keys($newSubmissionFile->getData('name')), basename($originalName))
]);
Services::get('submissionFile')->add($newSupplementaryFile, $request);
unlink($tmpfnameSuppl);
}
}