169 lines
4.3 KiB
PHP
169 lines
4.3 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\{Request, Response, RedirectResponse};
|
|
|
|
trait FormControllerTrait {
|
|
/**
|
|
* Create a form generator
|
|
*/
|
|
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())
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->persist($item);
|
|
$em->flush();
|
|
|
|
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
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$items = $em->getRepository(self::ENTITY)->findBy([], $sort);
|
|
|
|
return $this->render($template, [
|
|
$templateKey => $items,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* View details for a specific 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
|
|
*/
|
|
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())
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->persist($item);
|
|
$em->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, $item, string $redirectRoute): RedirectResponse
|
|
{
|
|
$form = $this->createDeacquireForm($item);
|
|
$form->handleRequest($request);
|
|
|
|
$repository = $this->getDoctrine()->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, $item, string $redirectRoute): RedirectResponse
|
|
{
|
|
$form = $this->createReacquireForm($item);
|
|
$form->handleRequest($request);
|
|
|
|
$repository = $this->getDoctrine()->getRepository(self::ENTITY);
|
|
$repository->reacquire($item);
|
|
|
|
return $this->redirectToRoute($redirectRoute);
|
|
}
|
|
|
|
/**
|
|
* Actually delete an item
|
|
*/
|
|
protected function itemDelete(Request $request, $item, string $redirectRoute): RedirectResponse
|
|
{
|
|
$form = $this->createDeleteForm($item);
|
|
$form->handleRequest($request);
|
|
|
|
// if ($form->isSubmitted() && $form->isValid())
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->remove($item);
|
|
$em->flush();
|
|
}
|
|
|
|
return $this->redirectToRoute($redirectRoute);
|
|
}
|
|
}
|