WIP: Implement even more stuff

This commit is contained in:
Björn Fromme
2023-10-04 17:54:35 +02:00
parent cc1ab396af
commit 96d6170907
31 changed files with 874 additions and 68 deletions
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace App\Form;
use App\Entity\Assignment;
use App\Entity\Fee;
use App\Entity\JobProfile;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AssignmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('available', IntegerType::class, [
'label' => 'Anzahl',
'required' => false,
])
->add('destinationBusProId', AutocompleteChoiceType::class, [
'label' => 'Destination',
'endpoint_route' => 'app_admin_autocomplete_hotel',
])
->add('jobProfile', EntityType::class, [
'label' => 'Jobprofil',
'class' => JobProfile::class,
'choice_label' => 'name',
'placeholder' => 'Bitte auswählen',
])
->add('dateFrom', DatepickerType::class, [
'label' => 'Einsatztermin von',
'mode' => 'range',
])
->add('dateTo', DatepickerType::class, [
'label' => 'Einsatztermin bis',
])
->add('pickupDate', DatepickerType::class, [
'label' => 'Busabfahrt',
'required' => false,
])
->add('pickup', BpnPickupType::class, [
'label' => 'Buszustieg',
'required' => false,
'placeholder' => 'Keine Busbegleitung',
])
->add('benefits', TextareaType::class, [
'label' => 'Benefits',
'required' => false,
])
->add('fees', EntityType::class, [
'label' => 'Honorar(e)',
'class' => Fee::class,
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Assignment::class,
]);
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AutocompleteChoiceType extends AbstractType
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'endpoint_route' => null,
'choices' => [],
'compound' => false,
'placeholder' => null,
'endpoint_parameters' => [],
'controller_action' => null,
]);
$resolver->setAllowedTypes('choices', 'array');
}
public function buildView(FormView $view, FormInterface $form, array $options): void
{
if ($options['endpoint_route']) {
$view->vars['endpoint_url'] = $this->urlGenerator->generate($options['endpoint_route'], $options['endpoint_parameters']);
} else {
$view->vars['choices'] = $options['choices'];
}
$view->vars['placeholder'] = $options['placeholder'];
$view->vars['initial_label'] = $form->getData();
$view->vars['initial_value'] = $form->getData();
$view->vars['action'] = $options['controller_action'];
}
public function getBlockPrefix(): string
{
return 'autocomplete';
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Form;
use App\Form\DataTransformer\EntityToIdTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AutocompleteEntityType extends AbstractType
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UrlGeneratorInterface $urlGenerator
) {
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$dataTransformer = new EntityToIdTransformer($this->entityManager, $options['class']);
$builder->addModelTransformer($dataTransformer);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['class']);
$resolver->setDefaults([
'endpoint_route' => null,
'choices' => [],
'compound' => false,
'label_property' => 'label',
'label_function' => null,
'placeholder' => null,
'endpoint_parameters' => [],
'controller_action' => null,
]);
$resolver->setAllowedTypes('choices', 'array');
$resolver->setAllowedTypes('label_function', ['null', 'callable']);
}
public function buildView(FormView $view, FormInterface $form, array $options): void
{
if ($options['endpoint_route']) {
$view->vars['endpoint_url'] = $this->urlGenerator->generate($options['endpoint_route'], $options['endpoint_parameters']);
} else {
$view->vars['choices'] = $options['choices'];
}
$view->vars['label_property'] = $options['label_property'];
$view->vars['placeholder'] = $options['placeholder'];
$view->vars['initial_label'] = '';
$view->vars['initial_value'] = null;
$view->vars['action'] = $options['controller_action'];
$entity = $form->getData();
if (null !== $entity) {
if (null !== $options['label_function']) {
$labelFunction = $options['label_function'];
$view->vars['initial_label'] = $labelFunction($entity);
} else {
$getter = 'get'.ucfirst($options['label_property']);
if (method_exists($entity, $getter)) {
$view->vars['initial_label'] = $entity->$getter();
}
}
$view->vars['initial_value'] = $entity->getId();
}
}
public function getBlockPrefix(): string
{
return 'autocomplete';
}
}
+2 -2
View File
@@ -2,7 +2,7 @@
namespace App\Form;
use App\BusProNet\DataProvider\Countries;
use App\BusProNet\DataProvider\CountryDataProvider;
use App\Form\ChoiceLoader\BpnCountryChoiceLoader;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
@@ -12,7 +12,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class BpnCountryType extends AbstractType
{
public function __construct(private readonly Countries $countries)
public function __construct(private readonly CountryDataProvider $countries)
{}
public function getParent(): string
+2 -2
View File
@@ -2,7 +2,7 @@
namespace App\Form;
use App\BusProNet\DataProvider\Pickups;
use App\BusProNet\DataProvider\PickupDataProvider;
use App\Form\ChoiceLoader\BpnPickupsChoiceLoader;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -10,7 +10,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class BpnPickupType extends AbstractType
{
public function __construct(private readonly Pickups $pickups)
public function __construct(private readonly PickupDataProvider $pickups)
{}
public function getParent(): string
@@ -2,7 +2,7 @@
namespace App\Form\ChoiceLoader;
use App\BusProNet\DataProvider\Countries;
use App\BusProNet\DataProvider\CountryDataProvider;
use App\BusProNet\Model\Country;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
@@ -10,7 +10,7 @@ use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class BpnCountryChoiceLoader implements ChoiceLoaderInterface
{
public function __construct(private readonly Countries $countries, private readonly string $property)
public function __construct(private readonly CountryDataProvider $countries, private readonly string $property)
{}
public function loadChoiceList(callable $value = null): ChoiceListInterface
@@ -2,7 +2,7 @@
namespace App\Form\ChoiceLoader;
use App\BusProNet\DataProvider\Pickups;
use App\BusProNet\DataProvider\PickupDataProvider;
use App\BusProNet\Model\Pickup;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
@@ -10,7 +10,7 @@ use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class BpnPickupsChoiceLoader implements ChoiceLoaderInterface
{
public function __construct(private readonly Pickups $pickups)
public function __construct(private readonly PickupDataProvider $pickups)
{}
public function loadChoiceList(callable $value = null): ChoiceListInterface
@@ -0,0 +1,41 @@
<?php
namespace App\Form\ChoiceLoader;
use App\BusProNet\DataProvider\ProductDataProvider;
use App\BusProNet\Model\Product;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class BpnProductsChoiceLoader implements ChoiceLoaderInterface
{
public function __construct(private readonly ProductDataProvider $products)
{}
public function loadChoiceList(callable $value = null): ChoiceListInterface
{
$choices = [];
/** @var Product[] $products */
$products = $this->products->getAll();
foreach ($products as $product) {
$choices[$product->getName()] = $product->getId();
}
ksort($choices);
return new ArrayChoiceList($choices);
}
public function loadChoicesForValues(array $values, callable $value = null): array
{
return $values;
}
public function loadValuesForChoices(array $choices, callable $value = null): array
{
return $choices;
}
}
+4
View File
@@ -19,10 +19,13 @@ class DatepickerType extends AbstractType
'min_date' => null,
'max_date' => null,
'disable_weekends' => false,
'mode' => 'single', // 'single', 'multiple' or 'range'
]);
$resolver->setAllowedTypes('min_date', ['null', \DateTimeImmutable::class]);
$resolver->setAllowedTypes('max_date', ['null', \DateTimeImmutable::class]);
$resolver->setAllowedTypes('disable_weekends', 'bool');
$resolver->setAllowedTypes('mode', 'string');
$resolver->setAllowedValues('mode', ['single', 'multiple', 'range']);
}
public function buildView(FormView $view, FormInterface $form, array $options): void
@@ -30,6 +33,7 @@ class DatepickerType extends AbstractType
$view->vars['min_date'] = $options['min_date'];
$view->vars['max_date'] = $options['max_date'];
$view->vars['disable_weekends'] = $options['disable_weekends'];
$view->vars['mode'] = $options['mode'];
}
public function getParent(): string