From 3c5724cbab5076515b2fc8c6303fbb4850627da4 Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 13 Jun 2024 23:57:57 +0200 Subject: [PATCH] search + fix --- public/css/components/form.css | 6 ++++ public/css/components/search.css | 31 +++++++++++++++++++ src/Controller/PostController.php | 26 +++++++++++++++- src/Form/SimpleSearchType.php | 23 ++++++++++++++ src/Form/Type/PostType.php | 6 ++-- src/Repository/PostRepository.php | 48 +++++++++++++++--------------- templates/base.html.twig | 1 + templates/post/search.html.twig | 24 +++++++++++++++ var/data.db | Bin 114688 -> 114688 bytes 9 files changed, 137 insertions(+), 28 deletions(-) create mode 100644 public/css/components/search.css create mode 100644 src/Form/SimpleSearchType.php create mode 100644 templates/post/search.html.twig diff --git a/public/css/components/form.css b/public/css/components/form.css index 8682925..6cf334a 100644 --- a/public/css/components/form.css +++ b/public/css/components/form.css @@ -1,5 +1,11 @@ .form-container { + &.form-row { + display: flex; + flex-direction: row; + align-items: center; + } + .form-group { display: flex; flex-direction: column; diff --git a/public/css/components/search.css b/public/css/components/search.css new file mode 100644 index 0000000..e3cfe4e --- /dev/null +++ b/public/css/components/search.css @@ -0,0 +1,31 @@ +.wrapper { + display: flex; + flex-direction: column; + width: 70%; + background-color: #f2f2f7; + margin: 0 auto; + padding: 20px; + border-radius: 1rem; + margin-top: 5vh; + border: 3px solid black; + gap: 1rem; + + .form-container { + #simple_search { + display: flex; + flex-direction: row; + align-items: center; + gap: 2rem; + justify-content: space-between; + + :first-child { + width: 100%; + } + + #simple_search_search { + display: flex; + flex-grow: 1; + } + } + } +} \ No newline at end of file diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index f16d3fd..26577c6 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -10,6 +10,7 @@ use Doctrine\ORM\EntityManagerInterface; use App\Entity\Post; use App\Form\Type\PostType; use App\Form\Type\CommentType; +use App\Form\Type\SimpleSearchType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class PostController extends AbstractController @@ -28,7 +29,7 @@ class PostController extends AbstractController $posts = $this->em->getRepository(Post::class)->findAll(); return $this->render('post/all.html.twig', [ - "posts" => $posts, + "posts" => array_reverse($posts), "title" => "Derniers Posts" ]); } @@ -135,4 +136,27 @@ class PostController extends AbstractController 'form' => $form, ]); } + + #[Route('/post/search', name: 'search_post', methods: ['GET', 'POST'])] + public function searchPost(Request $request): Response + { + $form = $this->createForm(SimpleSearchType::class); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $searchString = $form->get('search')->getData(); + $posts = $this->em->getRepository(Post::class)->searchByTitleOrText($searchString); + + return $this->render('post/search.html.twig', [ + 'posts' => $posts, + 'form' => $this->createForm(SimpleSearchType::class) + ]); + + // return new Response(print_r($posts, true)); + } + + return $this->render('post/search.html.twig', [ + 'form' => $form + ]); + } } diff --git a/src/Form/SimpleSearchType.php b/src/Form/SimpleSearchType.php new file mode 100644 index 0000000..09969b0 --- /dev/null +++ b/src/Form/SimpleSearchType.php @@ -0,0 +1,23 @@ +add('search', TextType::class, [ + 'label' => false + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Search' + ]) + ; + } +} diff --git a/src/Form/Type/PostType.php b/src/Form/Type/PostType.php index 6ca98ac..df56454 100644 --- a/src/Form/Type/PostType.php +++ b/src/Form/Type/PostType.php @@ -18,17 +18,17 @@ class PostType extends AbstractType ->add('text', TextareaType::class, [ 'attr' => ['rows' => '10'], 'label' => 'Your dream' - ]) + ]) ->add('dream', CheckboxType::class, [ 'required' => false, 'label' => 'Was it a nightmare ?' - ]) + ]) // ->add('tags', ChoiceType::class, [ // "multiple" => true // ]) ->add('submit', SubmitType::class, [ 'label' => 'Publish' - ]) + ]) ; } } diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index 119a378..0cba40c 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -17,29 +17,29 @@ class PostRepository extends ServiceEntityRepository parent::__construct($registry, Post::class); } - /** - * @return Post[] Returns an array of Post objects - */ - public function getPostFromFollowed(Profil $profil): array - { - return $this->createQueryBuilder('p') - ->innerJoin('p.profil', 'a') - ->innerJoin('a.followers', 'f') - ->where('f.id = :userId') - ->setParameter('userId', $profil->getId()) - ->orderBy('p.createdAt', 'DESC') - ->getQuery() - ->getResult(); - ; - } + /** + * @return Post[] Returns an array of Post objects + */ + public function getPostFromFollowed(Profil $profil): array + { + return $this->createQueryBuilder('p') + ->innerJoin('p.profil', 'a') + ->innerJoin('a.followers', 'f') + ->where('f.id = :userId') + ->setParameter('userId', $profil->getId()) + ->orderBy('p.createdAt', 'DESC') + ->getQuery() + ->getResult(); + ; + } - // public function findOneBySomeField($value): ?Post - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } + public function searchByTitleOrText(string $searchString): array + { + return $this->createQueryBuilder('p') + ->where('p.title LIKE :searchTerm') + ->orWhere('p.text LIKE :searchTerm') + ->setParameter('searchTerm', '%'.$searchString.'%') + ->getQuery() + ->getResult(); + } } diff --git a/templates/base.html.twig b/templates/base.html.twig index 8e1db6d..828ae1b 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -23,6 +23,7 @@

Fukafukashita