<?php
namespace App\Controller;
use App\Entity\Section;
use App\Form\Type\SearchType;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SearchController extends AbstractController
{
/**
* @Route("/search", name="search", methods={"POST"})
*/
public function searchAction(
Request $request,
ProductRepositoryInterface $productRepository,
TaxonRepositoryInterface $taxonRepository,
EntityManagerInterface $entityManager,
ChannelContextInterface $channelContext
): Response {
$form = $this->createForm(SearchType::class);
$form->handleRequest($request);
if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$searchInput = $data['search'];
$sections = $entityManager->getRepository(Section::class)->getByTitleAndContent($searchInput);
$products = $productRepository->findBySearchInput($searchInput, $channelContext->getChannel());
$parentGroup = $taxonRepository->findOneByCode('group');
return $this->render('app/Search/results.html.twig', [
'searchInput' => $searchInput,
'sections' => $sections,
'products' => $products,
'parentGroup' => $parentGroup,
]);
}
return $this->render('app/base/header/_search.html.twig', [
'form' => $form->createView(),
]);
}
}