Skip to content

Commit

Permalink
pkp#9425 Make submission language selection and metadata forms indepe…
Browse files Browse the repository at this point in the history
…ndent from website language settings
  • Loading branch information
jyhein committed Oct 27, 2023
1 parent 87c6724 commit c7684ad
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 111 deletions.
5 changes: 3 additions & 2 deletions api/v1/vocabs/PKPVocabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PKP\API\v1\vocabs;

use APP\core\Application;
use APP\facades\Repo;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
Expand Down Expand Up @@ -107,9 +108,9 @@ public function getMany(Request $illuminateRequest): JsonResponse
$vocab = $requestParams['vocab'] ?? '';
$locale = $requestParams['locale'] ?? Locale::getLocale();
$term = $requestParams['term'] ?? null;
$isSubmissionPrimaryLocale = isset($requestParams['submissionId']) && $locale === (Repo::submission()->get((int) $requestParams['submissionId']))?->getData('locale');
$locales = array_merge($context->getSupportedSubmissionMetadataLocales(), isset($requestParams['publicationId']) ? Repo::publication()->get((int) $requestParams['publicationId'])?->getLanguages() ?? [] : []);

if (!in_array($locale, $context->getData('supportedSubmissionLocales'))) {
if (!in_array($locale, $locales)) {
return response()->json([
'error' => __('api.vocabs.400.localeNotSupported', ['locale' => $locale]),
], Response::HTTP_BAD_REQUEST);
Expand Down
4 changes: 2 additions & 2 deletions classes/author/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function validate($author, $props, Submission $submission, Context $conte
{
$schemaService = Services::get('schema');
$primaryLocale = $submission->getData('locale');
$allowedLocales = $context->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);
$publicationLocales = isset($props['publicationId']) ? Repo::publication()->get($props['publicationId'])?->getLanguages() ?? [$primaryLocale] : [$primaryLocale];
$allowedLocales = array_values(array_unique(array_merge($context->getSupportedSubmissionMetadataLocales(), $publicationLocales)));

$validator = ValidatorFactory::make(
$props,
Expand Down
4 changes: 3 additions & 1 deletion classes/author/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ protected function mapByProperties(array $props, Author $item): array
}
}

$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedSubmissionMetadataLocales());
$locales = Repo::publication()->get($item->getData('publicationId'))->getLanguages($this->context->getSupportedSubmissionMetadataLocales());

$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $locales);

ksort($output);

Expand Down
20 changes: 9 additions & 11 deletions classes/context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,8 @@ public function getSupportedLocaleNames(int $langLocaleStatus = LocaleMetadata::

/**
* Get the supported added submission locales.
*
* @return array
*/
public function getSupportedAddedSubmissionLocales()
public function getSupportedAddedSubmissionLocales(): array
{
return $this->getData('supportedAddedSubmissionLocales');
}
Expand All @@ -407,42 +405,42 @@ public function getSupportedAddedSubmissionLocales()
* Return associative array of added locales supported by submissions on the
* context.
*
* @param int $langLocaleStatus The const value of one of LocaleMetadata:LANGUAGE_LOCALE_*
*
* @return array
* $langLocaleStatus The const value of one of LocaleMetadata:LANGUAGE_LOCALE_*
*/
public function getSupportedAddedSubmissionLocaleNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT)
public function getSupportedAddedSubmissionLocaleNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array
{
return $this->getData('supportedAddedSubmissionLocaleNames') ?? Locale::getFormattedDisplayNames($this->getSupportedAddedSubmissionLocales(), null, $langLocaleStatus);
return Locale::getFormattedDisplayNames($this->getSupportedAddedSubmissionLocales(), null, $langLocaleStatus);
}

/**
* Get the supported default submission locale.
*/
public function getSupportedDefaultSubmissionLocale(): ?string
public function getSupportedDefaultSubmissionLocale(): string
{
return $this->getData('supportedDefaultSubmissionLocale');
}

