2018-07-16 13:10:00 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2022-09-30 10:48:14 -04:00
|
|
|
namespace App\Traits;
|
2018-07-16 13:10:00 -04:00
|
|
|
|
2022-09-29 18:35:53 -04:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2018-07-16 13:10:00 -04:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2022-03-03 11:15:12 -05:00
|
|
|
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
|
2022-03-03 10:53:48 -05:00
|
|
|
|
|
|
|
trait FormControllerTrait
|
|
|
|
{
|
2022-09-29 18:35:53 -04:00
|
|
|
private readonly EntityManagerInterface $entityManager;
|
2022-03-03 10:53:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a form generator
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function buildForm(mixed $item, string $actionRoute, string $method = 'POST'): FormInterface
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
|
|
|
return $this->createFormBuilder()
|
|
|
|
->setAction($this->generateUrl($actionRoute, ['id' => $item->getId()]))
|
|
|
|
->setMethod($method)
|
|
|
|
->getForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show create form / create an item
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemCreate(Request $request, string $templateKey): RedirectResponse|Response
|
|
|
|
{
|
|
|
|
$template = self::TEMPLATE_PATH . 'new.html.twig';
|
|
|
|
$redirectRoute = self::ROUTE_PREFIX . 'show';
|
|
|
|
|
2022-03-03 10:53:48 -05:00
|
|
|
$Entity = self::ENTITY;
|
|
|
|
$item = new $Entity();
|
|
|
|
$form = $this->createForm(self::FORM, $item);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
// If creating the item
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2022-09-29 18:35:53 -04:00
|
|
|
$this->entityManager->persist($item);
|
|
|
|
$this->entityManager->flush();
|
2022-03-03 10:53:48 -05:00
|
|
|
|
|
|
|
return $this->redirectToRoute($redirectRoute, ['id' => $item->getId()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If showing the form
|
|
|
|
return $this->render($template, [
|
|
|
|
$templateKey => $item,
|
|
|
|
'form' => $form->createView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function indexView(string $templateKey, array $sort = []): Response
|
|
|
|
{
|
|
|
|
return $this->itemListView($templateKey, $sort);
|
|
|
|
}
|
|
|
|
|
2022-03-03 10:53:48 -05:00
|
|
|
/**
|
|
|
|
* List view for the data type
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemListView(string $templateKey, array $sort = []): Response
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
2022-09-30 10:48:14 -04:00
|
|
|
$template = self::TEMPLATE_PATH . 'index.html.twig';
|
|
|
|
|
2022-09-29 18:35:53 -04:00
|
|
|
$items = $this->entityManager->getRepository(self::ENTITY)->findBy([], $sort);
|
2022-03-03 10:53:48 -05:00
|
|
|
|
|
|
|
return $this->render($template, [
|
|
|
|
$templateKey => $items,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View details for a specific item
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemView(mixed $item, string $templateKey): Response
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
2022-09-30 10:48:14 -04:00
|
|
|
$template = self::TEMPLATE_PATH . 'show.html.twig';
|
|
|
|
|
2022-03-03 10:53:48 -05:00
|
|
|
$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
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemUpdate(Request $request, mixed $item, string $templateKey): RedirectResponse|Response
|
|
|
|
{
|
|
|
|
$template = self::TEMPLATE_PATH . 'edit.html.twig';
|
|
|
|
$redirectRoute = self::ROUTE_PREFIX . 'show';
|
|
|
|
|
2022-03-03 10:53:48 -05:00
|
|
|
$editForm = $this->createForm(self::FORM, $item);
|
|
|
|
$editForm->handleRequest($request);
|
|
|
|
|
|
|
|
// If updating the item
|
|
|
|
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
2022-09-29 18:35:53 -04:00
|
|
|
$this->entityManager->persist($item);
|
|
|
|
$this->entityManager->flush();
|
2022-03-03 10:53:48 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemDeacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
|
|
|
$form = $this->createDeacquireForm($item);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2022-09-29 18:35:53 -04:00
|
|
|
$repository = $this->entityManager->getRepository(self::ENTITY);
|
2022-03-03 10:53:48 -05:00
|
|
|
$repository->deacquire($item);
|
|
|
|
|
|
|
|
return $this->redirectToRoute($redirectRoute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move an item from a previously_owned table back to the original table
|
|
|
|
*/
|
2022-09-30 10:48:14 -04:00
|
|
|
protected function itemReacquire(Request $request, mixed $item, string $redirectRoute): RedirectResponse
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
|
|
|
$form = $this->createReacquireForm($item);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2022-09-29 18:35:53 -04:00
|
|
|
$repository = $this->entityManager->getRepository(self::ENTITY);
|
2022-03-03 10:53:48 -05:00
|
|
|
$repository->reacquire($item);
|
|
|
|
|
|
|
|
return $this->redirectToRoute($redirectRoute);
|
|
|
|
}
|
|
|
|
|
2022-09-30 10:48:14 -04:00
|
|
|
/**
|
|
|
|
* Actually delete an item
|
|
|
|
*/
|
|
|
|
protected function itemDelete(Request $request, mixed $item): RedirectResponse
|
2022-03-03 10:53:48 -05:00
|
|
|
{
|
2022-09-30 10:48:14 -04:00
|
|
|
$redirectRoute = self::ROUTE_PREFIX . 'index';
|
|
|
|
|
2022-03-03 10:53:48 -05:00
|
|
|
$form = $this->createDeleteForm($item);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2022-09-30 10:48:14 -04:00
|
|
|
// if ($this->isCsrfTokenValid((string)$item->getId(), $request->request->get('_token'))) {
|
|
|
|
$this->entityManager->remove($item);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
//}
|
2022-03-03 10:53:48 -05:00
|
|
|
|
2022-09-30 10:48:14 -04:00
|
|
|
return $this->redirectToRoute($redirectRoute, [], Response::HTTP_SEE_OTHER);
|
2022-03-03 10:53:48 -05:00
|
|
|
}
|
2022-09-30 10:48:14 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-07-16 13:10:00 -04:00
|
|
|
}
|