Skip to content

Commit

Permalink
pkp/pkp-lib#8338 Partial move of old submission forms to QuickSubmit …
Browse files Browse the repository at this point in the history
…plugin
  • Loading branch information
asmecher committed Mar 9, 2023
1 parent dd63fd5 commit e7147f1
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 23 deletions.
30 changes: 12 additions & 18 deletions QuickSubmitForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,29 @@
use APP\template\TemplateManager;
use PKP\submission\PKPSubmission;
use PKP\linkAction\request\AjaxModal;
use PKP\context\Context as PKPContext;
use PKP\context\Context;
use PKP\core\PKPRequest;
use PKP\submissionFile\SubmissionFile;
use APP\submission\SubmissionMetadataFormImplementation;
use APP\plugins\importexport\quickSubmit\QuickSubmitPlugin;

class QuickSubmitForm extends Form {
/** @var Request */
protected $_request;

/** @var Submission */
protected $_submission;

/** @var Journal */
protected $_context;

/** @var SubmissionMetadataFormImplementation */
protected $_metadataFormImplem;
protected PKPRequest $_request;
protected ?PKPSubmission $_submission = null;
protected Journal $_context;
protected classes\form\SubmissionMetadataFormImplementation $_metadataFormImplem;

/**
* Constructor
* @param $plugin object
* @param $request object
*/
function __construct($plugin, $request) {
function __construct(QuickSubmitPlugin $plugin, PKPRequest $request) {
parent::__construct($plugin->getTemplateResource('index.tpl'));

$this->_request = $request;
$this->_context = $request->getContext();

$this->_metadataFormImplem = new SubmissionMetadataFormImplementation($this);
$this->_metadataFormImplem = new classes\form\SubmissionMetadataFormImplementation($this);

$locale = $request->getUserVar('locale');
if ($locale && ($locale != Locale::getLocale())) {
Expand Down Expand Up @@ -133,8 +127,8 @@ function display($request = null, $template = null) {
// 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(PKPContext::METADATA_ENABLE, PKPContext::METADATA_REQUEST, PKPContext::METADATA_REQUIRE)),
$field . 'Required' => $this->_context->getData($field) === PKPContext::METADATA_REQUIRE,
$field . 'Enabled' => in_array($this->_context->getData($field), array(Context::METADATA_ENABLE, Context::METADATA_REQUEST, Context::METADATA_REQUIRE)),
$field . 'Required' => $this->_context->getData($field) === Context::METADATA_REQUIRE,
));
}

Expand Down Expand Up @@ -173,7 +167,6 @@ function display($request = null, $template = null) {
];
})
->toArray();
error_log(print_r($sectionTitles,true));
$sectionOptions = [0 => ''] + $sectionTitles;
$templateMgr->assign('sectionOptions', $sectionOptions);

