forked from ajnyga/quickSubmit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSubmitForm.inc.php
447 lines (377 loc) · 16.1 KB
/
QuickSubmitForm.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
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
<?php
/**
* @file QuickSubmitForm.inc.php
*
* Copyright (c) 2013-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class QuickSubmitForm
* @ingroup plugins_importexport_quickSubmit
*
* @brief Form for QuickSubmit one-page submission plugin
*/
use APP\core\Application;
use APP\submission\SubmissionMetadataFormImplementation;
use APP\publication\Publication;
use APP\facades\Repo;
use PKP\form\Form;
use PKP\facades\Locale;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\core\PKPString;
class QuickSubmitForm extends Form {
/** @var Request */
protected $_request;
/** @var Submission */
protected $_submission;
/** @var Journal */
protected $context;
/** @var SubmissionMetadataFormImplementation */
protected $_metadataFormImplem;
/**
* Constructor
* @param $plugin object
* @param $request object
*/
function __construct($plugin, $request) {
parent::__construct($plugin->getTemplateResource('index.tpl'));
$this->_request = $request;
$this->_context = $request->getContext();
$this->_metadataFormImplem = new SubmissionMetadataFormImplementation($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->getContextId() != $this->_context->getId()) throw new Exeption('Submission not in context!');
$this->_submission->setLocale($this->getDefaultFormLocale());
$publication = $this->_submission->getCurrentPublication();
$publication->setData('locale', $this->getDefaultFormLocale());
Repo::submission()->edit($this->_submission, []);
Repo::publication()->edit($publication, []);
$this->_metadataFormImplem->addChecks($this->_submission);
}
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'sectionId', 'required', 'author.submit.form.sectionRequired', array(DAORegistry::getDAO('SectionDAO'), 'sectionExists'), array($this->_context->getId())));
// Validation checks for this form
$supportedSubmissionLocales = $this->_context->getSupportedSubmissionLocales();
if (!is_array($supportedSubmissionLocales) || count($supportedSubmissionLocales) < 1)
$supportedSubmissionLocales = array($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
*/
function getSubmission() {
return $this->_submission;
}
/**
* Get the names of fields for which data should be localized
* @return array
*/
function getLocaleFieldNames() {
return $this->_metadataFormImplem->getLocaleFieldNames();
}
/**
* Display the form.
*/
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(array(
$field . 'Enabled' => in_array($this->_context->getData($field), array(METADATA_ENABLE, METADATA_REQUEST, METADATA_REQUIRE)),
$field . 'Required' => $this->_context->getData($field) === 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', array('plugin', 'QuickSubmitPlugin', 'uploadCoverImage'), array(
'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
$sectionDao = DAORegistry::getDAO('SectionDAO');
$sectionOptions = array('0' => '') + $sectionDao->getTitlesByContextId($this->_context->getId());
$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(array(
'issueOptions' => $this->getIssueOptions($this->_context),
'submission' => $this->_submission,
'locale' => $this->getDefaultFormLocale(),
'publicationId' => $publication->getId(),
));
$sectionDao = DAORegistry::getDAO('SectionDAO');
$sectionId = $this->getData('sectionId') ?: $this->_submission->getSectionId();
$section = $sectionDao->getById($sectionId, $this->_context->getId());
$templateMgr->assign(array(
'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->_metadataFormImplem->getTagitFieldNames();
$locales = array_keys($this->supportedLocales);
$formTagitData = array();
foreach ($tagitFieldNames as $tagitFieldName) {
foreach ($locales as $locale) {
$formTagitData[$locale] = array_key_exists($locale . "-$tagitFieldName", $tagitKeywords) ? $tagitKeywords[$locale . "-$tagitFieldName"] : array();
}
$this->setData($tagitFieldName, $formTagitData);
}
}
parent::display($request, $template);
}
/**
* @copydoc Form::validate
*/
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.
*/
function initData() {
$this->_data = array();
if (!$this->_submission) {
$this->_data['locale'] = $this->getDefaultFormLocale();
// Get Sections
$sectionDao = DAORegistry::getDAO('SectionDAO');
$sectionOptions = $sectionDao->getTitlesByContextId($this->_context->getId());
// Create and insert a new submission and publication
$this->_submission = Repo::submission()->dao->newDataObject();
$this->_submission->setContextId($this->_context->getId());
$this->_submission->setStatus(STATUS_QUEUED);
$this->_submission->setSubmissionProgress(1);
$this->_submission->stampStatusModified();
$this->_submission->setStageId(WORKFLOW_STAGE_ID_SUBMISSION);
$this->_submission->setData('sectionId', $sectionId = current(array_keys($sectionOptions)));
$this->_submission->setLocale($this->getDefaultFormLocale());
$publication = new Publication();
$publication->setData('locale', $this->getDefaultFormLocale());
$publication->setData('sectionId', $sectionId);
$publication->setData('status', STATUS_QUEUED);
$publication->setData('version', 1);
Repo::submission()->add($this->_submission, $publication);
$this->_submission = Repo::submission()->get($this->_submission->getId());
$this->setData('submissionId', $this->_submission->getId());
$this->_metadataFormImplem->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 $stageAssignmentDao->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
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$stageAssignmentDao->build($this->_submission->getId(), $userGroupId, $user->getId());
}
}
/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->_metadataFormImplem->readInputData();
$this->readUserVars(
array(
'issueId',
'pages',
'datePublished',
'licenseUrl',
'copyrightHolder',
'copyrightYear',
'sectionId',
'submissionId',
'articleStatus',
'locale'
)
);
}
/**
* cancel submit
*/
function cancel() {
$submission = Repo::submission()->get((int) $this->getData('submissionId'));
if ($this->_submission->getContextId() != $this->_context->getId()) throw new Exeption('Submission not in context!');
if ($submission) Repo::submission()->delete($submission->getId());
}
/**
* Save settings.
*/
function execute(...$functionParams) {
// Execute submission metadata related operations.
$this->_metadataFormImplem->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', SUBMISSION_FILE_SUBMISSION);
$newSubmissionFile->setData('viewable', true);
$newSubmissionFile->setData('sourceSubmissionFileId', $file->getId());
$newSubmissionFile = Repo::submissionFile()->add($newSubmissionFile);
}
}
}
$this->_submission->setLocale($this->getData('locale'));
$this->_submission->setStageId(WORKFLOW_STAGE_ID_PRODUCTION);
$this->_submission->setDateSubmitted(Core::getCurrentDate());
$this->_submission->setSubmissionProgress(0);
parent::execute($this->_submission, ...$functionParams);
Repo::submission()->edit($this->_submission, []);
$this->_submission = Repo::submission()->get($this->_submission->getId());
if ($publication->getData('sectionId') !== (int) $this->getData('sectionId')) {
$publication = Repo::publication()->edit($publication, ['sectionId' => (int) $this->getData('sectionId')], $this->_request);
}
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', 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);
// If this submission's issue uses custom section ordering and this is the first
// article published in a section, make sure we enter a custom ordering
// for that section to place it at the end.
if (DAORegistry::getDAO('SectionDAO')->customSectionOrderingExists($publication->getData('issueId'))) {
$sectionOrder = DAORegistry::getDAO('SectionDAO')->getCustomSectionOrder($publication->getData('issueId'), $publication->getData('sectionId'));
if ($sectionOrder === null) {
DAORegistry::getDAO('SectionDAO')->insertCustomSectionOrder($publication->getData('issueId'), $publication->getData('sectionId'), REALLY_BIG_NUMBER);
DAORegistry::getDAO('SectionDAO')->resequenceCustomSectionOrders($publication->getData('issueId'));
}
}
}
// 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
*/
function getIssueOptions($journal) {
$issuesPublicationDates = array();
$issueOptions = array();
$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;
}
}