collection-crud/src/Controller/CameraController.php

184 lines
4.4 KiB
PHP

<?php
namespace CameraBundle\Controller;
use CameraBundle\Entity\Camera;
use CameraBundle\Form\CameraType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Camera controller.
*
* @Route("camera")
*/
class CameraController extends Controller
{
/**
* Lists all camera entities.
*
* @Route("/", name="camera_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$cameras = $em->getRepository('CameraBundle:Camera')->findBy([], [
'received' => 'DESC',
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
return $this->render('camera/index.html.twig', array(
'cameras' => $cameras,
));
}
/**
* Creates a new camera entity.
*
* @Route("/new", name="camera_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$camera = new Camera();
$form = $this->createForm(CameraType::class, $camera);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($camera);
$em->flush();
return $this->redirectToRoute('camera_show', array('id' => $camera->getId()));
}
return $this->render('camera/new.html.twig', array(
'camera' => $camera,
'form' => $form->createView(),
));
}
/**
* Finds and displays a camera entity.
*
* @Route("/{id}", name="camera_show")
* @Method("GET")
*/
public function showAction(Camera $camera)
{
$deleteForm = $this->createDeleteForm($camera);
return $this->render('camera/show.html.twig', array(
'camera' => $camera,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing camera entity.
*
* @Route("/{id}/edit", name="camera_edit")
* @Method({"GET", "POST"})
* @throws \LogicException
*/
public function editAction(Request $request, Camera $camera)
{
$deleteForm = $this->createDeleteForm($camera);
$deacquireForm = $this->createDeacquireForm($camera);
$editForm = $this->createForm(CameraType::class, $camera);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('camera_edit', array('id' => $camera->getId()));
}
return $this->render('camera/edit.html.twig', array(
'camera' => $camera,
'edit_form' => $editForm->createView(),
'deacquire_form' => $deacquireForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a camera entity.
*
* @Route("/{id}", name="camera_delete")
* @Method("DELETE")
* @throws \LogicException
*/
public function deleteAction(Request $request, Camera $camera)
{
$form = $this->createDeleteForm($camera);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$em->remove($camera);
$em->flush();
return $this->redirectToRoute('camera_index');
}
/**
* Moves a camera to the previouslyOwned table
*
* @Route("/{id}/deacquire", name="camera_deacquire")
* @Method("POST")
* @param Request $request
* @param Camera $camera
* @return RedirectResponse
*/
public function deacquireAction(Request $request, Camera $camera)
{
$form = $this->createDeacquireForm($camera);
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(Camera::class);
$repository->deacquire($camera);
return $this->redirectToRoute('camera_index');
}
/**
* Creates a form to delete a camera entity.
*
* @param Camera $camera The camera entity
*
* @return \Symfony\Component\Form\FormInterface The form
*/
private function createDeleteForm(Camera $camera): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('camera_delete', ['id' => $camera->getId()]))
->setMethod('DELETE')
->getForm();
}
/**
* Creates a form to move
*
* @param Camera $camera The camera entity
*
* @return FormInterface
*/
private function createDeacquireForm(Camera $camera): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('camera_deacquire', ['id' => $camera->getId()]))
->setMethod('POST')
->getForm();
}
}