Skip to content

Commit

Permalink
Merge pull request #9309 from jyhein/f5000_7431
Browse files Browse the repository at this point in the history
Submission and metadata languages separated from UI and form languages
  • Loading branch information
bozana authored Mar 21, 2024
2 parents 6fc1552 + 7781b8a commit c6b863d
Show file tree
Hide file tree
Showing 57 changed files with 1,130 additions and 173 deletions.
16 changes: 8 additions & 8 deletions api/v1/submissions/PKPSubmissionFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ public function add(Request $illuminateRequest): JsonResponse
$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 @@ -344,7 +344,7 @@ public function add(Request $illuminateRequest): JsonResponse
null,
$params,
$allowedLocales,
$primaryLocale
$submissionLocale
);

if (!empty($errors)) {
Expand Down Expand Up @@ -430,15 +430,15 @@ public function edit(Request $illuminateRequest): JsonResponse
], Response::HTTP_BAD_REQUEST);
}

$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 @@ -466,7 +466,7 @@ public function edit(Request $illuminateRequest): JsonResponse
$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/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 @@ -104,8 +105,9 @@ 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['submissionId']) ? Repo::submission()->get((int) $requestParams['submissionId'])?->getPublicationLanguages() ?? [] : []);

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
2 changes: 1 addition & 1 deletion classes/author/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ 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 = $submission->getPublicationLanguages($context->getSupportedSubmissionMetadataLocales());

$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->getSupportedSubmissionLocales());
$locales = Repo::submission()->get(Repo::publication()->get($item->getData('publicationId'))->getData('submissionId'))->getPublicationLanguages($this->context->getSupportedSubmissionMetadataLocales());

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