/**
* Return string default submission locale supported by the site.
*
* $langLocaleStatus The const value of one of LocaleMetadata:LANGUAGE_LOCALE_*
*/
public function getSupportedDefaultSubmissionLocaleName(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): string
{
return Locale::getFormattedDisplayNames($this->getSupportedDefaultSubmissionLocale(), null, $langLocaleStatus);
return Locale::getFormattedDisplayNames([$locale = $this->getSupportedDefaultSubmissionLocale()], null, $langLocaleStatus)[$locale];
}

/**
* Get the supported metadata locales.
*/
public function getSupportedSubmissionMetadataLocales(): ?array
public function getSupportedSubmissionMetadataLocales(): array
{
return $this->getData('supportedSubmissionMetadataLocales');
}

/**
* Return associative array of all locales supported by submission metadata forms on the site.
*
* $langLocaleStatus The const value of one of LocaleMetadata:LANGUAGE_LOCALE_*
*/
public function getSupportedSubmissionMetadataLocaleNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array
Expand Down
3 changes: 1 addition & 2 deletions classes/galley/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ protected function mapByProperties(array $props, Galley $galley): array
}
}

$locales = $this->context->getSupportedSubmissionMetaDataLocales();
in_array($primaryLocale = $this->submission->getData('locale'), $locales) || array_push($locales, $primaryLocale);
$locales = $this->publication->getLanguages($this->context->getSupportedSubmissionMetadataLocales());

$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $locales);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/I9425_SeparateUIAndSubmissionLocales.php
*
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I9425_SeparateUIAndSubmissionLocales
*
* @brief pkp/pkp-lib#9425 Make submission language selection and metadata forms independent from website language settings
*/

namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Support\Facades\DB;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

class I9425_SeparateUIAndSubmissionLocales extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// DB changes
$insert = function (int $journalId, string $settingName, string $settingValue) {
DB::table('journal_settings')->insert(
[
'journal_id' => $journalId,
'locale' => '',
'setting_name' => $settingName,
'setting_value' => $settingValue,
]
);
};
$pluck = fn (int $journalId, string $settingName) => DB::table('journal_settings')->where('journal_id', '=', $journalId)->where('setting_name', '=', $settingName)->pluck('setting_value')[0];

foreach (DB::table('journals')->select('primary_locale', 'journal_id')->get() as $localeId) {
// supportedDefaultSubmissionLocale from primary locale
$insert($localeId->journal_id, 'supportedDefaultSubmissionLocale', $localeId->primary_locale);
// supportedAddedSubmissionLocales from supportedLocales
$insert($localeId->journal_id, 'supportedAddedSubmissionLocales', $pluck($localeId->journal_id, 'supportedLocales'));
// supportedSubmissionMetadataLocales from supportedSubmissionLocales
$insert($localeId->journal_id, 'supportedSubmissionMetadataLocales', $pluck($localeId->journal_id, 'supportedSubmissionLocales'));
}
}

/**
* Reverse the downgrades
*/
public function down(): void
{
throw new DowngradeNotSupportedException();
}
}
38 changes: 38 additions & 0 deletions classes/publication/PKPPublication.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
namespace PKP\publication;

use APP\author\Author;
use APP\core\Services;
use APP\facades\Repo;
use PKP\core\Core;
use PKP\core\PKPString;
use PKP\facades\Locale;
use PKP\i18n\LocaleMetadata;
use PKP\services\PKPSchemaService;
use PKP\userGroup\UserGroup;

class PKPPublication extends \PKP\core\DataObject
Expand Down Expand Up @@ -431,6 +435,40 @@ public function setStoredPubId($pubIdType, $pubId)
$this->setData('pub-id::' . $pubIdType, $pubId);
}
}

/**
* Get metadata language names
*/
public function getLanguageNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array
{
return Locale::getFormattedDisplayNames($this->getLanguages(), null, $langLocaleStatus);
}

