collection-crud/src/Controller/FormControllerTrait.php

172 lines
5.1 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace App\Controller;
2022-09-29 18:35:53 -04:00
use Doctrine\ORM\EntityManagerInterface;
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
*
* @param mixed $item
*/
protected function buildForm($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 $template, string $templateKey, string $redirectRoute)
{
$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(),
]);
}
/**
* List view for the data type
*/
protected function itemListView(string $template, string $templateKey, array $sort = []): Response
{
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
*
* @param mixed $item
*/
protected function itemView($item, string $template, string $templateKey): Response
{
$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
*
* @param mixed $item
*/
protected function itemUpdate(Request $request, $item, string $template, string $templateKey, string $redirectRoute)
{
$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
*
* @param mixed $item
*/
protected function itemDeacquire(Request $request, $item, string $redirectRoute): RedirectResponse
{
$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
*
* @param mixed $item
*/
protected function itemReacquire(Request $request, $item, string $redirectRoute): RedirectResponse
{
$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);
}
/**
* Actually delete an item
*
* @param mixed $item
*/
2022-09-29 18:35:53 -04:00
protected function itemDelete(Request $request, mixed $item, string $redirectRoute): RedirectResponse
2022-03-03 10:53:48 -05:00
{
$form = $this->createDeleteForm($item);
$form->handleRequest($request);
2022-09-29 18:35:53 -04:00
$this->entityManager->remove($item);
$this->entityManager->flush();
2022-03-03 10:53:48 -05:00
return $this->redirectToRoute($redirectRoute);
}
}