فهرست منبع

Fonction de recherche

François 6 سال پیش
والد
کامیت
f561f8da5c

+ 9 - 8
app/Resources/views/base.html.twig

@@ -43,15 +43,20 @@
 						</ul>
 					{%  endif %}
 				</ul>
-				{%  if not is_granted('IS_AUTHENTICATED_REMEMBERED') %}
+
+				<form class="form-inline my-2 my-lg-0" action="{{ path('search_recherche') }}">
+					<input class="form-control mr-sm-2" name="q" type="text" placeholder="Rechercher" aria-label="Rechercher">
+					<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Go</button>
+				</form>
+                {%  if not is_granted('IS_AUTHENTICATED_REMEMBERED') %}
 					<ul class="navbar-nav">
 						<li class="nav-item"><a class="nav-link" href="{{ path('login') }}">Se connecter</a></li>
 					</ul>
-				{% else %}
+                {% else %}
 					<ul class="navbar-nav">
 						<li class="nav-item dropdown">
 							<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
-								{{ app.user.nomComplet }}
+                                {{ app.user.nomComplet }}
 							</a>
 							<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
 								<a class="dropdown-item" href="{{ path('user_profil') }}">Profil</a>
@@ -60,11 +65,7 @@
 							</div>
 						</li>
 					</ul>
-				{% endif %}
-				{#<form class="form-inline my-2 my-lg-0">
-					<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
-					<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
-				</form>#}
+                {% endif %}
 
 			</div>
 		</nav>

+ 7 - 1
app/config/services.yml

@@ -36,4 +36,10 @@ services:
 
     AppBundle\Service\ActivityListener:
         tags:
-        - { name: 'kernel.event_listener', event: 'kernel.controller', method: onCoreController }
+        - { name: 'kernel.event_listener', event: 'kernel.controller', method: onCoreController }
+
+    film.searcher:
+        class: AppBundle\Service\Search
+        public: true
+        arguments:
+        - "@doctrine.orm.entity_manager"

+ 23 - 0
src/AppBundle/Controller/SearchController.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace AppBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Routing\Annotation\Route;
+
+class SearchController extends Controller
+{
+    /**
+     * @Route("/recherche", name="search_recherche")
+     */
+    public function searchAction(Request $request)
+    {
+        $filmSearch = $this->get('film.searcher');
+        $query = $request->query->get('q', "");
+        return $this->render('@App/videotheque/liste.html.twig', array(
+            'listeFilms'    =>  $filmSearch->search($query),
+            'query'         =>  $query
+        ));
+    }
+}

+ 28 - 0
src/AppBundle/Repository/FilmRepository.php

@@ -46,6 +46,34 @@ class FilmRepository extends \Doctrine\ORM\EntityRepository
             ->getResult();
     }
 
+    public function findFilmWithRealLike($query)
+    {
+        $qb = $this->createQueryBuilder('f');
+        $qb
+            ->innerJoin('f.realisateurs', 'r')
+            ->addSelect('f');
+        $qb
+            ->where($qb->expr()->like('r.nomComplet', ':nomComplet'))
+            ->setParameter('nomComplet', '%'.$query.'%');
+
+        return $qb->getQuery()->getResult();
+
+    }
+
+    public function findFilmWithGenreLike($genreName) {
+        $qb = $this->createQueryBuilder('f');
+        $qb
+            ->innerJoin('f.genres', 'g')
+            ->addSelect('f');
+
+        $qb->where($qb->expr()->like('g.name', ':genreName'))
+            ->setParameter('genreName', '%'.$genreName.'%');
+
+        return $qb
+            ->getQuery()
+            ->getResult();
+    }
+
 
 
     public function findTousDesc() {

+ 72 - 0
src/AppBundle/Service/Search.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace AppBundle\Service;
+
+use Doctrine\ORM\EntityManagerInterface;
+
+/**
+ * Faire des recherches de films
+ */
+class Search
+{
+    protected $em;
+
+    /**
+     * Search constructor.
+     * @param EntityManagerInterface $em
+     */
+    public function __construct(EntityManagerInterface $em)
+    {
+        $this->em = $em;
+    }
+
+    /**
+     * @param string $query
+     * @return \AppBundle\Entity\Film[]
+     */
+    public function search($query)
+    {
+        return \array_merge(
+            $this->searchByFilm($query),
+            $this->searchByRealisateur($query),
+            $this->searchByGenre($query)
+        );
+    }
+
+    /**
+     * @param $query
+     * @return array
+     */
+    protected function searchByFilm($query)
+    {
+        $q = $this->em
+            ->createQuery('SELECT f from AppBundle:Film f WHERE f.titre like :titre')
+        ->setParameter('titre', '%'.$query.'%')
+        ->getResult();
+            ;
+        return $q;
+    }
+
+    /**
+     * @param $query
+     * @return array
+     */
+    protected function searchByRealisateur($query)
+    {
+        $q = $this->em->getRepository('AppBundle:Film');
+        return $q->findFilmWithRealLike($query);
+    }
+
+    /**
+     * @param $query
+     * @return array
+     */
+    protected function searchByGenre($query)
+    {
+        $q = $this->em->getRepository('AppBundle:Film');
+        return $q->findFilmWithGenreLike($query);
+    }
+
+
+
+}