collection-crud/src/Controller/PreviouslyOwnedCameraController.php

119 lines
3.4 KiB
PHP

<?php
namespace CameraBundle\Controller;
use CameraBundle\Entity\PreviouslyOwnedCamera;
use CameraBundle\Form\PreviouslyOwnedCameraType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\{Method, Route};
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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")
* @Method("GET")
* @throws \UnexpectedValueException
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$previouslyOwnedCameras = $em->getRepository('CameraBundle: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")
* @Method("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")
* @Method({"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")
* @Method("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();
}
}