/**
* Get languages from locale, metadata, and authors' props.
* Include optional additional languages.
*
* Publication: locale, multilingual metadata props
* Authors: multilingual props
*/
public function getLanguages(array $additionalLocales = []): array
{
$getMProps = fn (string $schema) => Services::get('schema')->getMultilingualProps($schema);
$metadataprops = $getMProps(PKPSchemaService::SCHEMA_PUBLICATION);
$authorProps = $getMProps(PKPSchemaService::SCHEMA_AUTHOR);

$getlocales = fn (array $props, object $item) => array_map(fn (string $prop) => array_keys($item->getData($prop) ?? []), $props);
$metadataLocales = $getlocales($metadataprops, $this);
$authorsLocales = $this->getData('authors')?->map(fn (Author $author) => $getlocales($authorProps, $author)) ?? [];

return collect([$this->getData('locale')])
->concat($metadataLocales)
->concat($authorsLocales)
->concat($additionalLocales)
->flatten()
->unique()
->toArray();
}
}
if (!PKP_STRICT_MODE) {
class_alias('\PKP\publication\PKPPublication', '\PKPPublication');
Expand Down
9 changes: 3 additions & 6 deletions classes/publication/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public function getDateBoundaries(Collector $query): object
public function validate(?Publication $publication, array $props, Submission $submission, Context $context): array
{
$primaryLocale = $submission->getData('locale');
$allowedLocales = $context->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);
$allowedLocales = array_values(array_unique(array_merge($context->getSupportedSubmissionMetadataLocales(), $publication?->getLanguages() ?? [$primaryLocale])));

$errors = [];

Expand Down Expand Up @@ -287,8 +286,7 @@ public function add(Publication $publication): int
$submissionContext = Services::get('context')->get($submission->getData('contextId'));
}

$supportedLocales = $submissionContext->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale = $submission->getData('locale'), $supportedLocales) || array_push($supportedLocales, $primaryLocale);
$supportedLocales = $publication->getLanguages($submissionContext->getSupportedSubmissionMetadataLocales());
foreach ($supportedLocales as $localeKey) {
if (!array_key_exists($localeKey, $publication->getData('coverImage'))) {
continue;
Expand Down Expand Up @@ -385,8 +383,7 @@ public function edit(Publication $publication, array $params): Publication
$submissionContext = Services::get('context')->get($submission->getData('contextId'));
}

$supportedLocales = $submissionContext->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale = $submission->getData('locale'), $supportedLocales) || array_push($supportedLocales, $primaryLocale);
$supportedLocales = $publication->getLanguages($submissionContext->getSupportedSubmissionMetadataLocales());
foreach ($supportedLocales as $localeKey) {
if (!array_key_exists($localeKey, $params['coverImage'])) {
continue;
Expand Down
5 changes: 4 additions & 1 deletion classes/submission/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ public function validate(?Submission $submission, array $props, Context $context
{
$primaryLocale = $props['locale'] ?? $submission?->getData('locale') ?? $context->getSupportedDefaultSubmissionLocale();
$allowedLocales = $context->getSupportedSubmissionLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);

if (!in_array($primaryLocale, $allowedLocales) ) {
$allowedLocales[] = $primaryLocale;
}

$errors = [];

Expand Down
5 changes: 3 additions & 2 deletions classes/submissionFile/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ protected function mapByProperties(array $props, SubmissionFile $submissionFile)
$output[$prop] = $submissionFile->getData($prop);
}

$locales = $this->context->getSupportedSubmissionMetaDataLocales();
in_array($submissionLocale = $submissionFile->getData('locale'), $locales) || array_push($locales, $submissionLocale);
$publicationId = $submissionFile->getData('assocId') ? Repo::galley()->get($submissionFile->getData('assocId'))?->getData('publicationId') : null;
$publicationLocales = $publicationId ? Repo::publication()->get($publicationId)?->getLanguages() ?? [] : [];
$locales = array_values(array_unique(array_merge($this->context->getSupportedSubmissionMetadataLocales(), $publicationLocales, [$submissionFile->getData('locale')])));

$output = $this->schemaService->addMissingMultilingualValues(
$this->schema,
Expand Down
4 changes: 2 additions & 2 deletions controllers/api/file/PKPManageFileApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function editMetadataTab($args, $request)
$submissionFile = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION_FILE);
$reviewRound = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_REVIEW_ROUND);
$stageId = $request->getUserVar('stageId');
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $request->getContext(), $reviewRound);
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $reviewRound);
$form->setShowButtons(true);
return new JSONMessage(true, $form->fetch($request));
}
Expand All @@ -213,7 +213,7 @@ public function saveMetadata($args, $request)
$submissionFile = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION_FILE);
$reviewRound = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_REVIEW_ROUND);
$stageId = $request->getUserVar('stageId');
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $request->getContext(), $reviewRound);
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $reviewRound);
$form->readInputData();
if ($form->validate()) {
$form->execute();
Expand Down
18 changes: 3 additions & 15 deletions controllers/grid/languages/LanguageGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PKP\controllers\grid\GridColumn;
use PKP\controllers\grid\GridHandler;
use PKP\core\JSONMessage;
use PKP\db\DAO;
use PKP\facades\Locale;
use PKP\notification\PKPNotification;
use PKP\security\Role;
Expand Down Expand Up @@ -137,19 +138,6 @@ public function saveLanguageSetting($args, $request)
}
$context = $contextService->edit($context, ['supportedSubmissionLocales' => $supportedSubmissionLocales], $request);
}

