File: /var/www/limestate-api/Controllers/CityDistrictsController.php
<?php
class CityDistrictsController {
public static function actionDefault($id = null) {
$flat = CityDistricts::find($id);
Ajax::success([
'district' => $flat,
]);
}
public static function actionList() {
$districts = CityDistricts::$db->select()->from(CityDistricts::$table_name . ' cd')
->fetchAll();
Ajax::success($districts);
}
public static function actionExpandList() {
$districts = CityDistricts::$db->select('cd.*', 'COUNT(c.id) complex_count')
->from(CityDistricts::$table_name . ' cd')
->joinLeft(Complex::$table_name . ' c', 'cd.id = c.city_district_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')
->where('b.published = 1')
->where('c.published = 1')
->group('cd.id')
->fetchAll();
Ajax::success($districts);
}
public static function actionBestOffers() {
$flats = Flat::$db->select('SUM(f.total_area) area, MIN(f.price) min_price, f.rooms, b.city_district_id')->from(Flat::$table_name . ' f')
->join(Building::$table_name . ' b', 'b.id = f.building_id')
->group('f.rooms, b.city_district_id')
->where('f.price > 0')
->where('b.published = 1')
->where('f.published = 1')
->where('b.city_district_id IS NOT NULL')
->fetchAll();
$offers = Flat::$db->select('COUNT(f.id) total, b.city_district_id')->from(Flat::$table_name . ' f')
->join(Building::$table_name . ' b', 'b.id = f.building_id')
->group('b.city_district_id')
->where('f.price > 0')
->where('b.published = 1')
->where('f.published = 1')
->where('b.city_district_id IS NOT NULL')
->fetchAll();
$districts = CityDistricts::$db->select()->from(CityDistricts::$table_name . ' cd')
->fetchAll();
Ajax::success([
'data' => $flats,
'offers' => $offers,
'districts' => $districts
]);
}
}