148 lines
3.4 KiB
PHP
148 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Film;
|
|
use App\Form\FilmType;
|
|
use Doctrine\ORM\OptimisticLockException;
|
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
|
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;
|
|
|
|
/**
|
|
* Film controller.
|
|
*
|
|
* @Route("film")
|
|
*/
|
|
class FilmController extends Controller
|
|
{
|
|
/**
|
|
* Lists all film entities.
|
|
*
|
|
* @Route("/", name="film_index")
|
|
* @Method("GET")
|
|
*/
|
|
public function indexAction()
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$films = $em->getRepository('App:Film')->findBy([], [
|
|
'brand' => 'ASC',
|
|
'productLine' => 'ASC',
|
|
'filmFormat' => 'ASC',
|
|
]);
|
|
|
|
return $this->render('film/index.html.twig', array(
|
|
'films' => $films,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Creates a new film entity.
|
|
*
|
|
* @Route("/new", name="film_new")
|
|
* @Method({"GET", "POST"})
|
|
*/
|
|
public function newAction(Request $request)
|
|
{
|
|
$film = new Film();
|
|
$form = $this->createForm(FilmType::class, $film);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->persist($film);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute('film_show', array('id' => $film->getId()));
|
|
}
|
|
|
|
return $this->render('film/new.html.twig', array(
|
|
'film' => $film,
|
|
'form' => $form->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Finds and displays a film entity.
|
|
*
|
|
* @Route("/{id}", name="film_show")
|
|
* @Method("GET")
|
|
*/
|
|
public function showAction(Film $film)
|
|
{
|
|
$deleteForm = $this->createDeleteForm($film);
|
|
|
|
return $this->render('film/show.html.twig', array(
|
|
'film' => $film,
|
|
'delete_form' => $deleteForm->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Displays a form to edit an existing film entity.
|
|
*
|
|
* @Route("/{id}/edit", name="film_edit")
|
|
* @Method({"GET", "POST"})
|
|
* @throws \LogicException
|
|
*/
|
|
public function editAction(Request $request, Film $film)
|
|
{
|
|
$deleteForm = $this->createDeleteForm($film);
|
|
|
|
$editForm = $this->createForm(FilmType::class, $film);
|
|
$editForm->handleRequest($request);
|
|
|
|
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
|
$this->getDoctrine()->getManager()->flush();
|
|
|
|
return $this->redirectToRoute('film_edit', array('id' => $film->getId()));
|
|
}
|
|
|
|
return $this->render('film/edit.html.twig', array(
|
|
'film' => $film,
|
|
'edit_form' => $editForm->createView(),
|
|
'delete_form' => $deleteForm->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Deletes a film entity.
|
|
*
|
|
* @Route("/{id}", name="film_delete")
|
|
* @Method("DELETE")
|
|
* @throws \LogicException
|
|
*/
|
|
public function deleteAction(Request $request, Film $film)
|
|
{
|
|
$form = $this->createDeleteForm($film);
|
|
$form->handleRequest($request);
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->remove($film);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute('film_index');
|
|
}
|
|
|
|
/**
|
|
* Creates a form to delete a film entity.
|
|
*
|
|
* @param Film $film The film entity
|
|
*
|
|
* @return \Symfony\Component\Form\FormInterface The form
|
|
*/
|
|
private function createDeleteForm(Film $film): FormInterface
|
|
{
|
|
return $this->createFormBuilder()
|
|
->setAction($this->generateUrl('film_delete', ['id' => $film->getId()]))
|
|
->setMethod('DELETE')
|
|
->getForm();
|
|
}
|
|
}
|