Expand Down Expand Up @@ -395,6 +388,7 @@ function execute(...$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')], $this->_request);
Expand Down
1 change: 1 addition & 0 deletions QuickSubmitPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getDescription() {
public function display($args, $request) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', array($this, 'smartyPluginUrl'));
$templateMgr->assign('quickSubmitPlugin', $this);

switch (array_shift($args)) {
case 'saveSubmit':
Expand Down
279 changes: 279 additions & 0 deletions classes/form/SubmissionMetadataFormImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
<?php

/**
* @file classes/submission/PKPSubmissionMetadataFormImplementation.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 SubmissionMetadataFormImplementation
* @ingroup submission
*
* @deprecated 3.4
*
* @brief This can be used by other forms that want to
* implement submission metadata data and form operations.
*/

namespace APP\plugins\importexport\quickSubmit\classes\form;

use APP\core\Application;
use APP\facades\Repo;
use APP\log\SubmissionEventLogEntry;
use PKP\context\Context;
use PKP\db\DAORegistry;
use PKP\log\SubmissionLog;

class SubmissionMetadataFormImplementation
{
/** @var Form Form that uses this implementation */
public $_parentForm;

/**
* Constructor.
*
* @param Form $parentForm A form that can use this form.
*/
public function __construct($parentForm = null)
{
assert($parentForm instanceof \PKP\form\Form);
$this->_parentForm = $parentForm;
}

/**
* @copydoc PKPSubmissionMetadataFormImplementation::_getAbstractsRequired
*/
public function _getAbstractsRequired($submission)
{
$section = Repo::section()->get($submission->getCurrentPublication()->getData('sectionId'), $submission->getData('contextId'));
return !$section->getAbstractsNotRequired();
}

/**
* Add checks to form.
*
* @param Submission $submission
*/
public function addChecks($submission)
{
// Validation checks.
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorLocale($this->_parentForm, 'title', 'required', 'submission.submit.form.titleRequired', $submission->getCurrentPublication()->getData('locale')));
if ($this->_getAbstractsRequired($submission)) {
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorLocale($this->_parentForm, 'abstract', 'required', 'submission.submit.form.abstractRequired', $submission->getCurrentPublication()->getData('locale')));
}

// Validates that at least one author has been added.
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorCustom(
$this->_parentForm,
'authors',
'required',
'submission.submit.form.authorRequired',
function () use ($submission) {
return !empty($submission->getCurrentPublication()->getData('authors'));
}
));

$contextDao = Application::getContextDao();
$context = $contextDao->getById($submission->getContextId());
$metadataFields = Application::getMetadataFields();
foreach ($metadataFields as $field) {
$requiredLocaleKey = 'submission.submit.form.' . $field . 'Required';
if ($context->getData($field) === Context::METADATA_REQUIRE) {
switch ($field) {
case in_array($field, $this->getLocaleFieldNames()):
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorLocale($this->_parentForm, $field, 'required', $requiredLocaleKey, $submission->getCurrentPublication()->getData('locale')));
break;
case in_array($field, $this->getTagitFieldNames()):
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorCustom($this->_parentForm, $field, 'required', $requiredLocaleKey, function ($field, $form, $name) {
$data = (array) $form->getData('keywords');
return array_key_exists($name, $data);
}, [$this->_parentForm, $submission->getCurrentPublication()->getData('locale') . '-' . $field]));
break;
case 'citations':
$form = $this->_parentForm;
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorCustom($this->_parentForm, 'citationsRaw', 'required', $requiredLocaleKey, function ($key) use ($form) {
return !empty($form->getData('citationsRaw'));
}));
break;
default:
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidator($this->_parentForm, $field, 'required', $requiredLocaleKey));
}
}
}

$section = Repo::section()->get($submission->getCurrentPublication()->getData('sectionId'), $submission->getData('contextId'));
$wordCount = $section->getAbstractWordCount();
if (isset($wordCount) && $wordCount > 0) {
$this->_parentForm->addCheck(new \PKP\form\validation\FormValidatorCustom($this->_parentForm, 'abstract', 'required', 'submission.submit.form.wordCountAlert', function ($abstract) use ($wordCount) {
foreach ($abstract as $localizedAbstract) {
if (count(preg_split('/\s+/', trim(str_replace('&nbsp;', ' ', strip_tags($localizedAbstract))))) > $wordCount) {
return false;
}
}
return true;
}));
}
}

