collection-crud/src/Controller/LensesController.php

144 lines
3.3 KiB
PHP
Raw Normal View History

2017-11-30 15:06:13 -05:00
<?php
namespace CameraBundle\Controller;
use CameraBundle\Entity\Lenses;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Lense controller.
*
* @Route("lens")
*/
class LensesController extends Controller
{
/**
* Lists all lense 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 lense entity.
*
* @Route("/new", name="lens_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$lense = new Lenses();
$form = $this->createForm('CameraBundle\Form\LensesType', $lense);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($lense);
$em->flush();
return $this->redirectToRoute('lens_show', array('id' => $lense->getId()));
}
return $this->render('lenses/new.html.twig', array(
'lense' => $lense,
'form' => $form->createView(),
));
}
/**
* Finds and displays a lense 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 lense entity.
*
* @Route("/{id}/edit", name="lens_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Lenses $lense)
{
$deleteForm = $this->createDeleteForm($lense);
$editForm = $this->createForm('CameraBundle\Form\LensesType', $lense);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('lens_edit', array('id' => $lense->getId()));
}
return $this->render('lenses/edit.html.twig', array(
'lense' => $lense,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a lense entity.
*
* @Route("/{id}", name="lens_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, Lenses $lense)
{
$form = $this->createDeleteForm($lense);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($lense);
$em->flush();
}
return $this->redirectToRoute('lens_index');
}
/**
* Creates a form to delete a lense entity.
*
* @param Lenses $lense The lense entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Lenses $lense)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('lens_delete', array('id' => $lense->getId())))
->setMethod('DELETE')
->getForm();
}
}