feat: custom destinations
This commit is contained in:
@@ -181,7 +181,11 @@ class AssignmentType extends AbstractType
|
||||
$choices = [];
|
||||
|
||||
foreach ($destination->getPickups() as $pickup) {
|
||||
$label = sprintf('%s, %s Uhr', trim($pickup['city']), trim($pickup['time']));
|
||||
if ($pickup['time']) {
|
||||
$label = sprintf('%s, %s Uhr', trim($pickup['city']), trim($pickup['time']));
|
||||
} else {
|
||||
$label = trim($pickup['city']);
|
||||
}
|
||||
$choices[$label] = $pickup['busProId'];
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ class AutocompleteChoiceType extends AbstractType
|
||||
'placeholder' => null,
|
||||
'endpoint_parameters' => [],
|
||||
'controller_action' => null,
|
||||
'initial_label' => null,
|
||||
]);
|
||||
|
||||
$resolver->setAllowedTypes('choices', 'array');
|
||||
@@ -37,7 +38,7 @@ class AutocompleteChoiceType extends AbstractType
|
||||
}
|
||||
$view->vars['placeholder'] = $options['placeholder'];
|
||||
|
||||
$view->vars['initial_label'] = $form->getData();
|
||||
$view->vars['initial_label'] = $options['initial_label'] ?? $form->getData();
|
||||
$view->vars['initial_value'] = $form->getData();
|
||||
$view->vars['action'] = $options['controller_action'];
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\Form;
|
||||
|
||||
use App\BusProNet\DataProvider\PickupDataProvider;
|
||||
use App\Form\ChoiceLoader\BpnPickupsChoiceLoader;
|
||||
use App\Form\ChoiceLoader\BpnPickupChoiceLoader;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
@@ -21,7 +21,7 @@ class BpnPickupType extends AbstractType
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'choice_loader' => new BpnPickupsChoiceLoader($this->pickups),
|
||||
'choice_loader' => new BpnPickupChoiceLoader($this->pickups),
|
||||
'preferred_choices' => [
|
||||
40, // Köln
|
||||
18, // Essen
|
||||
|
||||
+9
-8
@@ -2,25 +2,26 @@
|
||||
|
||||
namespace App\Form\ChoiceLoader;
|
||||
|
||||
use App\BusProNet\DataProvider\ProductDataProvider;
|
||||
use App\BusProNet\Model\Product;
|
||||
use App\BusProNet\DataProvider\HotelDataProvider;
|
||||
use App\BusProNet\Model\Hotel;
|
||||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
|
||||
class BpnProductsChoiceLoader implements ChoiceLoaderInterface
|
||||
class BpnHotelChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
public function __construct(private readonly ProductDataProvider $products)
|
||||
public function __construct(private readonly HotelDataProvider $dataProvider)
|
||||
{}
|
||||
|
||||
public function loadChoiceList(callable $value = null): ChoiceListInterface
|
||||
{
|
||||
$choices = [];
|
||||
/** @var Product[] $products */
|
||||
$products = $this->products->getAll();
|
||||
/** @var Hotel[] $hotels */
|
||||
$hotels = $this->dataProvider->getAll();
|
||||
|
||||
foreach ($products as $product) {
|
||||
$choices[$product->getName()] = $product->getId();
|
||||
foreach ($hotels as $hotel) {
|
||||
$label = sprintf('%s (%s)', $hotel->getName(), $hotel->getCode());
|
||||
$choices[$label] = $hotel->getBusProId();
|
||||
}
|
||||
|
||||
ksort($choices);
|
||||
+3
-3
@@ -8,16 +8,16 @@ use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
|
||||
class BpnPickupsChoiceLoader implements ChoiceLoaderInterface
|
||||
class BpnPickupChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
public function __construct(private readonly PickupDataProvider $pickups)
|
||||
public function __construct(private readonly PickupDataProvider $dataProvider)
|
||||
{}
|
||||
|
||||
public function loadChoiceList(callable $value = null): ChoiceListInterface
|
||||
{
|
||||
$choices = [];
|
||||
/** @var Pickup[] $pickups */
|
||||
$pickups = $this->pickups->getAll();
|
||||
$pickups = $this->dataProvider->getAll();
|
||||
|
||||
foreach ($pickups as $pickup) {
|
||||
$choices[$pickup->getCity()] = $pickup->getBusProId();
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\BusProNet\DataProvider\HotelDataProvider;
|
||||
use App\Model\DestinationDto;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
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 DestinationType extends AbstractType
|
||||
{
|
||||
public function __construct(private readonly HotelDataProvider $hotelDataProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('product', TextType::class, [
|
||||
'label' => 'Bezeichnung',
|
||||
])
|
||||
->add('dateFrom', DatepickerType::class, [
|
||||
'label' => 'Datum von',
|
||||
])
|
||||
->add('dateTo', DatepickerType::class, [
|
||||
'label' => 'Datum bis',
|
||||
])
|
||||
->add('pickups', CollectionType::class, [
|
||||
'label' => 'Zustiege',
|
||||
'entry_type' => BpnPickupType::class,
|
||||
'allow_add' => true,
|
||||
'allow_delete' => true,
|
||||
])
|
||||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$data = $event->getData();
|
||||
|
||||
$this->addHotelField($form, $data->getHotelBusProId());
|
||||
})
|
||||
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$data = $event->getData();
|
||||
|
||||
$this->addHotelField($form, $data['hotelBusProId'] ?? null);
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
private function addHotelField(FormInterface $form, ?string $hotelBusProId): void
|
||||
{
|
||||
$hotelLabel = null;
|
||||
|
||||
if (null !== $hotelBusProId) {
|
||||
$hotel = $this->hotelDataProvider->get($hotelBusProId);
|
||||
$hotelLabel = sprintf('%s (%s)', $hotel->getName(), $hotel->getCode());
|
||||
}
|
||||
|
||||
$form
|
||||
->remove('hotelBusProId')
|
||||
->add('hotelBusProId', AutocompleteChoiceType::class, [
|
||||
'label' => 'Vertragspartner',
|
||||
'endpoint_route' => 'app_admin_autocomplete_hotel',
|
||||
'initial_label' => $hotelLabel,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => DestinationDto::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user