src/Action/SendSearchAction.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace App\Action;
  4. use App\Infra\Services\ApiService;
  5. use App\Infra\Services\FilterApiService;
  6. use App\Responder\RedirectResponder;
  7. use App\Responder\TemplateResponder;
  8. use Psr\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Twig\Environment;
  13. class SendSearchAction extends AbstractAction
  14. {
  15.     public function __construct(
  16.         TemplateResponder $templateResponder,
  17.         RedirectResponder $redirectResponder,
  18.         Environment $environment,
  19.         ApiService $apiService,
  20.         EventDispatcherInterface $eventDispatcher,
  21.         FilterApiService $filterApiService,
  22.         SessionInterface $session
  23.     )
  24.     {
  25.         $this->templateResponder $templateResponder;
  26.         $this->redirectResponder $redirectResponder;
  27.         $this->environnement $environment;
  28.         $this->apiService $apiService;
  29.         $this->eventDispatcherInterface $eventDispatcher;
  30.         $this->filterApiService $filterApiService;
  31.         $this->session $session;
  32.     }
  33.     /**
  34.      * @Route("/resultats", name="results", methods={"GET"})
  35.      */
  36.     public function __invoke(Request $request)
  37.     {
  38.         $properties $this->filterApiService->getAllPropertiesForSearch();
  39.         $results = [];
  40.         $arrayFilters = [
  41.             'category' => (int) $this->filterApiService->getInversePropertyCategory($request->query->get('transaction-type')),
  42.             'type' => $this->filterApiService->getInverseRealSubTypeProject($request->query->get('property-type')),
  43.             'district' => $request->query->get('location'),
  44.             'budget' => $request->query->get('budget'),
  45.             'rooms' => (int) $request->query->get('rooms'),
  46.             'areaMinimum' => $request->query->get('area-minimum')
  47.         ];
  48.         if ($arrayFilters['district'] === 'Tous') {
  49.             unset($arrayFilters['district']);
  50.         }
  51.         $this->session->set('filter'$request->query->all());
  52.         foreach ($arrayFilters as $key => $arrayFilter) {
  53.             if (empty($arrayFilter)) {
  54.                 unset($arrayFilters[$key]);
  55.             }
  56.         }
  57.         foreach($properties as $property){
  58.             
  59.             $error false;
  60.             $city $property['city'];
  61.             $bedrooms $property['bedrooms'];
  62.             $area $property['area'];
  63.             $budget $property['price'];
  64.             foreach ($property as $key => $item){
  65.                 if (array_key_exists('district'$arrayFilters)) {
  66.                     if(($arrayFilters['district'] !== $city['name'])) {
  67.                         $error true;
  68.                     }
  69.                 }
  70.                 if (array_key_exists('rooms'$arrayFilters)) {
  71.                     if(($arrayFilters['rooms'] > $bedrooms) || ($bedrooms == 0)) {
  72.                         $error true;
  73.                     }
  74.                 }
  75.                 if (array_key_exists('budget'$arrayFilters)) {
  76.                     if(($arrayFilters['budget'] < $budget['value']) || ($budget == 0)) {
  77.                         $error true;
  78.                     }
  79.                 }
  80.                 if (array_key_exists('areaMinimum'$arrayFilters)) {
  81.                     if(($arrayFilters['areaMinimum'] > $area['value']) || ($area == 0)) {
  82.                         $error true;
  83.                     }
  84.                 }
  85.                 if (array_key_exists($key$arrayFilters) && !empty($item)) {
  86.                     if ($item != $arrayFilters[$key]) {
  87.                         $error true;
  88.                     }
  89.                 }
  90.             }
  91.             if(!$error) {
  92.                 $results[] = $property;
  93.             }
  94.         }
  95.         $saleProperties = [];
  96.         foreach ($properties as $property) {
  97.             if ($arrayFilters) {
  98.                 if (isset($arrayFilters['category'])) {
  99.                     if ($arrayFilters['category'] == $property['category']) {
  100.                         if (isset($arrayFilters['type'])) {
  101.                             if ($property[$arrayFilters['type']['status']] == $arrayFilters['type']['id']) {
  102.                                 if (isset($arrayFilters['district'])) {
  103.                                     if ($arrayFilters['district'] == $property['city']['name']) {
  104.                                         $saleProperties[] = [$property][0];
  105.                                     } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  106.                                         $saleProperties[] = [$property][0];
  107.                                     }
  108.                                 } else {
  109.                                     $saleProperties[] = [$property][0];
  110.                                 }
  111.                             }
  112.                             if ($arrayFilters['type'] === && $property['subtype'] === 6) {
  113.                                 if (isset($arrayFilters['district'])) {
  114.                                     if ($arrayFilters['district'] == $property['city']['name']) {
  115.                                         $saleProperties[] = [$property][0];
  116.                                     } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  117.                                         $saleProperties[] = [$property][0];
  118.                                     }
  119.                                 } else {
  120.                                     $saleProperties[] = [$property][0];
  121.                                 }
  122.                             }
  123.                         } elseif ($property['category'] == $arrayFilters['category'] && !isset($arrayFilters['district'])) {
  124.                             $saleProperties[] = [$property][0];
  125.                         } elseif ($arrayFilters['district'] == $property['city']['name']) {
  126.                             $saleProperties[] = [$property][0];
  127.                         } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  128.                             $saleProperties[] = [$property][0];
  129.                         }
  130.                     }
  131.                 } else {
  132.                     if (isset($arrayFilters['type'])) {
  133.                         if ($property[$arrayFilters['type']['status']] == $arrayFilters['type']['id']) {
  134.                             if (isset($arrayFilters['district'])) {
  135.                                 if ($arrayFilters['district'] == $property['city']['name']) {
  136.                                     $saleProperties[] = [$property][0];
  137.                                 } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  138.                                     $saleProperties[] = [$property][0];
  139.                                 }
  140.                             } elseif (isset($arrayFilters['areaMinimum'])) {
  141.                                 if(($arrayFilters['areaMinimum'] < $property['area']['value'])) {
  142.                                     $saleProperties[] = [$property][0];
  143.                                 }
  144.                             } else {
  145.                                 $saleProperties[] = [$property][0];
  146.                             }
  147.                         } else {
  148.                             if (isset($arrayFilters['district'])) {
  149.                                 if ($arrayFilters['district'] == $property['city']['name']) {
  150.                                     $saleProperties[] = [$property][0];
  151.                                 } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  152.                                     $saleProperties[] = [$property][0];
  153.                                 }
  154.                             } elseif (isset($arrayFilters['areaMinimum'])) {
  155.                                 if(($arrayFilters['areaMinimum'] < $property['area']['value'])) {
  156.                                     $saleProperties[] = [$property][0];
  157.                                 }
  158.                             }
  159.                         }
  160.                         if ($arrayFilters['type'] === && $property['subtype'] === 6) {
  161.                             $saleProperties[] = [$property][0];
  162.                         }
  163.                     } elseif (isset($arrayFilters['district'])) {
  164.                         if ($arrayFilters['district'] == $property['city']['name']) {
  165.                             $saleProperties[] = [$property][0];
  166.                         } elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
  167.                             $saleProperties[] = [$property][0];
  168.                         }
  169.                     } elseif (isset($arrayFilters['areaMinimum'])) {
  170.                         if(((int) $arrayFilters['areaMinimum'] < $property['area']['value'])) {
  171.                             $saleProperties[] = [$property][0];
  172.                         }
  173.                     }
  174.                 }
  175.                 /*
  176.                 if (isset($arrayFilters['category']) && $arrayFilters['category'] == $property['category']
  177.                     && (isset($arrayFilters['type']) && $property['type'] == $arrayFilters['type'])) {
  178.                     if (isset($arrayFilters['district'])) {
  179.                         if ($arrayFilters['district'] == $property['city']['name']) {
  180.                             $saleProperties[] = [$property][0];
  181.                         }
  182.                     } else {
  183.                         $saleProperties[] = [$property][0];
  184.                     }
  185.                 }*/
  186.             } else {
  187.                 if ($property['category'] == or $property['category'] == 2) {
  188.                     $saleProperties[] = [$property][0];
  189.                 }
  190.             }
  191.         }
  192.         // Advanced filter
  193.         foreach ($saleProperties as $key => $property) {
  194.             if (isset($arrayFilters['areaMinimum'])) {
  195.                 if ($property['area']['value'] < $arrayFilters['areaMinimum']) {
  196.                     unset($saleProperties[$key]);
  197.                 }
  198.             }
  199.             if (isset($arrayFilters['rooms'])) {
  200.                 if ($property['bedrooms'] != $arrayFilters['rooms']) {
  201.                     unset($saleProperties[$key]);
  202.                 }
  203.             }
  204.             if (isset($arrayFilters['budget'])) {
  205.                 if ($property['price']['value'] > $arrayFilters['budget']) {
  206.                     unset($saleProperties[$key]);
  207.                 }
  208.             }
  209.         }
  210.         $arrayOtherProperties = [];
  211.         $idCategory null;
  212.         if (isset($arrayFilters['category'])) {
  213.             $idCategory $arrayFilters['category'];
  214.             if ($idCategory === 1) {
  215.                 $idCategory 2;
  216.             } else {
  217.                 $idCategory 1;
  218.             }
  219.             $otherProperties $this->filterApiService->getPropertiesByCategory($idCategory);
  220.             foreach ($otherProperties as $property) {
  221.                 //if ($property['category'] == 1 or $property['category'] == 2) {
  222.                 $arrayOtherProperties[] = [$property][0];
  223.                 //}
  224.             }
  225.         } else {
  226.             $arrayOtherProperties $saleProperties;
  227.         }
  228.         return $this->templateResponder->__invoke('property.html.twig', [
  229.             'otherProperties' => array_slice($arrayOtherProperties3),
  230.             'properties' => $saleProperties,
  231.             'idCategory' => $idCategory
  232.         ]);
  233.     }
  234. }