collection-crud/src/Traits/FormControllerTrait.php

189 lines
5.8 KiB
PHP

<?php declare(strict_types=1);
namespace App\Traits;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
trait FormControllerTrait
{
private readonly EntityManagerInterface $entityManager;
/**
* Create a form generator
*/
protected function buildForm(mixed $item, string $actionRoute, string $method = 'POST'): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl($actionRoute, ['id' => $item->getId()]))
->setMethod($method)
->getForm();
}
/**
* Show create form / create an item
*/
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);
$form->handleRequest($request);
// If creating the item
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($item);
$this->entityManager->flush();
return $this->redirectToRoute($redirectRoute, ['id' => $item->getId()]);
}
// If showing the form
return $this->render($template, [
$templateKey => $item,
'form' => $form->createView(),
]);
}
protected function indexView(string $templateKey, array $sort = []): Response
{
return $this->itemListView($templateKey, $sort);
}
/**
* List view for the data type
*/
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, [
$templateKey => $items,
]);
}
/**
* View details for a specific item
*/
protected function itemView(mixed $item, string $templateKey): Response
{
$template = self::TEMPLATE_PATH . 'show.html.twig';
$templateData = [
$templateKey => $item,
];
if (method_exists($this, 'createDeleteForm')) {
$deleteForm = $this->createDeleteForm($item);
$templateData['delete_form'] = $deleteForm->createView();
}
return $this->render($template, $templateData);
}
/**
* Show edit form / update an item
*/
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);
// If updating the item
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->entityManager->persist($item);
$this->entityManager->flush();
return $this->redirectToRoute($redirectRoute, ['id' => $item->getId()]);
}
// If showing the edit form
$templateData = [
$templateKey => $item,
'edit_form' => $editForm->createView(),
];
if (method_exists($this, 'createDeleteForm')) {
$deleteForm = $this->createDeleteForm($item);
$templateData['delete_form'] = $deleteForm->createView();
}
if (method_exists($this, 'createDeacquireForm')) {
$deacquireForm = $this->createDeacquireForm($item);
$templateData['deacquire_form'] = $deacquireForm->createView();
}
if (method_exists($this, 'createReacquireForm')) {
$reacquireForm = $this->createReacquireForm($item);
$templateData['reacquire_form'] = $reacquireForm->createView();
}
return $this->render($template, $templateData);
}
/**
* Move an item to a previously_owned table
*/
protected function itemDeacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse
{
$form = $this->createDeacquireForm($item);
$form->handleRequest($request);
$repository = $this->entityManager->getRepository(self::ENTITY);
$repository->deacquire($item);
return $this->redirectToRoute($redirectRoute);
}
/**
* Move an item from a previously_owned table back to the original table
*/
protected function itemReacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse
{
$form = $this->createReacquireForm($item);
$form->handleRequest($request);
$repository = $this->entityManager->getRepository(self::ENTITY);
$repository->reacquire($item);
return $this->redirectToRoute($redirectRoute);
}
/**
* Actually delete an item
*/
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, [], 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);
}
}