<?php
namespace AppBundle\Controller;
use Doctrine\ORM\Query\Expr\Func;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use AppBundle\Includes\Database;
use AppBundle\Includes\Settings;
use AppBundle\Includes\Helper;
use AppBundle\Includes\Tracker;
use AppBundle\Includes\Users;
use AppBundle\Includes\Functions;
use AppBundle\Includes\Constants;
class SearchController extends AbstractController
{
/*
* @Route("/{one_type}/", name="search_results_comm_type_only", methods={"GET", "POST"})
*/
/* TODO: disabled this
public function resultsCommTypeOnlyAction(Request $request, $one_type)
{
if (in_array($one_type, Constants::all_url_types())) {
if (in_array($one_type, Constants::all_url_types_com())) {
$type = 'entreprises-commerces';
} elseif (in_array($one_type, Constants::all_url_types_murs())) {
$type = 'murs-locaux-terrains';
} else {
throw $this->createNotFoundException('PAGE NON TROUVÉE');
}
$commerce_type = $one_type;
$region = Constants::generic_region;
$dep = Constants::generic_departement;
} else {
throw $this->createNotFoundException('PAGE NON TROUVÉE');
}
$response = $this->forward('AppBundle:Search:results',
array(
'type' => $type,
'region' => $region,
'departement' => $dep,
'commerce_type' => $commerce_type,
'_route' => 'search_results',
'_route_params' => array_merge($request->attributes->get('_route_params'),
array('type' => $type,
'commerce_type' => $commerce_type,
'region' => $region,
'departement' => $dep,)
)),
$request->query->all());
#'_route' => $request->attributes->get('_route'),
#'_route_params' => $request->attributes->get('_route_params')
return $response;
}*/
/**
* @Route("/{type}/",
* requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
* name="search_results_type_only",
* methods={"GET", "POST"})
*/
public function resultsTypeOnlyAction (Request $request, $type)
{
$type_new = '';
if ($type == 'entreprises-commerces') {
$type_new = 'vente';
} elseif ($type == 'murs-locaux-terrains') {
$type_new = 'tout';
}
$dep = Constants::generic_departement;
$commerce_type = Constants::generic_commerce_type;
$response = $this->forward('AppBundle:Search:results',
array(
'type' => $type_new,
'departement' => $dep,
'commerce_type' => $commerce_type,
'_route' => 'search_results',
'_route_params' => array_merge($request->attributes->get('_route_params'),
array('type' => $type_new,
'commerce_type' => $commerce_type,
'departement' => $dep,)
)),
$request->query->all());
return $response;
}
/**
* @Route("/{type}/{one_type}/",
* requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
* name="search_results_comm_type",
* methods={"GET", "POST"})
*/
public function resultsCommTypeAction(Request $request, $type, $one_type)
{
$type_new = '';
if ($type == 'entreprises-commerces') {
$type_new = 'vente';
} elseif ($type == 'murs-locaux-terrains') {
$type_new = 'tout';
}
if (in_array($one_type, Constants::all_url_types())) {
$commerce_type = $one_type;
$region = Constants::generic_region;
$dep = Constants::generic_departement;
} elseif (in_array($one_type, Constants::all_url_regions())) {
$commerce_type = Constants::generic_commerce_type;
$region = $one_type;
$dep = Constants::generic_departement;
} else {
throw $this->createNotFoundException('PAGE NON TROUVÉE');
}
$response = $this->forward('AppBundle:Search:results',
array(
'type' => $type_new,
'departement' => $dep,
'commerce_type' => $commerce_type,
'_route' => 'search_results',
'_route_params' => array_merge($request->attributes->get('_route_params'),
array('type' => $type_new,
'commerce_type' => $commerce_type,
'departement' => $dep,)
)),
$request->query->all());
return $response;
}
# Handle region/department
/**
* @Route("/{type}/{region}/{departement}/",
* requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
* name="search_results_dep_type",
* methods={"GET", "POST"})
*/
public function resultsDepTypeAction(Request $request, $type, $region, $departement)
{
if (in_array($region, Constants::all_url_regions()) && in_array($departement, Constants::all_url_deps())) {
$commerce_type = Constants::generic_commerce_type;
$region = $region;
$dep = $departement;
} else {
throw $this->createNotFoundException('PAGE NON TROUVÉE');
}
$response = $this->forward('AppBundle:Search:results',
array(
'type' => $type,
'region' => $region,
'departement' => $dep,
'commerce_type' => $commerce_type,
'_route' => 'search_results',
'_route_params' => array_merge($request->attributes->get('_route_params'),
array('type' => $type,
'commerce_type' => $commerce_type,
'region' => $region,
'departement' => $dep,)
)),
$request->query->all());
return $response;
}
/**
* @Route("/{type}/{region}/{departement}/{commerce_type}/",
* requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
* name="search_results_old",
* methods={"GET", "POST"})
*/
public function resultsOldAction(Request $request, $type, $region, $departement, $commerce_type)
{
$type_new = '';
if ($type == 'entreprises-commerces') {
$type_new = 'vente';
} elseif ($type == 'murs-locaux-terrains') {
if (!empty($_GET['transaction'])) {
if ($_GET['transaction'] == 'vente') {
$type_new = 'vente-immobilier';
} elseif ($_GET['transaction'] == 'location') {
$type_new = 'location';
} elseif ($_GET['transaction'] == 'cession-de-droit-au-bail') {
$type_new = 'opportunites-investisseurs';
} elseif ($_GET['transaction'] == 'tout') {
$type_new = 'tout';
}
} else {
$type_new = 'tout';
}
}
# Remove default departement
if ($departement == Constants::generic_departement) {
$departement = null;
}
# This should redirect to new
$input = [
'type' => $type_new,
'commerce_type' => $commerce_type,
'departement' => $departement
];
return $this->redirectToRoute('search_results', $input, 301);
}
/**
* @Route("/{type}/{commerce_type}/{departement}",
* requirements={"type": "vente|vente-immobilier|location|opportunites-investisseurs|tout"},
* defaults={"commerce_type"=null, "departement"=null},
* name="search_results",
* methods={"GET", "POST"})
*/
public function resultsAction(Request $request, $type, $commerce_type=null, $departement=null)
{
# Instant redirect /vente/commerce_type/departement -> remove departement
if ($departement == Constants::generic_departement && $commerce_type != null) {
$input_red = [
'type' => $type,
'commerce_type' => $commerce_type,
'departement' => null
];
return $this->redirectToRoute('search_results', $input_red, 301);
} elseif ($departement == Constants::generic_departement && $commerce_type == null) {
$input_red = [
'type' => $type,
'commerce_type' => null,
'departement' => null
];
return $this->redirectToRoute('search_results', $input_red, 301);
}
if ($commerce_type == Constants::generic_commerce_type && !$departement) {
$input_red = [
'type' => $type,
'commerce_type' => null,
'departement' => null
];
return $this->redirectToRoute('search_results', $input_red, 301);
}
# Handle the case of optional commerce_type
if (!$commerce_type) {
$commerce_type = Constants::generic_commerce_type;
}
$users = new Users();
$helper = new Helper($this->get('router'));
# This gets $_GET params
$get_params = $request->query->all();
$session = $request->getSession();
# Changed links
if ($commerce_type == 'boutiques-diverses-pret-a-porter-chaussures') {
$input = array_merge(
Array('type' => $type,
'departement' => $departement,
'commerce-type' => $commerce_type,
'found_communes' => isset($_GET['ville']) ? Functions::str_filter($_GET['ville']) : null),
$get_params);
$input['commerce_type'] = 'boutiques-diverses';
unset($input['commerce-type']);
return $this->redirectToRoute('search_results', $input, 301);
}
# Handle malformed urls
if (# !in_array($departement, Constants::all_url_deps()) || # Make departement optional
!in_array($commerce_type, Constants::all_url_types())
) {
throw $this->createNotFoundException('PAGE NON TROUVÉE');
}
$seo_strategy = 'index';
# Decide canonical, or noindex
if ($type == 'tout') { # In case of tout, do no-index
$seo_strategy = 'noindex';
} elseif ($departement == Constants::generic_departement && $commerce_type == Constants::generic_commerce_type) {
$seo_strategy = 'noindex';
} elseif ($commerce_type == Constants::generic_commerce_type && $departement) {
# vente/commerce-a-vendre/3-allier no index
$seo_strategy = 'noindex';
} elseif (isset($_GET['order']) && $_GET['order'] != 'default'){
$seo_strategy = 'noindex';
} elseif (isset($_GET['ville']) && strpos($_GET['ville'], ',')) {
# In case of multiple ville, do not index
$seo_strategy = 'noindex';
} elseif (isset($_GET['agent_id'])) {
$seo_strategy = 'noindex';
} elseif (isset($_GET['price-low']) || isset($_GET['price-high'])) {
$seo_strategy = 'noindex';
} elseif (isset($_GET['surface-min']) || isset($_GET['surface-max'])) {
$seo_strategy = 'noindex';
} elseif (isset($_GET['mode'])) {
$seo_strategy = 'noindex';
} else {
$seo_strategy = 'index';
}
if ($departement == Constants::generic_departement) {
$departement = Null;
}
if ($commerce_type == Constants::generic_commerce_type){
$commerce_type = Null;
}
# Check if page is viewall
if (isset($get_params['mode']) && $get_params['mode'] == 'viewall') {
$viewall = True;
} else {
$viewall = False;
}
$input = array_merge(
Array('type' => $type,
'departement' => $departement,
'commerce-type' => $commerce_type,
'found_communes' => isset($_GET['ville']) ? Functions::str_filter($_GET['ville']) : null),
$get_params);
$results = $helper->db_select($input, 'bulk', $viewall);
$num_results = Functions::euro_num($helper->total_results);
# Empty $results means some error, redirect to homepage
if (empty($results)) {
# if dep set, go to comm_type
if (!empty($input['departement']) && !empty($input['commerce-type'])) {
# $input['region'] = Constants::generic_region;
$input['departement'] = Constants::generic_departement;
$input['commerce_type'] = $input['commerce-type'];
unset($input['commerce-type']);
return $this->redirectToRoute('search_results', $input, 301);
}
$session->set('error', $helper->error_obj);
return $this->redirect('/', 301);
}
$tracker = new Tracker($helper->refined_input);
# Handle problematic agents with their images:
for ($i=0; $i<count($results); $i++) {
if (in_array($results[$i]['site_id'], Constants::$block_images_agents)) {
$agents_info = $users->get_agents_list($results[$i]['site_id']);
$replace_img = !empty($agents_info['logo']) ? $agents_info['logo'] : '/images/agents/default.png';
$results[$i] = $helper->replace_image($results[$i], $replace_img);
}
}
# Do some stuff before sending results array to screen
$results_refined = $helper->refine_results($results);
# Create title and H1
if (isset($input_arr['transaction'])) {
if ($input_arr['transaction'] == 'vente' || $input_arr['transaction'] == 1) {
$pre_com_type = 'Vente';
}
elseif ($input_arr['transaction'] == 'location' || $input_arr['transaction'] == 2) {
$pre_com_type = 'Location';
}
elseif ($input_arr['transaction'] == 'cession-de-droit-au-bail' || $input_arr['transaction'] == 3) {
$pre_com_type = 'Cession du droit au bail';
}
}
#$page_title_ch = implode(' - ', array_filter(array($commerce_type, $region, $departement)));
#<title>Vente bar-snack (63)puy-de-dome, auvergne</title>
$page_title_part = Array();
# Look for most important keywords and use them to create title
foreach(array('com_type', 'dep1', 'dep2') as $ctrl) {
if (isset($helper->keywords[$ctrl]['text'])) {
$page_title_part[] = $helper->keywords[$ctrl]['text'];
}
}
if (isset($_GET['ville'])) {
$page_title_part[] = Functions::str_filter($_GET['ville']);
}
$page_title_ch = implode(' - ', $page_title_part);
if (!isset($helper->keywords['com_type'])) {
if ($input['type'] == 'location') {
$page_title_ch = 'Local commercial à louer ' . $page_title_ch;
} elseif ($input['type'] == 'opportunites-investisseurs') {
$page_title_ch = 'Cession droit au bail ' . $page_title_ch;
} else {
$page_title_ch = 'Fonds de commerce à vendre ' . $page_title_ch;
}
}
if (isset($_GET['page']) && $_GET['page'] > 1) {
$page_title_ch .= ' - ' . $_GET['page'];
}
# Save last search setting to a session cookie, so the form is pre-set already
$session->set('last_search', $input);
# In case of agent_id set, display only annonces of a specif agent + some info
$agent_info = [];
if (isset($_GET['agent_id'])) {
$agent_info = $users->ret_user_info(null, intval($_GET['agent_id']));
# $agent_info = $users->ret_user_info(null, -45); # Test sql injection, bad input
}
# print_r($results_refined);
# $session->set('last_search_results', $results_refined);
$output = array(
'page_title' => $page_title_ch,
'page_description' => $page_title_ch,
'page' => 'search',
'h1_main' => $page_title_ch,
'loggedin' => $users->loggedin(),
'items' => $results_refined,
'number_results' => $num_results,
'seo_strategy' => $seo_strategy,
'pagination' => $helper->list_pagination(),
'keywords' => $helper->keywords,
'keywords_suggestions' => $helper->keywords_suggestions,
'order_select' => $helper->list_order_select(),
'agent_info' => $agent_info,
);
# Check for errors to display
if ($session->get('error')) {
$output['error'] = $session->get('error');
$session->set('error', null);
}
return $this->render('search/results.html.twig', $output);
}
}