File: /var/www/limestate-api/Controllers/ComplexController.php
<?php
include_once ROOT . 'Controllers/SearchController.php';
class ComplexController
{
public static function actionAutocomplete()
{
$s = Data::getVar('s', '');
$type = Data::getVar('type', 'all');
$sRu = Data::puntoSwitcher($s, 'T->ru');
$sEn = Data::puntoSwitcher($s, 'T->eng');
$suggestionsComplex = Complex::$db->select('c.name')->from(Complex::$table_name . ' c')
->where('c.published = ?', 1)
->where('c.name LIKE ? OR c.name LIKE ? OR c.name LIKE ?', ['%' . $s . '%', '%' . $sRu . '%', '%' . $sEn . '%'])
->fetchCol();
if ($type == 'all') {
// Добавляем станции метро и районы города
$suggestionsStations = Complex::$db->select('m.name')->from(Metro::$table_name . ' m')
->where('m.name LIKE ? OR m.name LIKE ? OR m.name LIKE ?', ['%' . $s . '%', '%' . $sRu . '%', '%' . $sEn . '%'])
->fetchCol();
$suggestionsDistricts = Complex::$db->select('cd.name')->from(CityDistricts::$table_name . ' cd')
->where('cd.name LIKE ? OR cd.name LIKE ? OR cd.name LIKE ?', ['%' . $s . '%', '%' . $sRu . '%', '%' . $sEn . '%'])
->fetchCol();
$suggestions = array_merge($suggestionsComplex, $suggestionsStations, $suggestionsDistricts);
} else {
$suggestions = $suggestionsComplex;
}
sort($suggestions);
header('Access-Control-Allow-Origin: *');
echo json_encode($suggestions);
exit;
}
public static function actionList()
{
$order = Data::getVar('order', 'id');
$categoryId = Data::getVar('category_id', 0);
$isApartment = Data::getVar('is_apartment');
$limit = (int) Data::getVar('limit', 10);
$offset = (int) Data::getVar('offset', 0);
$complexesSql = Complex::$db->select(
'c.id, c.alias, c.old_ID, c.name, city.name city, cd.name city_district',
'MIN(f.price) startPrice, GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'c.latlng'
)
->from(Complex::$table_name . ' c')
->joinLeft(CityDistricts::$table_name . ' cd', 'cd.id = c.city_district_id')
->joinLeft(City::$table_name . ' city', 'city.id = c.city_id')
->joinLeft(Building::$table_name . ' b', 'b.complex_id = c.id AND b.published = 1')
->joinLeft(Flat::$table_name . ' f', 'f.building_id = b.id AND f.published = 1')
->joinLeft(FlatOptionRelation::$table_name . ' fro', 'f.id = fro.flat_id')
->joinLeft(FlatOption::$table_name . ' fo', 'fo.id = fro.option_id')
->where('f.price > 0')
->where('f.published = 1')
->where('b.published = 1')
->where('c.published = 1')
->where('c.deleted_at IS NULL')
->where('b.deleted_at IS NULL')
->where('f.deleted_at IS NULL')
->group('c.id');
if ($order == 'random') {
$complexesSql->order('RAND()');
}
if (!empty($categoryId)) {
$complexesSql->where('f.is_secondary = ?', SearchController::CATEGORY_SECONDARY == $categoryId);
}
if (isset($isApartment)) {
$complexesSql->where('c.is_apartment = ?', (int) $isApartment);
}
$totalCount = count($complexesSql->fetchCol());
$complexesSql = $complexesSql->limit($limit, $offset);
$complexes = $complexesSql->fetchAll();
foreach ($complexes as $complex) {
Complex::fillGallery($complex);
}
Ajax::success(compact('complexes', 'totalCount'));
}
public static function actionAlias($alias)
{
self::actionDefault($alias);
}
public static function actionDefault($id)
{
$id = urldecode($id);
$complexesSql = Complex::$db->select(
'c.id, c.old_ID, c.name, c.alias, city.name city, cd.name city_district, cd.alias city_district_alias, '
. 'c.completed_year, c.completed_quarter, c.is_completed, '
. 'c.description, c.infrastructure, c.beautification, '
. 'c.street_name AS street, '
. 'c.is_apartment, '
. 'c.profitability_1, c.profitability_2, c.profitability_3, c.profitability_4, c.profitability_5, c.terms_of_purchase, '
. 'MIN(f.price) startPrice, MAX(f.price) maxPrice, '
. 'm.name metroStation, ml.name metroLineName, ml.color metroLineColor, '
. 'GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'c.latlng'
)
->from(Complex::$table_name . ' c')
->joinLeft(CityDistricts::$table_name . ' cd', 'cd.id = c.city_district_id')
->joinLeft(City::$table_name . ' city', 'city.id = c.city_id')
->joinLeft(Building::$table_name . ' b', 'b.complex_id = c.id AND b.published = 1 AND b.deleted_at IS NULL')
->joinLeft(Flat::$table_name . ' f', 'f.building_id = b.id AND f.published = 1 AND f.deleted_at IS NULL')
->joinLeft(Metro::$table_name . ' m', 'm.id = c.metro_id')
->joinLeft(MetroLine::$table_name . ' ml', 'ml.id = m.line_id')
->joinLeft(FlatOptionRelation::$table_name . ' fro', 'f.id = fro.flat_id')
->joinLeft(FlatOption::$table_name . ' fo', 'fo.id = fro.option_id')
->where('f.price > 0')
->where('c.published = 1')
->where('c.deleted_at IS NULL')
->group('c.id');
if (is_numeric($id)) {
$complexesSql->where('c.id = ?', (int) $id);
} else {
$complexesSql->where('LOWER(c.alias) = ?', mb_strtolower($id));
}
$complex = $complexesSql->fetchRow();
if (empty($complex)) {
$complex = [];
Ajax::success(compact('complex'));
}
Complex::fillGallery($complex);
$complex->buildings = Building::getList($complex->id);
$complex->flatsStarters = Flat::getComplexStarers($complex->id);
$rangeValues = Flat::getRangeValues($complex->id);
$complex->maxPrice = $rangeValues->max_price;
$complex->minPrice = $rangeValues->min_price;
$complex->minArea = $rangeValues->min_area;
$complex->maxArea = $rangeValues->max_area;
Ajax::success(compact('complex'));
}
}