From 1c4093922cdceadd87c39ca84b17b5fce9faf133 Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Sat, 15 Feb 2025 14:30:49 +0000 Subject: [PATCH] add checkin button (#25) Reviewed-on: https://git.simonis.lol/projects/abiball/pulls/25 --- src/Controller/Admin/CheckinAction.php | 36 +++++++++++++++++++ src/Controller/Admin/TicketCrudController.php | 8 +++-- src/Entity/Ticket.php | 15 ++++++++ src/Repository/TicketRepository.php | 1 + src/Twig/Environment.php | 14 +++++++- templates/admin/checked_in.html.twig | 7 ++++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/Controller/Admin/CheckinAction.php create mode 100644 templates/admin/checked_in.html.twig diff --git a/src/Controller/Admin/CheckinAction.php b/src/Controller/Admin/CheckinAction.php new file mode 100644 index 0000000..22f4056 --- /dev/null +++ b/src/Controller/Admin/CheckinAction.php @@ -0,0 +1,36 @@ +query->get('entityId'); + + if (!$entityId || !$ticket = $this->ticketRepository->findOneById($entityId)) { + noty()->error('Ein Fehler ist aufgetreten.'); + + return $this->redirect($request->get('referer')); + } + + $ticket->setCheckedIn(true); + $this->entityManager->flush(); + + return $this->redirect($request->headers->get('referer')); + } +} \ No newline at end of file diff --git a/src/Controller/Admin/TicketCrudController.php b/src/Controller/Admin/TicketCrudController.php index a61dc3e..af34e44 100644 --- a/src/Controller/Admin/TicketCrudController.php +++ b/src/Controller/Admin/TicketCrudController.php @@ -11,6 +11,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField; +use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; class TicketCrudController extends AbstractCrudController @@ -22,16 +23,19 @@ class TicketCrudController extends AbstractCrudController public function configureFields(string $pageName): iterable { - yield ChoiceField::new('type', 'Name') + yield ChoiceField::new('type', 'Typ') ->setChoices(TicketData::TYPES); yield ChoiceField::new('foodType', 'Ernährung') ->setChoices(FoodData::TYPES); - yield AssociationField::new('customer', 'Kunde') + yield AssociationField::new('customer', 'Käufer') ->setCrudController(CustomerCrudController::class) ->formatValue(fn(Customer $customer) => $customer->getEmail()) ->hideOnForm(); + + yield BooleanField::new('checkedIn', '') + ->setTemplatePath('admin/checked_in.html.twig'); } diff --git a/src/Entity/Ticket.php b/src/Entity/Ticket.php index 914c208..2fa1358 100644 --- a/src/Entity/Ticket.php +++ b/src/Entity/Ticket.php @@ -27,6 +27,9 @@ class Ticket implements \Stringable #[ORM\JoinColumn(nullable: false)] private ?Customer $customer = null; + #[ORM\Column(options: ['default' => false])] + private ?bool $checkedIn = null; + public function getId(): ?int { return $this->id; @@ -84,4 +87,16 @@ class Ticket implements \Stringable { return TicketData::TICKET_DATA[$this->type]['name']; } + + public function isCheckedIn(): ?bool + { + return $this->checkedIn; + } + + public function setCheckedIn(bool $checkedIn): static + { + $this->checkedIn = $checkedIn; + + return $this; + } } diff --git a/src/Repository/TicketRepository.php b/src/Repository/TicketRepository.php index cd6e338..3e5a0ce 100644 --- a/src/Repository/TicketRepository.php +++ b/src/Repository/TicketRepository.php @@ -9,6 +9,7 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository + * @method Ticket|null findOneById(int $entityId) */ class TicketRepository extends ServiceEntityRepository { diff --git a/src/Twig/Environment.php b/src/Twig/Environment.php index 8f363be..96766ad 100644 --- a/src/Twig/Environment.php +++ b/src/Twig/Environment.php @@ -2,6 +2,7 @@ namespace App\Twig; +use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -9,11 +10,22 @@ class Environment extends AbstractExtension { public function getFunctions(): array { - return [new TwigFunction('env', $this->getVar(...))]; + return [ + new TwigFunction('env', $this->getVar(...)), + new TwigFunction('entityId', $this->getEntityId(...)), + ]; } public function getVar(string $name): string { return $_ENV[$name]; } + + public function getEntityId(FieldDto $field): string + { + $url = $field->getCustomOption('toggleUrl'); + preg_match('/entityId=(\d+)/', $url, $matches); + + return $matches[1]; + } } \ No newline at end of file diff --git a/templates/admin/checked_in.html.twig b/templates/admin/checked_in.html.twig new file mode 100644 index 0000000..24a928c --- /dev/null +++ b/templates/admin/checked_in.html.twig @@ -0,0 +1,7 @@ +{% if not field.value %} +
+ +
+{% else %} + +{% endif %}