Sangfroid 9 miesięcy temu
rodzic
commit
5a312b68f0

+ 18 - 2
src/Controller/VideothequeController.php

@@ -2,6 +2,7 @@
 
 namespace App\Controller;
 
+use App\Dto\FiltersDTO;
 use App\Entity\Commentaire;
 use App\Entity\Realisateur;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -11,30 +12,45 @@ use Symfony\Component\HttpFoundation\Request;
 use App\Entity\Film;
 use App\Form\CommentaireType;
 use App\Form\FilmType;
+use App\Form\FiltersType;
 use App\Repository\CommentaireRepository;
 use App\Repository\FilmRepository;
 use App\Repository\GenreRepository;
 use App\Repository\RealisateurRepository;
 use App\Service\CommentaireManager;
 use App\Service\FilmManager;
+use App\Service\FiltersService;
 use App\Service\OptionsManager;
 use App\Service\TmdbApiService;
 use Symfony\Bundle\SecurityBundle\Security;
 use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Security\Http\Attribute\IsGranted;
-use Symfony\Component\Serializer\SerializerInterface;
 
 class VideothequeController extends AbstractController
 {
 	#[Route("/", name: "videotheque_liste")]
-	public function listeAction(Request $request, FilmRepository $repo, OptionsManager $options): Response
+	public function listeAction(Request $request, FilmRepository $repo, OptionsManager $options, FiltersService $filtersService): Response
 	{
         $listeFilms = $repo->findTous();
 
+        $filterDto = new FiltersDTO();
+        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
+
+        $filtersForm->handleRequest($request);
+
+        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
+            // Récupérer les données de tri et de filtre
+            $filters = $filtersForm->getData();
+    
+            // Appliquer les filtres et les tris
+            $listeFilms = $filtersService->applyFiltersAndSort($listeFilms, $filters);
+        }
+
 		return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
             'listeFilms'	=>	$listeFilms,
             'titre'         =>  'Liste complète',
+            'filtersForm'   =>  $filtersForm->createView()
 		));
     }
 

+ 26 - 0
src/Dto/FiltersDTO.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Dto;
+
+use Symfony\Component\Validator\Constraints as Assert;
+
+class FiltersDTO
+{
+    public function __construct(
+        #[Assert\NotBlank()]
+        #[Assert\GreaterThanOrEqual(10)]
+        #[Assert\LessThanOrEqual(100)]
+        public int $nbResults = 25,
+
+        #[Assert\NotBlank()]
+        public string $userName = '',
+
+        public string $sortBy = '',
+
+        #[Assert\Choice(['ASC', 'DESC'])]
+        public string $sortOrder = 'DESC'
+    )
+    {
+        
+    }
+}

+ 29 - 0
src/Form/FiltersType.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Form;
+
+use App\Dto\FiltersDTO;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\Extension\Core\Type\IntegerType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class FiltersType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options): void
+    {
+        $builder
+            ->add('nbResults', IntegerType::class)
+            ->add('userName')
+            ->add('sortBy')
+            ->add('sortOrder')
+        ;
+    }
+
+    public function configureOptions(OptionsResolver $resolver): void
+    {
+        $resolver->setDefaults([
+            'data_class' => FiltersDTO::class
+        ]);
+    }
+}

+ 14 - 0
src/Service/FiltersService.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Service;
+
+use App\Dto\FiltersDTO;
+
+class FiltersService
+{
+    public function applyFiltersAndSort(?array $data, FiltersDTO $filters)
+    {
+        $filteredData = $data;
+        return $filteredData;
+    }
+}

+ 6 - 0
templates/videotheque/_filters_form.html.twig

@@ -0,0 +1,6 @@
+{{ form_start(filtersForm) }}
+
+{{ form_rest(filtersForm)}}
+<button class="btn btn-primary mb-3" type="submit">Filtrer</button>
+
+{{ form_end(filtersForm) }}

+ 2 - 0
templates/videotheque/liste_vignettes.html.twig

@@ -12,6 +12,8 @@
 
 	{{ include ('videotheque/_liste_header.html.twig') }}
 
+	{{ include ('videotheque/_filters_form.html.twig') }}
+
 	<section id="tableFilms">
 		
 		{% for film in listeFilms %}