<?php
namespace App\Controller;
use App\Form\Search\SearchAcheteurType;
use App\Repository\ConcessionRepository;
use App\Repository\MarchePublicRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
class ShowAllController extends AbstractController
{
#[Route('/liste', name:'app_show_all_marches_and_concessions', methods: ['GET', 'POST'])]
public function showAll(
MarchePublicRepository $mpRepository,
ConcessionRepository $concessionRepository,
PaginatorInterface $paginator,
Request $request,
SessionInterface $session
): Response {
$searchForm = $this->createForm(SearchAcheteurType::class);
$searchForm->handleRequest($request);
$searchTerm = '';
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$searchTerm = $searchForm->get('searchTerm')->getData();
$session->set('searchTerm', $searchTerm);
} else {
if ($session->has('searchTerm')) {
$searchTerm = $session->get('searchTerm');
}
}
if ($searchTerm) {
$allMarches = $mpRepository->findByAcheteurNom($searchTerm);
$allConcessions = $concessionRepository->findByAutoriteConcedanteNom($searchTerm);
} else {
$allMarches = $mpRepository->findAll();
$allConcessions = $concessionRepository->findAll();
}
$combinedData = array_merge($allMarches, $allConcessions);
usort($combinedData, function($a, $b) {
return $b->getDateMade() <=> $a->getDateMade();
});
$pagination = $paginator->paginate(
$combinedData,
$request->query->getInt('page', 1),
10
);
return $this->render('show_all/show_all.html.twig', [
'pagination' => $pagination,
'searchForm' => $searchForm->createView(),
'searchTerm' => $searchTerm
]);
}}