diff --git a/src/Controller/BrandController.php b/src/Controller/BrandController.php index 126126d..9832b7d 100644 --- a/src/Controller/BrandController.php +++ b/src/Controller/BrandController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Brand; use App\Form\BrandType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -25,75 +26,48 @@ class BrandController extends AbstractController } #[Route('/', name: 'brand_index', methods: ['GET'])] - public function index(EntityManagerInterface $entityManager): Response + public function index(): Response { - $brands = $entityManager - ->getRepository(Brand::class) - ->findBy([ - - ], [ - 'name' => 'asc' - ]); - - return $this->render('brand/index.html.twig', [ - 'brands' => $brands, - ]); + return $this->itemListView('brands', [ + 'name' => 'asc' + ]); } #[Route('/new', name: 'brand_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager): Response + public function new(Request $request): Response { - $brand = new Brand(); - $form = $this->createForm(BrandType::class, $brand); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->persist($brand); - $entityManager->flush(); - - return $this->redirectToRoute('brand_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('brand/new.html.twig', [ - 'brand' => $brand, - 'form' => $form, - ]); + return $this->itemCreate($request, 'brand'); } #[Route('/{id}', name: 'brand_show', methods: ['GET'])] public function show(Brand $brand): Response { - return $this->render('brand/show.html.twig', [ - 'brand' => $brand, - ]); + return $this->itemView($brand, 'brand'); } #[Route('/{id}/edit', name: 'brand_edit', methods: ['GET', 'POST'])] - public function edit(Request $request, Brand $brand, EntityManagerInterface $entityManager): Response + public function edit(Request $request, Brand $brand): Response { - $form = $this->createForm(BrandType::class, $brand); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->flush(); - - return $this->redirectToRoute('brand_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('brand/edit.html.twig', [ - 'brand' => $brand, - 'form' => $form, - ]); + return $this->itemUpdate($request, $brand, 'brand'); +// +// $form = $this->createForm(BrandType::class, $brand); +// $form->handleRequest($request); +// +// if ($form->isSubmitted() && $form->isValid()) { +// $entityManager->flush(); +// +// return $this->redirectToRoute('brand_index', [], Response::HTTP_SEE_OTHER); +// } +// +// return $this->renderForm('brand/edit.html.twig', [ +// 'brand' => $brand, +// 'form' => $form, +// ]); } #[Route('/{id}', name: 'brand_delete', methods: ['POST'])] - public function delete(Request $request, Brand $brand, EntityManagerInterface $entityManager): Response + public function delete(Request $request, Brand $brand): Response { - if ($this->isCsrfTokenValid('delete'.$brand->getId(), $request->request->get('_token'))) { - $entityManager->remove($brand); - $entityManager->flush(); - } - - return $this->redirectToRoute('brand_index', [], Response::HTTP_SEE_OTHER); + return $this->deleteCSRF($request, $brand); } } diff --git a/src/Controller/CameraController.php b/src/Controller/CameraController.php index 1d89922..8c5d8f9 100644 --- a/src/Controller/CameraController.php +++ b/src/Controller/CameraController.php @@ -4,6 +4,8 @@ namespace App\Controller; use App\Entity\Camera; use App\Form\CameraType; +use App\Traits\DeleteFormTrait; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMInvalidArgumentException; use LogicException; @@ -19,6 +21,7 @@ use Symfony\Component\Routing\Annotation\Route; class CameraController extends AbstractController { use FormControllerTrait; + use DeleteFormTrait; protected const ENTITY = Camera::class; protected const TEMPLATE_PATH = 'camera/'; @@ -66,7 +69,7 @@ class CameraController extends AbstractController #[Route(path: '/new', name: 'camera_new', methods: ['GET', 'POST'])] public function newAction(Request $request): RedirectResponse|Response { - return $this->itemCreate($request, 'camera/new.html.twig', 'camera', 'camera_show'); + return $this->itemCreate($request,'camera'); } /** @@ -75,7 +78,7 @@ class CameraController extends AbstractController #[Route(path: '/{id}', name: 'camera_show', methods: ['GET'])] public function showAction(Camera $camera): Response { - return $this->itemView($camera, 'camera/show.html.twig', 'camera'); + return $this->itemView($camera,'camera'); } /** @@ -86,7 +89,7 @@ class CameraController extends AbstractController #[Route(path: '/{id}/edit', name: 'camera_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, Camera $camera): RedirectResponse|Response { - return $this->itemUpdate($request, $camera, 'camera/edit.html.twig', 'camera', 'camera_show'); + return $this->itemUpdate($request, $camera,'camera'); } /** @@ -94,10 +97,10 @@ class CameraController extends AbstractController * * @throws LogicException */ - #[Route(path: '/{id}', name: 'camera_delete', methods: ['DELETE'])] + #[Route(path: '/{id}', name: 'camera_delete', methods: ['POST','DELETE'])] public function deleteAction(Request $request, Camera $camera): RedirectResponse { - return $this->itemDelete($request, $camera, 'camera_index'); + return $this->itemDelete($request, $camera); } /** @@ -112,18 +115,6 @@ class CameraController extends AbstractController return $this->itemDeacquire($request, $camera, 'previously-owned-camera_index'); } - /** - * Creates a form to delete a camera entity. - * - * @param Camera $camera The camera entity - * - * @return FormInterface The form - */ - private function createDeleteForm(Camera $camera): FormInterface - { - return $this->buildForm($camera, 'camera_delete', 'DELETE'); - } - /** * Creates a form to move * diff --git a/src/Controller/CameraTypeController.php b/src/Controller/CameraTypeController.php index 775faba..64b1aec 100644 --- a/src/Controller/CameraTypeController.php +++ b/src/Controller/CameraTypeController.php @@ -4,9 +4,10 @@ namespace App\Controller; use App\Entity\CameraType; use App\Form\CameraTypeType; +use App\Traits\DeleteFormTrait; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; use Symfony\Component\Routing\Annotation\Route; @@ -14,6 +15,7 @@ use Symfony\Component\Routing\Annotation\Route; class CameraTypeController extends AbstractController { use FormControllerTrait; + use DeleteFormTrait; protected const ENTITY = CameraType::class; protected const TEMPLATE_PATH = 'cameratype/'; @@ -30,7 +32,7 @@ class CameraTypeController extends AbstractController #[Route(path: '/', name: 'camera-type_index', methods: ['GET'])] public function indexAction(): Response { - return $this->itemListView('cameratype/index.html.twig', 'cameraTypes', [ + return $this->itemListView('cameraTypes', [ 'type' => 'ASC', ]); } @@ -41,7 +43,7 @@ class CameraTypeController extends AbstractController #[Route(path: '/new', name: 'camera-type_new', methods: ['GET', 'POST'])] public function newAction(Request $request): Response { - return $this->itemCreate($request, 'cameratype/new.html.twig', 'cameraType', 'camera-type_show'); + return $this->itemCreate($request, 'cameraType'); } /** @@ -50,7 +52,7 @@ class CameraTypeController extends AbstractController #[Route(path: '/{id}', name: 'camera-type_show', methods: ['GET'])] public function showAction(CameraType $cameraType): Response { - return $this->itemView($cameraType, 'cameratype/show.html.twig', 'cameraType'); + return $this->itemView($cameraType, 'cameraType'); } /** @@ -59,23 +61,15 @@ class CameraTypeController extends AbstractController #[Route(path: '/{id}/edit', name: 'camera-type_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, CameraType $cameraType): RedirectResponse|Response { - return $this->itemUpdate($request, $cameraType, 'cameratype/edit.html.twig', 'cameraType', 'camera-type_show'); + return $this->itemUpdate($request, $cameraType, 'cameraType'); } /** * Deletes a cameraType entity. */ - #[Route(path: '/{id}', name: 'camera-type_delete', methods: ['DELETE'])] + #[Route(path: '/{id}', name: 'camera-type_delete', methods: ['POST', 'DELETE'])] public function deleteAction(Request $request, CameraType $cameraType): RedirectResponse { - return $this->itemDelete($request, $cameraType, 'camera-type_index'); - } - - /** - * Creates a form to delete a cameraType entity. - */ - private function createDeleteForm(CameraType $cameraType): FormInterface - { - return $this->buildForm($cameraType, 'camera-type_delete', 'DELETE'); + return $this->itemDelete($request, $cameraType); } } diff --git a/src/Controller/FilmController.php b/src/Controller/FilmController.php index d1ede4a..e0fc9a5 100644 --- a/src/Controller/FilmController.php +++ b/src/Controller/FilmController.php @@ -4,12 +4,12 @@ namespace App\Controller; use App\Entity\Film; use App\Form\FilmType; +use App\Traits\DeleteFormTrait; +use App\Traits\FormControllerTrait; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\Persistence\ManagerRegistry; use LogicException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; use Symfony\Component\Routing\Annotation\Route; @@ -20,6 +20,7 @@ use Symfony\Component\Routing\Annotation\Route; class FilmController extends AbstractController { use FormControllerTrait; + use DeleteFormTrait; protected const ENTITY = Film::class; protected const TEMPLATE_PATH = 'film/'; @@ -66,7 +67,7 @@ class FilmController extends AbstractController #[Route(path: '/new', name: 'film_new', methods: ['GET', 'POST'])] public function newAction(Request $request): RedirectResponse|Response { - return $this->itemCreate($request, 'film/new.html.twig', 'film', 'film_show'); + return $this->itemCreate($request, 'film'); } /** @@ -75,7 +76,7 @@ class FilmController extends AbstractController #[Route(path: '/{id}', name: 'film_show', methods: ['GET'])] public function showAction(Film $film): Response { - return $this->itemView($film, 'film/show.html.twig', 'film'); + return $this->itemView($film, 'film'); } /** @@ -86,7 +87,7 @@ class FilmController extends AbstractController #[Route(path: '/{id}/edit', name: 'film_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, Film $film): RedirectResponse|Response { - return $this->itemUpdate($request, $film, 'film/edit.html.twig', 'film', 'film_show'); + return $this->itemUpdate($request, $film, 'film'); } /** @@ -94,17 +95,9 @@ class FilmController extends AbstractController * * @throws LogicException */ - #[Route(path: '/{id}', name: 'film_delete', methods: ['DELETE'])] + #[Route(path: '/{id}', name: 'film_delete', methods: ['POST','DELETE'])] public function deleteAction(Request $request, Film $film): RedirectResponse { - return $this->itemDelete($request, $film, 'film_index'); - } - - /** - * Creates a form to delete a film entity. - */ - private function createDeleteForm(Film $film): FormInterface - { - return $this->buildForm($film, 'film_delete', 'DELETE'); + return $this->itemDelete($request, $film); } } diff --git a/src/Controller/FlashController.php b/src/Controller/FlashController.php index acd5696..bca9e77 100644 --- a/src/Controller/FlashController.php +++ b/src/Controller/FlashController.php @@ -4,8 +4,9 @@ namespace App\Controller; use App\Entity\Flash; use App\Form\FlashType; +use App\Traits\DeleteFormTrait; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\Persistence\ManagerRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; @@ -18,6 +19,7 @@ use Symfony\Component\Routing\Annotation\Route; class FlashController extends AbstractController { use FormControllerTrait; + use DeleteFormTrait; protected const ENTITY = Flash::class; protected const TEMPLATE_PATH = 'flash/'; @@ -34,7 +36,7 @@ class FlashController extends AbstractController #[Route(path: '/', name: 'flash_index', methods: ['GET'])] public function indexAction(): Response { - return $this->itemListView('flash/index.html.twig', 'flashes'); + return $this->itemListView('flashes'); } /** @@ -43,7 +45,7 @@ class FlashController extends AbstractController #[Route(path: '/new', name: 'flash_new', methods: ['GET', 'POST'])] public function newAction(Request $request): RedirectResponse|Response { - return $this->itemCreate($request, 'flash/new.html.twig', 'flash', 'flash_show'); + return $this->itemCreate($request,'flash'); } /** @@ -52,7 +54,7 @@ class FlashController extends AbstractController #[Route(path: '/{id}', name: 'flash_show', methods: ['GET'])] public function showAction(Flash $flash): Response { - return $this->itemView($flash, 'flash/show.html.twig', 'flash'); + return $this->itemView($flash,'flash'); } /** @@ -61,23 +63,15 @@ class FlashController extends AbstractController #[Route(path: '/{id}/edit', name: 'flash_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, Flash $flash): RedirectResponse|Response { - return $this->itemUpdate($request, $flash, 'flash/edit.html.twig', 'flash', 'flash_show'); + return $this->itemUpdate($request, $flash, 'flash'); } /** * Deletes a flash entity. */ - #[Route(path: '/{id}', name: 'flash_delete', methods: ['DELETE'])] + #[Route(path: '/{id}', name: 'flash_delete', methods: ['POST', 'DELETE'])] public function deleteAction(Request $request, Flash $flash): RedirectResponse { - return $this->itemDelete($request, $flash, 'flash_index'); - } - - /** - * Creates a form to delete a flash entity. - */ - private function createDeleteForm(Flash $flash): FormInterface - { - return $this->buildForm($flash, 'flash_delete', 'DELETE'); + return $this->itemDelete($request, $flash); } } diff --git a/src/Controller/GpuCoreController.php b/src/Controller/GpuCoreController.php index 526ad89..2bc370c 100644 --- a/src/Controller/GpuCoreController.php +++ b/src/Controller/GpuCoreController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\GpuCore; use App\Form\GPUCoreType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -13,67 +14,44 @@ use Symfony\Component\Routing\Annotation\Route; #[Route('/gpu-core')] class GpuCoreController extends AbstractController { - #[Route('/', name: 'gpu-core_index', methods: ['GET'])] - public function index(EntityManagerInterface $entityManager): Response - { - $gPUCores = $entityManager - ->getRepository(GpuCore::class) - ->findBy([], [ - 'brand' => 'asc', - 'architecture' => 'asc', - 'name' => 'asc', - 'variant' => 'asc', - ]); + use FormControllerTrait; - return $this->render('gpu_core/index.html.twig', [ - 'gpu_cores' => $gPUCores, - ]); + protected const ENTITY = GpuCore::class; + protected const TEMPLATE_PATH = 'gpu_core/'; + protected const ROUTE_PREFIX = 'gpu-core_'; + protected const FORM = GPUCoreType::class; + + public function __construct(private readonly EntityManagerInterface $entityManager) + { + } + + #[Route('/', name: 'gpu-core_index', methods: ['GET'])] + public function index(): Response + { + return $this->indexView('gpu_cores', [ + 'brand' => 'asc', + 'architecture' => 'asc', + 'name' => 'asc', + 'variant' => 'asc', + ]); } #[Route('/new', name: 'gpu-core_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager): Response + public function new(Request $request): Response { - $gPUCore = new GpuCore(); - $form = $this->createForm(GPUCoreType::class, $gPUCore); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->persist($gPUCore); - $entityManager->flush(); - - return $this->redirectToRoute('gpu-core_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('gpu_core/new.html.twig', [ - 'gpu_core' => $gPUCore, - 'form' => $form, - ]); + return $this->itemCreate($request, 'gpu_core'); } #[Route('/{id}', name: 'gpu-core_show', methods: ['GET'])] public function show(GpuCore $gPUCore): Response { - return $this->render('gpu_core/show.html.twig', [ - 'gpu_core' => $gPUCore, - ]); + return $this->itemView($gPUCore, 'gpu_core'); } #[Route('/{id}/edit', name: 'gpu-core_edit', methods: ['GET', 'POST'])] - public function edit(Request $request, GpuCore $gPUCore, EntityManagerInterface $entityManager): Response + public function edit(Request $request, GpuCore $gPUCore): Response { - $form = $this->createForm(GPUCoreType::class, $gPUCore); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->flush(); - - return $this->redirectToRoute('gpu-core_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->renderForm('gpu_core/edit.html.twig', [ - 'gpu_core' => $gPUCore, - 'form' => $form, - ]); + return $this->itemUpdate($request, $gPUCore, 'gpu_core'); } #[Route('/{id}', name: 'gpu-core_delete', methods: ['POST'])] diff --git a/src/Controller/LensesController.php b/src/Controller/LensesController.php index 587ea80..856869f 100644 --- a/src/Controller/LensesController.php +++ b/src/Controller/LensesController.php @@ -4,8 +4,9 @@ namespace App\Controller; use App\Entity\Lenses; use App\Form\LensesType; +use App\Traits\DeleteFormTrait; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\Persistence\ManagerRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; @@ -18,6 +19,7 @@ use Symfony\Component\Routing\Annotation\Route; class LensesController extends AbstractController { use FormControllerTrait; + use DeleteFormTrait; protected const ENTITY = Lenses::class; protected const TEMPLATE_PATH = 'lenses/'; @@ -65,7 +67,7 @@ class LensesController extends AbstractController #[Route(path: '/new', name: 'lens_new', methods: ['GET', 'POST'])] public function newAction(Request $request): RedirectResponse|Response { - return $this->itemCreate($request, 'lenses/new.html.twig', 'lense', 'lens_show'); + return $this->itemCreate($request,'lense'); } /** @@ -74,7 +76,7 @@ class LensesController extends AbstractController #[Route(path: '/{id}', name: 'lens_show', methods: ['GET'])] public function showAction(Lenses $lens): Response { - return $this->itemView($lens, 'lenses/show.html.twig', 'lense'); + return $this->itemView($lens, 'lense'); } /** @@ -83,7 +85,7 @@ class LensesController extends AbstractController #[Route(path: '/{id}/edit', name: 'lens_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, Lenses $lens): RedirectResponse|Response { - return $this->itemUpdate($request, $lens, 'lenses/edit.html.twig', 'lense', 'lens_show'); + return $this->itemUpdate($request, $lens, 'lense'); } /** @@ -102,22 +104,10 @@ class LensesController extends AbstractController /** * Deletes a lens entity. */ - #[Route(path: '/{id}', name: 'lens_delete', methods: ['DELETE'])] + #[Route(path: '/{id}', name: 'lens_delete', methods: ['POST','DELETE'])] public function deleteAction(Request $request, Lenses $lens): RedirectResponse { - return $this->itemDelete($request, $lens, 'lens_index'); - } - - /** - * Creates a form to delete a lens entity. - * - * @param Lenses $lens The lens entity - * - * @return FormInterface The form - */ - private function createDeleteForm(Lenses $lens): FormInterface - { - return $this->buildForm($lens, 'lens_delete', 'DELETE'); + return $this->itemDelete($request, $lens); } /** diff --git a/src/Controller/PreviouslyOwnedCameraController.php b/src/Controller/PreviouslyOwnedCameraController.php index 9876b7e..e5d0023 100644 --- a/src/Controller/PreviouslyOwnedCameraController.php +++ b/src/Controller/PreviouslyOwnedCameraController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\PreviouslyOwnedCamera; use App\Form\PreviouslyOwnedCameraType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMInvalidArgumentException; use LogicException; @@ -36,13 +37,13 @@ class PreviouslyOwnedCameraController extends AbstractController * @throws UnexpectedValueException */ #[Route(path: '/', name: 'previously-owned-camera_index', methods: ['GET'])] - public function indexAction(EntityManagerInterface $entityManager): Response + public function indexAction(): Response { - return $this->itemListView('previouslyownedcamera/index.html.twig', 'previouslyOwnedCameras', [ + return $this->itemListView( 'previouslyOwnedCameras', [ 'brand' => 'ASC', 'mount' => 'ASC', 'model' => 'ASC', - ], $entityManager); + ]); } /** @@ -51,7 +52,7 @@ class PreviouslyOwnedCameraController extends AbstractController #[Route(path: '/{id}', name: 'previously-owned-camera_show', methods: ['GET'])] public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera): Response { - return $this->itemView($previouslyOwnedCamera, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedCamera'); + return $this->itemView($previouslyOwnedCamera, 'previouslyOwnedCamera'); } /** @@ -62,7 +63,7 @@ class PreviouslyOwnedCameraController extends AbstractController #[Route(path: '/{id}/edit', name: 'previously-owned-camera_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera): RedirectResponse|Response { - return $this->itemUpdate($request, $previouslyOwnedCamera, 'previouslyownedcamera/edit.html.twig', 'previouslyOwnedCamera', 'previously-owned-camera_show'); + return $this->itemUpdate($request, $previouslyOwnedCamera, 'previouslyOwnedCamera'); } /** diff --git a/src/Controller/PreviouslyOwnedFlashController.php b/src/Controller/PreviouslyOwnedFlashController.php index d09f284..6000a3a 100644 --- a/src/Controller/PreviouslyOwnedFlashController.php +++ b/src/Controller/PreviouslyOwnedFlashController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\PreviouslyOwnedFlash; use App\Form\PreviouslyOwnedFlashType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; @@ -32,7 +33,7 @@ class PreviouslyOwnedFlashController extends AbstractController #[Route(path: '/', name: 'previously-owned-flash_index', methods: ['GET'])] public function indexAction(): Response { - return $this->itemListView('previouslyownedflash/index.html.twig', 'previouslyOwnedFlashes'); + return $this->itemListView( 'previouslyOwnedFlashes'); } /** @@ -41,7 +42,7 @@ class PreviouslyOwnedFlashController extends AbstractController #[Route(path: '/new', name: 'previously-owned-flash_new', methods: ['GET', 'POST'])] public function newAction(Request $request): RedirectResponse|Response { - return $this->itemCreate($request, 'previouslyownedflash/new.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show'); + return $this->itemCreate($request, 'previouslyOwnedFlash'); } /** @@ -50,7 +51,7 @@ class PreviouslyOwnedFlashController extends AbstractController #[Route(path: '/{id}', name: 'previously-owned-flash_show', methods: ['GET'])] public function showAction(PreviouslyOwnedFlash $previouslyOwnedFlash): Response { - return $this->itemView($previouslyOwnedFlash, 'previouslyownedcamera/show.html.twig', 'previouslyOwnedFlash'); + return $this->itemView($previouslyOwnedFlash, 'previouslyOwnedFlash'); } /** @@ -59,6 +60,6 @@ class PreviouslyOwnedFlashController extends AbstractController #[Route(path: '/{id}/edit', name: 'previously-owned-flash_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, PreviouslyOwnedFlash $previouslyOwnedFlash): RedirectResponse|Response { - return $this->itemUpdate($request, $previouslyOwnedFlash, 'previouslyownedflash/edit.html.twig', 'previouslyOwnedFlash', 'previously-owned-flash_show'); + return $this->itemUpdate($request, $previouslyOwnedFlash, 'previouslyOwnedFlash'); } } diff --git a/src/Controller/PreviouslyOwnedLensesController.php b/src/Controller/PreviouslyOwnedLensesController.php index d18fc24..3dc17e3 100644 --- a/src/Controller/PreviouslyOwnedLensesController.php +++ b/src/Controller/PreviouslyOwnedLensesController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\PreviouslyOwnedLenses; use App\Form\PreviouslyOwnedLensesType; +use App\Traits\FormControllerTrait; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response}; @@ -29,7 +30,7 @@ class PreviouslyOwnedLensesController extends AbstractController #[Route(path: '/', name: 'previously-owned-lens_index', methods: ['GET'])] public function indexAction(): Response { - return $this->itemListView('previouslyownedlenses/index.html.twig', 'previouslyOwnedLenses', [ + return $this->itemListView('previouslyOwnedLenses', [ 'brand' => 'ASC', 'productLine' => 'ASC', 'mount' => 'ASC', @@ -44,7 +45,7 @@ class PreviouslyOwnedLensesController extends AbstractController #[Route(path: '/{id}', name: 'previously-owned-lens_show', methods: ['GET'])] public function showAction(PreviouslyOwnedLenses $previouslyOwnedLens): Response { - return $this->itemView($previouslyOwnedLens, 'previouslyownedlenses/show.html.twig', 'previouslyOwnedLense'); + return $this->itemView($previouslyOwnedLens, 'previouslyOwnedLense'); } /** @@ -53,6 +54,6 @@ class PreviouslyOwnedLensesController extends AbstractController #[Route(path: '/{id}/edit', name: 'previously-owned-lens_edit', methods: ['GET', 'POST'])] public function editAction(Request $request, PreviouslyOwnedLenses $previouslyOwnedLens): RedirectResponse|Response { - return $this->itemUpdate($request, $previouslyOwnedLens, 'previouslyownedlenses/edit.html.twig', 'previouslyOwnedLense', 'previously-owned-lens_show'); + return $this->itemUpdate($request, $previouslyOwnedLens, 'previouslyOwnedLense'); } } diff --git a/src/Entity/LensTrait.php b/src/Entity/LensTrait.php index e995867..3721e5e 100644 --- a/src/Entity/LensTrait.php +++ b/src/Entity/LensTrait.php @@ -62,4 +62,14 @@ trait LensTrait #[ORM\Column(name: 'aperture_blades', type: 'smallint', nullable: TRUE)] private readonly ?int $apertureBlades; + + public function __get(string $name): mixed + { + if (property_exists($this, $name)) + { + return $this->$name; + } + + return null; + } } diff --git a/src/Repository/AcquireRepository.php b/src/Repository/AcquireRepository.php new file mode 100644 index 0000000..05b4cb1 --- /dev/null +++ b/src/Repository/AcquireRepository.php @@ -0,0 +1,48 @@ +getEntityManager(); + + $old = new ReflectionObject($currentRecord); + $new = new ReflectionObject($newRecord); + + foreach ($old->getProperties() as $property) { + $propertyName = $property->getName(); + if ($new->hasProperty($propertyName)) { + $newProperty = $new->getProperty($propertyName); + $newProperty->setAccessible(TRUE); + $property->setAccessible(TRUE); + $newProperty->setValue($newRecord, $property->getValue($currentRecord)); + } + } + + try { + $em->persist($newRecord); + $em->remove($currentRecord); + $em->flush(); + } catch (Throwable) { + dump($newRecord); + } + } +} diff --git a/src/Repository/AcquireTrait.php b/src/Repository/AcquireTrait.php deleted file mode 100644 index fa912a8..0000000 --- a/src/Repository/AcquireTrait.php +++ /dev/null @@ -1,42 +0,0 @@ -getEntityManager(); - - $old = new ReflectionObject($currentRecord); - $new = new ReflectionObject($newRecord); - - foreach ($old->getProperties() as $property) { - $propertyName = $property->getName(); - if ($new->hasProperty($propertyName)) { - $newProperty = $new->getProperty($propertyName); - $newProperty->setAccessible(TRUE); - $property->setAccessible(TRUE); - $newProperty->setValue($newRecord, $property->getValue($currentRecord)); - } - } - - try { - $em->persist($newRecord); - $em->remove($currentRecord); - $em->flush(); - } catch (Throwable) { - dump($newRecord); - } - } -} diff --git a/src/Repository/CameraRepository.php b/src/Repository/CameraRepository.php index 090651b..f7fa76c 100644 --- a/src/Repository/CameraRepository.php +++ b/src/Repository/CameraRepository.php @@ -3,16 +3,14 @@ namespace App\Repository; use App\Entity\{Camera, PreviouslyOwnedCamera}; -use Doctrine\ORM\{EntityRepository, ORMInvalidArgumentException}; +use Doctrine\ORM\{ORMInvalidArgumentException}; -class CameraRepository extends EntityRepository +class CameraRepository extends AcquireRepository { - use AcquireTrait; - /** * @throws ORMInvalidArgumentException */ - public function deacquire(Camera $currentRecord): void + public function deacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(TRUE) ->setReceived(TRUE); @@ -23,7 +21,7 @@ class CameraRepository extends EntityRepository /** * @throws ORMInvalidArgumentException */ - public function reacquire(PreviouslyOwnedCamera $currentRecord): void + public function reacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(FALSE); diff --git a/src/Repository/FlashRepository.php b/src/Repository/FlashRepository.php index f5845a6..029f6bc 100644 --- a/src/Repository/FlashRepository.php +++ b/src/Repository/FlashRepository.php @@ -3,16 +3,14 @@ namespace App\Repository; use App\Entity\{Flash, PreviouslyOwnedFlash}; -use Doctrine\ORM\{EntityRepository, ORMInvalidArgumentException}; +use Doctrine\ORM\{ORMInvalidArgumentException}; -class FlashRepository extends EntityRepository +class FlashRepository extends AcquireRepository { - use AcquireTrait; - /** * @throws ORMInvalidArgumentException */ - public function deacquire(Flash $currentRecord): void + public function deacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(TRUE) ->setReceived(TRUE); @@ -23,7 +21,7 @@ class FlashRepository extends EntityRepository /** * @throws ORMInvalidArgumentException */ - public function reacquire(PreviouslyOwnedFlash $currentRecord): void + public function reacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(FALSE); diff --git a/src/Repository/LensesRepository.php b/src/Repository/LensesRepository.php index 08163d5..f3fa640 100644 --- a/src/Repository/LensesRepository.php +++ b/src/Repository/LensesRepository.php @@ -3,13 +3,10 @@ namespace App\Repository; use App\Entity\{Lenses, PreviouslyOwnedLenses}; -use Doctrine\ORM\EntityRepository; -class LensesRepository extends EntityRepository +class LensesRepository extends AcquireRepository { - use AcquireTrait; - - public function deacquire(Lenses $currentRecord): void + public function deacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(TRUE) ->setReceived(TRUE); @@ -17,7 +14,7 @@ class LensesRepository extends EntityRepository $this->moveRecord($currentRecord, new PreviouslyOwnedLenses()); } - public function reacquire(PreviouslyOwnedLenses $currentRecord): void + public function reacquire(mixed $currentRecord): void { $currentRecord->setFormerlyOwned(FALSE); diff --git a/src/Traits/DeleteFormTrait.php b/src/Traits/DeleteFormTrait.php new file mode 100644 index 0000000..b4b5127 --- /dev/null +++ b/src/Traits/DeleteFormTrait.php @@ -0,0 +1,16 @@ +buildForm($item, self::ROUTE_PREFIX . 'delete', 'DELETE'); + } +} diff --git a/src/Controller/FormControllerTrait.php b/src/Traits/FormControllerTrait.php similarity index 68% rename from src/Controller/FormControllerTrait.php rename to src/Traits/FormControllerTrait.php index d0cd895..2b9927f 100644 --- a/src/Controller/FormControllerTrait.php +++ b/src/Traits/FormControllerTrait.php @@ -1,6 +1,6 @@ createFormBuilder() ->setAction($this->generateUrl($actionRoute, ['id' => $item->getId()])) @@ -26,8 +24,11 @@ trait FormControllerTrait /** * Show create form / create an item */ - protected function itemCreate(Request $request, string $template, string $templateKey, string $redirectRoute) - { + protected function itemCreate(Request $request, string $templateKey): RedirectResponse|Response + { + $template = self::TEMPLATE_PATH . 'new.html.twig'; + $redirectRoute = self::ROUTE_PREFIX . 'show'; + $Entity = self::ENTITY; $item = new $Entity(); $form = $this->createForm(self::FORM, $item); @@ -48,11 +49,18 @@ trait FormControllerTrait ]); } + protected function indexView(string $templateKey, array $sort = []): Response + { + return $this->itemListView($templateKey, $sort); + } + /** * List view for the data type */ - protected function itemListView(string $template, string $templateKey, array $sort = []): Response + protected function itemListView(string $templateKey, array $sort = []): Response { + $template = self::TEMPLATE_PATH . 'index.html.twig'; + $items = $this->entityManager->getRepository(self::ENTITY)->findBy([], $sort); return $this->render($template, [ @@ -62,11 +70,11 @@ trait FormControllerTrait /** * View details for a specific item - * - * @param mixed $item */ - protected function itemView($item, string $template, string $templateKey): Response + protected function itemView(mixed $item, string $templateKey): Response { + $template = self::TEMPLATE_PATH . 'show.html.twig'; + $templateData = [ $templateKey => $item, ]; @@ -81,11 +89,12 @@ trait FormControllerTrait /** * Show edit form / update an item - * - * @param mixed $item */ - protected function itemUpdate(Request $request, $item, string $template, string $templateKey, string $redirectRoute) - { + protected function itemUpdate(Request $request, mixed $item, string $templateKey): RedirectResponse|Response + { + $template = self::TEMPLATE_PATH . 'edit.html.twig'; + $redirectRoute = self::ROUTE_PREFIX . 'show'; + $editForm = $this->createForm(self::FORM, $item); $editForm->handleRequest($request); @@ -123,10 +132,8 @@ trait FormControllerTrait /** * Move an item to a previously_owned table - * - * @param mixed $item */ - protected function itemDeacquire(Request $request, $item, string $redirectRoute): RedirectResponse + protected function itemDeacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse { $form = $this->createDeacquireForm($item); $form->handleRequest($request); @@ -139,10 +146,8 @@ trait FormControllerTrait /** * Move an item from a previously_owned table back to the original table - * - * @param mixed $item */ - protected function itemReacquire(Request $request, $item, string $redirectRoute): RedirectResponse + protected function itemReacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse { $form = $this->createReacquireForm($item); $form->handleRequest($request); @@ -153,19 +158,31 @@ trait FormControllerTrait return $this->redirectToRoute($redirectRoute); } - /** - * Actually delete an item - * - * @param mixed $item - */ - protected function itemDelete(Request $request, mixed $item, string $redirectRoute): RedirectResponse + /** + * Actually delete an item + */ + protected function itemDelete(Request $request, mixed $item): RedirectResponse { + $redirectRoute = self::ROUTE_PREFIX . 'index'; + $form = $this->createDeleteForm($item); $form->handleRequest($request); - $this->entityManager->remove($item); - $this->entityManager->flush(); + // if ($this->isCsrfTokenValid((string)$item->getId(), $request->request->get('_token'))) { + $this->entityManager->remove($item); + $this->entityManager->flush(); + //} - return $this->redirectToRoute($redirectRoute); + return $this->redirectToRoute($redirectRoute, [], Response::HTTP_SEE_OTHER); } + + protected function deleteCSRF(Request $request, mixed $item): RedirectResponse + { + if ($this->isCsrfTokenValid('delete'.$item->getId(), $request->request->get('_token'))) { + $this->entityManager->remove($item); + $this->entityManager->flush(); + } + + return $this->redirectToRoute(self::ROUTE_PREFIX . 'index', [], Response::HTTP_SEE_OTHER); + } } diff --git a/templates/brand/edit.html.twig b/templates/brand/edit.html.twig index 68036ee..c2a9a4a 100644 --- a/templates/brand/edit.html.twig +++ b/templates/brand/edit.html.twig @@ -14,13 +14,13 @@