/**
* Initialize form data from current submission.
*
* @param Submission $submission
*/
public function initData($submission)
{
if (isset($submission)) {
$publication = $submission->getCurrentPublication();
$formData = [
'title' => $publication->getData('title'),
'prefix' => $publication->getData('prefix'),
'subtitle' => $publication->getData('subtitle'),
'abstract' => $publication->getData('abstract'),
'coverage' => $publication->getData('coverage'),
'type' => $publication->getData('type'),
'source' => $publication->getData('source'),
'rights' => $publication->getData('rights'),
'citationsRaw' => $publication->getData('citationsRaw'),
'locale' => $publication->getData('locale'),
'dataAvailability' => $publication->getData('dataAvailability'),
];

foreach ($formData as $key => $data) {
$this->_parentForm->setData($key, $data);
}

// get the supported locale keys
$locales = array_keys($this->_parentForm->supportedLocales);

// load the persisted metadata controlled vocabularies
$submissionKeywordDao = DAORegistry::getDAO('SubmissionKeywordDAO'); /** @var SubmissionKeywordDAO $submissionKeywordDao */
$submissionSubjectDao = DAORegistry::getDAO('SubmissionSubjectDAO'); /** @var SubmissionSubjectDAO $submissionSubjectDao */
$submissionDisciplineDao = DAORegistry::getDAO('SubmissionDisciplineDAO'); /** @var SubmissionDisciplineDAO $submissionDisciplineDao */
$submissionAgencyDao = DAORegistry::getDAO('SubmissionAgencyDAO'); /** @var SubmissionAgencyDAO $submissionAgencyDao */
$submissionLanguageDao = DAORegistry::getDAO('SubmissionLanguageDAO'); /** @var SubmissionLanguageDAO $submissionLanguageDao */

$this->_parentForm->setData('subjects', $submissionSubjectDao->getSubjects($publication->getId(), $locales));
$this->_parentForm->setData('keywords', $submissionKeywordDao->getKeywords($publication->getId(), $locales));
$this->_parentForm->setData('disciplines', $submissionDisciplineDao->getDisciplines($publication->getId(), $locales));
$this->_parentForm->setData('agencies', $submissionAgencyDao->getAgencies($publication->getId(), $locales));
$this->_parentForm->setData('languages', $submissionLanguageDao->getLanguages($publication->getId(), $locales));
$this->_parentForm->setData('abstractsRequired', $this->_getAbstractsRequired($submission));
}
}

/**
* Assign form data to user-submitted data.
*/
public function readInputData()
{
// 'keywords' is a tagit catchall that contains an array of values for each keyword/locale combination on the form.
$userVars = ['title', 'prefix', 'subtitle', 'abstract', 'coverage', 'type', 'source', 'rights', 'keywords', 'citationsRaw', 'locale', 'dataAvailability'];
$this->_parentForm->readUserVars($userVars);
}

/**
* Get the names of fields for which data should be localized
*
* @return array
*/
public function getLocaleFieldNames()
{
return ['title', 'prefix', 'subtitle', 'abstract', 'coverage', 'type', 'source', 'rights', 'dataAvailability'];
}

/**
* Get the names of fields for which tagit is used
*
* @return array
*/
public function getTagitFieldNames()
{
return ['subjects', 'keywords', 'disciplines', 'agencies', 'languages'];
}

