first commit
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/wizard/fileUpload/form/PKPSubmissionFilesUploadBaseForm.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 PKPSubmissionFilesUploadBaseForm
|
||||
*
|
||||
* @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\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
use Exception;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\ConfirmationModal;
|
||||
use PKP\security\Role;
|
||||
use PKP\stageAssignment\StageAssignmentDAO;
|
||||
use PKP\submission\reviewAssignment\ReviewAssignmentDAO;
|
||||
use PKP\submission\reviewRound\ReviewRound;
|
||||
use PKP\submission\reviewRound\ReviewRoundDAO;
|
||||
use PKP\submissionFile\SubmissionFile;
|
||||
use PKP\user\User;
|
||||
|
||||
class PKPSubmissionFilesUploadBaseForm extends Form
|
||||
{
|
||||
/** @var int */
|
||||
public $_stageId;
|
||||
|
||||
/** @var ReviewRound */
|
||||
public $_reviewRound;
|
||||
|
||||
/** @var array the submission files for this submission and file stage */
|
||||
public $_submissionFiles;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $template
|
||||
* @param int $submissionId
|
||||
* @param int $stageId One of the WORKFLOW_STAGE_ID_* constants.
|
||||
* @param int $fileStage
|
||||
* @param bool $revisionOnly
|
||||
* @param ReviewRound $reviewRound
|
||||
* @param int $revisedFileId
|
||||
* @param int $assocType
|
||||
* @param int $assocId
|
||||
* @param int $queryId
|
||||
*/
|
||||
public function __construct(
|
||||
$request,
|
||||
$template,
|
||||
$submissionId,
|
||||
$stageId,
|
||||
$fileStage,
|
||||
$revisionOnly = false,
|
||||
$reviewRound = null,
|
||||
$revisedFileId = null,
|
||||
$assocType = null,
|
||||
$assocId = null,
|
||||
$queryId = null
|
||||
) {
|
||||
// Check the incoming parameters.
|
||||
if (!is_numeric($submissionId) || $submissionId <= 0 ||
|
||||
!is_numeric($fileStage) || $fileStage <= 0 ||
|
||||
!is_numeric($stageId) || $stageId < 1 || $stageId > 5 ||
|
||||
isset($assocType) !== isset($assocId)) {
|
||||
fatalError('Invalid parameters!');
|
||||
}
|
||||
|
||||
// Initialize class.
|
||||
parent::__construct($template);
|
||||
$this->_stageId = $stageId;
|
||||
|
||||
if ($reviewRound) {
|
||||
$this->_reviewRound = & $reviewRound;
|
||||
} elseif ($assocType == Application::ASSOC_TYPE_REVIEW_ASSIGNMENT && !$reviewRound) {
|
||||
// Get the review assignment object.
|
||||
/** @var ReviewAssignmentDAO */
|
||||
$reviewAssignmentDao = DAORegistry::getDAO('ReviewAssignmentDAO');
|
||||
/** @var \PKP\submission\reviewAssignment\ReviewAssignment */
|
||||
$reviewAssignment = $reviewAssignmentDao->getById((int) $assocId);
|
||||
if ($reviewAssignment->getDateCompleted()) {
|
||||
fatalError('Review already completed!');
|
||||
}
|
||||
|
||||
// Get the review round object.
|
||||
/** @var ReviewRoundDAO */
|
||||
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
|
||||
$this->_reviewRound = $reviewRoundDao->getById($reviewAssignment->getReviewRoundId());
|
||||
} elseif (!$assocType && !$reviewRound) {
|
||||
$reviewRound = null;
|
||||
}
|
||||
|
||||
$this->setData('fileStage', (int)$fileStage);
|
||||
$this->setData('submissionId', (int)$submissionId);
|
||||
$this->setData('revisionOnly', (bool)$revisionOnly);
|
||||
$this->setData('revisedFileId', $revisedFileId ? (int)$revisedFileId : null);
|
||||
$this->setData('reviewRoundId', $reviewRound ? $reviewRound->getId() : null);
|
||||
$this->setData('assocType', $assocType ? (int)$assocType : null);
|
||||
$this->setData('assocId', $assocId ? (int)$assocId : null);
|
||||
$this->setData('queryId', $queryId ? (int) $queryId : null);
|
||||
|
||||
// Add validators.
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Setters and Getters
|
||||
//
|
||||
/**
|
||||
* Get the workflow stage id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStageId()
|
||||
{
|
||||
return $this->_stageId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the review round object (if any).
|
||||
*
|
||||
* @return ReviewRound
|
||||
*/
|
||||
public function getReviewRound()
|
||||
{
|
||||
return $this->_reviewRound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the revised file id (if any).
|
||||
*
|
||||
* @return int the revised file id
|
||||
*/
|
||||
public function getRevisedFileId()
|
||||
{
|
||||
return $this->getData('revisedFileId') ? (int)$this->getData('revisedFileId') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the associated type
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAssocType()
|
||||
{
|
||||
return $this->getData('assocType');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the associated id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAssocId()
|
||||
{
|
||||
return $this->getData('assocId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the submission files belonging to the
|
||||
* submission and to the file stage.
|
||||
*
|
||||
* @return array a list of SubmissionFile instances.
|
||||
*/
|
||||
public function getSubmissionFiles()
|
||||
{
|
||||
if (is_null($this->_submissionFiles)) {
|
||||
if ($this->getStageId() == WORKFLOW_STAGE_ID_INTERNAL_REVIEW || $this->getStageId() == WORKFLOW_STAGE_ID_EXTERNAL_REVIEW) {
|
||||
// If we have a review stage id then we also expect a review round.
|
||||
if (!$this->getData('fileStage') == SubmissionFile::SUBMISSION_FILE_QUERY && !is_a($this->getReviewRound(), 'ReviewRound')) {
|
||||
throw new Exception('Can not request submission files for a review stage without specifying a review round.');
|
||||
}
|
||||
// Can only upload submission files, review files, review attachments, dependent files, or query attachments.
|
||||
if (!in_array($this->getData('fileStage'), [
|
||||
SubmissionFile::SUBMISSION_FILE_SUBMISSION,
|
||||
SubmissionFile::SUBMISSION_FILE_REVIEW_FILE,
|
||||
SubmissionFile::SUBMISSION_FILE_INTERNAL_REVIEW_FILE,
|
||||
SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT,
|
||||
SubmissionFile::SUBMISSION_FILE_REVIEW_REVISION,
|
||||
SubmissionFile::SUBMISSION_FILE_INTERNAL_REVIEW_REVISION,
|
||||
SubmissionFile::SUBMISSION_FILE_QUERY,
|
||||
SubmissionFile::SUBMISSION_FILE_DEPENDENT,
|
||||
SubmissionFile::SUBMISSION_FILE_ATTACHMENT,
|
||||
])) {
|
||||
throw new Exception('The file stage is not valid for the review stage.');
|
||||
}
|
||||
|
||||
// Hide the revision selector for review
|
||||
// attachments to make it easier for reviewers
|
||||
$reviewRound = $this->getReviewRound();
|
||||
if ($this->getData('fileStage') == SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT) {
|
||||
$this->_submissionFiles = [];
|
||||
} elseif ($reviewRound) {
|
||||
// Retrieve the submission files for the given review round.
|
||||
$submissionId = (int) $this->getData('submissionId');
|
||||
$submission = Repo::submission()->get($submissionId);
|
||||
if ($submission->getData('contextId') !== Application::get()->getRequest()->getContext()->getId()) {
|
||||
throw new Exception('Can not request submission files from another context.');
|
||||
}
|
||||
|
||||
$this->_submissionFiles = Repo::submissionFile()
|
||||
->getCollector()
|
||||
->filterByReviewRoundIds([(int) $reviewRound->getId()])
|
||||
->filterBySubmissionIds([$submissionId])
|
||||
->getMany()
|
||||
->toArray();
|
||||
} else {
|
||||
// No review round, e.g. for dependent or query files
|
||||
$this->_submissionFiles = [];
|
||||
}
|
||||
} else {
|
||||
$collector = Repo::submissionFile()
|
||||
->getCollector()
|
||||
->filterByFileStages([(int) $this->getData('fileStage')])
|
||||
->filterBySubmissionIds([(int) $this->getData('submissionId')]);
|
||||
if ($this->getAssocType() && $this->getAssocType() != Application::ASSOC_TYPE_SUBMISSION) {
|
||||
$collector = $collector->filterByAssoc(
|
||||
$this->getAssocType(),
|
||||
[$this->getAssocId()]
|
||||
);
|
||||
}
|
||||
|
||||
$this->_submissionFiles = $collector->getMany()->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_submissionFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the submission files possible to select/consider for revision by the given user.
|
||||
*
|
||||
* @param User $user
|
||||
* @param SubmissionFile $uploadedFile uploaded file
|
||||
*
|
||||
* @return array a list of SubmissionFile instances.
|
||||
*/
|
||||
public function getRevisionSubmissionFilesSelection($user, $uploadedFile = null)
|
||||
{
|
||||
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO'); /** @var StageAssignmentDAO $stageAssignmentDao */
|
||||
$allSubmissionFiles = $this->getSubmissionFiles();
|
||||
$submissionFiles = [];
|
||||
foreach ($allSubmissionFiles as $submissionFile) {
|
||||
// The uploaded file must be excluded from the list of revisable files.
|
||||
if ($uploadedFile && $uploadedFile->getId() == $submissionFile->getId()) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
($submissionFile->getFileStage() == SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT || $submissionFile->getFileStage() == SubmissionFile::SUBMISSION_FILE_REVIEW_FILE) &&
|
||||
$stageAssignmentDao->getBySubmissionAndRoleIds($submissionFile->getData('submissionId'), [Role::ROLE_ID_AUTHOR], $this->getStageId(), $user->getId())
|
||||
) {
|
||||
// Authors are not permitted to revise reviewer documents.
|
||||
continue;
|
||||
}
|
||||
$submissionFiles[] = $submissionFile;
|
||||
}
|
||||
return $submissionFiles;
|
||||
}
|
||||
|
||||
//
|
||||
// Implement template methods from Form
|
||||
//
|
||||
/**
|
||||
* @copydoc Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
// Only Genre and revised file can be set in the form. All other
|
||||
// information is generated on our side.
|
||||
$this->readUserVars(['revisedFileId']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
// Set the workflow stage.
|
||||
$this->setData('stageId', $this->getStageId());
|
||||
|
||||
// Set the review round id, if any.
|
||||
$reviewRound = $this->getReviewRound();
|
||||
if (is_a($reviewRound, 'ReviewRound')) {
|
||||
$this->setData('reviewRoundId', $reviewRound->getId());
|
||||
}
|
||||
|
||||
// Retrieve the uploaded file (if any).
|
||||
$uploadedFile = $this->getData('uploadedFile');
|
||||
|
||||
$user = $request->getUser();
|
||||
|
||||
// Initialize the list with files available for review.
|
||||
$submissionFileOptions = [];
|
||||
$currentSubmissionFileGenres = [];
|
||||
|
||||
// Go through all files and build a list of files available for review.
|
||||
$revisedFileId = $this->getRevisedFileId();
|
||||
$foundRevisedFile = false;
|
||||
$submissionFiles = $this->getRevisionSubmissionFilesSelection($user, $uploadedFile);
|
||||
|
||||
foreach ((array) $submissionFiles as $submissionFile) {
|
||||
// Is this the revised file?
|
||||
if ($revisedFileId && $revisedFileId == $submissionFile->getId()) {
|
||||
// This is the revised submission file, so pass its data on to the form.
|
||||
$this->setData('genreId', $submissionFile->getGenreId());
|
||||
$foundRevisedFile = true;
|
||||
}
|
||||
|
||||
// Create an entry in the list of existing files which
|
||||
// the user can select from in case he chooses to upload
|
||||
// a revision.
|
||||
$fileName = $submissionFile->getLocalizedData('name') != '' ? $submissionFile->getLocalizedData('name') : __('common.untitled');
|
||||
|
||||
$submissionFileOptions[$submissionFile->getId()] = $fileName;
|
||||
$currentSubmissionFileGenres[$submissionFile->getId()] = $submissionFile->getGenreId();
|
||||
|
||||
$lastSubmissionFile = $submissionFile;
|
||||
}
|
||||
|
||||
// If there is only one option for a file to review, and user must revise, do not show the selector.
|
||||
if (count($submissionFileOptions) == 1 && $this->getData('revisionOnly')) {
|
||||
// There was only one option, use the last added submission file
|
||||
$this->setData('revisedFileId', $lastSubmissionFile->getId());
|
||||
$this->setData('genreId', $lastSubmissionFile->getGenreId());
|
||||
}
|
||||
|
||||
// If this is not a "review only" form then add a default item.
|
||||
if (count($submissionFileOptions) && !$this->getData('revisionOnly')) {
|
||||
$submissionFileOptions = ['' => __('submission.upload.uploadNewFile')] + $submissionFileOptions;
|
||||
}
|
||||
|
||||
// Make sure that the revised file (if any) really was among
|
||||
// the retrieved submission files in the current file stage.
|
||||
if ($revisedFileId && !$foundRevisedFile) {
|
||||
fatalError('Invalid revised file id!');
|
||||
}
|
||||
|
||||
// Set the review file candidate data in the template.
|
||||
$this->setData('currentSubmissionFileGenres', $currentSubmissionFileGenres);
|
||||
$this->setData('submissionFileOptions', $submissionFileOptions);
|
||||
|
||||
// Show ensuring an anonymous review link.
|
||||
$context = $request->getContext();
|
||||
if ($context->getData('showEnsuringLink') && in_array($this->getStageId(), [WORKFLOW_STAGE_ID_SUBMISSION, WORKFLOW_STAGE_ID_INTERNAL_REVIEW, WORKFLOW_STAGE_ID_EXTERNAL_REVIEW])) {
|
||||
$ensuringLink = new LinkAction(
|
||||
'addUser',
|
||||
new ConfirmationModal(
|
||||
__('review.anonymousPeerReview'),
|
||||
__('review.anonymousPeerReview.title')
|
||||
),
|
||||
__('review.anonymousPeerReview.title')
|
||||
);
|
||||
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign('ensuringLink', $ensuringLink);
|
||||
}
|
||||
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/wizard/fileUpload/form/SubmissionFilesMetadataForm.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 SubmissionFilesMetadataForm
|
||||
*
|
||||
* @ingroup controllers_wizard_fileUpload_form
|
||||
*
|
||||
* @brief Form for editing a submission file's metadata
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\wizard\fileUpload\form;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
use PKP\submission\GenreDAO;
|
||||
use PKP\submission\reviewRound\ReviewRound;
|
||||
use PKP\submissionFile\SubmissionFile;
|
||||
|
||||
class SubmissionFilesMetadataForm extends Form
|
||||
{
|
||||
/** @var SubmissionFile */
|
||||
public $_submissionFile;
|
||||
|
||||
/** @var int */
|
||||
public $_stageId;
|
||||
|
||||
/** @var ReviewRound */
|
||||
public $_reviewRound;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SubmissionFile $submissionFile
|
||||
* @param int $stageId One of the WORKFLOW_STAGE_ID_* constants.
|
||||
* @param ReviewRound $reviewRound (optional) Current review round, if any.
|
||||
* @param string $template Path and filename to template file (optional).
|
||||
*/
|
||||
public function __construct($submissionFile, $stageId, $reviewRound = null, $template = null)
|
||||
{
|
||||
if ($template === null) {
|
||||
$template = 'controllers/wizard/fileUpload/form/submissionFileMetadataForm.tpl';
|
||||
}
|
||||
parent::__construct($template);
|
||||
|
||||
// Initialize the object.
|
||||
$this->_submissionFile = $submissionFile;
|
||||
$this->_stageId = $stageId;
|
||||
if (is_a($reviewRound, 'ReviewRound')) {
|
||||
$this->_reviewRound = $reviewRound;
|
||||
}
|
||||
|
||||
$submissionLocale = $submissionFile->getData('locale');
|
||||
$this->setDefaultFormLocale($submissionLocale);
|
||||
|
||||
// Add validation checks.
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'name', 'required', 'submission.submit.fileNameRequired', $submissionLocale));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Getters and Setters
|
||||
//
|
||||
/**
|
||||
* Get the submission file.
|
||||
*
|
||||
* @return SubmissionFile
|
||||
*/
|
||||
public function getSubmissionFile()
|
||||
{
|
||||
return $this->_submissionFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the workflow stage id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStageId()
|
||||
{
|
||||
return $this->_stageId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get review round.
|
||||
*
|
||||
* @return ReviewRound
|
||||
*/
|
||||
public function getReviewRound()
|
||||
{
|
||||
return $this->_reviewRound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "show buttons" flag
|
||||
*
|
||||
* @param bool $showButtons
|
||||
*/
|
||||
public function setShowButtons($showButtons)
|
||||
{
|
||||
$this->setData('showButtons', $showButtons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "show buttons" flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getShowButtons()
|
||||
{
|
||||
return $this->getData('showButtons');
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Implement template methods from Form
|
||||
//
|
||||
/**
|
||||
* @copydoc Form::getLocaleFieldNames()
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
return ['name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['name', 'showButtons',
|
||||
'artworkCaption', 'artworkCredit', 'artworkCopyrightOwner',
|
||||
'artworkCopyrightOwnerContact', 'artworkPermissionTerms',
|
||||
'creator', 'subject', 'description', 'publisher', 'sponsor', 'source', 'language', 'dateCreated',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$reviewRound = $this->getReviewRound();
|
||||
/** @var GenreDAO */
|
||||
$genreDao = DAORegistry::getDAO('GenreDAO');
|
||||
$genre = $genreDao->getById($this->getSubmissionFile()->getData('genreId'), $request->getContext()->getId());
|
||||
|
||||
$templateMgr->assign([
|
||||
'submissionFile' => $this->getSubmissionFile(),
|
||||
'stageId' => $this->getStageId(),
|
||||
'reviewRoundId' => $reviewRound ? $reviewRound->getId() : null,
|
||||
'supportsDependentFiles' => Repo::submissionFile()->supportsDependentFiles($this->getSubmissionFile()),
|
||||
'genre' => $genre,
|
||||
]);
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionParams)
|
||||
{
|
||||
$props = [
|
||||
'name' => $this->getData('name'),
|
||||
];
|
||||
|
||||
// Artwork metadata
|
||||
$props = array_merge($props, [
|
||||
'caption' => $this->getData('artworkCaption'),
|
||||
'credit' => $this->getData('artworkCredit'),
|
||||
'copyrightOwner' => $this->getData('artworkCopyrightOwner'),
|
||||
'terms' => $this->getData('artworkPermissionTerms'),
|
||||
]);
|
||||
|
||||
// Supplementary file metadata
|
||||
$props = array_merge($props, [
|
||||
'subject' => $this->getData('subject'),
|
||||
'creator' => $this->getData('creator'),
|
||||
'description' => $this->getData('description'),
|
||||
'publisher' => $this->getData('publisher'),
|
||||
'sponsor' => $this->getData('sponsor'),
|
||||
'source' => $this->getData('source'),
|
||||
'language' => $this->getData('language'),
|
||||
'dateCreated' => $this->getData('dateCreated'),
|
||||
]);
|
||||
|
||||
Repo::submissionFile()->edit($this->getSubmissionFile(), $props);
|
||||
$this->_submissionFile = Repo::submissionFile()->get(
|
||||
$this->getSubmissionFile()->getId()
|
||||
);
|
||||
|
||||
parent::execute(...$functionParams);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
<?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\Request;
|
||||
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\submission\GenreDAO;
|
||||
use PKP\submission\reviewRound\ReviewRound;
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user