feat: pickup selection depending on bus chaperon
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Form;
|
||||
use App\Entity\Application;
|
||||
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\TextareaType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
@@ -27,32 +28,36 @@ class TeamerApplicationType extends AbstractType
|
||||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
/** @var Application $application */
|
||||
$application = $event->getData();
|
||||
$form = $event->getForm();
|
||||
|
||||
if (null === $application) {
|
||||
return;
|
||||
}
|
||||
|
||||
$assignment = $application->getAssignment();
|
||||
|
||||
if (0 === (int) $assignment->getPickup()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$teamer = $application->getTeamer();
|
||||
|
||||
if (in_array($assignment->getPickup(), $teamer->getPickups())) {
|
||||
return;
|
||||
if (0 === (int) $assignment->getPickup()) {
|
||||
$pickupChoices = [];
|
||||
foreach ($assignment->getDestination()->getPickups() as $pickup) {
|
||||
$pickupChoices[$pickup['city']] = $pickup['busProId'];
|
||||
}
|
||||
ksort($pickupChoices);
|
||||
$form->add('pickup', ChoiceType::class, [
|
||||
'label' => 'Zustieg in',
|
||||
'choices' => $pickupChoices,
|
||||
]);
|
||||
} elseif (false === in_array($assignment->getPickup(), $teamer->getPickups())) {
|
||||
$form->add('confirmPickup', CheckboxType::class, [
|
||||
'label' => 'Ich bestätige den abweichenden Buszustieg',
|
||||
'mapped' => false,
|
||||
'constraints' => [
|
||||
new IsTrue([
|
||||
'message' => 'Deine Bestätigung ist erforderlich',
|
||||
]),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$event->getForm()->add('confirmPickup', CheckboxType::class, [
|
||||
'label' => 'Ich bestätige den abweichenden Buszustieg',
|
||||
'mapped' => false,
|
||||
'constraints' => [
|
||||
new IsTrue([
|
||||
'message' => 'Deine Bestätigung ist erforderlich',
|
||||
]),
|
||||
],
|
||||
]);
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class TeamerDispositionType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if (true === $options['enable_pickup']) {
|
||||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
/** @var Disposition $disposition */
|
||||
$disposition = $event->getData();
|
||||
$assignment = $disposition->getAssignment();
|
||||
|
||||
// Add pickup select field only to assignments that are not including bus chaperon
|
||||
if (0 === (int) $assignment->getPickup()) {
|
||||
$pickupChoices = [];
|
||||
foreach ($assignment->getDestination()->getPickups() as $pickup) {
|
||||
$pickupChoices[$pickup['city']] = $pickup['busProId'];
|
||||
}
|
||||
ksort($pickupChoices);
|
||||
$event->getForm()->add('pickup', ChoiceType::class, [
|
||||
'label' => 'Zustieg in',
|
||||
'choices' => $pickupChoices,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Disposition::class,
|
||||
'enable_pickup' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user