mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-13 11:54:32 +02:00
Fixed multi-part action selectors.
This commit is contained in:
parent
b8da4c62d0
commit
7ff1584eb9
8 changed files with 266 additions and 190 deletions
|
@ -25,6 +25,7 @@ namespace App\Form\Type;
|
|||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use App\Services\Trees\NodesListBuilder;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
@ -41,9 +42,9 @@ class CurrencyEntityType extends StructuralEntityType
|
|||
{
|
||||
protected ?string $base_currency;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator, ?string $base_currency)
|
||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, TranslatorInterface $translator, StructuralEntityChoiceHelper $choiceHelper, ?string $base_currency)
|
||||
{
|
||||
parent::__construct($em, $builder, $attachmentURLGenerator, $translator);
|
||||
parent::__construct($em, $builder, $translator, $choiceHelper);
|
||||
$this->base_currency = $base_currency;
|
||||
}
|
||||
|
||||
|
@ -59,6 +60,12 @@ class CurrencyEntityType extends StructuralEntityType
|
|||
// This options allows you to override the currency shown for the null value
|
||||
$resolver->setDefault('base_currency', null);
|
||||
|
||||
$resolver->setDefault('choice_attr', function (Options $options) {
|
||||
return function ($choice) use ($options) {
|
||||
return $this->choice_helper->generateChoiceAttrCurrency($choice, $options);
|
||||
};
|
||||
});
|
||||
|
||||
$resolver->setDefault('empty_message', function (Options $options) {
|
||||
//By default we use the global base currency:
|
||||
$iso_code = $this->base_currency;
|
||||
|
@ -75,62 +82,4 @@ class CurrencyEntityType extends StructuralEntityType
|
|||
//If short is set to true, then the name of the entity will only shown in the dropdown list not in the selected value.
|
||||
$resolver->setDefault('short', false);
|
||||
}
|
||||
|
||||
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
|
||||
{
|
||||
$tmp = parent::generateChoiceAttr($choice, $key, $value, $options);
|
||||
|
||||
if (!$choice instanceof Currency) {
|
||||
throw new RuntimeException('The choice must be an instance of '.Currency::class);
|
||||
}
|
||||
|
||||
if(!empty($choice->getIsoCode())) {
|
||||
$symbol = Currencies::getSymbol($choice->getIsoCode());
|
||||
} else {
|
||||
$symbol = null;
|
||||
}
|
||||
|
||||
if ($options['short']) {
|
||||
$tmp['data-short'] = $symbol;
|
||||
} else {
|
||||
$tmp['data-short'] = $choice->getName();
|
||||
}
|
||||
|
||||
$tmp += [
|
||||
'data-symbol' => $symbol,
|
||||
];
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, $value, $options): string
|
||||
{
|
||||
if(!$choice instanceof Currency) {
|
||||
throw new RuntimeException('$choice must be an instance of Currency!');
|
||||
}
|
||||
|
||||
//Generate the level spacing
|
||||
/** @var AbstractStructuralDBElement|null $parent */
|
||||
$parent = $options['subentities_of'];
|
||||
/*** @var AbstractStructuralDBElement $choice */
|
||||
$level = $choice->getLevel();
|
||||
//If our base entity is not the root level, we need to change the level, to get zero position
|
||||
if (null !== $options['subentities_of']) {
|
||||
$level -= $parent->getLevel() - 1;
|
||||
}
|
||||
|
||||
$tmp = str_repeat('<span class="picker-level"></span>', $level);
|
||||
|
||||
//Show currency symbol or ISO code and the name of the currency
|
||||
if(!empty($choice->getIsoCode())) {
|
||||
$tmp .= Currencies::getSymbol($choice->getIsoCode());
|
||||
//Add currency name as badge
|
||||
$tmp .= sprintf('<span class="badge bg-primary ms-2 %s">%s</span>', $options['short'] ? 'picker-hs' : '' , htmlspecialchars($choice->getName()));
|
||||
} else {
|
||||
$tmp .= htmlspecialchars($choice->getName());
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
161
src/Form/Type/Helper/StructuralEntityChoiceHelper.php
Normal file
161
src/Form/Type/Helper/StructuralEntityChoiceHelper.php
Normal file
|
@ -0,0 +1,161 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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/>.
|
||||
*/
|
||||
|
||||
namespace App\Form\Type\Helper;
|
||||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Intl\Currencies;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class StructuralEntityChoiceHelper
|
||||
{
|
||||
|
||||
private AttachmentURLGenerator $attachmentURLGenerator;
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
|
||||
{
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the choice attributes for the given AbstractStructuralDBElement.
|
||||
* @param AbstractStructuralDBElement $choice
|
||||
* @param Options|array $options
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function generateChoiceAttr(AbstractStructuralDBElement $choice, $options): array
|
||||
{
|
||||
$tmp = [];
|
||||
|
||||
//Disable attribute if the choice is marked as not selectable
|
||||
if (($options['disable_not_selectable'] ?? false) && $choice->isNotSelectable()) {
|
||||
$tmp += ['disabled' => 'disabled'];
|
||||
}
|
||||
|
||||
if ($choice instanceof AttachmentType) {
|
||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||
}
|
||||
|
||||
$level = $choice->getLevel();
|
||||
/** @var AbstractStructuralDBElement|null $parent */
|
||||
$parent = $options['subentities_of'] ?? null;
|
||||
if (null !== $parent) {
|
||||
$level -= $parent->getLevel() - 1;
|
||||
}
|
||||
|
||||
$tmp += [
|
||||
'data-level' => $level,
|
||||
'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
|
||||
'data-path' => $choice->getFullPath('->'),
|
||||
'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
|
||||
];
|
||||
|
||||
if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
|
||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the choice attributes for the given AbstractStructuralDBElement.
|
||||
* @param Currency $choice
|
||||
* @param Options|array $options
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function generateChoiceAttrCurrency(Currency $choice, $options): array
|
||||
{
|
||||
$tmp = $this->generateChoiceAttr($choice, $options);
|
||||
|
||||
if(!empty($choice->getIsoCode())) {
|
||||
$symbol = Currencies::getSymbol($choice->getIsoCode());
|
||||
} else {
|
||||
$symbol = null;
|
||||
}
|
||||
|
||||
if ($options['short']) {
|
||||
$tmp['data-short'] = $symbol;
|
||||
} else {
|
||||
$tmp['data-short'] = $choice->getName();
|
||||
}
|
||||
|
||||
$tmp += [
|
||||
'data-symbol' => $symbol,
|
||||
];
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the choice label for the given AbstractStructuralDBElement.
|
||||
* @param AbstractStructuralDBElement $choice
|
||||
* @return string
|
||||
*/
|
||||
public function generateChoiceLabel(AbstractStructuralDBElement $choice): string
|
||||
{
|
||||
return $choice->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the choice value for the given AbstractStructuralDBElement.
|
||||
* @param AbstractStructuralDBElement|null $element
|
||||
* @return string|int|null
|
||||
*/
|
||||
public function generateChoiceValue(?AbstractStructuralDBElement $element)
|
||||
{
|
||||
if ($element === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not change the structure below, even when inspection says it can be replaced with a null coalescing operator.
|
||||
* It is important that the value returned here for a existing element is an int, and for a new element a string.
|
||||
* I dont really understand why, but it seems to be important for the choice_loader to work correctly.
|
||||
* So please do not change this!
|
||||
*/
|
||||
if ($element->getID() === null) {
|
||||
//Must be the same as the separator in the choice_loader, otherwise this will not work!
|
||||
return $element->getFullPath('->');
|
||||
}
|
||||
|
||||
return $element->getID();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractStructuralDBElement|null $element
|
||||
* @return string|null
|
||||
*/
|
||||
public function generateGroupBy(AbstractStructuralDBElement $element): ?string
|
||||
{
|
||||
//Show entities that are not added to DB yet separately from other entities
|
||||
if ($element->getID() === null) {
|
||||
return $this->translator->trans('entity.select.group.new_not_added_to_DB');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ namespace App\Form\Type;
|
|||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
|
||||
use App\Form\Type\Helper\StructuralEntityChoiceLoader;
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use App\Services\Trees\NodesListBuilder;
|
||||
|
@ -53,20 +54,20 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
|||
class StructuralEntityType extends AbstractType
|
||||
{
|
||||
protected EntityManagerInterface $em;
|
||||
protected AttachmentURLGenerator $attachmentURLGenerator;
|
||||
protected TranslatorInterface $translator;
|
||||
protected StructuralEntityChoiceHelper $choice_helper;
|
||||
|
||||
/**
|
||||
* @var NodesListBuilder
|
||||
*/
|
||||
protected $builder;
|
||||
protected NodesListBuilder $builder;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
|
||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, TranslatorInterface $translator, StructuralEntityChoiceHelper $choice_helper)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->builder = $builder;
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
$this->translator = $translator;
|
||||
$this->choice_helper = $choice_helper;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
|
@ -105,44 +106,23 @@ class StructuralEntityType extends AbstractType
|
|||
'subentities_of' => null, //Only show entities with the given parent class
|
||||
'disable_not_selectable' => false, //Disable entries with not selectable property
|
||||
'choice_value' => function (?AbstractStructuralDBElement $element) {
|
||||
if ($element === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not change the structure below, even when inspection says it can be replaced with a null coalescing operator.
|
||||
* It is important that the value returned here for a existing element is an int, and for a new element a string.
|
||||
* I dont really understand why, but it seems to be important for the choice_loader to work correctly.
|
||||
* So please do not change this!
|
||||
*/
|
||||
if ($element->getID() === null) {
|
||||
//Must be the same as the separator in the choice_loader, otherwise this will not work!
|
||||
return $element->getFullPath('->');
|
||||
}
|
||||
|
||||
return $element->getID();
|
||||
return $this->choice_helper->generateChoiceValue($element);
|
||||
}, //Use the element id as option value and for comparing items
|
||||
'choice_loader' => function (Options $options) {
|
||||
return new StructuralEntityChoiceLoader($options, $this->builder, $this->em);
|
||||
},
|
||||
'choice_label' => function (Options $options) {
|
||||
return function ($choice, $key, $value) use ($options) {
|
||||
return $this->generateChoiceLabels($choice, $key, $value, $options);
|
||||
return $this->choice_helper->generateChoiceLabel($choice, $options);
|
||||
};
|
||||
},
|
||||
'choice_attr' => function (Options $options) {
|
||||
return function ($choice, $key, $value) use ($options) {
|
||||
return $this->generateChoiceAttr($choice, $key, $value, $options);
|
||||
return $this->choice_helper->generateChoiceAttr($choice, $options);
|
||||
};
|
||||
},
|
||||
'group_by' => function (AbstractStructuralDBElement $element)
|
||||
{
|
||||
//Show entities that are not added to DB yet separately from other entities
|
||||
if ($element->getID() === null) {
|
||||
return $this->translator->trans('entity.select.group.new_not_added_to_DB');
|
||||
}
|
||||
|
||||
return null;
|
||||
'group_by' => function (AbstractStructuralDBElement $element) {
|
||||
return $this->choice_helper->generateGroupBy($element);
|
||||
},
|
||||
'choice_translation_domain' => false, //Don't translate the entity names
|
||||
]);
|
||||
|
@ -207,61 +187,4 @@ class StructuralEntityType extends AbstractType
|
|||
//Otherwise just return the value
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
|
||||
{
|
||||
$tmp = [];
|
||||
|
||||
//Disable attribute if the choice is marked as not selectable
|
||||
if ($options['disable_not_selectable'] && $choice->isNotSelectable()) {
|
||||
$tmp += ['disabled' => 'disabled'];
|
||||
}
|
||||
|
||||
if ($choice instanceof AttachmentType) {
|
||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||
}
|
||||
|
||||
$level = $choice->getLevel();
|
||||
/** @var AbstractStructuralDBElement|null $parent */
|
||||
$parent = $options['subentities_of'];
|
||||
if (null !== $parent) {
|
||||
$level -= $parent->getLevel() - 1;
|
||||
}
|
||||
|
||||
$tmp += [
|
||||
'data-level' => $level,
|
||||
'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
|
||||
'data-path' => $choice->getFullPath('->'),
|
||||
'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
|
||||
];
|
||||
|
||||
if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
|
||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
protected function getElementNameWithLevelWhitespace(AbstractStructuralDBElement $choice, $options, $whitespace = " "): string
|
||||
{
|
||||
/** @var AbstractStructuralDBElement|null $parent */
|
||||
$parent = $options['subentities_of'];
|
||||
|
||||
/*** @var AbstractStructuralDBElement $choice */
|
||||
$level = $choice->getLevel();
|
||||
//If our base entity is not the root level, we need to change the level, to get zero position
|
||||
if (null !== $options['subentities_of']) {
|
||||
$level -= $parent->getLevel() - 1;
|
||||
}
|
||||
|
||||
$tmp = str_repeat($whitespace, $level); //Use 3 spaces for intendation
|
||||
$tmp .= htmlspecialchars($choice->getName());
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
protected function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value, $options): string
|
||||
{
|
||||
return $choice->getName();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue