<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Component\Routing\Annotation\Route;
use AppBundle\Includes\Helper;
use AppBundle\Includes\Tracker;
use AppBundle\Includes\Functions;
use AppBundle\Includes\Constants;
class DefaultController extends AbstractController
{
/**
* @Route("/{uri}", name="dbslashesfix",
* requirements={"uri": "^/+(.+)"},
* methods={"GET"})
*/
public function dbslashesfixAction(Request $request, $uri)
{
$uri = preg_replace('~/{2,}~', '/', $uri);
return $this->redirect($uri, 301);
}
/**
* @Route("/test/home", name="homepage_old", methods={"GET"})
*/
public function defaultOldAction(Request $request)
{
$session = $request->getSession();
$helper = new Helper($this->get('router'));
$user = $this->get('app.users');
$tracker = new Tracker();
# Using of APCU cache highly updates speed
$cache = new ApcuAdapter($namespace='bizenco',$defaultLifetime=0,$version=null);
$popular_searches_entre = $cache->get('popular_searches_entre', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(7200);
$value = $tracker->get_popular_searches($this->get('router'), 1, 12);
return $value;
});
$popular_searches_murs = $cache->get('popular_searches_murs', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(7200);
$value = $tracker->get_popular_searches($this->get('router'), 2, 12);
return $value;
});
$tracker->get_popular_searches($this->get('router'), 2, 10);
$recently_joined = $cache->get('recently_joined', function (ItemInterface $item) use ($helper) {
$item->expiresAfter(86400);
$value = $helper->recently_joined();
return $value;
});
$latest_views = $cache->get('latest_views', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(3600);
$value = $tracker->get_latest_biens();
return $value;
});
# print_R($helper->html_region_list('entreprises-commerces', '', '', true)); exit();
$output = array(
'page_title' => 'Vente Commerce, local commercial à louer, immobilier d’entreprise - Bizenco',
'page' => 'homepage',
'loggedin' => $user->loggedin(),
'search_comm_entre' => $helper->html_comm_list('entreprises-commerces', '', '', true),
'search_region_entre' => $helper->html_region_list('entreprises-commerces', '', '', true),
'search_comm_murs' => $helper->html_comm_list('murs-locaux-terrains', '', '', true),
'search_region_murs' => $helper->html_region_list('murs-locaux-terrains', '', '', true),
'popular_searches_entre' => $popular_searches_entre,
'popular_searches_murs' => $popular_searches_murs,
'recently_joined' => $recently_joined,
'latest_views' => $latest_views,
);
# Check for errors to display
if ($session->get('error')) {
$output['error'] = $session->get('error');
$session->set('error', null);
}
if ($session->get('last_search')) {
$output['last_search'] = $session->get('last_search');
}
return $this->render('default/index.html.twig', $output);
}
/**
* @Route("/", name="homepage", methods={"GET"})
*/
public function defaultAction(Request $request)
{
$session = $request->getSession();
$helper = new Helper($this->get('router'));
$user = $this->get('app.users');
$tracker = new Tracker();
# Using of APCU cache highly updates speed
$cache = new ApcuAdapter($namespace='bizenco',$defaultLifetime=0,$version=null);
$popular_searches_entre = $cache->get('popular_searches_entre', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(7200);
$value = $tracker->get_popular_searches($this->get('router'), 1, 12);
return $value;
});
$popular_searches_murs = $cache->get('popular_searches_murs', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(7200);
$value = $tracker->get_popular_searches($this->get('router'), 2, 12);
return $value;
});
$tracker->get_popular_searches($this->get('router'), 2, 10);
$recently_joined = $cache->get('recently_joined', function (ItemInterface $item) use ($helper) {
$item->expiresAfter(86400);
$value = $helper->recently_joined();
return $value;
});
$latest_views = $cache->get('latest_views', function (ItemInterface $item) use ($tracker) {
$item->expiresAfter(3600);
$value = $tracker->get_latest_biens();
return $value;
});
#print_R($helper->html_region_list2('entreprises-commerces', '', '', true)); exit();
#print_R($helper->html_region_list2('entreprises-commerces', '', '', false)); exit();
$output = array(
'page_title' => 'Vente Commerce, local commercial à louer, immobilier d’entreprise - Bizenco',
'page' => 'homepage',
'loggedin' => $user->loggedin(),
'search_comm_entre' => $helper->html_comm_list('entreprises-commerces', '', '', true),
'search_comm_murs' => $helper->html_comm_list('murs-locaux-terrains', '', '', true),
'search_region_murs' => $helper->html_region_list2('murs-locaux-terrains', '', '', true),
'search_region_entre' => $helper->html_region_list2('entreprises-commerces', '', '', true),
'search_region_entre_ville' => $helper->html_region_list_ville('entreprises-commerces', '', '', '', true),
'search_region_murs_ville' => $helper->html_region_list_ville('murs-locaux-terrains', '', '', '',true),
'popular_searches_entre' => $popular_searches_entre,
'popular_searches_murs' => $popular_searches_murs,
'recently_joined' => $recently_joined,
'latest_views' => $latest_views,
'form_active' => 2
);
# Check for errors to display
if ($session->get('error')) {
$output['error'] = $session->get('error');
$session->set('error', null);
}
if ($session->get('last_search')) {
$output['last_search'] = $session->get('last_search');
}
return $this->render('default/index2.html.twig', $output);
}
/**
* @Route("/", name="search_redirects", methods={"POST"})
*/
public function redirectAction(Request $request)
{
$session = $request->getSession();
$params = $request->request->all();
# Error detection
$error = FALSE;
# Actaully allow blank search
#if ((empty($params['type']) &&
# ($params['type'] != 'entreprises-commerces' || $params['type'] != 'murs-locaux-terrains')) ||
# (empty($params['commerce_type']) && empty($params['departement']) && empty($params['region'])))
# {
# $error = TRUE;
# }
if ($error) {
$error_obj = Functions::alert_obj("Votre recherche n’a pas abouti!", 'Veuillez réessayer', 0);
$session->set('error', $error_obj);
return $this->redirect('/', 301);
} else {
if (empty($params['departement'])) {
$params['departement'] = null;
# $params['departement'] = Constants::generic_departement;
# $params['region'] = Constants::generic_region;
} elseif (empty($params['departement']) && !empty($params['region'])) {
# $params['region'] = Functions::url_friendly($params['region']);
$params['departement'] = Constants::generic_departement;
} else {
#departement contains midi-pyrenees/aveyron, split and recreate
# TODO: edit in case of ville?
list($params['region'], $params['departement']) = explode('/', $params['departement']);
$dep_id = Functions::departement2id($params['departement'] . '-' . $params['region']);
$params['departement'] = $dep_id . '-' . $params['departement'];
}
if (empty($params['commerce_type']) || $params['commerce_type'] == '') {
if ($params['departement']) {
$params['commerce_type'] = Constants::generic_commerce_type;
} else {
$params['commerce_type'] = null;
}
}
# Covert type to new form
if (!empty($params['type']) && $params['type'] == 'entreprises-commerces') {
$params['type'] = 'vente';
} elseif (!empty($params['type']) && $params['type'] == 'murs-locaux-terrains') {
if (!empty($params['transaction']) && $params['transaction'] == 'vente') {
$params['type'] = 'vente-immobilier';
} elseif (!empty($params['transaction']) && $params['transaction'] == 'location') {
$params['type'] = 'location';
} elseif (!empty($params['transaction']) && $params['transaction'] == 'cession-de-droit-au-bail') {
$params['type'] = 'opportunites-investisseurs';
} else {
$params['type'] = 'tout';
}
}
# Remove default values from query, to have better URLS
$twig = $this->container->get('twig');
$globals = $twig->getGlobals();
$max_slider = $globals['max_slider'];
$price_arr = [];
if ($params['type'] == 'vente') {
if (!empty($params['price_f'])) {
$price_arr = $params['price_f'];
}
} else {
if (!empty($params['price_m'])) {
$price_arr = $params['price_m'];
}
}
$surface_arr = [];
if (!empty($params['surface_m'])) {
$surface_arr = $params['surface_m'];
}
// Clean up extra parameters:
unset($params['price_f']);
unset($params['price_m']);
unset($params['surface_m']);
# return Math.round((100 * (Math.pow(1.0471285480508996, num)-1))/10) * 10;
if (!empty($price_arr)) {
$prices = explode(';', $price_arr);
if (isset($prices[0])) {
$params['price-low'] = round(100 * (pow(1.0471285480508996, intval($prices[0]))-1)/10) * 10 * 1000;
}
if (isset($prices[1])) {
$params['price-high'] = round(100 * (pow(1.0471285480508996, intval($prices[1]))-1)/10) * 10 * 1000;
}
}
if (!empty($surface_arr)) {
$surface = explode(';', $surface_arr);
if (isset($surface[0])) {
$params['surface-min'] = round(100 * (pow(1.0471285480508996, intval($surface[0]))-1)/10) * 10;
}
if (isset($surface[1])) {
$params['surface-max'] = round(100 * (pow(1.0471285480508996, intval($surface[1]))-1)/10) * 10;
}
}
if (isset($params['price-low']) && $params['price-low'] == 0) unset($params['price-low']);
if (isset($params['price-high']) && $params['price-high'] == 9900000) unset($params['price-high']);
if (isset($params['surface-min']) && $params['surface-min'] == 0) unset($params['surface-min']);
if (isset($params['surface-max']) && ($params['surface-max'] == 9900 || $params['surface-max'] == '')) unset($params['surface-max']);
unset($params['region']);
unset($params['transaction']);
# Remove ville, if it is blanks
if (isset($params['ville']) && $params['ville'] == '') unset($params['ville']);
return $this->redirect($this->get('router')->generate(
'search_results',
$params), 301);
}
}
}