ksort($output);

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
{
uksort($this->locales, fn ($a, $b) => $a === $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
57 changes: 51 additions & 6 deletions classes/context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,10 @@ public function getSupportedSubmissionLocales()
/**
* Return associative array of all locales supported by submissions on the
* context.
*
* @param int $langLocaleStatus The const value of one of LocaleMetadata:LANGUAGE_LOCALE_*
*
* @return array
*/
public function getSupportedSubmissionLocaleNames(int $langLocaleStatus = LocaleMetadata::LANGUAGE_LOCALE_WITHOUT)
public function getSupportedSubmissionLocaleNames(): array
{
return $this->getData('supportedSubmissionLocaleNames') ?? Locale::getFormattedDisplayNames($this->getSupportedSubmissionLocales(), null, $langLocaleStatus);
return $this->getData('supportedSubmissionLocaleNames') ?? Locale::getSubmissionLocaleDisplayNames($this->getSupportedSubmissionLocales());
}

/**
Expand All @@ -393,6 +389,55 @@ public function getSupportedLocaleNames(int $langLocaleStatus = LocaleMetadata::
return $this->getData('supportedLocaleNames') ?? Locale::getFormattedDisplayNames($this->getSupportedLocales(), null, $langLocaleStatus);
}

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

/**
* Return associative array of added locales supported by submissions on the
* context.
*/
public function getSupportedAddedSubmissionLocaleNames(): array
{
return Locale::getSubmissionLocaleDisplayNames($this->getSupportedAddedSubmissionLocales());
}

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

/**
* Return string default submission locale supported by the site.
*/
public function getSupportedDefaultSubmissionLocaleName(): string
{
return Locale::getSubmissionLocaleDisplayNames([$l = $this->getSupportedDefaultSubmissionLocale()])[$l];
}

/**
* 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.
*/
public function getSupportedSubmissionMetadataLocaleNames(): array
{
return Locale::getSubmissionLocaleDisplayNames($this->getSupportedSubmissionMetadataLocales());
}

/**
* Return date or/and time formats available for forms, fallback to the default if not set
*
Expand Down
14 changes: 14 additions & 0 deletions classes/core/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ class DataObject
/** @var bool whether injection adapters have already been loaded from the database */
public $_injectionAdaptersLoaded = false;

/** @var array conversion table for locales */
public $_localesTable = [
"be@cyrillic" => "be",
"bs" => "bs_Latn",
"fr_FR" => "fr",
"nb" => "nb_NO",
"sr@cyrillic" => "sr_Cyrl",
"sr@latin" => "sr_Latn",
"uz@cyrillic" => "uz",
"uz@latin" => "uz_Latn",
"zh_CN" => "zh_Hans",
];

/**
* Constructor
*/
Expand Down Expand Up @@ -101,6 +114,7 @@ public function getLocalePrecedence(string $preferredLocale = null): array
return array_unique(
array_filter([
$preferredLocale ?? Locale::getLocale(),
$this->_localesTable[$preferredLocale ?? Locale::getLocale()] ?? null,
$this->getDefaultLocale(),
$request->getContext()?->getPrimaryLocale(),
$request->getSite()->getPrimaryLocale(),
Expand Down
40 changes: 40 additions & 0 deletions classes/dev/ComposerScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,44 @@ public static function isoFileCheck(): void
throw new Exception("The ISO639-2b file {$iso6392bFile} does not exist.");
}
}

/**
* A post-install-cmd custom composer script that
* creates languages.json from downloaded Weblate languages.csv.
*/
public static function weblateFilesDownload(): void
{
try {
$dirPath = dirname(__FILE__, 3) . "/lib/weblateLanguages";
$langFilePath = "$dirPath/languages.json";
$urlCsv = 'https://raw.githubusercontent.com/WeblateOrg/language-data/main/languages.csv';

if (!is_dir($dirPath)) {
mkdir($dirPath);
}

$streamContext = stream_context_create(['http' => ['method' => 'HEAD']]);
$languagesCsv = !preg_match('/200 OK/', get_headers($urlCsv, false, $streamContext)[0] ?? "") ?: file($urlCsv, FILE_SKIP_EMPTY_LINES);
if (!is_array($languagesCsv) || !$languagesCsv) {
throw new Exception(__METHOD__ . " : The Weblate file 'languages.csv' cannot be downloaded !");
}

array_shift($languagesCsv);
$languages = [];
foreach($languagesCsv as $languageCsv) {
$localeAndName = str_getcsv($languageCsv, ",");
if (isset($localeAndName[0], $localeAndName[1]) && preg_match('/^[\w@-]{2,50}$/', $localeAndName[0])) {
$displayName = locale_get_display_name($localeAndName[0], 'en');
$languages[$localeAndName[0]] = (($displayName && $displayName !== $localeAndName[0]) ? $displayName : $localeAndName[1]);
}
}

$languagesJson = json_encode($languages, JSON_THROW_ON_ERROR);
if (!$languagesJson || !file_put_contents($langFilePath, $languagesJson)) {
throw new Exception(__METHOD__ . " : Json file empty, or save unsuccessful: $langFilePath !");
}
} catch (Exception $e) {
error_log($e->getMessage());
}
}
}
31 changes: 29 additions & 2 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 @@ -127,8 +130,8 @@ public function isPdfGalley()
public function getGalleyLabel()
{
$label = $this->getLabel();
if ($this->getLocale() && $this->getLocale() != Locale::getLocale()) {
$label .= ' (' . Locale::getMetadata($this->getLocale())->getDisplayName() . ')';
if ($this->getLocale() && $this->getLocale() !== Locale::getLocale()) {
$label .= ' (' . Locale::getSubmissionLocaleDisplayNames([$this->getLocale()])[$this->getLocale()] . ')';
}
return $label;
}
Expand Down Expand Up @@ -183,6 +186,30 @@ public function setStoredPubId($pubIdType, $pubId)
parent::setStoredPubId($pubIdType, $pubId);
}
}

/**
* Get metadata language names
*/
public function getLanguageNames(): array
{
return Locale::getSubmissionLocaleDisplayNames($this->getLanguages());
}

/**
* Get metadata languages
*/
public function getLanguages(): array
{
$props = Services::get('schema')->getMultilingualProps(PKPSchemaService::SCHEMA_GALLEY);
$locales = array_map(fn (string $prop): array => array_keys($this->getData($prop) ?? []), $props);
return collect([$this->getData('locale')])
->concat($locales)
->flatten()
->filter()
->unique()
->values()
->toArray();
}
}

if (!PKP_STRICT_MODE) {
Expand Down
4 changes: 3 additions & 1 deletion classes/galley/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ protected function mapByProperties(array $props, Galley $galley): array
}
}

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

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

ksort($output);

Expand Down
Loading

0 comments on commit c6b863d

Please sign in to comment.