<?php
namespace Troika\RealEstateBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Troika\MainBundle\Command\GenerateShitFiltersCommand;
class ListController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
{
private function action($repa_name, $type_lot, $page, $add_fields, $twig, $is_rent, $request, $seo = null)
{
$repository = $this->getDoctrine()->getRepository('TroikaMainBundle:' . $repa_name);
$repa = $repository->find(1);
$route = $this->getRouteData($type_lot, $request);
$data = [
'lots' => $this->lotList($type_lot, $route['type_operation'], $page, $request),
'page' => $page,
'lotsAddresses' => $this->model('lot')->getLotAddresses($type_lot, $route['type_operation']),
'custompage' => empty($_GET) ? 'false' : 'true',
'title' => $seo ? $seo->getSeotitle() : $repa->getTitle($is_rent),
'keywords' => $seo ? $seo->getMetaKeywords() : $repa->getKeywords($is_rent),
'description' => $seo ? $seo->getSeodesc() : $repa->getDescription($is_rent),
'h1' => $seo ? $seo->getH1() : $repa->getH1($is_rent),
'seoFirst' => $seo ? $seo->getSeotext1() : $repa->getSeoLabelFirst(),
'seoSecond' => $seo ? $seo->getSeotext2() : $repa->getSeoLabelSecond(),
'seoFirstRent' => $seo ? $seo->getSeotext1() : $repa->getSeoLabelRentFirst(),
'seoSecondRent' => $seo ? $seo->getSeotext2() : $repa->getSeoLabelRentSecond(),
];
$translit = new GenerateShitFiltersCommand();
$translits = [];
foreach ($add_fields as $key => $item) {
switch ($item['logic']) {
case 'rent':
$adds = $route['type_operation'] === 1
? $this->model($item['model'])->getInUseSale($type_lot)
: $this->model($item['model'])->getInUseRent($type_lot);
break;
case 'by_type':
$adds = $this->model($item['model'])->getByType($type_lot);
break;
case 'all':
$adds = $this->model($item['model'])->getAll();
break;
default:
$adds = [];
break;
}
foreach ($adds as $add) {
if (!array_key_exists($item['model'], $translits)) {
$translits[$item['model']] = [];
}
if (!is_array($add)) {
$add = [
'id' => $add->getId(),
'name' => $add->getName(),
];
}
if ($add['id'] && $add['name']) {
$tr_model = array_key_exists('preset', $item) ? $item['preset'] : $item['model'];
$tr_key = $translit->toAscii($translit->str2url($add['name']));
$translits[$tr_model][$tr_key] = $add['id'];
}
}
$data[$key] = $adds;
}
$this->parseQuery($type_lot, $route, $data, $translits);
return $this->render('TroikaRealEstateBundle:EstateList:' . $twig . '.html.twig', $data);
}
private function getRouteData($type_lot, $request = null)
{
$rt = [
1 => [
'route' => 'elit_kvartiry',
'title' => 'Городская недвижимость',
],
[
'route' => 'zagorodnaya_nedvizhimost',
'title' => 'Загородная недвижимость',
],
[
'route' => 'commercial_nedvigimost',
'title' => 'Коммерческая недвижимость',
],
[
'route' => 'zarubezhnaya_nedvizhimost',
'title' => 'Зарубежная недвижимость',
],
[
'route' => 'special_offers',
'title' => 'Эксклюзивная недвижимость',
],
];
$request = $this->container->get('request_stack')->getCurrentRequest();
//$query = $request->getQueryString();
$query = $request->get('query');
$q_len = mb_strlen($query);
$rent = '_arenda';
$r_len = mb_strlen($rent);
if (mb_substr($query, max($q_len - $r_len, 0), min($r_len, $q_len)) === $rent) {
$query = str_replace($rent, '', $query);
$type_operation = 2;
} else {
$type_operation = 1;
}
return [
'route' => $rt[$type_lot]['route'],
'title' => $rt[$type_lot]['title'],
'query' => $query,
'type_operation' => $type_operation,
];
}
private function lotList($type, $operation, $page, $request)
{
$price_a = [
[
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(30000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(30000, 1),
'max' => $this->get('troika_main.currency')->getDollars(100000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(100000, 1),
'max' => $this->get('troika_main.currency')->getDollars(1_000_000, 1),
],
],
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(300, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(300, 1),
'max' => $this->get('troika_main.currency')->getDollars(1000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(1000, 1),
'max' => $this->get('troika_main.currency')->getDollars(10000, 1),
],
],
],
[
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(60000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(60000, 1),
'max' => $this->get('troika_main.currency')->getDollars(100000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(100000, 1),
'max' => $this->get('troika_main.currency')->getDollars(1_000_000, 1),
],
],
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(600, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(600, 1),
'max' => $this->get('troika_main.currency')->getDollars(1000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(1000, 1),
'max' => $this->get('troika_main.currency')->getDollars(10000, 1),
],
],
],
[
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(60000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(60000, 1),
'max' => $this->get('troika_main.currency')->getDollars(100000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(100000, 1),
'max' => $this->get('troika_main.currency')->getDollars(1_000_000, 1),
],
],
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => $this->get('troika_main.currency')->getDollars(500, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(500, 1),
'max' => $this->get('troika_main.currency')->getDollars(1000, 1),
],
[
'min' => $this->get('troika_main.currency')->getDollars(1000, 1),
'max' => $this->get('troika_main.currency')->getDollars(10000, 1),
],
],
],
[
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => 1000,
],
[
'min' => 1000,
'max' => 3000,
],
[
'min' => 3000,
'max' => 300000,
],
],
[
[
'min' => 0,
'max' => 15000,
],
[
'min' => 0,
'max' => 50,
],
[
'min' => 50,
'max' => 100,
],
[
'min' => 100,
'max' => 10000,
],
],
],
];
$space_a = [
[
[
'min' => 0,
'max' => 100000,
],
[
'min' => 0,
'max' => 100,
],
[
'min' => 100,
'max' => 200,
],
[
'min' => 200,
'max' => 10000,
],
],
[
[
'min' => 0,
'max' => 100000,
],
[
'min' => 0,
'max' => 600,
],
[
'min' => 300,
'max' => 1000,
],
[
'min' => 1000,
'max' => 10000,
],
],
[
[
'min' => 0,
'max' => 100000,
],
[
'min' => 0,
'max' => 300,
],
[
'min' => 300,
'max' => 1000,
],
[
'min' => 1000,
'max' => 10000,
],
],
[
[
'min' => 0,
'max' => 100000,
],
[
'min' => 0,
'max' => 300,
],
[
'min' => 300,
'max' => 1000,
],
[
'min' => 1000,
'max' => 10000,
],
],
];
$a = $request->get('price') ? (int)$request->get('price') : 0;
$b = $request->get('space') ? (int)$request->get('space') : 0;
$p = $price_a[$type - 1][$operation - 1][$a];
$s = $space_a[$type - 1][$b];
$repair = $request->get('repair');
$rooms = $request->get('rooms');
return $lots = $this
->model('lot')
->getPageByType(
[
'price' => $p,
'space' => $s,
'repair' => $repair,
'rooms' => $rooms,
'type' => $type,
'sale' => (bool)$request->get('sale'),
'operation' => $operation,
'text' => $request->get('text'),
'page' => $request->get('page') ?: 1,
'pageSize' => 10,
]
);
}
private function model($name)
{
try {
return $this
->container
->get($name . '.model');
} catch (\Exception $e) {
throw $e;
}
}
private function parseQuery($type_lot, $route, &$data, $translits)
{
$preset = [];
foreach (explode('-', $route['query']) as $q) {
if (!mb_strlen($q)) {
continue;
}
foreach ($translits as $key => $translit) {
if (!array_key_exists($q, $translit)) {
continue;
}
if (!array_key_exists($key, $preset)) {
$preset[$key] = [];
}
$preset[$key][] = $translit[$q];
break;
}
}
if ($preset) {
$preset['seotitle'] = $route['title'];
$preset['seodesc'] = '';
$preset['name'] = '';
$preset['typelot'] = $type_lot;
$preset['typeoperation'] = $route['type_operation'];
$data['filterpreset'] = $preset;
}
}
public function cityAction(Request $request, \Symfony\Component\HttpFoundation\Request $mainRequest)
{
if (
(!empty($_GET['price']) && $_GET['price'] === '[Array]') ||
(!empty($_GET['space']) && $_GET['space'] === '[Array]')
) {
$a = explode("?", $request->getUri());
$a = $a[0];
return $this->redirect($a, 301);
}
$request = $mainRequest;
$page = $request->get('page') ?: 1;
$isRent = (strpos($mainRequest->getUri(), "arenda") !== false);
$currentUrl = $mainRequest->getUri();
$currentUrl = array_reverse(explode("/", $currentUrl));
$currentUrl = $currentUrl[0];
$currentUrl = explode("?", $currentUrl);
$currentUrl = $currentUrl[0];
$seo = $this
->getDoctrine()
->getRepository('TroikaMainBundle:FilterPreset')
->findBy(['alias' => $currentUrl]);
if (empty($seo)) {
$seo = null;
} else {
$seo = $seo[0];
}
if (empty($seo)) {
$seo = $this->getDoctrine()->getRepository('TroikaMainBundle:SeoText')
->findOneBy(['route' => $mainRequest->getRequestUri()], ['id' => 'desc']);
}
return $this->action('City', 1, $page, [
'subways' => [
'model' => 'subway',
'logic' => 'rent',
],
'districts' => [
'model' => 'district',
'logic' => 'rent',
],
'types' => [
'model' => 'type_house',
'logic' => 'by_type',
'preset' => 'type',
],
'stages' => [
'model' => 'stage_house',
'logic' => 'all',
'preset' => 'stage',
],
'complexes' => [
'model' => 'complex',
'logic' => 'all',
],
'tags' => [
'model' => 'tag',
'logic' => 'all',
],
], 'city', $isRent, $mainRequest, $seo);
}
public function citySaleAction()
{
}
public function commercialAction(\Symfony\Component\HttpFoundation\Request $request)
{
$page = $request->get('page') ?: 1;
$isRent = (strpos($request->getUri(), "arenda") !== false);
$currentUrl = $request->getUri();
$currentUrl = array_reverse(explode("/", $currentUrl));
$currentUrl = $currentUrl[0];
$currentUrl = explode("?", $currentUrl);
$currentUrl = $currentUrl[0];
$seo = $this
->getDoctrine()
->getRepository('TroikaMainBundle:FilterPreset')
->findBy(['alias' => $currentUrl]);
if (empty($seo)) {
$seo = null;
} else {
$seo = $seo[0];
}
if (empty($seo)) {
$seo = $this->getDoctrine()->getRepository('TroikaMainBundle:SeoText')
->findOneBy(['route' => $request->getRequestUri()], ['id' => 'desc']);
}
return $this->action('Commercial', 3, $page, [
'subways' => [
'model' => 'subway',
'logic' => 'rent',
],
'districts' => [
'model' => 'district',
'logic' => 'rent',
],
'types' => [
'model' => 'type_house',
'logic' => 'by_type',
'preset' => 'type',
],
'stages' => [
'model' => 'stage_house',
'logic' => 'all',
'preset' => 'stage',
],
'tags' => [
'model' => 'tag',
'logic' => 'all',
],
], 'commercial', $isRent, $request, $seo);
}
/*
* bldzhad
*/
public function errorAction()
{
header('HTTP/1.0 404');
echo "Страница не найдена. <a href='/'>На главную</a>";
}
public function foreignAction(\Symfony\Component\HttpFoundation\Request $request)
{
$page = $request->get('page') ?: 1;
$isRent = (strpos($request->getUri(), "arenda") !== false);
$currentUrl = $request->getUri();
$currentUrl = array_reverse(explode("/", $currentUrl));
$currentUrl = $currentUrl[0];
$currentUrl = explode("?", $currentUrl);
$currentUrl = $currentUrl[0];
$seo = $this
->getDoctrine()
->getRepository('TroikaMainBundle:FilterPreset')
->findBy(['alias' => $currentUrl]);
if (empty($seo)) {
$seo = null;
} else {
$seo = $seo[0];
}
if (empty($seo)) {
$seo = $this->getDoctrine()->getRepository('TroikaMainBundle:SeoText')
->findOneBy(['route' => $request->getRequestUri()], ['id' => 'desc']);
}
return $this->action('ForeignEstate', 4, $page, [
'countries' => [
'model' => 'country',
'logic' => 'rent',
],
'countryCities' => [
'model' => 'country_city',
'logic' => 'rent',
],
'types' => [
'model' => 'type_house',
'logic' => 'by_type',
'preset' => 'type',
],
'stages' => [
'model' => 'stage_house',
'logic' => 'all',
'preset' => 'stage',
],
'tags' => [
'model' => 'tag',
'logic' => 'all',
],
], 'foreign', $isRent, $request, $seo);
}
public function specialAction()
{
$data = [
'lots' => $this->model('lot')->getSpecial(),
'lotsAddresses' => $this->model('lot')->getSpecialLotAddresses(),
];
return $this->render('TroikaRealEstateBundle:EstateList:special.html.twig', $data);
}
public function suburbanAction(\Symfony\Component\HttpFoundation\Request $request)
{
$request = $request;
$page = $request->get('page') ?: 1;
$isRent = (strpos($request->getUri(), "arenda") !== false);
$currentUrl = $request->getUri();
$currentUrl = array_reverse(explode("/", $currentUrl));
$currentUrl = $currentUrl[0];
$currentUrl = explode("?", $currentUrl);
$currentUrl = $currentUrl[0];
$seo = $this
->getDoctrine()
->getRepository('TroikaMainBundle:FilterPreset')
->findBy(['alias' => $currentUrl]);
if (empty($seo)) {
$seo = null;
} else {
$seo = $seo[0];
}
if (empty($seo)) {
$seo = $this->getDoctrine()->getRepository('TroikaMainBundle:SeoText')
->findOneBy(['route' => $request->getRequestUri()], ['id' => 'desc']);
}
return $this->action('SubUrban', 2, $page, [
'highways' => [
'model' => 'highway',
'logic' => 'rent',
],
'settlements' => [
'model' => 'settlement',
'logic' => 'rent',
],
'types' => [
'model' => 'type_house',
'logic' => 'by_type',
'preset' => 'type',
],
'stages' => [
'model' => 'stage_house',
'logic' => 'all',
'preset' => 'stage',
],
'tags' => [
'model' => 'tag',
'logic' => 'all',
],
], 'suburban', $isRent, $request, $seo);
}
}