Browse Source

Ajout de 5 films au hasard

Sangfroid 4 months ago
parent
commit
2449f121f2

+ 13 - 2
src/Controller/VideothequeListController.php

@@ -136,12 +136,23 @@ class VideothequeListController extends AbstractController
         $qb = $filmRepository->queryFilmWithReal([$realisateur->getNomComplet()], $filtersForm->getData()->page, $filtersForm->getData()->toArray());
         $pagination = new Pagination($qb, $filtersForm->getData()->limit, $filtersForm->getData()->page);
 
-		return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
+		return $this->render('videotheque/liste_'.$options->vue().'.html.twig', [
             'listeFilms'	=>	$pagination->getPaginator(),
             'titre'         =>  'Films par réalisateur : ' . $realisateur->getNomComplet(),
             'filtersForm'   =>  $filtersForm->createView(),
             'pagination'    =>  $pagination
-		));
+		]);
+    }
+
+    #[Route('/pif', name: 'videotheque_random')]
+    public function randomAction(FilmRepository $filmRepository, OptionsManager $options): Response
+    {
+        $films = $filmRepository->findRandom(5);
+
+        return $this->render('videotheque/liste_'.$options->vue().'.html.twig', [
+            'listeFilms'    => $films,
+            'titre'         => '5 films au hasard'
+        ]);
     }
 
 	#[Route("/fichefilm/{id}", name: "videotheque_voirfilm")]

+ 20 - 0
src/Repository/FilmRepository.php

@@ -200,4 +200,24 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult()
         ;
     }
+
+    public function findRandom(int $nb = 5): array
+    {
+        $films = $this->createQueryBuilder('f')
+            ->select('f.id')
+            ->getQuery()
+            ->getResult()
+        ;
+        $ids = array_flip(array_map(fn($value): int => $value['id'], $films));
+
+        $random = array_rand($ids, $nb);
+
+        $qb = $this->createQueryBuilder('f');
+        $qb->andWhere($qb->expr()->in('f.id', $random));
+        
+        return $qb
+            ->getQuery()
+            ->getResult()
+        ;
+    }
 }

+ 12 - 6
templates/navbar.html.twig

@@ -22,13 +22,19 @@
                 {% if app.request.attributes.get('_route') == 'prochaines_sorties' %}active{% endif %}"
                 href="{{ path('prochaines_sorties') }}">Prochaines sorties</a>
             </li>
-            {% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
-            <li class="nav-item">
-                <a class="nav-link
-                {% if app.request.attributes.get('_route') == 'realisateur_liste' %}active{% endif %}"
-                href="{{ path('realisateur_liste') }}">Réalisateurs</a>
+            <li class="nav-item dropdown">
+                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownActions" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+                    Actions
+                </a>
+                <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
+                    <li><a class="dropdown-item" href="{{ path('videotheque_random') }}">5 films au hasard</a></li>
+                    {% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
+                    <li class="nav-item">
+                        <a class="dropdown-item" href="{{ path('realisateur_liste') }}">Réalisateurs</a>
+                    </li>
+                    {% endif %}
+                </ul>
             </li>
-            {% endif %}
             {%  if is_granted('ROLE_MODERATEUR') %}
             <li class="nav-item dropdown">
                 <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownAdmin" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">