collection-crud/src/Controller/FilmController.php

112 lines
2.8 KiB
PHP
Raw Normal View History

2018-07-18 11:35:27 -04:00
<?php declare(strict_types=1);
2018-02-15 09:48:57 -05:00
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use LogicException;
2018-02-15 09:48:57 -05:00
use App\Entity\Film;
use App\Form\FilmType;
2018-07-18 11:35:27 -04:00
use Doctrine\Common\Collections\Criteria;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2020-06-02 16:08:08 -04:00
use Symfony\Component\HttpFoundation\Response;
2018-07-11 09:18:46 -04:00
use Symfony\Component\Routing\Annotation\Route;
2018-02-15 09:48:57 -05:00
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Film controller.
*/
#[Route(path: 'film')]
class FilmController extends AbstractController
2018-02-15 09:48:57 -05:00
{
use FormControllerTrait;
protected const ENTITY = Film::class;
2022-02-18 11:34:25 -05:00
protected const FORM = FilmType::class;
2022-02-18 11:34:25 -05:00
public function __construct(private readonly ManagerRegistry $managerRegistry)
{
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Lists all film entities.
*/
#[Route(path: '/', name: 'film_index', methods: ['GET'])]
public function indexAction() : Response
2018-02-15 09:48:57 -05:00
{
$repo = $this->managerRegistry->getManager()->getRepository(self::ENTITY);
2018-07-18 11:35:27 -04:00
$criteria = Criteria::create()
->where(Criteria::expr()->gt('rollsInCamera', 0))
->orderBy([
'filmFormat' => Criteria::ASC,
'brand' => Criteria::ASC,
'rollsInCamera' => Criteria::DESC,
'productLine' => Criteria::ASC,
]);
$inCamera = $repo->matching($criteria);
$notInCamera = $repo->findBy([
'rollsInCamera' => 0,
], [
2018-02-15 09:48:57 -05:00
'brand' => 'ASC',
'productLine' => 'ASC',
'filmFormat' => 'ASC',
]);
2018-07-18 11:35:27 -04:00
return $this->render('film/index.html.twig', [
'in_camera' => $inCamera,
'films' => $notInCamera,
]);
2018-02-15 09:48:57 -05:00
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Creates a new film entity.
*/
#[Route(path: '/new', name: 'film_new', methods: ['GET', 'POST'])]
2018-02-15 09:48:57 -05:00
public function newAction(Request $request)
{
return $this->itemCreate($request, 'film/new.html.twig', 'film', 'film_show');
2018-02-15 09:48:57 -05:00
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Finds and displays a film entity.
*/
#[Route(path: '/{id}', name: 'film_show', methods: ['GET'])]
2018-02-15 09:48:57 -05:00
public function showAction(Film $film)
{
return $this->itemView($film, 'film/show.html.twig', 'film');
2018-02-15 09:48:57 -05:00
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Displays a form to edit an existing film entity.
*
* @throws LogicException
2018-02-15 09:48:57 -05:00
*/
#[Route(path: '/{id}/edit', name: 'film_edit', methods: ['GET', 'POST'])]
2018-02-15 09:48:57 -05:00
public function editAction(Request $request, Film $film)
{
return $this->itemUpdate($request, $film, 'film/edit.html.twig', 'film', 'film_show');
2018-02-15 09:48:57 -05:00
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Deletes a film entity.
*
* @throws LogicException
2018-02-15 09:48:57 -05:00
*/
#[Route(path: '/{id}', name: 'film_delete', methods: ['DELETE'])]
2018-02-15 09:48:57 -05:00
public function deleteAction(Request $request, Film $film)
{
return $this->itemDelete($request, $film, 'film_index');
2018-02-15 09:48:57 -05:00
}
2022-02-18 11:34:25 -05:00
2018-02-15 09:48:57 -05:00
/**
* Creates a form to delete a film entity.
*
* @param Film $film The film entity
*
2020-06-02 16:08:08 -04:00
* @return FormInterface The form
2018-02-15 09:48:57 -05:00
*/
private function createDeleteForm(Film $film): FormInterface
{
return $this->buildForm($film, 'film_delete', 'DELETE');
2018-02-15 09:48:57 -05:00
}
}