-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathSubmissionFilesUploadForm.php
270 lines (237 loc) · 8.3 KB
/
SubmissionFilesUploadForm.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @file controllers/wizard/fileUpload/form/SubmissionFilesUploadForm.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class SubmissionFilesUploadForm
*
* @ingroup controllers_wizard_fileUpload_form
*
* @brief Form for adding/editing a submission file
*/
namespace PKP\controllers\wizard\fileUpload\form;
use APP\core\Application;
use APP\core\Services;
use APP\facades\Repo;
use APP\submission\Submission;
use PKP\db\DAORegistry;
use PKP\file\FileManager;
use PKP\form\validation\FormValidator;
use PKP\submissionFile\SubmissionFile;
class SubmissionFilesUploadForm extends PKPSubmissionFilesUploadBaseForm
{
/** @var array */
public $_uploaderRoles;
/** @var Submission */
protected $_submission;
/**
* Constructor.
*
* @param Request $request
* @param int $submissionId
* @param int $stageId One of the WORKFLOW_STAGE_ID_* constants.
* @param array $uploaderRoles
* @param int $fileStage
* @param bool $revisionOnly
* @param int $stageId
* @param ReviewRound $reviewRound
* @param int $revisedFileId
* @param int $assocType
* @param int $assocId
* @param int $queryId
*/
public function __construct(
$request,
$submissionId,
$stageId,
$uploaderRoles,
$fileStage,
$revisionOnly = false,
$reviewRound = null,
$revisedFileId = null,
$assocType = null,
$assocId = null,
$queryId = null
) {
// Initialize class.
assert(is_null($uploaderRoles) || (is_array($uploaderRoles) && count($uploaderRoles) >= 1));
$this->_uploaderRoles = $uploaderRoles;
parent::__construct(
$request,
'controllers/wizard/fileUpload/form/fileUploadForm.tpl',
$submissionId,
$stageId,
$fileStage,
$revisionOnly,
$reviewRound,
$revisedFileId,
$assocType,
$assocId,
$queryId
);
$this->_submission = Repo::submission()->get($submissionId);
// Disable the genre selector for review file attachments
if ($fileStage == SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT) {
$this->setData('isReviewAttachment', true);
}
}
//
// Getters and Setters
//
/**
* Get the uploader roles.
*
* @return array
*/
public function getUploaderRoles()
{
assert(!is_null($this->_uploaderRoles));
return $this->_uploaderRoles;
}
//
// Implement template methods from Form
//
/**
* @copydoc Form::readInputData()
*/
public function readInputData()
{
$this->readUserVars(['genreId']);
return parent::readInputData();
}
/**
* @copydoc Form::validate()
*/
public function validate($callHooks = true)
{
// Is this a revision?
$revisedFileId = $this->getRevisedFileId();
if ($this->getData('revisionOnly')) {
assert($revisedFileId > 0);
}
// Retrieve the request context.
$request = Application::get()->getRequest();
$router = $request->getRouter();
$context = $router->getContext($request);
if (
$this->getData('fileStage') != SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT and
!$revisedFileId
) {
// Add an additional check for the genre to the form.
$this->addCheck(new \PKP\form\validation\FormValidatorCustom(
$this,
'genreId',
FormValidator::FORM_VALIDATOR_REQUIRED_VALUE,
'submission.upload.noGenre',
function ($genreId) use ($context) {
$genreDao = DAORegistry::getDAO('GenreDAO'); /** @var GenreDAO $genreDao */
return is_a($genreDao->getById($genreId, $context->getId()), 'Genre');
}
));
}
return parent::validate($callHooks);
}
/**
* @copydoc Form::fetch()
*
* @param null|mixed $template
*/
public function fetch($request, $template = null, $display = false)
{
// Retrieve available submission file genres.
$genreList = $this->_retrieveGenreList($request);
$this->setData('submissionFileGenres', $genreList);
return parent::fetch($request, $template, $display);
}
/**
* Save the submission file upload form.
*
* @see Form::execute()
*
* @return SubmissionFile if successful, otherwise null
*/
public function execute(...$functionParams)
{
// Identify the uploading user.
$request = Application::get()->getRequest();
$user = $request->getUser();
assert(is_a($user, 'User'));
// Upload the file.
$fileManager = new FileManager();
$extension = $fileManager->parseFileExtension($_FILES['uploadedFile']['name']);
$submissionDir = Repo::submissionFile()->getSubmissionDir($request->getContext()->getId(), $this->getData('submissionId'));
$fileId = Services::get('file')->add(
$_FILES['uploadedFile']['tmp_name'],
$submissionDir . '/' . uniqid() . '.' . $extension
);
if ($this->getRevisedFileId()) {
$submissionFile = Repo::submissionFile()->get($this->getRevisedFileId());
Repo::submissionFile()->edit(
$submissionFile,
[
'fileId' => $fileId,
'name' => [
$this->_submission->getLocale() => $_FILES['uploadedFile']['name'],
],
'uploaderUserId' => $user->getId(),
]
);
$submissionFile = Repo::submissionFile()->get($this->getRevisedFileId());
} else {
$submissionFile = Repo::submissionFile()->dao->newDataObject();
$submissionFile->setData('fileId', $fileId);
$submissionFile->setData('fileStage', $this->getData('fileStage'));
$submissionFile->setData('name', $_FILES['uploadedFile']['name'], $this->_submission->getLocale());
$submissionFile->setData('submissionId', $this->getData('submissionId'));
$submissionFile->setData('uploaderUserId', $user->getId());
$submissionFile->setData('assocType', $this->getData('assocType') ? (int) $this->getData('assocType') : null);
$submissionFile->setData('assocId', $this->getData('assocId') ? (int) $this->getData('assocId') : null);
$genreId = (int) $this->getData('genreId');
if ($genreId === 0) {
$genreId = null;
}
$submissionFile->setData('genreId', $genreId);
if ($this->getReviewRound() && $this->getReviewRound()->getId() && empty($submissionFile->getData('assocType'))) {
$submissionFile->setData('assocType', Application::ASSOC_TYPE_REVIEW_ROUND);
$submissionFile->setData('assocId', $this->getReviewRound()->getId());
}
$id = Repo::submissionFile()->add($submissionFile);
$submissionFile = Repo::submissionFile()->get($id);
}
if (!$submissionFile) {
return null;
}
$hookResult = parent::execute($submissionFile, ...$functionParams);
if ($hookResult) {
return $hookResult;
}
return $submissionFile;
}
//
// Private helper methods
//
/**
* Retrieve the genre list.
*
* @param Request $request
*
* @return array
*/
public function _retrieveGenreList($request)
{
$context = $request->getContext();
$genreDao = DAORegistry::getDAO('GenreDAO'); /** @var GenreDAO $genreDao */
$dependentFilesOnly = $request->getUserVar('dependentFilesOnly') ? true : false;
$genres = $genreDao->getByDependenceAndContextId($dependentFilesOnly, $context->getId());
// Transform the genres into an array and
// assign them to the form.
$genreList = [];
while ($genre = $genres->next()) {
$genreList[$genre->getId()] = $genre->getLocalizedName();
}
return $genreList;
}
}