diff --git a/src/Controller/InfoProviderController.php b/src/Controller/InfoProviderController.php index 3828a74e..d7debe56 100644 --- a/src/Controller/InfoProviderController.php +++ b/src/Controller/InfoProviderController.php @@ -88,9 +88,12 @@ class InfoProviderController extends AbstractController { $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); diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index 1fbaa55e..b15ec29f 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Form\Part; use App\Entity\Parts\ManufacturingStatus; +use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use Symfony\Bundle\SecurityBundle\Security; use App\Entity\Attachments\PartAttachment; 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\OptionsResolver\OptionsResolver; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; class PartBaseType extends AbstractType { @@ -64,6 +66,8 @@ class PartBaseType extends AbstractType $part = $builder->getData(); $new_part = null === $part->getID(); + /** @var PartDetailDTO|null $dto */ + $dto = $options['info_provider_dto']; //Common section $builder @@ -95,6 +99,7 @@ class PartBaseType extends AbstractType ->add('category', StructuralEntityType::class, [ 'class' => Category::class, 'allow_add' => $this->security->isGranted('@categories.create'), + 'dto_value' => $dto?->category, 'label' => 'part.edit.category', 'disable_not_selectable' => true, ]) @@ -102,6 +107,7 @@ class PartBaseType extends AbstractType 'class' => Footprint::class, 'required' => false, 'label' => 'part.edit.footprint', + 'dto_value' => $dto?->footprint, 'allow_add' => $this->security->isGranted('@footprints.create'), 'disable_not_selectable' => true, ]) @@ -122,6 +128,7 @@ class PartBaseType extends AbstractType 'required' => false, 'label' => 'part.edit.manufacturer.label', 'allow_add' => $this->security->isGranted('@manufacturers.create'), + 'dto_value' => $dto?->manufacturer, 'disable_not_selectable' => true, ]) ->add('manufacturer_product_url', UrlType::class, [ @@ -268,10 +275,15 @@ class PartBaseType extends AbstractType ->add('reset', ResetType::class, ['label' => 'part.edit.reset']); } + + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => Part::class, + 'info_provider_dto' => null, ]); + + $resolver->setAllowedTypes('info_provider_dto', [PartDetailDTO::class, 'null']); } } diff --git a/src/Form/Type/StructuralEntityType.php b/src/Form/Type/StructuralEntityType.php index fbc294e4..18368289 100644 --- a/src/Form/Type/StructuralEntityType.php +++ b/src/Form/Type/StructuralEntityType.php @@ -100,6 +100,17 @@ class StructuralEntityType extends AbstractType $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) { $tmp = [ 'data-controller' => $options['controller'], @@ -114,6 +125,16 @@ class StructuralEntityType extends AbstractType }); } + private function dtoText(?string $text): ?string + { + if ($text === null) { + return null; + } + + $result = '' . $this->translator->trans('info_providers.form.help_prefix') . ': '; + + return $result . htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ; + } public function getParent(): string { diff --git a/src/Services/InfoProviderSystem/PartInfoRetriever.php b/src/Services/InfoProviderSystem/PartInfoRetriever.php index 9b0c3d15..17f4acbc 100644 --- a/src/Services/InfoProviderSystem/PartInfoRetriever.php +++ b/src/Services/InfoProviderSystem/PartInfoRetriever.php @@ -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 { 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 { $details = $this->getDetails($provider_key, $part_id); diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 20680fb7..6a321966 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -11573,5 +11573,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g The alternative names given here, are used to find this element based on the results of the information providers. + + + info_providers.form.help_prefix + Provider + +