Skip to content

Commit

Permalink
#5000 Submission and metadata languages separated from UI and form la…
Browse files Browse the repository at this point in the history
…nguages
  • Loading branch information
jyhein committed Sep 18, 2023
1 parent 242b6c2 commit 34cd3fe
Show file tree
Hide file tree
Showing 30 changed files with 706 additions and 87 deletions.
16 changes: 8 additions & 8 deletions api/v1/submissions/PKPSubmissionFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ public function add($slimRequest, $response, $args)
$params['submissionId'] = $submission->getId();
$params['uploaderUserId'] = (int) $request->getUser()->getId();

$primaryLocale = $request->getContext()->getPrimaryLocale();
$allowedLocales = $request->getContext()->getData('supportedSubmissionLocales');
$submissionLocale = $submission->getData('locale');
$allowedLocales = $request->getContext()->getSupportedSubmissionMetadataLocales();

// Set the name if not passed with the request
if (empty($params['name'])) {
$params['name'][$primaryLocale] = $_FILES['file']['name'];
$params['name'][$submissionLocale] = $_FILES['file']['name'];
}

// If no genre has been set and there is only one genre possible, set it automatically
Expand All @@ -318,7 +318,7 @@ public function add($slimRequest, $response, $args)
null,
$params,
$allowedLocales,
$primaryLocale
$submissionLocale
);

if (!empty($errors)) {
Expand Down Expand Up @@ -402,15 +402,15 @@ public function edit($slimRequest, $response, $args)
return $response->withStatus(400)->withJsonError('api.submissionsFiles.400.noParams');
}

$primaryLocale = $request->getContext()->getPrimaryLocale();
$allowedLocales = $request->getContext()->getData('supportedSubmissionLocales');
$submissionLocale = $submission->getData('locale');
$allowedLocales = $request->getContext()->getSupportedSubmissionMetadataLocales();

$errors = Repo::submissionFile()
->validate(
$submissionFile,
$params,
$allowedLocales,
$primaryLocale
$submissionLocale
);

if (!empty($errors)) {
Expand Down Expand Up @@ -438,7 +438,7 @@ public function edit($slimRequest, $response, $args)
$params['fileId'] = $fileId;
$params['uploaderUserId'] = $request->getUser()->getId();
if (empty($params['name'])) {
$params['name'][$primaryLocale] = $_FILES['file']['name'];
$params['name'][$submissionLocale] = $_FILES['file']['name'];
}
}

Expand Down
4 changes: 3 additions & 1 deletion api/v1/vocabs/PKPVocabHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PKP\core\PKPString;
use PKP\db\DAORegistry;
use PKP\facades\Locale;
use APP\facades\Repo;
use PKP\handler\APIHandler;
use PKP\plugins\Hook;
use PKP\security\authorization\ContextAccessPolicy;
Expand Down Expand Up @@ -84,8 +85,9 @@ public function getMany(Request $slimRequest, APIResponse $response, array $args
$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');

if (!in_array($locale, $context->getData('supportedSubmissionLocales'))) {
if (!in_array($locale, $context->getData('supportedSubmissionMetadataLocales')) && !$isSubmissionPrimaryLocale) {
return $response->withStatus(400)->withJsonError('api.vocabs.400.localeNotSupported', ['locale' => $locale]);
}

Expand Down
3 changes: 2 additions & 1 deletion classes/author/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public function getSchemaMap(): maps\Schema
public function validate($author, $props, Submission $submission, Context $context)
{
$schemaService = Services::get('schema');
$allowedLocales = $context->getSupportedSubmissionLocales();
$primaryLocale = $submission->getData('locale');
$allowedLocales = $context->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);

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

$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedSubmissionLocales());
$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedSubmissionMetadataLocales());

ksort($output);