if ($settingName == 'supportedSubmissionLocales') {
// If someone tried to disable all submissions checkboxes, we should display an error message.
$supportedSubmissionLocales = (array) $context->getSupportedSubmissionLocales();
$key = array_search($locale, $supportedSubmissionLocales);
if ($key !== false) {
unset($supportedSubmissionLocales[$key]);
}
$supportedSubmissionLocales = array_values($supportedSubmissionLocales);
if ($supportedSubmissionLocales == []) {
return new JSONMessage(false, __('notification.localeSettingsCannotBeSaved'));
}
}
}
}
}
Expand Down Expand Up @@ -212,7 +200,7 @@ public function setContextPrimaryLocale($args, $request)
);
}

return \PKP\db\DAO::getDataChangedEvent();
return DAO::getDataChangedEvent();
}

/**
Expand Down Expand Up @@ -252,7 +240,7 @@ public function setDefaultSubmissionLocale(array $args, Request $request): JSONM
);
}

return \PKP\db\DAO::getDataChangedEvent();
return DAO::getDataChangedEvent();
}

//
Expand Down
14 changes: 10 additions & 4 deletions controllers/grid/languages/form/AddLanguageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file controllers/grid/languages/form/AddLanguageForm.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class AddLanguageForm
Expand Down Expand Up @@ -108,8 +108,14 @@ public function execute(...$functionArgs)

if (in_array($context->getSupportedDefaultSubmissionLocale(), $removedLocales)) {
$context = $contextService->edit($context, ['supportedDefaultSubmissionLocale' => $locales[0]], $request);
(in_array($locales[0], $submToAdd) || array_push($submToAdd, $locales[0]) && sort($submToAdd));
(in_array($locales[0], $metaToAdd) || array_push($metaToAdd, $locales[0]) && sort($metaToAdd));
if (!in_array($locales[0], $submToAdd)) {
array_push($submToAdd, $locales[0]);
sort($submToAdd);
}
if (!in_array($locales[0], $metaToAdd)) {
array_push($metaToAdd, $locales[0]);
sort($metaToAdd);
}
}

$context = $contextService->edit($context, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file controllers/grid/settings/languages/SubmissionLanguageGridHandler.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2000-2023 John Willinsky
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class SubmissionLanguageGridHandler
Expand Down
2 changes: 1 addition & 1 deletion controllers/wizard/fileUpload/FileUploadWizardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public function editMetadata($args, $request)

$context = $request->getContext();
$submissionFile = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION_FILE);
$form = new SubmissionFilesMetadataForm($submissionFile, $this->getStageId(), $context, $this->getReviewRound());
$form = new SubmissionFilesMetadataForm($submissionFile, $this->getStageId(), $this->getReviewRound());
$form->initData();

/** @var GenreDAO $genreDao */
Expand Down
Loading

0 comments on commit c7684ad

Please sign in to comment.