Much refactoring

This commit is contained in:
Timothy Warren 2022-09-30 10:48:14 -04:00
parent 5065bef545
commit 41d19ad84b
23 changed files with 254 additions and 294 deletions

View File

@ -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([
], [
return $this->itemListView('brands', [
'name' => 'asc'
]);
return $this->render('brand/index.html.twig', [
'brands' => $brands,
]);
}
#[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);
}
}

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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
use FormControllerTrait;
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)
{
$gPUCores = $entityManager
->getRepository(GpuCore::class)
->findBy([], [
}
#[Route('/', name: 'gpu-core_index', methods: ['GET'])]
public function index(): Response
{
return $this->indexView('gpu_cores', [
'brand' => 'asc',
'architecture' => 'asc',
'name' => 'asc',
'variant' => 'asc',
]);
return $this->render('gpu_core/index.html.twig', [
'gpu_cores' => $gPUCores,
]);
}
#[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'])]

View File

@ -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);
}
/**

View File

@ -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');
}
/**

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,48 @@
<?php declare(strict_types=1);
namespace App\Repository;
use Doctrine\ORM\{EntityRepository};
use ReflectionObject;
use Throwable;
abstract class AcquireRepository extends EntityRepository
{
abstract public function reacquire(mixed $currentRecord): void;
abstract public function deacquire(mixed $currentRecord): void;
/**
* Move a record from the table represented by $currentRecord
* into the table represented by $newRecord
*
* @param mixed $currentRecord
* @param mixed $newRecord
*/
protected function moveRecord(mixed $currentRecord, mixed $newRecord): void
{
$em = $this->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);
}
}
}

View File

@ -1,42 +0,0 @@
<?php declare(strict_types=1);
namespace App\Repository;
use ReflectionObject;
use Throwable;
trait AcquireTrait
{
/**
* Move a record from the table represented by $currentRecord
* into the table represented by $newRecord
*
* @param mixed $currentRecord
* @param mixed $newRecord
*/
protected function moveRecord($currentRecord, $newRecord): void
{
$em = $this->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);
}
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
namespace App\Traits;
use Symfony\Component\Form\FormInterface;
trait DeleteFormTrait
{
/**
* Creates a form to delete an entity.
*/
private function createDeleteForm(mixed $item): FormInterface
{
return $this->buildForm($item, self::ROUTE_PREFIX . 'delete', 'DELETE');
}
}

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace App\Controller;
namespace App\Traits;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormInterface;
@ -12,10 +12,8 @@ trait FormControllerTrait
/**
* Create a form generator
*
* @param mixed $item
*/
protected function buildForm($item, string $actionRoute, string $method = 'POST'): FormInterface
protected function buildForm(mixed $item, string $actionRoute, string $method = 'POST'): FormInterface
{
return $this->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);
@ -155,17 +160,29 @@ trait FormControllerTrait
/**
* Actually delete an item
*
* @param mixed $item
*/
protected function itemDelete(Request $request, mixed $item, string $redirectRoute): RedirectResponse
protected function itemDelete(Request $request, mixed $item): RedirectResponse
{
$redirectRoute = self::ROUTE_PREFIX . 'index';
$form = $this->createDeleteForm($item);
$form->handleRequest($request);
// 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);
}
}

View File

@ -14,13 +14,13 @@
</div>
<div class="large primary callout">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
<button
type="submit"
class="success button expanded"
>Update</button>
{{ form_end(form) }}
{{ form_end(edit_form) }}
<form method="post" action="{{ path('brand_delete', {'id': brand.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ brand.id) }}">

View File

@ -1,4 +0,0 @@
<form method="post" action="{{ path('gpu-core_delete', {'id': gpu_core.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ gpu_core.id) }}">
<button type="submit" class="alert button expanded">Delete</button>
</form>

View File

@ -1,4 +0,0 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="success button expanded">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -14,8 +14,14 @@
</div>
<div class="large primary callout">
{{ include('gpu_core/_form.html.twig', {'button_label': 'Update'}) }}
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
<button type="submit" class="success button expanded">Update</button>
{{ form_end(edit_form) }}
{{ include('gpu_core/_delete_form.html.twig') }}
<form method="post" action="{{ path('gpu-core_delete', {'id': gpu_core.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ gpu_core.id) }}">
<button type="submit" class="alert button expanded">Delete</button>
</form>
</div>
{% endblock %}

View File

@ -14,6 +14,9 @@
</div>
<div class="large primary callout">
{{ include('gpu_core/_form.html.twig') }}
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="success button expanded">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
</div>
{% endblock %}