diff --git a/api/v1/vocabs/PKPVocabController.php b/api/v1/vocabs/PKPVocabController.php index 6fc2dcef8a7..74fef96f4ff 100644 --- a/api/v1/vocabs/PKPVocabController.php +++ b/api/v1/vocabs/PKPVocabController.php @@ -108,7 +108,7 @@ public function getMany(Request $illuminateRequest): JsonResponse $vocab = $requestParams['vocab'] ?? ''; $locale = $requestParams['locale'] ?? Locale::getLocale(); $term = $requestParams['term'] ?? null; - $locales = array_merge($context->getSupportedSubmissionMetadataLocales(), isset($requestParams['publicationId']) ? Repo::publication()->get((int) $requestParams['publicationId'])?->getLanguages() ?? [] : []); + $locales = array_merge($context->getSupportedSubmissionMetadataLocales(), isset($requestParams['submissionId']) ? Repo::submission()->get((int) $requestParams['submissionId'])?->getPublicationLanguages() ?? [] : []); if (!in_array($locale, $locales)) { return response()->json([ diff --git a/classes/components/listPanels/ContributorsListPanel.php b/classes/components/listPanels/ContributorsListPanel.php index f1e0e6a3455..6071e599d3b 100644 --- a/classes/components/listPanels/ContributorsListPanel.php +++ b/classes/components/listPanels/ContributorsListPanel.php @@ -107,8 +107,6 @@ protected function getPublicationUrlFormat(): string */ protected function getLocalizedForm(): array { - usort($this->locales, fn ($a, $b) => $a['key'] === $this->submission->getData('locale') ? -1 : 1); - $apiUrl = Application::get()->getRequest()->getDispatcher()->url( Application::get()->getRequest(), Application::ROUTE_API, @@ -116,11 +114,15 @@ protected function getLocalizedForm(): array 'submissions/' . $this->submission->getId() . '/publications/__publicationId__/contributors' ); - $form = $this->getForm($apiUrl); + $submissionLocale = $this->submission->getData('locale'); + $data = $this->getForm($apiUrl)->getConfig(); - $data = $form->getConfig(); - $data['primaryLocale'] = $this->submission->getData('locale'); - $data['visibleLocales'] = [$this->submission->getData('locale')]; + $data['primaryLocale'] = $submissionLocale; + $data['visibleLocales'] = [$submissionLocale]; + $data['supportedFormLocales'] = collect($this->locales) + ->sortBy([fn (array $a, array $b) => $b['key'] === $submissionLocale ? 1 : -1]) + ->values() + ->toArray(); return $data; } diff --git a/classes/galley/Galley.php b/classes/galley/Galley.php index 268e0377898..a3b35cec100 100644 --- a/classes/galley/Galley.php +++ b/classes/galley/Galley.php @@ -16,8 +16,11 @@ namespace PKP\galley; +use APP\core\Services; use APP\facades\Repo; use PKP\facades\Locale; +use PKP\i18n\LocaleMetadata; +use PKP\services\PKPSchemaService; use PKP\submission\Representation; use PKP\submissionFile\SubmissionFile; @@ -183,6 +186,29 @@ public function setStoredPubId($pubIdType, $pubId) parent::setStoredPubId($pubIdType, $pubId); } } + + /** + * Get metadata language names + */ + public function getLanguageNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array + { + return Locale::getFormattedDisplayNames($this->getLanguages(), null, $langLocaleStatus); + } + + /** + * Get metadata languages + */ + public function getLanguages(): array + { + $props = Services::get('schema')->getMultilingualProps(PKPSchemaService::SCHEMA_GALLEY); + $locales = array_map(fn (string $prop) => array_keys($this->getData($prop) ?? []), $props); + return collect([$this->getData('locale')]) + ->concat($locales) + ->flatten() + ->unique() + ->values() + ->toArray(); + } } if (!PKP_STRICT_MODE) { diff --git a/classes/galley/maps/Schema.php b/classes/galley/maps/Schema.php index b13e478988f..6ae2e3a4040 100644 --- a/classes/galley/maps/Schema.php +++ b/classes/galley/maps/Schema.php @@ -140,7 +140,7 @@ protected function mapByProperties(array $props, Galley $galley): array } } - $locales = $this->publication->getLanguages($this->context->getSupportedSubmissionMetadataLocales()); + $locales = $this->publication->getLanguages(array_merge($this->context->getSupportedSubmissionMetadataLocales(), $galley->getLanguages())); $output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $locales); diff --git a/classes/migration/upgrade/v3_5_0/I9425_SeparateUIAndSubmissionLocales.php b/classes/migration/upgrade/v3_5_0/I9425_SeparateUIAndSubmissionLocales.php index 51abe0c4331..d5ea553067d 100644 --- a/classes/migration/upgrade/v3_5_0/I9425_SeparateUIAndSubmissionLocales.php +++ b/classes/migration/upgrade/v3_5_0/I9425_SeparateUIAndSubmissionLocales.php @@ -30,28 +30,46 @@ abstract protected function getContextIdColumn(): string; public function up(): void { // DB changes - $insert = function (int $contextId, string $settingName, string $settingValue) { + $insert = function (object $localeId, string $settingName, string $settingValue): void { DB::table($this->getContextSettingsTable())->insert( [ - $this->getContextIdColumn() => $contextId, + $this->getContextIdColumn() => $localeId->{$this->getContextIdColumn()}, 'locale' => '', 'setting_name' => $settingName, 'setting_value' => $settingValue, ] ); }; - $pluck = fn (int $contextId, string $settingName) => DB::table($this->getContextSettingsTable()) - ->where($this->getContextIdColumn(), '=', $contextId) + $update = function (object $localeId, string $settingName, string $settingValue): void { + DB::table($this->getContextSettingsTable()) + ->where($this->getContextIdColumn(), '=', $localeId->{$this->getContextIdColumn()}) ->where('setting_name', '=', $settingName) - ->pluck('setting_value')[0]; + ->update(['setting_value' => $settingValue]); + }; + + $pluck = fn (object $localeId, string $settingName): array => json_decode( + DB::table($this->getContextSettingsTable()) + ->where($this->getContextIdColumn(), '=', $localeId->{$this->getContextIdColumn()}) + ->where('setting_name', '=', $settingName) + ->pluck('setting_value')[0] + ); + + $union = fn (array $a0, array $a1): string => collect($a0)->concat($a1)->unique()->sort()->values()->toJson(); foreach (DB::table($this->getContextTable())->select('primary_locale', $this->getContextIdColumn())->get() as $localeId) { + // Add primary locale to form locales + $formLocales = $union([$localeId->primary_locale], $pluck($localeId, 'supportedFormLocales')); + $update($localeId, 'supportedFormLocales', $formLocales); + // Add primary locale to submission locales + $submissionLocales = $union([$localeId->primary_locale], $pluck($localeId, 'supportedSubmissionLocales')); + $update($localeId, 'supportedSubmissionLocales', $submissionLocales); // supportedDefaultSubmissionLocale from primary locale - $insert($localeId->{$this->getContextIdColumn()}, 'supportedDefaultSubmissionLocale', $localeId->primary_locale); - // supportedAddedSubmissionLocales from supportedLocales - $insert($localeId->{$this->getContextIdColumn()}, 'supportedAddedSubmissionLocales', $pluck($localeId->{$this->getContextIdColumn()}, 'supportedLocales')); - // supportedSubmissionMetadataLocales from supportedSubmissionLocales - $insert($localeId->{$this->getContextIdColumn()}, 'supportedSubmissionMetadataLocales', $pluck($localeId->{$this->getContextIdColumn()}, 'supportedSubmissionLocales')); + $insert($localeId, 'supportedDefaultSubmissionLocale', $localeId->primary_locale); + // supportedAddedSubmissionLocales from supportedSubmissionLocales and supportedFormLocales + $submFormLocales = $union(json_decode($submissionLocales), json_decode($formLocales)); + $insert($localeId, 'supportedAddedSubmissionLocales', $submFormLocales); + // supportedSubmissionMetadataLocales from supportedSubmissionLocales and supportedFormLocales + $insert($localeId, 'supportedSubmissionMetadataLocales', $submFormLocales); } } diff --git a/classes/submission/PKPSubmission.php b/classes/submission/PKPSubmission.php index ea83b06a829..75a804390e0 100644 --- a/classes/submission/PKPSubmission.php +++ b/classes/submission/PKPSubmission.php @@ -32,6 +32,7 @@ use Illuminate\Support\LazyCollection; use PKP\core\Core; use PKP\facades\Locale; +use PKP\i18n\LocaleMetadata; /** * @extends \PKP\core\DataObject @@ -213,6 +214,29 @@ public function getDAO(): DAO return Repo::submission()->dao; } + /** + * Get metadata language names from publications + */ + public function getPublicationLanguageNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array + { + return collect($this->getData('publications')) + ->flatMap(fn (Publication $p): array => $p->getLanguageNames($langLocaleStatus)) + ->toArray(); + } + + /** + * Get metadata languages from publications + */ + public function getPublicationLanguages(): array + { + return $this->getData('publications') + ->map(fn (Publication $p): array => $p->getLanguages()) + ->flatten() + ->unique() + ->values() + ->toArray(); + } + // // Abstract methods. // diff --git a/classes/submissionFile/SubmissionFile.php b/classes/submissionFile/SubmissionFile.php index bdd41e612d9..ca640e8bb55 100644 --- a/classes/submissionFile/SubmissionFile.php +++ b/classes/submissionFile/SubmissionFile.php @@ -16,7 +16,11 @@ namespace PKP\submissionFile; +use APP\core\Services; use APP\facades\Repo; +use PKP\facades\Locale; +use PKP\i18n\LocaleMetadata; +use PKP\services\PKPSchemaService; /** * @extends \PKP\core\DataObject @@ -381,6 +385,29 @@ public function getDAO(): DAO { return Repo::submissionFile()->dao; } + + /** + * Get metadata language names + */ + public function getLanguageNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT): array + { + return Locale::getFormattedDisplayNames($this->getLanguages(), null, $langLocaleStatus); + } + + /** + * Get metadata languages + */ + public function getLanguages(): array + { + $props = Services::get('schema')->getMultilingualProps(PKPSchemaService::SCHEMA_SUBMISSION_FILE); + $locales = array_map(fn (string $prop) => array_keys($this->getData($prop) ?? []), $props); + return collect([$this->getData('locale')]) + ->concat($locales) + ->flatten() + ->unique() + ->values() + ->toArray(); + } } if (!PKP_STRICT_MODE) { diff --git a/classes/submissionFile/maps/Schema.php b/classes/submissionFile/maps/Schema.php index 37e0fbce086..9f9fcac10b2 100644 --- a/classes/submissionFile/maps/Schema.php +++ b/classes/submissionFile/maps/Schema.php @@ -226,16 +226,14 @@ protected function mapByProperties(array $props, SubmissionFile $submissionFile) $output[$prop] = $submissionFile->getData($prop); } - $publicationLocales = !($assocId = $submissionFile->getData('assocId')) - ? [$submissionFile->getData('locale')] - : Repo::submission() - ->get($submissionFile->getData('submissionId')) - ->getData('publications') - ->first(fn ($p) => collect($p->getData(Application::get()->getName() === 'omp' ? 'publicationFormats' : 'galleys')) - ->contains(fn ($item) => $item->getId() === $assocId)) - ->getLanguages(); - - $locales = array_values(array_unique(array_merge($this->context->getSupportedSubmissionMetadataLocales(), $publicationLocales))); + $submission = Repo::submission()->get($submissionFile->getData('submissionId')); + $publicationLanguages = $submission->getData('publications') + ->first(fn ($p) => collect($p->getData(Application::get()->getName() === 'omp' ? 'publicationFormats' : 'galleys') ?? []) + ->contains(fn ($item) => $item->getId() === $submissionFile->getData('assocId'))) + ?->getLanguages() + ?? $submission->getPublicationLanguages(); + + $locales = array_values(array_unique(array_merge($this->context->getSupportedSubmissionMetadataLocales(), $publicationLanguages, $submissionFile->getLanguages()))); $output = $this->schemaService->addMissingMultilingualValues( $this->schema, diff --git a/controllers/grid/languages/LanguageGridHandler.php b/controllers/grid/languages/LanguageGridHandler.php index f5bb1edd897..6f967231f53 100644 --- a/controllers/grid/languages/LanguageGridHandler.php +++ b/controllers/grid/languages/LanguageGridHandler.php @@ -179,13 +179,13 @@ public function setContextPrimaryLocale($args, $request) if (Locale::isLocaleValid($locale) && array_key_exists($locale, $availableLocales)) { // Make sure at least the primary locale is chosen as available - foreach (['supportedLocales', 'supportedFormLocales'] as $name) { - $$name = $context->getData($name); - if (!in_array($locale, $$name)) { - array_push($$name, $locale); - $context->updateSetting($name, $$name); - } - } + $context = Services::get('context')->edit( + $context, + collect(['supportedLocales', 'supportedFormLocales']) + ->mapWithKeys(fn ($name) => [$name => collect($context->getData($name))->push($locale)->unique()->sort()->values()]) + ->toArray(), + $request + ); $context->setPrimaryLocale($locale); $contextDao = Application::getContextDAO(); @@ -213,23 +213,20 @@ public function setDefaultSubmissionLocale(array $args, Request $request): JSONM } $locale = (string) $request->getUserVar('rowId'); $context = $request->getContext(); - $contextService = Services::get('context'); $availableLocales = $this->getGridDataElements($request); if (Locale::isLocaleValid($locale) && array_key_exists($locale, $availableLocales)) { // Make sure at least the primary locale is chosen as available - foreach (['supportedSubmissionLocales', 'supportedSubmissionMetadataLocales'] as $name) { - $$name = $context->getData($name); - if (!in_array($locale, $$name)) { - array_push($$name, $locale); - $context = $contextService->edit($context, [$name => $$name], $request); - } - } - - $context = $contextService->edit($context, ['supportedDefaultSubmissionLocale' => $locale], $request); - - $contextDao = Application::getContextDAO(); - $contextDao->updateObject($context); + Services::get('context')->edit( + $context, + [ + 'supportedDefaultSubmissionLocale' => $locale, + ...collect(['supportedSubmissionLocales', 'supportedSubmissionMetadataLocales']) + ->mapWithKeys(fn ($name) => [$name => collect($context->getData($name))->push($locale)->unique()->sort()->values()]) + ->toArray(), + ], + $request + ); $notificationManager = new NotificationManager(); $user = $request->getUser(); diff --git a/controllers/grid/languages/form/AddLanguageForm.php b/controllers/grid/languages/form/AddLanguageForm.php index ea067d5ba50..1da4d9a3078 100644 --- a/controllers/grid/languages/form/AddLanguageForm.php +++ b/controllers/grid/languages/form/AddLanguageForm.php @@ -101,30 +101,27 @@ public function execute(...$functionArgs) if (!empty(count($locales))) { sort($locales); - $contextService = Services::get('context'); - $removedLocales = array_diff($context->getSupportedAddedSubmissionLocales(), $locales); - $submToAdd = array_values(array_diff($context->getSupportedSubmissionLocales(), $removedLocales)); - $metaToAdd = array_values(array_diff($context->getSupportedSubmissionMetadataLocales(), $removedLocales)); - - if (in_array($context->getSupportedDefaultSubmissionLocale(), $removedLocales)) { - $context = $contextService->edit($context, ['supportedDefaultSubmissionLocale' => $locales[0]], $request); - 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, [ - 'supportedAddedSubmissionLocales' => $locales, - 'supportedSubmissionLocales' => $submToAdd, - 'supportedSubmissionMetadataLocales' => $metaToAdd, - ], $request); - - (Application::getContextDAO())->updateObject($context); + $removedLocales = array_values(array_diff($context->getSupportedAddedSubmissionLocales(), $locales)); + $defaultLocaleRemoved = in_array($context->getSupportedDefaultSubmissionLocale(), $removedLocales); + + $edit = fn (array $ll): array => collect($ll) + ->diff($removedLocales) + ->concat($defaultLocaleRemoved ? [$locales[0]] : []) + ->unique() + ->sort() + ->values() + ->toArray(); + + Services::get('context')->edit( + $context, + [ + 'supportedAddedSubmissionLocales' => $locales, + 'supportedSubmissionLocales' => $edit($context->getSupportedSubmissionLocales()), + 'supportedSubmissionMetadataLocales' => $edit($context->getSupportedSubmissionMetadataLocales()), + ...($defaultLocaleRemoved ? ['supportedDefaultSubmissionLocale' => $locales[0]] : []), + ], + $request + ); } } } diff --git a/controllers/wizard/fileUpload/form/SubmissionFilesMetadataForm.php b/controllers/wizard/fileUpload/form/SubmissionFilesMetadataForm.php index 868492f9680..5d39b2b2a72 100644 --- a/controllers/wizard/fileUpload/form/SubmissionFilesMetadataForm.php +++ b/controllers/wizard/fileUpload/form/SubmissionFilesMetadataForm.php @@ -51,13 +51,14 @@ public function __construct($submissionFile, $stageId, $reviewRound = null, $tem } $submissionLocale = $submissionFile->getData('locale'); - $publicationLocaleNames = Repo::submission() - ->get($submissionFile->getData('submissionId')) - ->getData('publications') - ->first(fn ($p) => collect($p->getData(Application::get()->getName() === 'omp' ? 'publicationFormats' : 'galleys')) + $submission = Repo::submission()->get($submissionFile->getData('submissionId')); + $publicationLanguageNames = $submission->getData('publications') + ->first(fn ($p) => collect($p->getData(Application::get()->getName() === 'omp' ? 'publicationFormats' : 'galleys') ?? []) ->contains(fn ($item) => $item->getId() === $submissionFile->getData('assocId'))) - ->getLanguageNames(); - $localeNames = Application::get()->getRequest()->getContext()->getSupportedSubmissionMetadataLocaleNames() + $publicationLocaleNames; + ?->getLanguageNames() + ?? $submission->getPublicationLanguageNames(); + + $localeNames = Application::get()->getRequest()->getContext()->getSupportedSubmissionMetadataLocaleNames() + $publicationLanguageNames + $submissionFile->getLanguageNames(); ksort($localeNames); parent::__construct($template, true, $submissionLocale, $localeNames); diff --git a/pages/authorDashboard/PKPAuthorDashboardHandler.php b/pages/authorDashboard/PKPAuthorDashboardHandler.php index f0cd6fd0110..694ac4001fa 100644 --- a/pages/authorDashboard/PKPAuthorDashboardHandler.php +++ b/pages/authorDashboard/PKPAuthorDashboardHandler.php @@ -218,7 +218,8 @@ public function setupTemplate($request) $latestPublication = $submission->getLatestPublication(); - $locales = collect($submissionContext->getSupportedSubmissionMetadataLocaleNames() + $latestPublication->getLanguageNames()) + $submissionLocale = $submission->getData('locale'); + $locales = collect($submissionContext->getSupportedSubmissionMetadataLocaleNames() + $submission->getPublicationLanguageNames()) ->map(fn (string $name, string $locale) => ['key' => $locale, 'label' => $name]) ->sortBy('key') ->values() @@ -305,8 +306,8 @@ public function setupTemplate($request) $state = [ 'canEditPublication' => $canEditPublication, 'components' => [ - FORM_TITLE_ABSTRACT => $this->getLocalizedForm($titleAbstractForm, $latestPublication, $submissionContext), - FORM_CITATIONS => $this->getLocalizedForm($citationsForm, $latestPublication, $submissionContext), + FORM_TITLE_ABSTRACT => $this->getLocalizedForm($titleAbstractForm, $submissionLocale, $locales), + FORM_CITATIONS => $this->getLocalizedForm($citationsForm, $submissionLocale, $locales), $contributorsListPanel->id => $contributorsListPanel->getConfig(), ], 'currentPublication' => $currentPublicationProps, @@ -329,7 +330,7 @@ public function setupTemplate($request) ]; // Add the metadata form if one or more metadata fields are enabled - $vocabSuggestionUrlBase = $request->getDispatcher()->url($request, PKPApplication::ROUTE_API, $submissionContext->getData('urlPath'), 'vocabs', null, null, ['vocab' => '__vocab__', 'publicationId' => $latestPublication->getId()]); + $vocabSuggestionUrlBase = $request->getDispatcher()->url($request, PKPApplication::ROUTE_API, $submissionContext->getData('urlPath'), 'vocabs', null, null, ['vocab' => '__vocab__', 'submissionId' => $submission->getId()]); $metadataForm = new PKPMetadataForm($latestPublicationApiUrl, $locales, $latestPublication, $submissionContext, $vocabSuggestionUrlBase, true); $metadataEnabled = count($metadataForm->fields); @@ -337,7 +338,7 @@ public function setupTemplate($request) $templateMgr->setConstants([ 'FORM_METADATA' => FORM_METADATA, ]); - $state['components'][FORM_METADATA] = $this->getLocalizedForm($metadataForm, $latestPublication, $submissionContext); + $state['components'][FORM_METADATA] = $this->getLocalizedForm($metadataForm, $submissionLocale, $locales); $state['publicationFormIds'][] = FORM_METADATA; } @@ -374,26 +375,23 @@ protected function getContributorsListPanel(Submission $submission, Context $con /** * Get the form configuration data with the correct - * locale settings based on the publication's locale + * locale settings based on the submission's locale * - * Uses the publication locale as the primary and + * Uses the submission locale as the primary and * visible locale, and puts that locale first in the * list of supported locales. * * Call this instead of $form->getConfig() to display - * a form with the correct publication locales + * a form with the correct submission's publication locales */ - protected function getLocalizedForm(\PKP\components\forms\FormComponent $form, Publication $publication, Context $context): array + protected function getLocalizedForm(\PKP\components\forms\FormComponent $form, string $submissionLocale, array $locales): array { $config = $form->getConfig(); - $primaryLocale = $publication->getData('locale'); - $config['primaryLocale'] = $primaryLocale; - $config['visibleLocales'] = [$primaryLocale]; - - $config['supportedFormLocales'] = collect($context->getSupportedSubmissionMetadataLocaleNames() + $publication->getLanguageNames()) - ->map(fn (string $name, string $locale) => ['key' => $locale, 'label' => $name]) - ->sortBy([fn (array $a, array $b) => $a['key'] === $primaryLocale || $a['key'] < $b['key'] ? -1 : 1]) + $config['primaryLocale'] = $submissionLocale; + $config['visibleLocales'] = [$submissionLocale]; + $config['supportedFormLocales'] = collect($locales) + ->sortBy([fn (array $a, array $b) => $b['key'] === $submissionLocale ? 1 : -1]) ->values() ->toArray(); diff --git a/pages/submission/PKPSubmissionHandler.php b/pages/submission/PKPSubmissionHandler.php index f5876c70e3c..1760d3ef7e3 100644 --- a/pages/submission/PKPSubmissionHandler.php +++ b/pages/submission/PKPSubmissionHandler.php @@ -186,12 +186,16 @@ protected function showWizard(array $args, Request $request, Submission $submiss } - $supportedSubmissionMetadataLocales = $context->getSupportedSubmissionMetadataLocaleNames(); - $formLocales = array_map(fn (string $locale, string $name) => ['key' => $locale, 'label' => $name], array_keys($supportedSubmissionMetadataLocales), $supportedSubmissionMetadataLocales); + $supportedLocales = $context->getSupportedSubmissionMetadataLocaleNames() + $publication->getLanguageNames(); + $formLocales = collect($supportedLocales) + ->map(fn (string $name, string $locale) => ['key' => $locale, 'label' => $name]) + ->sortBy('key') + ->values() + ->toArray(); // Order locales with submission locale first - $orderedLocales = $supportedSubmissionMetadataLocales; - uksort($orderedLocales, fn ($a, $b) => $a === $submission->getData('locale') ? $a : $b); + $orderedLocales = $supportedLocales; + uksort($orderedLocales, fn ($a, $b) => $b === $submission->getData('locale') ? 1 : -1); $userGroups = Repo::userGroup() ->getCollector() @@ -301,7 +305,7 @@ public function saved(array $args, Request $request): void protected function getSteps(Request $request, Submission $submission, Publication $publication, array $locales, array $sections, LazyCollection $categories): array { $publicationApiUrl = $this->getPublicationApiUrl($request, $submission->getId(), $publication->getId()); - $controlledVocabUrl = $this->getControlledVocabBaseUrl($request); + $controlledVocabUrl = $this->getControlledVocabBaseUrl($request, $submission->getId()); $steps = []; $steps[] = $this->getDetailsStep($request, $submission, $publication, $locales, $publicationApiUrl, $sections, $controlledVocabUrl); @@ -384,7 +388,7 @@ protected function getSubmissionFilesApiUrl(Request $request, int $submissionId) * * The entry `__vocab__` will be replaced with the user's search phrase. */ - protected function getControlledVocabBaseUrl(Request $request): string + protected function getControlledVocabBaseUrl(Request $request, int $submissionId): string { return $request->getDispatcher()->url( $request, @@ -393,7 +397,7 @@ protected function getControlledVocabBaseUrl(Request $request): string 'vocabs', null, null, - ['vocab' => '__vocab__'] + ['vocab' => '__vocab__', 'submissionId' => $submissionId] ); } @@ -569,7 +573,7 @@ protected function getDetailsStep(Request $request, Submission $submission, Publ 'name' => __('submission.details'), 'type' => self::SECTION_TYPE_FORM, 'description' => $request->getContext()->getLocalizedData('detailsHelp'), - 'form' => $this->getLocalizedForm($titleAbstractForm, $submission, $request->getContext()), + 'form' => $this->getLocalizedForm($titleAbstractForm, $submission->getData('locale'), $locales), ], ]; @@ -619,7 +623,7 @@ protected function getEditorsStep(Request $request, Submission $submission, Publ 'vocabs', null, null, - ['vocab' => '__vocab__'] + ['vocab' => '__vocab__', 'submissionId' => $submission->getId()] ), $categories ); @@ -633,8 +637,8 @@ protected function getEditorsStep(Request $request, Submission $submission, Publ $hasMetadataForm = count($metadataForm->fields); - $metadataFormData = $this->getLocalizedForm($metadataForm, $submission, $request->getContext()); - $commentsFormData = $this->getLocalizedForm($commentsForm, $submission, $request->getContext()); + $metadataFormData = $this->getLocalizedForm($metadataForm, $submission->getData('locale'), $locales); + $commentsFormData = $this->getLocalizedForm($commentsForm, $submission->getData('locale'), $locales); $sections = [ [ @@ -828,30 +832,21 @@ protected function isEditor(): bool * * Uses the submission locale as the primary and * visible locale, and puts that locale first in the - * list of supported locales. + * list of supported and publication's locales. * * Call this instead of $form->getConfig() to display * a form with the correct submission locales */ - protected function getLocalizedForm(FormComponent $form, Submission $submission, Context $context): array + protected function getLocalizedForm(FormComponent $form, string $submissionLocale, array $locales): array { $config = $form->getConfig(); - $submissionLocale = $submission->getData('locale'); $config['primaryLocale'] = $submissionLocale; $config['visibleLocales'] = [$submissionLocale]; - - $supportedFormLocales = []; - foreach ($context->getSupportedSubmissionMetadataLocaleNames() as $localeKey => $name) { - $supportedFormLocales[] = [ - 'key' => $localeKey, - 'label' => $name, - ]; - } - - usort($supportedFormLocales, fn ($a, $b) => $a['key'] === $submissionLocale ? -1 : 1); - - $config['supportedFormLocales'] = $supportedFormLocales; + $config['supportedFormLocales'] = collect($locales) + ->sortBy([fn (array $a, array $b) => $b['key'] === $submissionLocale ? 1 : -1]) + ->values() + ->toArray(); return $config; } diff --git a/pages/workflow/PKPWorkflowHandler.php b/pages/workflow/PKPWorkflowHandler.php index 8841f953560..b2910f0e0b8 100644 --- a/pages/workflow/PKPWorkflowHandler.php +++ b/pages/workflow/PKPWorkflowHandler.php @@ -218,7 +218,8 @@ public function index($args, $request) $latestPublication = $submission->getLatestPublication(); - $locales = collect($submissionContext->getSupportedSubmissionMetadataLocaleNames() + $latestPublication->getLanguageNames()) + $submissionLocale = $submission->getData('locale'); + $locales = collect($submissionContext->getSupportedSubmissionMetadataLocaleNames() + $submission->getPublicationLanguageNames()) ->map(fn (string $name, string $locale) => ['key' => $locale, 'label' => $name]) ->sortBy('key') ->values() @@ -333,8 +334,8 @@ public function index($args, $request) 'components' => [ $contributorsListPanel->id => $contributorsListPanel->getConfig(), $citationsForm->id => $citationsForm->getConfig(), - $publicationLicenseForm->id => $this->getLocalizedForm($publicationLicenseForm, $latestPublication, $submissionContext), - $titleAbstractForm->id => $this->getLocalizedForm($titleAbstractForm, $latestPublication, $submissionContext), + $publicationLicenseForm->id => $this->getLocalizedForm($publicationLicenseForm, $submissionLocale, $locales), + $titleAbstractForm->id => $this->getLocalizedForm($titleAbstractForm, $submissionLocale, $locales), ], 'currentPublication' => $currentPublicationProps, 'decisionUrl' => $decisionUrl, @@ -369,7 +370,7 @@ public function index($args, $request) ]; // Add the metadata form if one or more metadata fields are enabled - $vocabSuggestionUrlBase = $request->getDispatcher()->url($request, PKPApplication::ROUTE_API, $submissionContext->getData('urlPath'), 'vocabs', null, null, ['vocab' => '__vocab__', 'publicationId' => $latestPublication->getId()]); + $vocabSuggestionUrlBase = $request->getDispatcher()->url($request, PKPApplication::ROUTE_API, $submissionContext->getData('urlPath'), 'vocabs', null, null, ['vocab' => '__vocab__', 'submissionId' => $submission->getId()]); $metadataForm = new PKPMetadataForm($latestPublicationApiUrl, $locales, $latestPublication, $submissionContext, $vocabSuggestionUrlBase, true); $metadataEnabled = count($metadataForm->fields); @@ -377,7 +378,7 @@ public function index($args, $request) $templateMgr->setConstants([ 'FORM_METADATA' => FORM_METADATA, ]); - $state['components'][FORM_METADATA] = $this->getLocalizedForm($metadataForm, $latestPublication, $submissionContext); + $state['components'][FORM_METADATA] = $this->getLocalizedForm($metadataForm, $submissionLocale, $locales); $state['publicationFormIds'][] = FORM_METADATA; } @@ -404,7 +405,7 @@ public function index($args, $request) $selectRevisionDecisionForm = new \PKP\components\forms\decision\SelectRevisionDecisionForm(); $selectRevisionRecommendationForm = new \PKP\components\forms\decision\SelectRevisionRecommendationForm(); $state['components'][$selectRevisionDecisionForm->id] = $selectRevisionDecisionForm->getConfig(); - $state['components'][$selectRevisionRecommendationForm->id] = $this->getLocalizedForm($selectRevisionRecommendationForm, $latestPublication, $submissionContext); + $state['components'][$selectRevisionRecommendationForm->id] = $this->getLocalizedForm($selectRevisionRecommendationForm, $submissionLocale, $locales); $templateMgr->setConstants([ 'FORM_SELECT_REVISION_DECISION' => FORM_SELECT_REVISION_DECISION, 'FORM_SELECT_REVISION_RECOMMENDATION' => FORM_SELECT_REVISION_RECOMMENDATION, @@ -835,17 +836,14 @@ protected function notificationOptionsByStage($user, $stageId, $contextId) * Call this instead of $form->getConfig() to display * a form with the correct publication locales */ - protected function getLocalizedForm(\PKP\components\forms\FormComponent $form, Publication $publication, Context $context): array + protected function getLocalizedForm(\PKP\components\forms\FormComponent $form, string $submissionLocale, array $locales): array { $config = $form->getConfig(); - $primaryLocale = $publication->getData('locale'); - $config['primaryLocale'] = $primaryLocale; - $config['visibleLocales'] = [$primaryLocale]; - - $config['supportedFormLocales'] = collect($context->getSupportedSubmissionMetadataLocaleNames() + $publication->getLanguageNames()) - ->map(fn (string $name, string $locale) => ['key' => $locale, 'label' => $name]) - ->sortBy([fn (array $a, array $b) => $a['key'] === $primaryLocale || $a['key'] < $b['key'] ? -1 : 1]) + $config['primaryLocale'] = $submissionLocale; + $config['visibleLocales'] = [$submissionLocale]; + $config['supportedFormLocales'] = collect($locales) + ->sortBy([fn (array $a, array $b) => $b['key'] === $submissionLocale ? 1 : -1]) ->values() ->toArray(); diff --git a/templates/controllers/grid/languages/addLanguageForm.tpl b/templates/controllers/grid/languages/addLanguageForm.tpl index ec31ab6179e..da4929f1eb4 100644 --- a/templates/controllers/grid/languages/addLanguageForm.tpl +++ b/templates/controllers/grid/languages/addLanguageForm.tpl @@ -1,5 +1,5 @@ {** - * controllers/grid/languages/addLanguageForm.tpl + * templates/controllers/grid/languages/addLanguageForm.tpl * * Copyright (c) 2023 Simon Fraser University * Copyright (c) 2023 John Willinsky