From 5e7658c7b922d92bb8228986b177aff3231830cf Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 3 Nov 2022 10:46:44 -0400 Subject: [PATCH] Update CPU list view to be a bit more managable --- src/Controller/BrandCategoryController.php | 69 +++++++--------------- src/Controller/CpuController.php | 21 ++++++- templates/cpu/index.html.twig | 38 +++++++++--- 3 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/Controller/BrandCategoryController.php b/src/Controller/BrandCategoryController.php index c664e4c..196dc56 100644 --- a/src/Controller/BrandCategoryController.php +++ b/src/Controller/BrandCategoryController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\BrandCategory; use App\Form\BrandCategoryType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -13,72 +14,44 @@ use Symfony\Component\Routing\Annotation\Route; #[Route('/brand/category')] class BrandCategoryController extends AbstractController { - #[Route('/', name: 'brand-category_index', methods: ['GET'])] - public function index(EntityManagerInterface $entityManager): Response - { - $brandCategories = $entityManager - ->getRepository(BrandCategory::class) - ->findAll(); + use FormControllerTrait; - return $this->render('brand_category/index.html.twig', [ - 'brand_categories' => $brandCategories, - ]); + protected const ENTITY = BrandCategory::class; + protected const TEMPLATE_PATH = 'brand_category/'; + protected const ROUTE_PREFIX = 'brand-category_'; + protected const FORM = BrandCategoryType::class; + + public function __construct(private readonly EntityManagerInterface $entityManager) + { + } + + #[Route('/', name: 'brand-category_index', methods: ['GET'])] + public function index(): Response + { + return $this->indexView('brand_categories'); } #[Route('/new', name: 'brand-category_new', methods: ['GET', 'POST'])] public function new(Request $request, EntityManagerInterface $entityManager): Response { - $brandCategory = new BrandCategory(); - $form = $this->createForm(BrandCategoryType::class, $brandCategory); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->persist($brandCategory); - $entityManager->flush(); - - return $this->redirectToRoute('brand-category_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('brand_category/new.html.twig', [ - 'brand_category' => $brandCategory, - 'form' => $form, - ]); + return $this->itemCreate($request, 'brand_category'); } #[Route('/{name}', name: 'brand-category_show', methods: ['GET'])] public function show(BrandCategory $brandCategory): Response { - return $this->render('brand_category/show.html.twig', [ - 'brand_category' => $brandCategory, - ]); + return $this->itemView($brandCategory, 'brand_category'); } #[Route('/{name}/edit', name: 'brand-category_edit', methods: ['GET', 'POST'])] - public function edit(Request $request, BrandCategory $brandCategory, EntityManagerInterface $entityManager): Response + public function edit(Request $request, BrandCategory $brandCategory): Response { - $form = $this->createForm(BrandCategoryType::class, $brandCategory); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->flush(); - - return $this->redirectToRoute('brand-category_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('brand_category/edit.html.twig', [ - 'brand_category' => $brandCategory, - 'form' => $form, - ]); + return $this->itemUpdate($request, $brandCategory, 'brand_category'); } #[Route('/{name}', name: 'brand-category_delete', methods: ['POST'])] - public function delete(Request $request, BrandCategory $brandCategory, EntityManagerInterface $entityManager): Response + public function delete(Request $request, BrandCategory $brandCategory): Response { - if ($this->isCsrfTokenValid('delete'.$brandCategory->getName(), $request->request->get('_token'))) { - $entityManager->remove($brandCategory); - $entityManager->flush(); - } - - return $this->redirectToRoute('brand_category_index', [], Response::HTTP_SEE_OTHER); + return $this->deleteCSRF($request, $brandCategory); } } diff --git a/src/Controller/CpuController.php b/src/Controller/CpuController.php index 579c38f..38aeb21 100644 --- a/src/Controller/CpuController.php +++ b/src/Controller/CpuController.php @@ -27,7 +27,26 @@ class CpuController extends AbstractController { #[Route('/', name: 'cpu_index', methods: ['GET'])] public function index(): Response { - return $this->itemListView('cpus', []); + $template = self::TEMPLATE_PATH . 'index.html.twig'; + + $items = $this->entityManager->getRepository(self::ENTITY)->findBy([], [ + 'productLine' => 'ASC', + 'model' => 'ASC', + ]); + + $amd = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'AMD' && $cpu->isReceived()); + $intel = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'Intel' && $cpu->isReceived()); + $others = array_filter($items, static fn (Cpu $cpu) => ( ! in_array($cpu->getBrand()->getName(), ['AMD','Intel'])) && $cpu->isReceived()); + $notReceived = array_filter($items, static fn(CPU $cpu) => $cpu->isReceived() === FALSE); + + return $this->render($template, [ + 'all' => [ + 'not_acquired' => $notReceived, + 'amd' => $amd, + 'intel' => $intel, + 'others' => $others, + ], + ]); } #[Route('/new', name: 'cpu_new', methods: ['GET', 'POST'])] diff --git a/templates/cpu/index.html.twig b/templates/cpu/index.html.twig index 3d9bb27..303553e 100644 --- a/templates/cpu/index.html.twig +++ b/templates/cpu/index.html.twig @@ -13,6 +13,33 @@ + + +
+ {% for label, cpus in all %} +
@@ -31,13 +58,9 @@ - - - - @@ -99,13 +122,9 @@ {% endif %} - - - - {% else %} @@ -115,4 +134,7 @@ {% endfor %}
L2 Cache L3 Cache IgpVoltageTdpProcessNode Count Usable ReceivedLink Notes
{{ cpu.igp }}{{ cpu.voltage }}{{ cpu.tdp }}{{ cpu.processNode }} {{ cpu.count }} {{ cpu.usable ? 'Yes' : 'No' }} {{ cpu.received ? 'Yes' : 'No' }}{{ cpu.link }} {{ cpu.notes }}
+
+ {% endfor %} +
{% endblock %}