/**
* Save changes to submission.
*
* @param Submission $submission
* @param PKPRequest $request
*
* @return Submission
*/
public function execute($submission, $request)
{
$publication = $submission->getCurrentPublication();
$context = $request->getContext();

// Get params to update
$params = [
'title' => $this->_parentForm->getData('title'),
'prefix' => $this->_parentForm->getData('prefix'),
'subtitle' => $this->_parentForm->getData('subtitle'),
'abstract' => $this->_parentForm->getData('abstract'),
'coverage' => $this->_parentForm->getData('coverage'),
'type' => $this->_parentForm->getData('type'),
'rights' => $this->_parentForm->getData('rights'),
'source' => $this->_parentForm->getData('source'),
'citationsRaw' => $this->_parentForm->getData('citationsRaw'),
'dataAvailability' => $this->_parentForm->getData('dataAvailability'),
];

// Update locale
$newLocale = $this->_parentForm->getData('locale');
if ($newLocale) {
$oldLocale = $publication->getData('locale');
if (in_array($newLocale, $context->getData('supportedSubmissionLocales'))) {
$params['locale'] = $newLocale;
}
if ($newLocale !== $oldLocale) {
Repo::author()->changePublicationLocale($publication->getId(), $oldLocale, $newLocale);
}
}

// Save the publication
Repo::publication()->edit($publication, $params);
$publication = Repo::publication()->get($publication->getId());

// get the supported locale keys
$locales = array_keys($this->_parentForm->supportedLocales);

// persist the metadata/keyword fields.
$submissionKeywordDao = DAORegistry::getDAO('SubmissionKeywordDAO'); /** @var SubmissionKeywordDAO $submissionKeywordDao */
$submissionSubjectDao = DAORegistry::getDAO('SubmissionSubjectDAO'); /** @var SubmissionSubjectDAO $submissionSubjectDao */
$submissionDisciplineDao = DAORegistry::getDAO('SubmissionDisciplineDAO'); /** @var SubmissionDisciplineDAO $submissionDisciplineDao */
$submissionAgencyDao = DAORegistry::getDAO('SubmissionAgencyDAO'); /** @var SubmissionAgencyDAO $submissionAgencyDao */
$submissionLanguageDao = DAORegistry::getDAO('SubmissionLanguageDAO'); /** @var SubmissionLanguageDAO $submissionLanguageDao */

$keywords = [];
$agencies = [];
$disciplines = [];
$languages = [];
$subjects = [];

$tagitKeywords = $this->_parentForm->getData('keywords');

if (is_array($tagitKeywords)) {
foreach ($locales as $locale) {
$keywords[$locale] = array_key_exists($locale . '-keywords', $tagitKeywords) ? $tagitKeywords[$locale . '-keywords'] : [];
$agencies[$locale] = array_key_exists($locale . '-agencies', $tagitKeywords) ? $tagitKeywords[$locale . '-agencies'] : [];
$disciplines[$locale] = array_key_exists($locale . '-disciplines', $tagitKeywords) ? $tagitKeywords[$locale . '-disciplines'] : [];
$languages[$locale] = array_key_exists($locale . '-languages', $tagitKeywords) ? $tagitKeywords[$locale . '-languages'] : [];
$subjects[$locale] = array_key_exists($locale . '-subjects', $tagitKeywords) ? $tagitKeywords[$locale . '-subjects'] : [];
}
}

// persist the controlled vocabs
$submissionKeywordDao->insertKeywords($keywords, $submission->getCurrentPublication()->getId());
$submissionAgencyDao->insertAgencies($agencies, $submission->getCurrentPublication()->getId());
$submissionDisciplineDao->insertDisciplines($disciplines, $submission->getCurrentPublication()->getId());
$submissionLanguageDao->insertLanguages($languages, $submission->getCurrentPublication()->getId());
$submissionSubjectDao->insertSubjects($subjects, $submission->getCurrentPublication()->getId());

// Only log modifications on completed submissions
if (!$submission->getSubmissionProgress()) {
// Log the metadata modification event.
SubmissionLog::logEvent($request, $submission, SubmissionEventLogEntry::SUBMISSION_LOG_METADATA_UPDATE, 'submission.event.general.metadataUpdated');
}
}
}
8 changes: 3 additions & 5 deletions templates/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@
{/fbvFormSection}
{/if}

{include file="submission/form/section.tpl" sectionOptions=$sectionOptions readOnly=$formParams.readOnly}

{include file="core:submission/submissionMetadataFormTitleFields.tpl"}
{include file="submission/submissionMetadataFormFields.tpl"}

{include file=$quickSubmitPlugin->getTemplateResource("section.tpl") sectionOptions=$sectionOptions readOnly=$formParams.readOnly}
{include file=$quickSubmitPlugin->getTemplateResource("submissionMetadataFormTitleFields.tpl")}
{include file=$quickSubmitPlugin->getTemplateResource("submissionMetadataFormFields.tpl")}

{fbvFormArea id="contributors"}
<!-- Contributors -->
Expand Down
22 changes: 22 additions & 0 deletions templates/section.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{**
* templates/submission/form/section.tpl
*
* 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.
*
* Include section placement for submissions.
*
* @deprecated 3.4
*}
{assign var=sectionDescription value=""}
{if !$readOnly}
{assign var=sectionDescription value="author.submit.journalSectionDescription"}
{/if}
{fbvFormSection title="section.section"}
{fbvElement type="select" id="sectionId" label=$sectionDescription from=$sectionOptions selected=$sectionId translate=false disabled=$readOnly size=$fbvStyles.size.MEDIUM required=true}
{/fbvFormSection}

{foreach from=$sectionPolicies key="sectionPolicySectionId" item="content"}
{include file="submission/form/sectionPolicy.tpl" sectionId=$sectionPolicySectionId content=$content}
{/foreach}
Loading

0 comments on commit e7147f1

Please sign in to comment.