-
Notifications
You must be signed in to change notification settings - Fork 43
/
QuickSubmitForm.php
513 lines (444 loc) · 19.7 KB
/
QuickSubmitForm.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* @file QuickSubmitForm.php
*
* Copyright (c) 2013-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class QuickSubmitForm
*
* @brief Form for QuickSubmit one-page submission plugin
*/
namespace APP\plugins\importexport\quickSubmit;
use APP\core\Application;
use APP\facades\Repo;
use APP\journal\Journal;
use APP\plugins\importexport\quickSubmit\classes\form\SubmissionMetadataForm;
use APP\publication\Publication;
use APP\submission\Submission;
use APP\template\TemplateManager;
use PKP\config\Config;
use PKP\context\Context;
use PKP\core\Core;
use PKP\core\PKPRequest;
use PKP\core\PKPString;
use PKP\db\DAORegistry;
use PKP\facades\Locale;
use PKP\form\Form;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\security\Role;
use PKP\submission\PKPSubmission;
use PKP\submissionFile\SubmissionFile;
class QuickSubmitForm extends Form
{
protected PKPRequest $_request;
protected ?PKPSubmission $_submission = null;
protected Journal $_context;
protected SubmissionMetadataForm $form;
/**
* Constructor
*
* @param $plugin object
* @param $request object
*/
public function __construct(QuickSubmitPlugin $plugin, PKPRequest $request)
{
parent::__construct($plugin->getTemplateResource('index.tpl'));
$this->_request = $request;
$this->_context = $request->getContext();
$this->form = new SubmissionMetadataForm($this);
$locale = $request->getUserVar('locale');
if ($locale && ($locale != Locale::getLocale())) {
$this->setDefaultFormLocale($locale);
}
if ($submissionId = $request->getUserVar('submissionId')) {
$this->_submission = Repo::submission()->get($submissionId);
if ($this->_submission->getData('contextId') != $this->_context->getId()) {
throw new \Exception('Submission not in context!');
}
$sectionId = $request->getUserVar('sectionId');
if (!empty($sectionId)) {
$this->_submission->setData('sectionId', $sectionId);
}
$this->_submission->setData('locale', $this->getDefaultFormLocale());
$publication = $this->_submission->getCurrentPublication();
$publication->setData('locale', $this->getDefaultFormLocale());
Repo::submission()->edit($this->_submission, []);
Repo::publication()->edit($publication, []);
$this->form->addChecks($this->_submission);
}
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
$contextId = $this->_context->getId();
$this->addCheck(
new \PKP\form\validation\FormValidatorCustom(
$this,
'sectionId',
'required',
'author.submit.form.sectionRequired',
function ($sectionId) use ($contextId) {
return Repo::section()->exists((int) $sectionId, $contextId);
}
)
);
// Validation checks for this form
$supportedSubmissionLocales = $this->_context->getSupportedSubmissionLocales();
if (!is_array($supportedSubmissionLocales) || count($supportedSubmissionLocales) < 1) {
$supportedSubmissionLocales = [$this->_context->getPrimaryLocale()];
}
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'locale', 'required', 'submission.submit.form.localeRequired', $supportedSubmissionLocales));
$this->addCheck(new \PKP\form\validation\FormValidatorUrl($this, 'licenseUrl', 'optional', 'form.url.invalid'));
}
/**
* Get the submission associated with the form.
*
* @return Submission
*/
public function getSubmission()
{
return $this->_submission;
}
/**
* Get the names of fields for which data should be localized
*
* @return array
*/
public function getLocaleFieldNames()
{
return $this->form->getLocaleFieldNames();
}
/**
* Display the form.
*
* @param null|mixed $request
* @param null|mixed $template
*/
public function display($request = null, $template = null)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(
'supportedSubmissionLocaleNames',
$this->_context->getSupportedSubmissionLocaleNames()
);
// Tell the form what fields are enabled (and which of those are required)
foreach (Application::getMetadataFields() as $field) {
$templateMgr->assign([
$field . 'Enabled' => in_array($this->_context->getData($field), [Context::METADATA_ENABLE, Context::METADATA_REQUEST, Context::METADATA_REQUIRE]),
$field . 'Required' => $this->_context->getData($field) === Context::METADATA_REQUIRE,
]);
}
// Cover image delete link action
$locale = Locale::getLocale();
$router = $this->_request->getRouter();
$publication = $this->_submission->getCurrentPublication();
$templateMgr->assign('openCoverImageLinkAction', new LinkAction(
'uploadFile',
new AjaxModal(
$router->url($this->_request, null, null, 'importexport', ['plugin', 'QuickSubmitPlugin', 'uploadCoverImage'], [
'coverImage' => $publication->getData('coverImage', $locale),
'submissionId' => $this->_submission->getId(),
'publicationId' => $publication->getId(),
// This action can be performed during any stage,
// but we have to provide a stage id to make calls
// to IssueEntryTabHandler
'stageId' => WORKFLOW_STAGE_ID_PRODUCTION,
]),
__('common.upload'),
'modal_add_file'
),
__('common.upload'),
'add'
));
// Get section for this context
$sectionTitles = Repo::section()
->getCollector()
->filterByContextIds([$this->_context->getId()])
->getMany()
->mapWithKeys(function ($section) {
return [
$section->getId() => $section->getLocalizedTitle()
];
})
->toArray();
$sectionOptions = [0 => ''] + $sectionTitles;
$templateMgr->assign('sectionOptions', $sectionOptions);
// Get published Issues
$issues = Repo::issue()->getCollector()
->filterByContextIds([$this->_context->getId()])
->orderBy(\APP\issue\Collector::ORDERBY_SHELF)
->getMany()
->toArray();
$templateMgr->assign('hasIssues', count($issues) > 0);
// Get Issues
$templateMgr->assign([
'issueOptions' => $this->getIssueOptions($this->_context),
'submission' => $this->_submission,
'locale' => $this->getDefaultFormLocale(),
'publicationId' => $publication->getId(),
]);
$sectionId = $this->getData('sectionId') ?: $this->_submission->getSectionId();
$section = Repo::section()->get($sectionId, $this->_context->getId());
$templateMgr->assign([
'wordCount' => $section->getAbstractWordCount(),
'abstractsRequired' => !$section->getAbstractsNotRequired(),
]);
// Process entered tagit fields values for redisplay.
// @see PKPSubmissionHandler::saveStep
$tagitKeywords = $this->getData('keywords');
if (is_array($tagitKeywords)) {
$tagitFieldNames = $this->form->getTagitFieldNames();
$locales = array_keys($this->supportedLocales);
$formTagitData = [];
foreach ($tagitFieldNames as $tagitFieldName) {
foreach ($locales as $locale) {
$formTagitData[$locale] = array_key_exists($locale . "-{$tagitFieldName}", $tagitKeywords) ? $tagitKeywords[$locale . "-{$tagitFieldName}"] : [];
}
$this->setData($tagitFieldName, $formTagitData);
}
}
$templateMgr->assign([
'primaryLocale' => $this->_submission->getData('locale'),
]);
parent::display($request, $template);
}
/**
* @copydoc Form::validate
*/
public function validate($callHooks = true)
{
if (!parent::validate($callHooks)) {
return false;
}
// Validate Issue if Published is selected
// if articleStatus == 1 => should have issueId
if ($this->getData('articleStatus') == 1) {
if ($this->getData('issueId') <= 0) {
$this->addError('issueId', __('plugins.importexport.quickSubmit.selectIssue'));
$this->errorFields['issueId'] = 1;
return false;
}
}
return true;
}
/**
* Initialize form data for a new form.
*/
public function initData()
{
$this->_data = [];
if (!$this->_submission) {
$this->_data['locale'] = $this->getDefaultFormLocale();
// Get Sections
$sectionOptions = Repo::section()
->getCollector()
->filterByContextIds([$this->_context->getId()])
->getMany()
->map(function ($section) {
return [
$section->getId() => $section->getLocalizedTitle()
];
})
->toArray();
// Create and insert a new submission and publication
$this->_submission = Repo::submission()->dao->newDataObject();
$this->_submission->setData('contextId', $this->_context->getId());
$this->_submission->setData('status', PKPSubmission::STATUS_QUEUED);
$this->_submission->setData('submissionProgress', 'start');
$this->_submission->stampLastActivity();
$this->_submission->setData('stageId', WORKFLOW_STAGE_ID_SUBMISSION);
$this->_submission->setData('sectionId', $sectionId = current(array_keys($sectionOptions)));
$this->_submission->setData('locale', $this->getDefaultFormLocale());
$publication = new Publication();
$publication->setData('locale', $this->getDefaultFormLocale());
$publication->setData('sectionId', $sectionId);
$publication->setData('status', PKPSubmission::STATUS_QUEUED);
$publication->setData('version', 1);
Repo::submission()->add($this->_submission, $publication, $this->_context);
$this->_submission = Repo::submission()->get($this->_submission->getId());
$this->setData('submissionId', $this->_submission->getId());
$this->form->initData($this->_submission);
// Add the user manager group (first that is found) to the stage_assignment for that submission
$user = $this->_request->getUser();
$managerUserGroups = Repo::userGroup()->getCollector()
->filterByUserIds([$user->getId()])
->filterByContextIds([$this->_context->getId()])
->filterByRoleIds([Role::ROLE_ID_MANAGER])
->getMany();
// $userGroupId is being used for Repo::stageAssignment()->build(...)
// This build function needs the userGroupId
// So here the first function should fail if no manager user group is found.
$userGroupId = $managerUserGroups->firstOrFail()->getId();
// Pre-fill the copyright information fields from setup (#7236)
$this->_data['licenseUrl'] = $this->_context->getData('licenseUrl');
switch ($this->_context->getData('copyrightHolderType')) {
case 'author':
// The author has not been entered yet; let the user fill it in.
break;
case 'context':
$this->_data['copyrightHolder'] = $this->_context->getData('name');
break;
case 'other':
$this->_data['copyrightHolder'] = $this->_context->getData('copyrightHolderOther');
break;
}
$this->_data['copyrightYear'] = date('Y');
// Assign the user author to the stage
Repo::stageAssignment()
->build(
$this->_submission->getId(),
$userGroupId,
$user->getId()
);
}
}
/**
* Assign form data to user-submitted data.
*/
public function readInputData()
{
$this->form->readInputData();
$this->readUserVars(
[
'issueId',
'pages',
'datePublished',
'licenseUrl',
'copyrightHolder',
'copyrightYear',
'sectionId',
'submissionId',
'articleStatus',
'locale'
]
);
}
/**
* cancel submit
*/
public function cancel()
{
$submission = Repo::submission()->get((int) $this->getData('submissionId')); /** @var Submission $submission */
if ($this->_submission->getData('contextId') != $this->_context->getId()) {
throw new \Exception('Submission not in context!');
}
if ($submission) {
Repo::submission()->delete($submission);
}
}
/**
* Save settings.
*/
public function execute(...$functionParams)
{
// Execute submission metadata related operations.
$this->form->execute($this->_submission, $this->_request);
$publication = $this->_submission->getCurrentPublication();
// Copy GalleyFiles to Submission Files
// Get Galley Files by SubmissionId
$galleyDao = Application::getRepresentationDAO();
$galleys = $galleyDao->getByPublicationId($publication->getId());
if (!is_null($galleys)) {
foreach ($galleys as $galley) {
$file = $galley->getFile();
if ($file) {
$newSubmissionFile = clone $file;
$newSubmissionFile->setData('fileStage', SubmissionFile::SUBMISSION_FILE_SUBMISSION);
$newSubmissionFile->unsetData('assocType');
$newSubmissionFile->unsetData('assocId');
$newSubmissionFile->setData('viewable', true);
$newSubmissionFile->setData('sourceSubmissionFileId', $file->getId());
$newSubmissionFile = Repo::submissionFile()->add($newSubmissionFile);
}
}
}
$this->_submission->setData('locale', $this->getData('locale'));
$this->_submission->setData('stageId', WORKFLOW_STAGE_ID_PRODUCTION);
$this->_submission->setData('dateSubmitted', Core::getCurrentDate());
$this->_submission->setData('submissionProgress', '');
parent::execute($this->_submission, ...$functionParams);
Repo::submission()->edit($this->_submission, []);
$this->_submission = Repo::submission()->get($this->_submission->getId());
$publication = $this->_submission->getCurrentPublication();
if ($publication->getData('sectionId') !== (int) $this->getData('sectionId')) {
$publication = Repo::publication()->edit($publication, ['sectionId' => (int) $this->getData('sectionId')]);
}
if ($this->getData('articleStatus') == 1) {
$publication->setData('copyrightYear', $this->getData('copyrightYear'));
$publication->setData('copyrightHolder', $this->getData('copyrightHolder'), null);
$publication->setData('licenseUrl', $this->getData('licenseUrl'));
$publication->setData('pages', $this->getData('pages'));
$publication->setData('datePublished', $this->getData('datePublished'));
$publication->setData('accessStatus', Submission::ARTICLE_ACCESS_ISSUE_DEFAULT);
$publication->setData('issueId', (int) $this->getData('issueId'));
// If other articles in this issue have a custom sequence, put this at the end
$otherSubmissionsInSection = Repo::submission()->getCollector()
->filterByContextIds([$this->_request->getContext()->getId()])
->filterByIssueIds([$publication->getData('issueId')])
->filterBySectionIds([$publication->getData('sectionId')])
->getMany()->toArray();
if (count($otherSubmissionsInSection)) {
$maxSequence = 0;
foreach ($otherSubmissionsInSection as $submission) {
if ($publication->getData('seq')) {
$maxSequence = max($maxSequence, $publication->getData('seq'));
}
}
$publication->setData('seq', $maxSequence + 1);
}
Repo::publication()->publish($publication);
}
// Index article.
$articleSearchIndex = Application::getSubmissionSearchIndex();
$articleSearchIndex->submissionMetadataChanged($this->_submission);
$articleSearchIndex->submissionFilesChanged($this->_submission);
$articleSearchIndex->submissionChangesFinished();
}
/**
* builds the issue options pulldown for published and unpublished issues
*
* @param $journal Journal
*
* @return array Associative list of options for pulldown
*/
public function getIssueOptions($journal)
{
$issuesPublicationDates = [];
$issueOptions = [];
$journalId = $journal->getId();
$issueOptions[-1] = '------ ' . __('editor.issues.futureIssues') . ' ------';
$issues = Repo::issue()->getCollector()
->filterByContextIds([$journalId])
->filterByPublished(false)
->orderBy(\APP\issue\Collector::ORDERBY_SHELF)
->getMany();
foreach ($issues as $issue) {
$issueOptions[$issue->getId()] = $issue->getIssueIdentification();
$issuesPublicationDates[$issue->getId()] = date(PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_short')), strtotime(Core::getCurrentDate()));
}
$issueOptions[-2] = '------ ' . __('editor.issues.currentIssue') . ' ------';
$issues = array_values(
Repo::issue()
->getCollector()
->filterByContextIds([$journalId])
->filterByPublished(true)
->orderBy(\APP\issue\Collector::ORDERBY_SHELF)
->getMany()
->toArray()
);
if (isset($issues[0]) && $issues[0]->getId() == $journal->getData('currentIssueId')) {
$issueOptions[$issues[0]->getId()] = $issues[0]->getIssueIdentification();
$issuesPublicationDates[$issues[0]->getId()] = date(PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_short')), strtotime($issues[0]->getDatePublished()));
array_shift($issues);
}
$issueOptions[-3] = '------ ' . __('editor.issues.backIssues') . ' ------';
foreach ($issues as $issue) {
$issueOptions[$issue->getId()] = $issue->getIssueIdentification();
$issuesPublicationDates[$issue->getId()] = date(PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_short')), strtotime($issues[0]->getDatePublished()));
}
$templateMgr = TemplateManager::getManager($this->_request);
$templateMgr->assign('issuesPublicationDates', json_encode($issuesPublicationDates));
return $issueOptions;
}
}