fix: only use destination's pickup values

This commit is contained in:
Björn Fromme committed 2024-01-28 17:49:46 +01:00
1 parent 85200e2960
commit 75bd055474
18 files changed
+286 -106

No files matched your search

+63 -5
View File
@@ -8,18 +8,27 @@ use App\Entity\Fee;
use App\Entity\JobProfile;
use App\Entity\Upload;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AssignmentType extends AbstractType
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
@@ -37,6 +46,7 @@ class AssignmentType extends AbstractType
'label_property' => 'product',
'label_function' => fn(Destination $destination) => (string) $destination,
'endpoint_route' => 'app_admin_autocomplete_destination',
'controller_action' => 'select->form-partial#update',
])
->add('jobProfile', EntityType::class, [
'label' => 'Jobprofil',
@@ -60,11 +70,6 @@ class AssignmentType extends AbstractType
'label' => 'abweichender Zustieg bei Busbegleitung',
'required' => false,
])
->add('pickup', BpnPickupType::class, [
'label' => 'Busbegleitung ab',
'required' => false,
'placeholder' => 'Keine Busbegleitung',
])
->add('benefits', TextareaType::class, [
'label' => 'Benefits',
'required' => false,
@@ -133,9 +138,62 @@ class AssignmentType extends AbstractType
'rows' => 3,
],
])
->add('pickup', ChoiceType::class, [
'label' => 'Busbegleitung ab',
'required' => false,
'placeholder' => 'Keine Busbegleitung',
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$destination = $data->getDestination();
if (null === $destination) {
return;
}
$this->addPickupField($destination, $form);
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$destinationId = $data['destination'] ?? null;
if (null === $destinationId) {
return;
}
$destination = $this
->entityManager
->getRepository(Destination::class)
->find($destinationId)
;
$this->addPickupField($destination, $form);
})
;
}
private function addPickupField(Destination $destination, FormInterface $form): void
{
$choices = [];
foreach ($destination->getPickups() as $pickup) {
$label = sprintf('%s, %s Uhr', trim($pickup['city']), trim($pickup['time']));
$choices[$label] = $pickup['busProId'];
}
// Remove placeholder field first
$form->remove('pickup');
$form->add('pickup', ChoiceType::class, [
'label' => 'Busbegleitung ab',
'required' => false,
'placeholder' => 'Keine Busbegleitung',
'choices' => $choices,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([