<?php
declare(strict_types = 1);
namespace App\Action;
use App\Infra\Services\ApiService;
use App\Infra\Services\FilterApiService;
use App\Responder\RedirectResponder;
use App\Responder\TemplateResponder;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class SendSearchAction extends AbstractAction
{
public function __construct(
TemplateResponder $templateResponder,
RedirectResponder $redirectResponder,
Environment $environment,
ApiService $apiService,
EventDispatcherInterface $eventDispatcher,
FilterApiService $filterApiService,
SessionInterface $session
)
{
$this->templateResponder = $templateResponder;
$this->redirectResponder = $redirectResponder;
$this->environnement = $environment;
$this->apiService = $apiService;
$this->eventDispatcherInterface = $eventDispatcher;
$this->filterApiService = $filterApiService;
$this->session = $session;
}
/**
* @Route("/resultats", name="results", methods={"GET"})
*/
public function __invoke(Request $request)
{
$properties = $this->filterApiService->getAllPropertiesForSearch();
$results = [];
$arrayFilters = [
'category' => (int) $this->filterApiService->getInversePropertyCategory($request->query->get('transaction-type')),
'type' => $this->filterApiService->getInverseRealSubTypeProject($request->query->get('property-type')),
'district' => $request->query->get('location'),
'budget' => $request->query->get('budget'),
'rooms' => (int) $request->query->get('rooms'),
'areaMinimum' => $request->query->get('area-minimum')
];
if ($arrayFilters['district'] === 'Tous') {
unset($arrayFilters['district']);
}
$this->session->set('filter', $request->query->all());
foreach ($arrayFilters as $key => $arrayFilter) {
if (empty($arrayFilter)) {
unset($arrayFilters[$key]);
}
}
foreach($properties as $property){
$error = false;
$city = $property['city'];
$bedrooms = $property['bedrooms'];
$area = $property['area'];
$budget = $property['price'];
foreach ($property as $key => $item){
if (array_key_exists('district', $arrayFilters)) {
if(($arrayFilters['district'] !== $city['name'])) {
$error = true;
}
}
if (array_key_exists('rooms', $arrayFilters)) {
if(($arrayFilters['rooms'] > $bedrooms) || ($bedrooms == 0)) {
$error = true;
}
}
if (array_key_exists('budget', $arrayFilters)) {
if(($arrayFilters['budget'] < $budget['value']) || ($budget == 0)) {
$error = true;
}
}
if (array_key_exists('areaMinimum', $arrayFilters)) {
if(($arrayFilters['areaMinimum'] > $area['value']) || ($area == 0)) {
$error = true;
}
}
if (array_key_exists($key, $arrayFilters) && !empty($item)) {
if ($item != $arrayFilters[$key]) {
$error = true;
}
}
}
if(!$error) {
$results[] = $property;
}
}
$saleProperties = [];
foreach ($properties as $property) {
if ($arrayFilters) {
if (isset($arrayFilters['category'])) {
if ($arrayFilters['category'] == $property['category']) {
if (isset($arrayFilters['type'])) {
if ($property[$arrayFilters['type']['status']] == $arrayFilters['type']['id']) {
if (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
} else {
$saleProperties[] = [$property][0];
}
}
if ($arrayFilters['type'] === 8 && $property['subtype'] === 6) {
if (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
} else {
$saleProperties[] = [$property][0];
}
}
} elseif ($property['category'] == $arrayFilters['category'] && !isset($arrayFilters['district'])) {
$saleProperties[] = [$property][0];
} elseif ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
}
} else {
if (isset($arrayFilters['type'])) {
if ($property[$arrayFilters['type']['status']] == $arrayFilters['type']['id']) {
if (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
} elseif (isset($arrayFilters['areaMinimum'])) {
if(($arrayFilters['areaMinimum'] < $property['area']['value'])) {
$saleProperties[] = [$property][0];
}
} else {
$saleProperties[] = [$property][0];
}
} else {
if (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
} elseif (isset($arrayFilters['areaMinimum'])) {
if(($arrayFilters['areaMinimum'] < $property['area']['value'])) {
$saleProperties[] = [$property][0];
}
}
}
if ($arrayFilters['type'] === 8 && $property['subtype'] === 6) {
$saleProperties[] = [$property][0];
}
} elseif (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
} elseif ($property['district'] && $arrayFilters['district'] == $property['district']['name']) {
$saleProperties[] = [$property][0];
}
} elseif (isset($arrayFilters['areaMinimum'])) {
if(((int) $arrayFilters['areaMinimum'] < $property['area']['value'])) {
$saleProperties[] = [$property][0];
}
}
}
/*
if (isset($arrayFilters['category']) && $arrayFilters['category'] == $property['category']
&& (isset($arrayFilters['type']) && $property['type'] == $arrayFilters['type'])) {
if (isset($arrayFilters['district'])) {
if ($arrayFilters['district'] == $property['city']['name']) {
$saleProperties[] = [$property][0];
}
} else {
$saleProperties[] = [$property][0];
}
}*/
} else {
if ($property['category'] == 1 or $property['category'] == 2) {
$saleProperties[] = [$property][0];
}
}
}
// Advanced filter
foreach ($saleProperties as $key => $property) {
if (isset($arrayFilters['areaMinimum'])) {
if ($property['area']['value'] < $arrayFilters['areaMinimum']) {
unset($saleProperties[$key]);
}
}
if (isset($arrayFilters['rooms'])) {
if ($property['bedrooms'] != $arrayFilters['rooms']) {
unset($saleProperties[$key]);
}
}
if (isset($arrayFilters['budget'])) {
if ($property['price']['value'] > $arrayFilters['budget']) {
unset($saleProperties[$key]);
}
}
}
$arrayOtherProperties = [];
$idCategory = null;
if (isset($arrayFilters['category'])) {
$idCategory = $arrayFilters['category'];
if ($idCategory === 1) {
$idCategory = 2;
} else {
$idCategory = 1;
}
$otherProperties = $this->filterApiService->getPropertiesByCategory($idCategory);
foreach ($otherProperties as $property) {
//if ($property['category'] == 1 or $property['category'] == 2) {
$arrayOtherProperties[] = [$property][0];
//}
}
} else {
$arrayOtherProperties = $saleProperties;
}
return $this->templateResponder->__invoke('property.html.twig', [
'otherProperties' => array_slice($arrayOtherProperties, 0 , 3),
'properties' => $saleProperties,
'idCategory' => $idCategory
]);
}
}