fix: only use destination's pickup values

This commit is contained in:
Björn Fromme
2024-01-28 17:49:46 +01:00
parent 85200e2960
commit 75bd055474
18 changed files with 286 additions and 106 deletions
+29 -2
View File
@@ -7,8 +7,23 @@ class Pickup
private ?int $id = null;
private ?int $busProId = null;
private ?string $code = null;
private ?string $city;
private ?string $street;
private ?string $city = null;
private ?string $street = null;
private ?string $time = null;
public static function fromArray(array $data): static
{
$instance = new static();
$instance
->setId($data['busProId'])
->setCode($data['code'] ?? null)
->setCity($data['city'])
->setStreet($data['street'])
->setTime($data['time'])
;
return $instance;
}
public function getId(): ?int
{
@@ -69,4 +84,16 @@ class Pickup
return $this;
}
public function getTime(): ?string
{
return $this->time;
}
public function setTime(?string $time): static
{
$this->time = $time;
return $this;
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Controller\Admin\Assignment;
use App\Entity\Assignment;
use App\Form\AssignmentType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class PickupFormController extends AbstractController
{
#[Route('/admin/assignment/pickup-form', name: 'app_admin_assignment_pickup_form')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): Response
{
$assignment = new Assignment();
$form = $this->createForm(AssignmentType::class, $assignment);
$form->handleRequest($request);
return $this->render('admin/assignment/_form_pickup.html.twig', [
'form' => $form->createView(),
]);
}
}
+12
View File
@@ -2,6 +2,7 @@
namespace App\Entity;
use App\BusProNet\Model\Pickup;
use App\Entity\Traits\SoftDeletableEntity;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\DestinationRepository;
@@ -171,6 +172,17 @@ class Destination implements TimestampableEntityInterface, SoftDeletableEntityIn
return $this;
}
public function getPickup(int $id): ?Pickup
{
foreach ($this->getPickups() as $pickup) {
if ($id === $pickup['busProId']) {
return Pickup::fromArray($pickup);
}
}
return null;
}
public function hasBusTransfer(): bool
{
return 0 < count($this->getPickups());
+1 -1
View File
@@ -43,7 +43,7 @@ class Disposition implements BlameableEntityInterface, TimestampableEntityInterf
private ?Teamer $teamer;
#[ORM\Column(nullable: true)]
private ?int $pickup = null;
private ?int $pickup;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $remarks;
+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([
+2 -2
View File
@@ -50,7 +50,7 @@ class AppRuntime implements RuntimeExtensionInterface
return 'nationality' === $property ? $country->getNationality() : $country->getName();
}
public function bpnPickupLabel(?int $id): string
public function bpnPickupLabel(?int $id): ?string
{
if (null === $id) {
return 'keine Busbegleitung';
@@ -59,7 +59,7 @@ class AppRuntime implements RuntimeExtensionInterface
$pickup = $this->pickups->get($id);
if (null === $pickup) {
return 'unbekannt';
return null;
}
return 'ab '.$pickup->getCity();