Modified form to work properly with new datastructure

This commit is contained in:
Jan Böhmer 2025-11-09 23:02:40 +01:00
parent 92ca46210a
commit 1ec34a2bb0
5 changed files with 141 additions and 120 deletions

View file

@ -27,38 +27,69 @@ class DataSourceSynonymsCollectionType extends AbstractType
{
}
private function flattenStructure(array $modelValue): array
{
//If the model is already flattened, return as is
if (array_is_list($modelValue)) {
return $modelValue;
}
$out = [];
foreach ($modelValue as $dataSource => $locales) {
if (!is_array($locales)) {
continue;
}
foreach ($locales as $locale => $translations) {
if (!is_array($translations)) {
continue;
}
$out[] = [
'dataSource' => $dataSource,
'locale' => $locale,
'translation_singular' => $translations['singular'] ?? '',
'translation_plural' => $translations['plural'] ?? '',
];
}
}
return $out;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
//Flatten the structure
$data = $event->getData();
$event->setData($this->flattenStructure($data));
});
$builder->addModelTransformer(new CallbackTransformer(
// Model -> View
function ($modelValue) {
if (!is_array($modelValue)) {
return [[
'dataSource' => null,
'locale' => null,
'translation_singular' => null,
'translation_plural' => null,
]];
// Model -> View
$this->flattenStructure(...),
// View -> Model (keep list; let existing behavior unchanged)
function (array $viewValue) {
//Turn our flat list back into the structured array
foreach ($viewValue as $row) {
if (!is_array($row)) {
continue;
}
$dataSource = $row['dataSource'] ?? null;
$locale = $row['locale'] ?? null;
$translation_singular = $row['translation_singular'] ?? null;
$translation_plural = $row['translation_plural'] ?? null;
if (!is_string($dataSource) || $dataSource === ''
|| !is_string($locale) || $locale === ''
) {
continue;
}
$out[$dataSource][$locale] = [
'singular' => is_string($translation_singular) ? $translation_singular : '',
'plural' => is_string($translation_plural) ? $translation_plural : '',
];
}
return $modelValue === [] ? [[
'dataSource' => null,
'locale' => null,
'translation_singular' => null,
'translation_plural' => null,
]] : $modelValue;
},
// View -> Model (keep list; let existing behavior unchanged)
function ($viewValue) {
if (!is_array($viewValue)) {
return [];
}
$out = [];
foreach ($viewValue as $row) {
if (is_array($row)) {
$out[] = $row;
}
}
return $out;
}
));

View file

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace App\Services\Misc;
use App\Settings\BehaviorSettings\DataSourceSynonymsSettings;
use App\Settings\SystemSettings\DataSourceSynonymsSettings;
use Symfony\Contracts\Translation\TranslatorInterface;
readonly class DataSourceSynonymResolver
@ -60,7 +60,7 @@ readonly class DataSourceSynonymResolver
private function synonyms(string $dataSource, string $locale): array
{
$all = $this->synonymsSettings->getSynonymsAsArray();
$all = [];
$row = $all[$dataSource][$locale] ?? ['singular' => '', 'plural' => ''];
return [

View file

@ -1,90 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Settings\BehaviorSettings;
use App\Form\Type\DataSourceSynonymsCollectionType;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Translation\TranslatableMessage as TM;
#[Settings(label: new TM("settings.system.data_source_synonyms"))]
#[SettingsIcon("fa-language")]
class DataSourceSynonymsSettings
{
use SettingsTrait;
#[SettingsParameter(
ArrayType::class,
label: new TM("settings.system.data_source_synonyms.configuration"),
description: new TM("settings.system.data_source_synonyms.configuration.help"),
options: ['type' => ArrayType::class, 'options' => ['type' => StringType::class]],
formType: DataSourceSynonymsCollectionType::class,
formOptions: [
'required' => false,
'data_sources' => [
'category' => new TM("settings.behavior.data_source_synonyms.category"),
'storagelocation' => new TM("settings.behavior.data_source_synonyms.storagelocation"),
'footprint' => new TM("settings.behavior.data_source_synonyms.footprint"),
'manufacturer' => new TM("settings.behavior.data_source_synonyms.manufacturer"),
'supplier' => new TM("settings.behavior.data_source_synonyms.supplier"),
'project' => new TM("settings.behavior.data_source_synonyms.project"),
],
],
)]
#[Assert\Type('array')]
#[Assert\All([new Assert\Type('array')])]
public array $dataSourceSynonyms = [
// flat list of rows, e.g.:
// ['dataSource' => 'category', 'locale' => 'en', 'translation_singular' => 'Category', 'translation_plural' => 'Categories'],
];
/**
* Normalize to map form:
* [dataSource => [locale => ['singular' => string, 'plural' => string]]]
* No preference/merging is applied; both values are returned as provided (missing ones as empty strings).
*
* @return array<string, array<string, array{singular: string, plural: string}>>
*/
public function getSynonymsAsArray(): array
{
$result = [];
foreach ($this->dataSourceSynonyms as $row) {
if (!is_array($row)) {
continue;
}
$ds = $row['dataSource'] ?? null;
$loc = $row['locale'] ?? null;
if (!is_string($ds) || $ds === '' || !is_string($loc) || $loc === '') {
continue;
}
// Read both fields independently; do not prefer one over the other.
$singular = isset($row['translation_singular']) && is_string($row['translation_singular'])
? $row['translation_singular'] : '';
$plural = isset($row['translation_plural']) && is_string($row['translation_plural'])
? $row['translation_plural'] : '';
// For legacy data (optional): if only "text" exists and both fields are empty, keep it as given in both slots or leave empty?
// Requirement says: no preference, just return values. We therefore do NOT map legacy automatically.
// If you want to expose legacy "text" as well, handle it outside or migrate data beforehand.
$result[$ds] ??= [];
$result[$ds][$loc] = [
'singular' => $singular,
'plural' => $plural,
];
}
return $result;
}
}

View file

@ -0,0 +1,81 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\SystemSettings;
use App\Form\Type\DataSourceSynonymsCollectionType;
use App\Services\ElementTypes;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\SerializeType;
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Translation\TranslatableMessage as TM;
use Symfony\Component\Validator\Constraints as Assert;
#[Settings(label: new TM("settings.system.data_source_synonyms"))]
#[SettingsIcon("fa-language")]
class DataSourceSynonymsSettings
{
use SettingsTrait;
#[SettingsParameter(
ArrayType::class,
label: new TM("settings.system.data_source_synonyms.configuration"),
description: new TM("settings.system.data_source_synonyms.configuration.help"),
options: ['type' => SerializeType::class],
formType: DataSourceSynonymsCollectionType::class,
formOptions: [
'required' => false,
'data_sources' => [
'category' => new TM("settings.behavior.data_source_synonyms.category"),
'storagelocation' => new TM("settings.behavior.data_source_synonyms.storagelocation"),
'footprint' => new TM("settings.behavior.data_source_synonyms.footprint"),
'manufacturer' => new TM("settings.behavior.data_source_synonyms.manufacturer"),
'supplier' => new TM("settings.behavior.data_source_synonyms.supplier"),
'project' => new TM("settings.behavior.data_source_synonyms.project"),
],
],
)]
#[Assert\Type('array')]
#[Assert\All([new Assert\Type('array')])]
/**
* @var array<string, array<string, array{singular: string, plural: string}>> $customTypeLabels
* An array of the form: [
* 'category' => [
* 'en' => ['singular' => 'Category', 'plural' => 'Categories'],
* 'de' => ['singular' => 'Kategorie', 'plural' => 'Kategorien'],
* ],
* 'manufacturer' => [
* 'en' => ['singular' => 'Manufacturer', 'plural' =>'Manufacturers'],
* ],
* ]
*/
public array $customTypeLabels = [];
public function isCustomLabelDefinedForType(ElementTypes $type): bool
{
return isset($this->customTypeLabels[$type->value]);
}
}

View file

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace App\Settings\SystemSettings;
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
use App\Settings\BehaviorSettings\DataSourceSynonymsSettings;
use Jbtronics\SettingsBundle\Settings\Settings;
use Symfony\Component\Translation\TranslatableMessage as TM;