collection-crud/src/Controller/LensesController.php

183 lines
4.2 KiB
PHP

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