collection-crud/src/Controller/LensesController.php

177 lines
4.1 KiB
PHP
Raw Normal View History

2017-11-30 15:06:13 -05:00
<?php
namespace App\Controller;
2017-11-30 15:06:13 -05:00
use App\Entity\Lenses;
use App\Form\LensesType;
2017-11-30 15:06:13 -05:00
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2018-07-11 09:18:46 -04:00
use Symfony\Component\Routing\Annotation\Route;
2018-01-04 10:42:36 -05:00
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{Request, RedirectResponse};
2017-11-30 15:06:13 -05:00
/**
2018-01-04 10:42:36 -05:00
* Lens controller.
2017-11-30 15:06:13 -05:00
*
* @Route("lens")
*/
class LensesController extends Controller
{
/**
2018-01-04 10:42:36 -05:00
* Lists all lens entities.
2017-11-30 15:06:13 -05:00
*
2018-07-11 09:18:46 -04:00
* @Route("/", name="lens_index", methods={"GET"})
2017-11-30 15:06:13 -05:00
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$lenses = $em->getRepository(Lenses::class)->findBy([], [
2017-11-30 15:06:13 -05:00
'received' => 'DESC',
'brand' => 'ASC',
'productLine' => 'ASC',
'mount' => 'ASC',
'minFocalLength' => 'ASC',
'maxFStop' => 'ASC',
]);
return $this->render('lenses/index.html.twig', array(
'lenses' => $lenses,
));
}
/**
2018-01-04 10:42:36 -05:00
* Creates a new lens entity.
2017-11-30 15:06:13 -05:00
*
2018-07-11 09:18:46 -04:00
* @Route("/new", name="lens_new", methods={"GET", "POST"})
2017-11-30 15:06:13 -05:00
*/
public function newAction(Request $request)
{
2018-01-04 10:42:36 -05:00
$lens = new Lenses();
$form = $this->createForm(LensesType::class, $lens);
2017-11-30 15:06:13 -05:00
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
2018-01-04 10:42:36 -05:00
$em->persist($lens);
2017-11-30 15:06:13 -05:00
$em->flush();
2018-01-04 10:42:36 -05:00
return $this->redirectToRoute('lens_show', array('id' => $lens->getId()));
2017-11-30 15:06:13 -05:00
}
return $this->render('lenses/new.html.twig', array(
2018-01-04 10:42:36 -05:00
'lense' => $lens,
2017-11-30 15:06:13 -05:00
'form' => $form->createView(),
));
}
/**
2018-01-04 10:42:36 -05:00
* Finds and displays a lens entity.
2017-11-30 15:06:13 -05:00
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}", name="lens_show", methods={"GET"})
2017-11-30 15:06:13 -05:00
*/
public function showAction(Lenses $lense)
{
$deleteForm = $this->createDeleteForm($lense);
return $this->render('lenses/show.html.twig', array(
'lense' => $lense,
'delete_form' => $deleteForm->createView(),
));
}
/**
2018-01-04 10:42:36 -05:00
* Displays a form to edit an existing lens entity.
2017-11-30 15:06:13 -05:00
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}/edit", name="lens_edit", methods={"GET", "POST"})
2017-11-30 15:06:13 -05:00
*/
2018-01-04 10:42:36 -05:00
public function editAction(Request $request, Lenses $lens)
2017-11-30 15:06:13 -05:00
{
2018-01-04 10:42:36 -05:00
$deleteForm = $this->createDeleteForm($lens);
$deacquireForm = $this->createDeacquireForm($lens);
$editForm = $this->createForm(LensesType::class, $lens);
2017-11-30 15:06:13 -05:00
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
2018-01-04 10:42:36 -05:00
return $this->redirectToRoute('lens_edit', array('id' => $lens->getId()));
2017-11-30 15:06:13 -05:00
}
return $this->render('lenses/edit.html.twig', array(
2018-01-04 10:42:36 -05:00
'lense' => $lens,
2017-11-30 15:06:13 -05:00
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
2018-01-04 10:42:36 -05:00
'deacquire_form' => $deacquireForm->createView()
2017-11-30 15:06:13 -05:00
));
}
/**
2018-01-04 10:42:36 -05:00
* Moves a camera to the previouslyOwned table
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}/deacquire", name="lens_deacquire", methods={"POST"})
2018-01-04 10:42:36 -05:00
* @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.
2017-11-30 15:06:13 -05:00
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}", name="lens_delete", methods={"DELETE"})
2017-11-30 15:06:13 -05:00
*/
2018-01-04 10:42:36 -05:00
public function deleteAction(Request $request, Lenses $lens)
2017-11-30 15:06:13 -05:00
{
2018-01-04 10:42:36 -05:00
$form = $this->createDeleteForm($lens);
2017-11-30 15:06:13 -05:00
$form->handleRequest($request);
2018-02-14 15:08:03 -05:00
$em = $this->getDoctrine()->getManager();
$em->remove($lens);
$em->flush();
2017-11-30 15:06:13 -05:00
return $this->redirectToRoute('lens_index');
}
/**
2018-01-04 10:42:36 -05:00
* Creates a form to delete a lens entity.
2017-11-30 15:06:13 -05:00
*
2018-01-04 10:42:36 -05:00
* @param Lenses $lens The lens entity
2017-11-30 15:06:13 -05:00
*
2018-01-04 10:42:36 -05:00
* @return FormInterface The form
2017-11-30 15:06:13 -05:00
*/
2018-01-04 10:42:36 -05:00
private function createDeleteForm(Lenses $lens): FormInterface
2017-11-30 15:06:13 -05:00
{
return $this->createFormBuilder()
2018-02-14 15:08:03 -05:00
->setAction(
$this->generateUrl('lens_delete', ['id' => $lens->getId()])
)
2017-11-30 15:06:13 -05:00
->setMethod('DELETE')
->getForm();
}
2018-01-04 10:42:36 -05:00
/**
* 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();
}
2017-11-30 15:06:13 -05:00
}