Sfoglia il codice sorgente

Ajout de la fonctionnalité de recherche

SPAETER NATHAN 4 anni fa
parent
commit
43b3ce9f6a

+ 10 - 1
src/Controller/ResearchController.php

@@ -5,14 +5,23 @@ namespace App\Controller;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Annotation\Route;
+use Symfony\Component\HttpFoundation\Request;
+use App\Entity\Message;
 
 class ResearchController extends AbstractController
 {
     #[Route('/research', name: 'research')]
-    public function index(): Response
+    public function index(Request $request): Response
     {
+        $em = $this->getDoctrine()->getManager();
+        $repository_message = $em->getRepository(Message::class);
+        $messages = $repository_message->findMessageContainString($request->query->get('s'));
+        // foreach ($messages as $message) {
+        //     array_push($sub_msg, $msg);
+        // }
         return $this->render('research/index.html.twig', [
             'username' => $this->get('session')->get('user'),
+            'messages' => $messages,
         ]);
     }
 }

+ 9 - 0
src/Repository/MessageRepository.php

@@ -47,4 +47,13 @@ class MessageRepository extends ServiceEntityRepository
         ;
     }
     */
+
+    public function findMessageContainString($value)
+    {
+        return $this->createQueryBuilder('a')
+            ->where('a.text LIKE :string')
+            ->setParameter('string', '%' . $value . '%')
+            ->getQuery()
+            ->execute();
+    }
 }

+ 2 - 2
templates/base.html.twig

@@ -37,8 +37,8 @@
 							</li>
 						</ul>
 						<form class="d-flex" action="{{ path('research') }}" method="get">
-							<input class="form-control me-2" type="search" placeholder="Search" name="s" aria-label="Search">
-							<button class="btn btn-outline-success" type="submit">Search</button>
+							<input class="form-control me-2" type="search" placeholder="Mot ou phrase" name="s">
+							<button class="btn btn-outline-success" type="submit">Rechercher</button>
 						</form>
 					{% else %}
 						<li class="nav-item">

+ 1 - 2
templates/home/index.html.twig

@@ -20,8 +20,7 @@
 					</h5>
 					<h6 class="card-subtitle mb-2 text-muted">{{ msg.getDate()|date('H:i - d/m/Y') }}</h6>
 					<p class="card-text">{{ msg.text | striptags('<a>') | raw }}</p>
-					<a href="#" class="card-link">RT</a>
-					{# <a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a> #}
+					<a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a>
 				</div>
 			</div>
 		{% endfor %}

+ 17 - 2
templates/research/index.html.twig

@@ -1,7 +1,22 @@
 {% extends 'base.html.twig' %}
 
-{% block title %}Hello ResearchController!{% endblock %}
+{% block title %}
+	Recherche
+{% endblock %}
 
 {% block body %}
-
+	<div class="d-flex flex-column align-items-center">
+		{% for msg in messages %}
+			<div class="card" style="width: 75rem;">
+				<div class="card-body">
+					<h5 class="card-title">
+						<a href={{ path('profile', {'username': msg.sender.getUsername() } ) }}>{{ msg.sender.getUsername() }}</a>
+					</h5>
+					<h6 class="card-subtitle mb-2 text-muted">{{ msg.getDate()|date('H:i - d/m/Y') }}</h6>
+					<p class="card-text">{{ msg.text | striptags('<a>') | raw }}</p>
+					<a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a>
+				</div>
+			</div>
+		{% endfor %}
+	</div>
 {% endblock %}