Expand Down
2 changes: 1 addition & 1 deletion classes/components/listPanels/ContributorsListPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function getPublicationUrlFormat(): string
*/
protected function getLocalizedForm(): array
{
uksort($this->locales, fn ($a, $b) => $a === $this->submission->getData('locale') ? -1 : 1);
usort($this->locales, fn ($a, $b) => $a['key'] === $this->submission->getData('locale') ? -1 : 1);

$apiUrl = Application::get()->getRequest()->getDispatcher()->url(
Application::get()->getRequest(),
Expand Down
57 changes: 57 additions & 0 deletions classes/context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,63 @@ public function getSupportedLocaleNames(int $langLocaleStatus = LocaleMetadata::
return $this->getData('supportedLocaleNames') ?? Locale::getFormattedDisplayNames($this->getSupportedLocales(), null, $langLocaleStatus);
}

/**
* Get the supported added submission locales.
*
* @return array
*/
public function getSupportedAddedSubmissionLocales()
{
return $this->getData('supportedAddedSubmissionLocales');
}

/**
* 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
*/
public function getSupportedAddedSubmissionLocaleNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT)
{
return $this->getData('supportedAddedSubmissionLocaleNames') ?? Locale::getFormattedDisplayNames($this->getSupportedAddedSubmissionLocales(), null, $langLocaleStatus);
}

/**
* Get the supported default submission locale.
*/
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);
}

/**
* Get the supported metadata locales.
*/
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
{
return Locale::getFormattedDisplayNames($this->getSupportedSubmissionMetadataLocales(), null, $langLocaleStatus);
}

/**
* Return date or/and time formats available for forms, fallback to the default if not set
*
Expand Down
5 changes: 4 additions & 1 deletion classes/galley/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ protected function mapByProperties(array $props, Galley $galley): array
}
}

$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedFormLocales());
$locales = $this->context->getSupportedSubmissionMetaDataLocales();
in_array($primaryLocale = $this->submission->getData('locale'), $locales) || array_push($locales, $primaryLocale);

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

ksort($output);

Expand Down
13 changes: 8 additions & 5 deletions classes/publication/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ public function getDateBoundaries(Collector $query): object
*/
public function validate(?Publication $publication, array $props, Submission $submission, Context $context): array
{
$allowedLocales = $context->getSupportedSubmissionLocales();
$primaryLocale = $submission->getLocale();
$primaryLocale = $submission->getData('locale');
$allowedLocales = $context->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);

$errors = [];

Expand Down Expand Up @@ -243,7 +244,7 @@ public function validate(?Publication $publication, array $props, Submission $su
* wants to enforce particular publishing requirements, such as
* requiring certain metadata or other information.
*
* @param array $allowedLocales The context's supported submission locales
* @param array $allowedLocales The context's supported submission metadata locales
* @param string $primaryLocale The submission's primary locale
*/
public function validatePublish(Publication $publication, Submission $submission, array $allowedLocales, string $primaryLocale): array
Expand Down Expand Up @@ -282,7 +283,8 @@ public function add(Publication $publication): int
$submissionContext = Services::get('context')->get($submission->getData('contextId'));
}

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

