collection-crud/src/Controller/PreviouslyOwnedCameraContro...

115 lines
3.3 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\PreviouslyOwnedCamera;
use App\Form\PreviouslyOwnedCameraType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request};
/**
* Previouslyownedcamera controller.
*
* @Route("previously-owned-camera")
*/
class PreviouslyOwnedCameraController extends Controller
{
/**
* Lists all previouslyOwnedCamera entities.
*
* @Route("/", name="previously-owned-camera_index", methods={"GET"})
* @throws \UnexpectedValueException
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$previouslyOwnedCameras = $em->getRepository('App:PreviouslyOwnedCamera')->findBy([], [
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
return $this->render('previouslyownedcamera/index.html.twig', array(
'previouslyOwnedCameras' => $previouslyOwnedCameras,
));
}
/**
* Finds and displays a previouslyOwnedCamera entity.
*
* @Route("/{id}", name="previously-owned-camera_show", methods={"GET"})
*/
public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera)
{
return $this->render('previouslyownedcamera/show.html.twig', array(
'previouslyOwnedCamera' => $previouslyOwnedCamera,
));
}
/**
* Displays a form to edit an existing previouslyOwnedCamera entity.
*
* @Route("/{id}/edit", name="previously-owned-camera_edit", methods={"GET", "POST"})
* @throws \LogicException
*/
public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera)
{
$editForm = $this->createForm(PreviouslyOwnedCameraType::class, $previouslyOwnedCamera);
$editForm->handleRequest($request);
$reacquireForm = $this->createReacquireForm($previouslyOwnedCamera);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('previously-owned-camera_edit', array('id' => $previouslyOwnedCamera->getId()));
}
return $this->render('previouslyownedcamera/edit.html.twig', array(
'previouslyOwnedCamera' => $previouslyOwnedCamera,
'edit_form' => $editForm->createView(),
'reacquire_form' => $reacquireForm->createView()
));
}
/**
* Moves a camera to the previouslyOwned table
*
* @Route("/{id}/reacquire", name="previously-owned-camera_reacquire", methods={"POST"})
* @param Request $request
* @param PreviouslyOwnedCamera $camera
* @throws \LogicException
* @throws \Doctrine\ORM\ORMInvalidArgumentException
* @return RedirectResponse
*/
public function reacquireAction(Request $request, PreviouslyOwnedCamera $camera): RedirectResponse
{
$form = $this->createReacquireForm($camera);
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(PreviouslyOwnedCamera::class);
$repository->reacquire($camera);
return $this->redirectToRoute('previously-owned-camera_index');
}
/**
* Creates a form to move
*
* @param PreviouslyOwnedCamera $camera The camera entity
*
* @return FormInterface
*/
private function createReacquireForm(PreviouslyOwnedCamera $camera): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('previously-owned-camera_reacquire', ['id' => $camera->getId()]))
->setMethod('POST')
->getForm();
}
}