collection-crud/src/Controller/FilmController.php

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