src/Aqarmap/Bundle/ListingBundle/Controller/CompoundSearchController.php line 145

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Controller;
  3. use Aqarmap\Bundle\ListingBundle\Entity\CompoundLocation;
  4. use Aqarmap\Bundle\ListingBundle\Service\CompoundService;
  5. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  6. use Aqarmap\Bundle\MainBundle\Constant\CustomParagraphPlaceTypes;
  7. use Aqarmap\Bundle\MainBundle\Entity\CustomParagraph;
  8. use Aqarmap\Bundle\MainBundle\Repository\CustomParagraphRepository;
  9. use Aqarmap\Bundle\SearchBundle\Services\CompoundAdviserSearchService;
  10. use Doctrine\ORM\NonUniqueResultException;
  11. use Gedmo\Translatable\TranslatableListener;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("compounds")
  21.  */
  22. class CompoundSearchController extends AbstractController
  23. {
  24.     /**
  25.      * @var CompoundAdviserSearchService
  26.      */
  27.     private $compoundAdvisorSearchService;
  28.     /**
  29.      * @var CompoundService
  30.      */
  31.     private $compoundService;
  32.     /**
  33.      * @var TranslatableListener
  34.      */
  35.     private $translatableListener;
  36.     public function __construct(
  37.         CompoundAdviserSearchService $compoundAdvisorSearchService,
  38.         CompoundService $compoundService,
  39.         TranslatableListener $translatableListener,
  40.         ListingManager $listingManager
  41.     ) {
  42.         $this->compoundAdvisorSearchService $compoundAdvisorSearchService;
  43.         $this->compoundService $compoundService;
  44.         $this->translatableListener $translatableListener;
  45.         $this->listingManager $listingManager;
  46.     }
  47.     /**
  48.      * @Route(path="/advisor", name="compound_adviser", methods={"GET"})
  49.      * @Route(path="/advisor/matcherLocation", name="compound_adviser_location", methods={"GET"})
  50.      * @Route(path="/advisor/matcherPriceLevel", name="compound_adviser_price", methods={"GET"})
  51.      * @Route(path="/advisor/matcherAreaMethod", name="compound_adviser_area", methods={"GET"})
  52.      * @Route(path="/advisor/matcherPropertyType", name="compound_adviser_property_type", methods={"GET"})
  53.      * @Route(path="/advisor/matcherFinishType", name="compound_adviser_finish_type", methods={"GET"})
  54.      * @Route(path="/advisor/matcherDelivery", name="compound_adviser_delivery", methods={"GET"})
  55.      *
  56.      * @return array|RedirectResponse
  57.      */
  58.     public function adviser(): Response
  59.     {
  60.         return $this->render('@AqarmapListing/CompoundSearch/adviser.html.twig', []);
  61.     }
  62.     /**
  63.      * Compound advisor Search (POST).
  64.      *
  65.      * @Route(path="/advisor", name="compound_adviser_search_post", methods={"POST"})
  66.      *
  67.      * @return RedirectResponse
  68.      */
  69.     public function adviserPost(Request $request)
  70.     {
  71.         $criteria $this->compoundAdvisorSearchService->prepareAdviserScoringCriteria(json_decode($request->request->get('data'), true));
  72.         $response = new RedirectResponse($this->generateUrl('compound_adviser_search_results', []));
  73.         $cookie = new Cookie('advisor-criteria'serialize($criteria));
  74.         $response->headers->setCookie($cookie);
  75.         $response->prepare($request);
  76.         $response->send();
  77.         return $response;
  78.     }
  79.     /**
  80.      * @Route("/advisor/search", name="compound_adviser_search_results", methods={"GET"})
  81.      *
  82.      * @throws \Exception
  83.      */
  84.     public function compoundAdviserResult(Request $request): Response
  85.     {
  86.         $cookies $request->cookies;
  87.         $criteria = [];
  88.         if ($cookies->has('advisor-criteria')) {
  89.             $criteria unserialize($cookies->get('advisor-criteria'));
  90.         }
  91.         $results $this->compoundAdvisorSearchService->getCompounds($criteria$request->query->get('page'1));
  92.         $entityManager $this->getDoctrine()->getManager();
  93.         $criteria['selectedParagraphId'] = (int) $request->get('custom_paragraph');
  94.         $isNoIndexMetaTag = (bool) $request->get('noindex') || $this->compoundAdvisorSearchService->isNoIndexMetaTag($request->query->all());
  95.         return $this->render(
  96.             '@AqarmapListing/CompoundSearch/compoundAdviserResult.html.twig',
  97.             [
  98.                 'canonicalUrl' => $canonicalUrl ?? null,
  99.                 'listings' => $results,
  100.                 'adviserCriteria' => $criteria,
  101.                 'listingsCount' => \count($results),
  102.                 'customParagraph' => $this->getCustomParagraph($criteria),
  103.                 'subLinksSections' => $entityManager->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Section::class)->getSearchableSection(),
  104.                 'isNoIndexMetaTag' => $isNoIndexMetaTag,
  105.                 'form' => $this->compoundService->createQuickLeadForm('add_quick_lead'),
  106.                 'liveListingsPerUser' => $this->listingManager->getLiveListingsCountPerUser($results->getItems()),
  107.                 'isMobile' => false,
  108.             ]
  109.         );
  110.     }
  111.     /**
  112.      * @Route("/", methods={"GET"}, name="compound_search")
  113.      * @Route (
  114.      *     "/{location}", methods={"GET"},
  115.      *     name="compound_search_with_location",
  116.      *     requirements={"location": ".+[^/]"},
  117.      * )
  118.      *
  119.      * @ParamConverter(
  120.      *     "location",
  121.      *     options={"mapping": {"location": "slug"}}
  122.      * )
  123.      *
  124.      * @return array|RedirectResponse
  125.      *
  126.      * @throws \Exception
  127.      */
  128.     public function compoundSearchResult(Request $request, ?CompoundLocation $location null)
  129.     {
  130.         $response $this->compoundService->search($request$location);
  131.         if ($response instanceof Response) {
  132.             return $response;
  133.         }
  134.         return $this->render(
  135.             '@AqarmapListing/CompoundSearch/compoundSearchResult.html.twig',
  136.             $response
  137.         );
  138.     }
  139.     /**
  140.      * Compound Search (POST).
  141.      *
  142.      * @Route("/compound", methods={"POST"}, name="compound_Search")
  143.      *
  144.      * @throws \Exception
  145.      */
  146.     public function searchPost(Request $request)
  147.     {
  148.         $redirect $this->redirect($this->generateUrl('compound_search'), Response::HTTP_MOVED_PERMANENTLY);
  149.         $compoundSearchForm $this->compoundService->compoundSearchForm();
  150.         $compoundSearchForm->handleRequest($request);
  151.         if ($compoundSearchForm->isValid()) {
  152.             $criteria $this->compoundService->prepareCriteria($compoundSearchForm$request);
  153.             $redirect $this->redirect(urldecode($this->generateUrl('compound_search'$criteria)), Response::HTTP_MOVED_PERMANENTLY);
  154.         }
  155.         return $redirect;
  156.     }
  157.     /**
  158.      * @return CustomParagraph|null
  159.      */
  160.     public function getCustomParagraph(array $criteria)
  161.     {
  162.         $selectedCustomParagraphId $criteria['selectedParagraphId'] ?? null;
  163.         $criteria = [
  164.             'place' => CustomParagraphPlaceTypes::SEARCH_RESULTS,
  165.             'section' => $criteria['section'] ?? null,
  166.             'location' => $criteria['location'] ?? null,
  167.             'propertyType' => $criteria['propertyType'] ?? null,
  168.         ];
  169.         /** @var CustomParagraphRepository $customParagraphRepository */
  170.         $customParagraphRepository $this->getDoctrine()->getRepository(CustomParagraph::class);
  171.         $this->translatableListener->setTranslationFallback(false);
  172.         if ($selectedCustomParagraphId) {
  173.             $criteria['id'] = $selectedCustomParagraphId;
  174.             try {
  175.                 return $customParagraphRepository
  176.                     ->get($criteria)
  177.                     ->getQuery()
  178.                     ->setMaxResults(1)
  179.                     ->getOneOrNullResult();
  180.             } catch (NonUniqueResultException $e) {
  181.                 return null;
  182.             }
  183.         }
  184.         $customParagraphs $customParagraphRepository
  185.             ->get(array_merge($criteria, ['published' => true]))
  186.             ->getQuery()
  187.             ->getResult();
  188.         if (empty($customParagraphs)) {
  189.             return null;
  190.         }
  191.         return $customParagraphs[array_rand($customParagraphs)];
  192.     }
  193. }