$supportedLocales = $submissionContext->getSupportedSubmissionLocales();
$supportedLocales = $submissionContext->getSupportedSubmissionMetadataLocales();
in_array($primaryLocale = $submission->getData('locale'), $supportedLocales) || array_push($supportedLocales, $primaryLocale);
foreach ($supportedLocales as $localeKey) {
if (!array_key_exists($localeKey, $params['coverImage'])) {
continue;
Expand Down
13 changes: 12 additions & 1 deletion classes/services/PKPContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ public function validate($action, $props, $allowedLocales, $primaryLocale)
[
'urlPath.regex' => __('admin.contexts.form.pathAlphaNumeric'),
'primaryLocale.regex' => __('validator.localeKey'),
'supportedDefaultSubmissionLocale.regex' => __('validator.localeKey'),
'supportedFormLocales.regex' => __('validator.localeKey'),
'supportedLocales.regex' => __('validator.localeKey'),
'supportedSubmissionLocales.*.regex' => __('validator.localeKey'),
'supportedSubmissionMetadataLocales.*.regex' => __('validator.localeKey'),
]
);

Expand Down Expand Up @@ -494,8 +496,17 @@ public function add($context, $request)
if (!$context->getData('supportedFormLocales')) {
$context->setData('supportedFormLocales', [$context->getData('primaryLocale')]);
}
if (!$context->getData('supportedDefaultSubmissionLocale')) {
$context->setData('supportedDefaultSubmissionLocale', $context->getData('primaryLocale'));
}
if (!$context->getData('supportedAddedSubmissionLocales')) {
$context->setData('supportedAddedSubmissionLocales', [$context->getData('supportedDefaultSubmissionLocale')]);
}
if (!$context->getData('supportedSubmissionLocales')) {
$context->setData('supportedSubmissionLocales', [$context->getData('primaryLocale')]);
$context->setData('supportedSubmissionLocales', [$context->getData('supportedDefaultSubmissionLocale')]);
}
if (!$context->getData('supportedSubmissionMetadataLocales')) {
$context->setData('supportedSubmissionMetadataLocales', [$context->getData('supportedDefaultSubmissionLocale')]);
}

$contextDao->insertObject($context);
Expand Down
5 changes: 3 additions & 2 deletions classes/submission/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ public function getWorkflowUrlByUserRoles(Submission $submission, ?int $userId =
*/
public function validate(?Submission $submission, array $props, Context $context): array
{
$primaryLocale = $props['locale'] ?? $submission?->getLocale() ?? $context->getPrimaryLocale();
$primaryLocale = $props['locale'] ?? $submission?->getData('locale') ?? $context->getSupportedDefaultSubmissionLocale();
$allowedLocales = $context->getSupportedSubmissionLocales();
in_array($primaryLocale, $allowedLocales) || array_push($allowedLocales, $primaryLocale);

$errors = [];

Expand Down Expand Up @@ -542,7 +543,7 @@ public function add(Submission $submission, Publication $publication, Context $c
$submission->setData('status', Submission::STATUS_QUEUED);
}
if (!$submission->getData('locale')) {
$submission->setData('locale', $context->getPrimaryLocale());
$submission->setData('locale', $context->getSupportedDefaultSubmissionLocale());
}
$submissionId = $this->dao->insert($submission);
$submission = Repo::submission()->get($submissionId);
Expand Down
10 changes: 5 additions & 5 deletions classes/submissionFile/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ public function getSchemaMap(): Schema
* Perform validation checks on data used to add or edit a submission file.
*
* @param array $props A key/value array with the new data to validate
* @param array $allowedLocales The context's supported locales
* @param string $primaryLocale The context's primary locale
* @param array $allowedLocales The supported submission metadata locales
* @param string $submissionLocale The submission's locale
*
* @return array A key/value array with validation errors. Empty if no errors
*/
public function validate(
?SubmissionFile $object,
array $props,
array $allowedLocales,
string $primaryLocale
string $submissionLocale
): array {
$validator = ValidatorFactory::make(
$props,
Expand All @@ -129,7 +129,7 @@ public function validate(
$this->schemaService->getRequiredProps($this->dao->schema),
$this->schemaService->getMultilingualProps($this->dao->schema),
$allowedLocales,
$primaryLocale
$submissionLocale
);

// Check for input from disallowed locales
Expand Down Expand Up @@ -242,7 +242,7 @@ public function validate(
$object,
$props,
$allowedLocales,
$primaryLocale
$submissionLocale
]
);

Expand Down
5 changes: 4 additions & 1 deletion classes/submissionFile/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ 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);

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

ksort($output);
Expand Down
3 changes: 2 additions & 1 deletion classes/template/PKPTemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ public function registerJSLibraryData()
$allLocales = array_merge(
$context->getSupportedLocales() ?? [],
$context->getSupportedFormLocales() ?? [],
$context->getSupportedSubmissionLocales() ?? []
$context->getSupportedSubmissionLocales() ?? [],
$context->getSupportedSubmissionMetadataLocales() ?? [],
);
} else {
$allLocales = $this->_request->getSite()->getSupportedLocales();
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, $reviewRound);
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $request->getContext(), $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, $reviewRound);
$form = new SubmissionFilesMetadataForm($submissionFile, $stageId, $request->getContext(), $reviewRound);
$form->readInputData();
if ($form->validate()) {
$form->execute();
Expand Down
5 changes: 3 additions & 2 deletions controllers/grid/admin/languages/AdminLanguageGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function loadData($request, $filter)
}

if ($this->_canManage($request)) {
$data = $this->addManagementData($request, $data);
$data = $this->addLocaleSettingData($request, $data);
}

return $data;
Expand Down Expand Up @@ -430,7 +430,8 @@ protected function _updateContextLocaleSettings($request)
while ($context = $contexts->next()) {
$params = [];
$primaryLocale = $context->getPrimaryLocale();
foreach (['supportedLocales', 'supportedFormLocales', 'supportedSubmissionLocales'] as $settingName) {
$params['supportedDefaultSubmissionLocale'] = $context->getData('supportedDefaultSubmissionLocale');
foreach (['supportedLocales', 'supportedFormLocales', 'supportedSubmissionLocales', 'supportedSubmissionMetadataLocales'] as $settingName) {
$localeList = $context->getData($settingName);

if (is_array($localeList)) {
Expand Down
Loading

0 comments on commit 34cd3fe

Please sign in to comment.