Use ElementTypeNameGenerator where possible

This commit is contained in:
Jan Böhmer 2025-11-10 00:48:25 +01:00
parent e95197b069
commit c372109a2f
5 changed files with 37 additions and 60 deletions

View file

@ -75,17 +75,17 @@ final readonly class ElementTypeNameGenerator
if ($this->synonymsSettings->isSynonymDefinedForType($type)) {
if ($plural) {
$syn = $this->synonymsSettings->getSingularSynonymForType($type, $locale);
} else {
$syn = $this->synonymsSettings->getPluralSynonymForType($type, $locale);
} else {
$syn = $this->synonymsSettings->getSingularSynonymForType($type, $locale);
}
if ($syn === null) {
//Try to fall back to english
if ($plural) {
$syn = $this->synonymsSettings->getSingularSynonymForType($type, 'en');
} else {
$syn = $this->synonymsSettings->getPluralSynonymForType($type, 'en');
} else {
$syn = $this->synonymsSettings->getSingularSynonymForType($type, 'en');
}
}

View file

@ -38,7 +38,7 @@ use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Helpers\Trees\TreeViewNode;
use App\Services\Cache\UserCacheKeyGenerator;
use App\Services\Misc\DataSourceSynonymResolver;
use App\Services\ElementTypeNameGenerator;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Cache\ItemInterface;
@ -57,7 +57,7 @@ class ToolsTreeBuilder
protected TagAwareCacheInterface $cache,
protected UserCacheKeyGenerator $keyGenerator,
protected Security $security,
protected readonly DataSourceSynonymResolver $synonymResolver,
private readonly ElementTypeNameGenerator $elementTypeNameGenerator,
) {
}
@ -167,90 +167,67 @@ class ToolsTreeBuilder
if ($this->security->isGranted('read', new AttachmentType())) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.attachment_types'),
$this->elementTypeNameGenerator->typeLabelPlural(AttachmentType::class),
$this->urlGenerator->generate('attachment_type_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-file-alt');
}
if ($this->security->isGranted('read', new Category())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'category',
'tree.tools.edit.categories',
$this->translator->getLocale()
),
$this->elementTypeNameGenerator->typeLabelPlural(Category::class),
$this->urlGenerator->generate('category_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-tags');
}
if ($this->security->isGranted('read', new Project())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'project',
'tree.tools.edit.projects',
$this->translator->getLocale()),
$this->elementTypeNameGenerator->typeLabelPlural(Project::class),
$this->urlGenerator->generate('project_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-archive');
}
if ($this->security->isGranted('read', new Supplier())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'supplier',
'tree.tools.edit.suppliers',
$this->translator->getLocale()
),
$this->elementTypeNameGenerator->typeLabelPlural(Supplier::class),
$this->urlGenerator->generate('supplier_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-truck');
}
if ($this->security->isGranted('read', new Manufacturer())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'manufacturer',
'tree.tools.edit.manufacturer',
$this->translator->getLocale()
),
$this->elementTypeNameGenerator->typeLabelPlural(Manufacturer::class),
$this->urlGenerator->generate('manufacturer_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-industry');
}
if ($this->security->isGranted('read', new StorageLocation())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'storagelocation',
'tree.tools.edit.storelocation',
$this->translator->getLocale()
),
$this->elementTypeNameGenerator->typeLabelPlural(StorageLocation::class),
$this->urlGenerator->generate('store_location_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-cube');
}
if ($this->security->isGranted('read', new Footprint())) {
$nodes[] = (new TreeViewNode(
$this->synonymResolver->displayNamePlural(
'footprint',
'tree.tools.edit.footprint',
$this->translator->getLocale()
),
$this->elementTypeNameGenerator->typeLabelPlural(Footprint::class),
$this->urlGenerator->generate('footprint_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-microchip');
}
if ($this->security->isGranted('read', new Currency())) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.currency'),
$this->elementTypeNameGenerator->typeLabelPlural(Currency::class),
$this->urlGenerator->generate('currency_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-coins');
}
if ($this->security->isGranted('read', new MeasurementUnit())) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.measurement_unit'),
$this->elementTypeNameGenerator->typeLabelPlural(MeasurementUnit::class),
$this->urlGenerator->generate('measurement_unit_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-balance-scale');
}
if ($this->security->isGranted('read', new LabelProfile())) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.label_profile'),
$this->elementTypeNameGenerator->typeLabelPlural(LabelProfile::class),
$this->urlGenerator->generate('label_profile_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-qrcode');
}
if ($this->security->isGranted('read', new PartCustomState())) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.part_custom_state'),
$this->elementTypeNameGenerator->typeLabelPlural(PartCustomState::class),
$this->urlGenerator->generate('part_custom_state_new')
))->setIcon('fa-fw fa-treeview fa-solid fa-tools');
}

View file

