src/Controller/ShowAllController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\Search\SearchAcheteurType;
  4. use App\Repository\ConcessionRepository;
  5. use App\Repository\MarchePublicRepository;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class ShowAllController extends AbstractController
  13. {
  14.     #[Route('/liste'name:'app_show_all_marches_and_concessions'methods: ['GET''POST'])]
  15.     public function showAll(
  16.         MarchePublicRepository $mpRepository,
  17.         ConcessionRepository $concessionRepository,
  18.         PaginatorInterface $paginator,
  19.         Request $request,
  20.         SessionInterface $session
  21.     ): Response {
  22.         $searchForm $this->createForm(SearchAcheteurType::class);
  23.         $searchForm->handleRequest($request);
  24.         $searchTerm '';
  25.         if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  26.             $searchTerm $searchForm->get('searchTerm')->getData();
  27.             $session->set('searchTerm'$searchTerm);
  28.         } else {
  29.             if ($session->has('searchTerm')) {
  30.                 $searchTerm $session->get('searchTerm');
  31.             }
  32.         }
  33.         if ($searchTerm) {
  34.             $allMarches $mpRepository->findByAcheteurNom($searchTerm);
  35.             $allConcessions $concessionRepository->findByAutoriteConcedanteNom($searchTerm);
  36.         } else {
  37.             $allMarches $mpRepository->findAll();
  38.             $allConcessions $concessionRepository->findAll();
  39.         }
  40.         $combinedData array_merge($allMarches$allConcessions);
  41.         usort($combinedData, function($a$b) {
  42.             return $b->getDateMade() <=> $a->getDateMade();
  43.         });
  44.         $pagination $paginator->paginate(
  45.             $combinedData,
  46.             $request->query->getInt('page'1),
  47.             10
  48.         );
  49.         return $this->render('show_all/show_all.html.twig', [
  50.             'pagination' => $pagination,
  51.             'searchForm' => $searchForm->createView(),
  52.             'searchTerm' => $searchTerm
  53.         ]);
  54.     }}