Show the value returned by the provider on part creation page.

This makes it easier to check or assign a element manually
This commit is contained in:
Jan Böhmer 2023-07-16 23:19:02 +02:00
parent b74ab18a6d
commit c810b6772c
5 changed files with 61 additions and 2 deletions

View file

@ -88,9 +88,12 @@ class InfoProviderController extends AbstractController
{ {
$this->denyAccessUnlessGranted('@info_providers.create_parts'); $this->denyAccessUnlessGranted('@info_providers.create_parts');
$new_part = $this->infoRetriever->createPart($providerKey, $providerId); $dto = $this->infoRetriever->getDetails($providerKey, $providerId);
$new_part = $this->infoRetriever->dtoToPart($dto);
$form = $this->createForm(PartBaseType::class, $new_part); $form = $this->createForm(PartBaseType::class, $new_part, [
'info_provider_dto' => $dto,
]);
$form->handleRequest($request); $form->handleRequest($request);

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Form\Part; namespace App\Form\Part;
use App\Entity\Parts\ManufacturingStatus; use App\Entity\Parts\ManufacturingStatus;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
use App\Entity\Attachments\PartAttachment; use App\Entity\Attachments\PartAttachment;
use App\Entity\Parameters\PartParameter; use App\Entity\Parameters\PartParameter;
@ -51,6 +52,7 @@ use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class PartBaseType extends AbstractType class PartBaseType extends AbstractType
{ {
@ -64,6 +66,8 @@ class PartBaseType extends AbstractType
$part = $builder->getData(); $part = $builder->getData();
$new_part = null === $part->getID(); $new_part = null === $part->getID();
/** @var PartDetailDTO|null $dto */
$dto = $options['info_provider_dto'];
//Common section //Common section
$builder $builder
@ -95,6 +99,7 @@ class PartBaseType extends AbstractType
->add('category', StructuralEntityType::class, [ ->add('category', StructuralEntityType::class, [
'class' => Category::class, 'class' => Category::class,
'allow_add' => $this->security->isGranted('@categories.create'), 'allow_add' => $this->security->isGranted('@categories.create'),
'dto_value' => $dto?->category,
'label' => 'part.edit.category', 'label' => 'part.edit.category',
'disable_not_selectable' => true, 'disable_not_selectable' => true,
]) ])
@ -102,6 +107,7 @@ class PartBaseType extends AbstractType
'class' => Footprint::class, 'class' => Footprint::class,
'required' => false, 'required' => false,
'label' => 'part.edit.footprint', 'label' => 'part.edit.footprint',
'dto_value' => $dto?->footprint,
'allow_add' => $this->security->isGranted('@footprints.create'), 'allow_add' => $this->security->isGranted('@footprints.create'),
'disable_not_selectable' => true, 'disable_not_selectable' => true,
]) ])
@ -122,6 +128,7 @@ class PartBaseType extends AbstractType
'required' => false, 'required' => false,
'label' => 'part.edit.manufacturer.label', 'label' => 'part.edit.manufacturer.label',
'allow_add' => $this->security->isGranted('@manufacturers.create'), 'allow_add' => $this->security->isGranted('@manufacturers.create'),
'dto_value' => $dto?->manufacturer,
'disable_not_selectable' => true, 'disable_not_selectable' => true,
]) ])
->add('manufacturer_product_url', UrlType::class, [ ->add('manufacturer_product_url', UrlType::class, [
@ -268,10 +275,15 @@ class PartBaseType extends AbstractType
->add('reset', ResetType::class, ['label' => 'part.edit.reset']); ->add('reset', ResetType::class, ['label' => 'part.edit.reset']);
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver): void
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => Part::class, 'data_class' => Part::class,
'info_provider_dto' => null,
]); ]);
$resolver->setAllowedTypes('info_provider_dto', [PartDetailDTO::class, 'null']);
} }
} }

View file

@ -100,6 +100,17 @@ class StructuralEntityType extends AbstractType
$resolver->setDefault('controller', 'elements--structural-entity-select'); $resolver->setDefault('controller', 'elements--structural-entity-select');
//Options for DTO values
$resolver->setDefault('dto_value', null);
$resolver->setAllowedTypes('dto_value', ['null', 'string']);
//If no help text is explicitly set, we use the dto value as help text and show it as html
$resolver->setDefault('help', function (Options $options) {
return $this->dtoText($options['dto_value']);
});
$resolver->setDefault('help_html', function (Options $options) {
return $options['dto_value'] !== null;
});
$resolver->setDefault('attr', function (Options $options) { $resolver->setDefault('attr', function (Options $options) {
$tmp = [ $tmp = [
'data-controller' => $options['controller'], 'data-controller' => $options['controller'],
@ -114,6 +125,16 @@ class StructuralEntityType extends AbstractType
}); });
} }
private function dtoText(?string $text): ?string
{
if ($text === null) {
return null;
}
$result = '<b>' . $this->translator->trans('info_providers.form.help_prefix') . ':</b> ';
return $result . htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ;
}
public function getParent(): string public function getParent(): string
{ {

View file

@ -100,11 +100,28 @@ final class PartInfoRetriever
}); });
} }
/**
* Retrieves the details for a part, based on the given search result.
* @param SearchResultDTO $search_result
* @return PartDetailDTO
*/
public function getDetailsForSearchResult(SearchResultDTO $search_result): PartDetailDTO public function getDetailsForSearchResult(SearchResultDTO $search_result): PartDetailDTO
{ {
return $this->getDetails($search_result->provider_key, $search_result->provider_id); return $this->getDetails($search_result->provider_key, $search_result->provider_id);
} }
/**
* Converts the given DTO to a part entity
* @return Part
*/
public function dtoToPart(PartDetailDTO $search_result): Part
{
return $this->createPart($search_result->provider_key, $search_result->provider_id);
}
/**
* Use the given details to create a part entity
*/
public function createPart(string $provider_key, string $part_id): Part public function createPart(string $provider_key, string $part_id): Part
{ {
$details = $this->getDetails($provider_key, $part_id); $details = $this->getDetails($provider_key, $part_id);

View file

@ -11573,5 +11573,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>The alternative names given here, are used to find this element based on the results of the information providers.</target> <target>The alternative names given here, are used to find this element based on the results of the information providers.</target>
</segment> </segment>
</unit> </unit>
<unit id="TRmIjDN" name="info_providers.form.help_prefix">
<segment>
<source>info_providers.form.help_prefix</source>
<target>Provider</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>