From 9e804c6113d48ef4236e2de5909892dbae257e54 Mon Sep 17 00:00:00 2001 From: lubiana Date: Sat, 17 Feb 2024 12:30:54 +0100 Subject: [PATCH] yolo --- src/Controller/FoodOrderController.php | 4 +++ src/Entity/FoodOrder.php | 21 ++------------- src/Form/OrderItemType.php | 6 +++++ src/Repository/ItemExtraRepository.php | 33 +++++++----------------- templates/food_order/orderitem.html.twig | 5 ++++ templates/food_order/show.html.twig | 10 +++---- 6 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/Controller/FoodOrderController.php b/src/Controller/FoodOrderController.php index 9c92f0e..ad1b2f8 100644 --- a/src/Controller/FoodOrderController.php +++ b/src/Controller/FoodOrderController.php @@ -6,6 +6,7 @@ use App\Entity\FoodOrder; use App\Entity\MenuItem; use App\Entity\OrderItem; use App\Form\OrderItemType; +use App\Repository\ItemExtraRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -60,6 +61,7 @@ class FoodOrderController extends AbstractController FoodOrder $foodOrder, MenuItem $menuItem, EntityManagerInterface $entityManager, + ItemExtraRepository $extraRepository, Request $request, ): Response { $orderItem = new OrderItem; @@ -86,8 +88,10 @@ class FoodOrderController extends AbstractController ); } + return $this->render('food_order/orderitem.html.twig', [ 'form' => $form, + 'extras' => $extraRepository->getUniqueNames(), ]); } diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index b7803cd..670c416 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -128,25 +128,8 @@ class FoodOrder return $this; } - public function groupedOrderItems(): array + public function groupedOrderItems(): Collection { - return $this->orderItems->reduce( - static function (array $carry, OrderItem $item): array { - $menuItemStringId = (string) $item->getMenuItem() - ->getId(); - - if (isset($carry[$menuItemStringId])) { - $carry[$menuItemStringId]['amount']++; - } else { - $carry[$menuItemStringId] = [ - 'item' => $item, - 'amount' => 1, - ]; - } - - return $carry; - }, - [], - ); + return $this->getOrderItems(); } } diff --git a/src/Form/OrderItemType.php b/src/Form/OrderItemType.php index af106c7..cb7ff04 100644 --- a/src/Form/OrderItemType.php +++ b/src/Form/OrderItemType.php @@ -8,6 +8,7 @@ use App\Entity\OrderItem; use Override; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -31,6 +32,11 @@ class OrderItemType extends AbstractType 'expanded' => true, ], ); + $builder->add('customextra', TextType::class, [ + 'label' => 'Not yet know extras, seperated by comma', + 'mapped' => false, + 'attr' => ['datalist' => 'item-extra-list'], + ]); } #[Override] diff --git a/src/Repository/ItemExtraRepository.php b/src/Repository/ItemExtraRepository.php index 0a63c73..2744542 100644 --- a/src/Repository/ItemExtraRepository.php +++ b/src/Repository/ItemExtraRepository.php @@ -4,6 +4,8 @@ namespace App\Repository; use App\Entity\ItemExtra; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\Persistence\ManagerRegistry; /** @@ -21,27 +23,12 @@ class ItemExtraRepository extends ServiceEntityRepository parent::__construct($registry, ItemExtra::class); } - // /** - // * @return ItemExtra[] Returns an array of ItemExtra objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('i') - // ->andWhere('i.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('i.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } - // public function findOneBySomeField($value): ?ItemExtra - // { - // return $this->createQueryBuilder('i') - // ->andWhere('i.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } + public function getUniqueNames(): Collection + { + $qb = $this->createQueryBuilder('p'); + + $qb->groupBy('p.name'); + + return new ArrayCollection($qb->getQuery()->getResult()); + } } diff --git a/templates/food_order/orderitem.html.twig b/templates/food_order/orderitem.html.twig index a647b6b..8c26a22 100644 --- a/templates/food_order/orderitem.html.twig +++ b/templates/food_order/orderitem.html.twig @@ -2,5 +2,10 @@ {% block body %} {{ include('_form.html.twig') }} + + {% for item in extras %} + + {% endfor %} + {% endblock %} diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index 3ac9853..371ee01 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -44,19 +44,15 @@ {% for orderItem in food_order.groupedOrderItems %} - {{ orderItem.item.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }} + {{ orderItem.menuItem.menuItemAliases|map(i => i.name)|join(' / ') }} - {{ orderItem.item.extras|map(i => i.name)|join(' / ') }} + {{ orderItem.extras|map(i => i.name)|join(' / ') }} - {{ orderItem.item.menuItem.price|cents_to_eur }} + {{ orderItem.menuItem.price|cents_to_eur }} - {{ orderItem.amount }} | - +1 | - -1 | - {% endfor %}