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 Nov 14, 2023
1 parent 180dad6 commit f166648
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 135 deletions.
2 changes: 1 addition & 1 deletion api/v1/vocabs/PKPVocabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
14 changes: 8 additions & 6 deletions classes/components/listPanels/ContributorsListPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,22 @@ 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,
$this->context->getPath(),
'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;
}
Expand Down
26 changes: 26 additions & 0 deletions classes/galley/Galley.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion classes/galley/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
24 changes: 24 additions & 0 deletions classes/submission/PKPSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Illuminate\Support\LazyCollection;
use PKP\core\Core;
use PKP\facades\Locale;
use PKP\i18n\LocaleMetadata;

/**
* @extends \PKP\core\DataObject<DAO>
Expand Down Expand Up @@ -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.
//
Expand Down
27 changes: 27 additions & 0 deletions classes/submissionFile/SubmissionFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<DAO>
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 8 additions & 10 deletions classes/submissionFile/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 17 additions & 20 deletions controllers/grid/languages/LanguageGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
45 changes: 21 additions & 24 deletions controllers/grid/languages/form/AddLanguageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit f166648

Please sign in to comment.