@ -36,6 +36,7 @@ use App\Helpers\Trees\TreeViewNodeIterator;
use App\Repository\NamedDBElementRepository;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
use App\Services\ElementTypeNameGenerator;
use App\Services\EntityURLGenerator;
use App\Services\Misc\DataSourceSynonymResolver;
use App\Settings\BehaviorSettings\SidebarSettings;
@ -67,7 +68,8 @@ class TreeViewGenerator
protected TranslatorInterface $translator,
private readonly UrlGeneratorInterface $router,
private readonly SidebarSettings $sidebarSettings,
protected readonly DataSourceSynonymResolver $synonymResolver
protected readonly DataSourceSynonymResolver $synonymResolver,
private readonly ElementTypeNameGenerator $elementTypeNameGenerator
) {
$this->rootNodeEnabled = $this->sidebarSettings->rootNodeEnabled;
$this->rootNodeExpandedByDefault = $this->sidebarSettings->rootNodeExpanded;
@ -213,17 +215,7 @@ class TreeViewGenerator
protected function entityClassToRootNodeString(string $class): string
{
$locale = $this->translator->getLocale();
return match ($class) {
Category::class => $this->synonymResolver->displayNamePlural('category', $locale),
StorageLocation::class => $this->synonymResolver->displayNamePlural('storelocation', $locale),
Footprint::class => $this->synonymResolver->displayNamePlural('footprint', $locale),
Manufacturer::class => $this->synonymResolver->displayNamePlural('manufacturer', $locale),
Supplier::class => $this->synonymResolver->displayNamePlural('supplier', $locale),
Project::class => $this->synonymResolver->displayNamePlural('project', $locale),
default => $this->translator->trans('tree.root_node.text'),
};
return $this->elementTypeNameGenerator->typeLabelPlural($class);
}
protected function entityClassToRootNodeIcon(string $class): ?string

View file

@ -76,6 +76,8 @@ final class EntityExtension extends AbstractExtension
/* Gets a human readable label for the type of the given entity */
new TwigFunction('entity_type_label', fn(object|string $entity): string => $this->nameGenerator->getLocalizedTypeLabel($entity)),
new TwigFunction('type_label', fn(object|string $entity): string => $this->nameGenerator->typeLabel($entity)),
new TwigFunction('type_label_p', fn(object|string $entity): string => $this->nameGenerator->typeLabelPlural($entity)),
];
}

View file

@ -3,12 +3,12 @@
{# Format is [mode, route, label, show_condition] #}
{% set data_sources = [
['categories', path('tree_category_root'), 'category.labelp', is_granted('@categories.read') and is_granted('@parts.read'), 'category'],
['locations', path('tree_location_root'), 'storelocation.labelp', is_granted('@storelocations.read') and is_granted('@parts.read'), 'storagelocation'],
['footprints', path('tree_footprint_root'), 'footprint.labelp', is_granted('@footprints.read') and is_granted('@parts.read'), 'footprint'],
['manufacturers', path('tree_manufacturer_root'), 'manufacturer.labelp', is_granted('@manufacturers.read') and is_granted('@parts.read'), 'manufacturer'],
['suppliers', path('tree_supplier_root'), 'supplier.labelp', is_granted('@suppliers.read') and is_granted('@parts.read'), 'supplier'],
['projects', path('tree_device_root'), 'project.labelp', is_granted('@projects.read'), 'project'],
['categories', path('tree_category_root'), '@category@@', is_granted('@categories.read') and is_granted('@parts.read')],
['locations', path('tree_location_root'), '@storage_location@@', is_granted('@storelocations.read') and is_granted('@parts.read'), ],
['footprints', path('tree_footprint_root'), '@footprint@@', is_granted('@footprints.read') and is_granted('@parts.read')],
['manufacturers', path('tree_manufacturer_root'), '@manufacturer@@', is_granted('@manufacturers.read') and is_granted('@parts.read'), 'manufacturer'],
['suppliers', path('tree_supplier_root'), '@supplier@@', is_granted('@suppliers.read') and is_granted('@parts.read'), 'supplier'],
['projects', path('tree_device_root'), '@project@@', is_granted('@projects.read'), 'project'],
['tools', path('tree_tools'), 'tools.label', true, 'tool'],
] %}
@ -21,12 +21,18 @@
{% for source in data_sources %}
{% if source[3] %} {# show_condition #}
<li>
{% if source[2] starts with '@' %}
{% set label = type_label_p(source[2]|replace({'@': ''})) %}
{% else %}
{% set label = source[2]|trans %}
{% endif %}
<button class="tree-btns dropdown-item"
data-mode="{{ source[0] }}"
data-url="{{ source[1] }}"
data-text="{{ get_data_source_name_plural(source[4], source[2]) }}"
data-text="{{ label }}"
{{ stimulus_action('elements/sidebar_tree', 'changeDataSource') }}
>{{ get_data_source_name_plural(source[4], source[2]) }}</button>
>{{ label }}</button>
</li>
{% endif %}
{% endfor %}