src/Controller/Website/OfferController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website;
  3. use App\Manager\LocationManager;
  4. use App\Manager\TypeManager;
  5. use App\Repository\OfferRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. class OfferController extends AbstractController
  11. {
  12.   public function __construct(TypeManager $typeManagerLocationManager $locationManager)
  13.   {
  14.     $this->typeManager $typeManager;
  15.     $this->locationManager $locationManager;
  16.   }
  17.   public function rent(Request $request)
  18.   {
  19.     return $this->render('pages/search_properties.html.twig', [
  20.         'seo' => $this->getSeo($requesttrue),
  21.         'urlImgSeo' =>  '/frontend/main/images/294-281-home.jpg',
  22.         'request' => $request,
  23.         'categories'=> $this->typeManager->mapData(),
  24.         'rent' => true,
  25.         'radiusOptions' => [05101520],
  26.     ]);
  27.   }
  28.  
  29.   public function buy(Request $request)
  30.   {
  31.     return $this->render('pages/search_properties.html.twig', [
  32.       'seo' => $this->getSeo($requestfalse),
  33.       'urlImgSeo' =>  '/frontend/main/images/294-281-home.jpg',
  34.       'request' => $request,
  35.       'categories'=> $this->typeManager->mapData(),
  36.       'rent' => false,
  37.       'radiusOptions' => [05101520],
  38.   ]);
  39.   }
  40.   public function rentDetail($encodedTitleOfferRepository $offerRepositoryRequest $request) {
  41.     return $this->detail($encodedTitle$offerRepository,  $requesttrue);
  42.   }
  43.   public function buyDetail($encodedTitleOfferRepository $offerRepositoryRequest $request) {
  44.     return $this->detail($encodedTitle$offerRepository,  $requestfalse);
  45.   }
  46.   public function searchCardsPaginated(OfferRepository $offerRepositoryRequest $requestPaginatorInterface $paginator$rent true)
  47.   {
  48.     $resultsArray $this->getResults($offerRepository$request$rent);
  49.     $priceSqm $request->query->get('priceSqm');
  50.     $priceField $priceSqm '[priceM2an]' '[price]';
  51.     $pagination $paginator->paginate(
  52.       $resultsArray,
  53.       $request->query->getInt('page'1),
  54.       8,
  55.       [
  56.         'defaultSortFieldName' => 'price',
  57.         'defaultSortDirection' => $request->query->get('direction') ?: 'desc',
  58.       ]
  59.     );
  60.     return $this->render('search_properties/_cards.html.twig', [
  61.       'pagination' => $pagination,
  62.       'sorting' => [
  63.         ['direction' => 'asc''field' => $priceField'title' => 'Prix croissant'],
  64.         ['direction' => 'desc''field' => $priceField'title' => 'Prix décroissant'],
  65.         ['direction' => 'asc''field' => '[surface]''title' => 'Surface croissante'],
  66.         ['direction' => 'desc''field' => '[surface]''title' => 'surface décroissante'],
  67.         ['direction' => 'desc''field' => '[dateModificationInt]''title' => 'Plus récent'],
  68.       ],
  69.       'parameters' => $request->query->all(),
  70.       'detailPath' => $rent 'rent_detail' 'buy_detail',
  71.     ]);
  72.   }
  73.   /**
  74.    * action that returns JSON with all results of search for map on the right side of offers page
  75.    */
  76.   public function searchJson(OfferRepository $offerRepositoryRequest $request$rent false)
  77.   {
  78.     return new JsonResponse([
  79.       'results' => $this->getResults($offerRepository$request$rent),
  80.       'seo' => $this->getSeo($request$rent)
  81.     ]);
  82.   }
  83.   public function getSeo(Request $request$rent=false)
  84.   {
  85.     /*** @var Array*/
  86.     $type $request->query->get('type');
  87.     /*** @var Array */
  88.     $locations $request->query->get('location');
  89.     $action $rent 'louer' 'acheter';
  90.     $template $type implode', '$type) : 'Biens';
  91.     $template .= ' à ' $action;
  92.     if ($locations) {
  93.       $template .= ' à ' implode(', 'array_map(function ($location) {
  94.         $parts explode(','$location);
  95.         return $parts[0];
  96.       }, $locations));
  97.     }
  98.     return [
  99.       // <title></title> of the page
  100.       'title' => $template,
  101.       // <meta name='description'> of the page
  102.       'metaDescription' => $template '(meta description)',
  103.       // title above the list of cards
  104.       'topTitle' => $template,
  105.       // title below the list of cards
  106.       'bottomTitle' => $template
  107.     ];
  108.   }
  109.   private function getResults(OfferRepository $offerRepositoryRequest $request$rent true)
  110.   {
  111.     $resultsArray $offerRepository->search($requestnull$rent);
  112.     foreach ($resultsArray as &$result) {//dd($result['status']);
  113.       $result['tags'] = [];
  114.       $result['tags'][] = $result['type'];
  115.       if ($result['surface']) {
  116.         $result['tags'][] = "~".$result['surface']."m<sup class='lh-0'>2</sup>";
  117.       }
  118.     }
  119.     return $resultsArray;
  120.   }
  121.   public function detail($encodedTitleOfferRepository $offerRepositoryRequest $request$rent true)
  122.   {
  123.     $detail $offerRepository->findByEncodedTitle($encodedTitle$rent);
  124.     if ($detail === null) {
  125.       throw $this->createNotFoundException('The offer does not exist');
  126.     }
  127.     $similar $offerRepository->search($request$detail['id'], $detail['isForRent'], true);
  128.     foreach ($similar as &$card) {
  129.       $card['tags'] = [];
  130.       $card['tags'][] = $card['type'];
  131.       $card['tags'][] = "~".$card['surface']."m<sup class='lh-0'>2</sup>";
  132.     }
  133.     $detail['similar'] = $similar;
  134.     $seo $this->getSeo($request);
  135.     // override title and metaDescription with information from this Offer
  136.     $seo['title'] = $detail['title'];
  137.     $seo['metaDescription'] = $detail['title'];
  138.     $dataToReturn = [
  139.       'seo' => $seo,
  140.       'request' => $request,
  141.       'detail' => $detail,
  142.     ];
  143.     $dataToReturn['radiusOptions'] = [05101520];
  144.     if ($request->query->get('json')) {
  145.       return new JsonResponse($dataToReturn);
  146.     }
  147.     return $this->render('pages/search_properties.html.twig'$dataToReturn);
  148.   }
  149.   /**
  150.    * action that returns JSON with all matching locations as user is typing in "location" field
  151.    */
  152.   public function locationsAutocomplete(Request $request)
  153.   {
  154.     if ($request->query->get('top')) {
  155.       return new JsonResponse($this->locationManager->mapDataTopSearch());
  156.     }
  157.     return new JsonResponse($this->locationManager->mapDataSearch());
  158.   }
  159. }