File: /var/www/limestate-api/Controllers/FlatController.php
<?php
include_once ROOT . 'Controllers/SearchController.php';
class FlatController
{
public static function actionDefault($id)
{
$flat = Flat::$db->select(
'f.*',
'GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'b.complex_id complex_id',
'b.latlng', 'b.house_type_id',
'b.housing_number building_number',
'cd.name city_district',
'b.category_id category_id',
'b.street_name',
'b.address',
'b.street_type_id',
'b.floors floors',
'b.house_number house_number',
'b.completed_year completed_year',
'b.completed_quarter completed_quarter',
'b.block_number block_number',
'c.name complex_name',
'c.street_name AS street',
'c.is_apartment',
'GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'm.name metroStation, ml.name metroLineName, ml.color metroLineColor'
)
->from(Flat::$table_name . ' f')
->join(Building::$table_name . ' b', 'b.id = f.building_id')
->joinLeft(FlatOptionRelation::$table_name . ' fro', 'f.id = fro.flat_id')
->joinLeft(FlatOption::$table_name . ' fo', 'fo.id = fro.option_id')
->joinLeft(CityDistricts::$table_name . ' cd', 'cd.id = b.city_district_id')
->joinLeft(Complex::$table_name . ' c', 'c.id = b.complex_id')
->joinLeft(Metro::$table_name . ' m', 'm.id = b.metro_id')
->joinLeft(MetroLine::$table_name . ' ml', 'ml.id = m.line_id')
->where('f.id = ?', $id)
->where('f.published = 1')
->where('f.deleted_at IS NULL')
->fetchRow();
if (empty($flat->id)) {
Ajax::notFound('flat not found');
}
$currencies = Currency::getRates();
$flat->price_usd = $flat->price / $currencies->USD->Value;
$flat->price_eur = $flat->price / $currencies->EUR->Value;
$gallery = Attachment::getFlatGallery($flat->id);
$flat->gallery = [];
$flat->gallery_thumbs = [];
foreach ($gallery as $image) {
$flat->gallery[] = $image->file;
$flat->gallery_thumbs[] = $image->file_thumb;
}
$gallery = Attachment::getBuildingGallery($flat->building_id);
foreach ($gallery as $image) {
$flat->gallery[] = $image->file;
$flat->gallery_thumbs[] = $image->file_thumb;
}
$plan = Attachment::getFlatPlan($flat->id);
$flat->plan = $plan->file ?? null;
$flat->plan_thumb = $plan->file_thumb ?? null;
$floorPlan = Attachment::getFlatFloorPlan($flat->id);
$flat->floor_plan = $floorPlan ? $floorPlan->file : null;
$flat->floor_plan_thumb = $floorPlan ? $floorPlan->file_thumb : null;
$flat->balcony = BalconyType::getName($flat->balcony_type_id);
$flat->bathroom = BathroomType::getName($flat->bathroom_type_id);
$flat->view = ViewType::getName($flat->view_type_id);
$flat->street_type = StreetType::$types[$flat->street_type_id] ?? '';
Ajax::success(compact('flat'));
}
public static function actionBest()
{
$rooms = Data::getVar('rooms');
$categoryId = Data::getVar('category_id', 0);
$isApartment = Data::getVar('is_apartment');
$cityDistrict = (int) Data::getVar('district');
$flats = Flat::$db->select(
'f.*',
'GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'b.complex_id complex_id',
'b.floors floors',
'b.house_number house_number',
'b.completed_year completed_year',
'b.completed_quarter completed_quarter',
'b.block_number block_number',
'c.name complex_name',
'c.street_name AS street',
'GROUP_CONCAT(DISTINCT(fo.alias) SEPARATOR ", ") options',
'm.name metroStation, ml.name metroLineName, ml.color metroLineColor',
'cd.name city_district'
)
->from(Flat::$table_name . ' f')
->join(Building::$table_name . ' b', 'b.id = f.building_id')
->joinLeft(FlatOptionRelation::$table_name . ' fro', 'f.id = fro.flat_id')
->joinLeft(FlatOption::$table_name . ' fo', 'fo.id = fro.option_id')
->joinLeft(Complex::$table_name . ' c', 'c.id = b.complex_id')
->joinLeft(Metro::$table_name . ' m', 'm.id = c.metro_id')
->joinLeft(MetroLine::$table_name . ' ml', 'ml.id = m.line_id')
->joinLeft(CityDistricts::$table_name . ' cd', 'cd.id = b.city_district_id')
->limit(5)
->order('rand()')
->group('f.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');
if (!empty($rooms)) {
$flats->where('f.rooms = ?', (int) $rooms);
}
if (!empty($categoryId)) {
$flats->where('f.is_secondary = ?', SearchController::CATEGORY_SECONDARY == $categoryId);
}
if (isset($isApartment)) {
$flats->where('c.is_apartment = ?', (int) $isApartment);
}
if (!empty($cityDistrict)) {
$flats->where('cd.name = ?', $cityDistrict);
}
$flats = $flats->fetchAll();
$currencies = Currency::getRates();
foreach ($flats as &$flat) {
$flat->price_usd = $flat->price / $currencies->USD->Value;
$flat->price_eur = $flat->price / $currencies->EUR->Value;
$gallery = Attachment::getFlatGallery($flat->id);
$flat->gallery = [];
$flat->gallery_thumbs = [];
foreach ($gallery as $image) {
$flat->gallery[] = $image->file;
$flat->gallery_thumbs[] = $image->file_thumb;
}
$plan = Attachment::getFlatPlan($flat->id);
if (!empty($plan)) {
$flat->plan = $plan->file;
$flat->plan_thumb = $plan->file_thumb;
} else {
$flat->plan = null;
$flat->plan_thumb = null;
}
$floorPlan = Attachment::getFlatFloorPlan($flat->id);
if (!empty($floorPlan)) {
$flat->floor_plan = $floorPlan->file;
$flat->floor_plan_thumb = $floorPlan->file_thumb;
} else {
$flat->floor_plan = null;
$flat->floor_plan_thumb = null;
}
}
Ajax::success(compact('flats'));
}
}