Created
August 2, 2017 14:47
-
-
Save ephrin/b96b0a8f5044a598c3497709a443782d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
error_reporting(E_WARNING || E_ERROR); | |
//error_reporting(E_ALL); | |
include($_SERVER["DOCUMENT_ROOT"] . "/sturtup.inc.php"); | |
include(LIBS_PATH . "functions.php"); | |
$calc_region = (int)$_POST["calсRegion"]; | |
$calc_sub_region = (int)$_POST["calсSubRegion"]; | |
$calc_category = (int)$_POST["calсCategory"]; | |
$calc_rooms = (int)$_POST["calсRooms"]; | |
$square_flat = (int)$_POST["squareFlat"]; | |
$square_land = (int)$_POST["squareLand"]; | |
$domain = 'http://olimp.vn.ua'; | |
// check, if search apartments (not buildings) - search with rooms, else - search without nubmers of rooms | |
if ($calc_category == 777) { | |
$select_category = 'category_id IN ("6","8") AND'; | |
$select_rooms = ''; | |
} else { | |
if ($calc_category == 6) { | |
$select_category = 'category_id = ' . $calc_category . ' AND'; | |
$select_rooms = 'count_rooms = ' . $calc_rooms . ' AND'; | |
if ($calc_rooms === 0) { | |
$select_rooms = 'count_rooms IN ("0","1","2","3","4","5","6","7") AND'; | |
} | |
} else { | |
$select_category = 'category_id = ' . $calc_category . ' AND'; | |
$select_rooms = ''; | |
} | |
} | |
if ($calc_sub_region == 777) { | |
$select_subregion = 'region_id IN ("153","9","3","15","11","6","7","19","4","12","13","14","26","32") AND'; | |
} else { | |
$select_subregion = 'region_id = ' . $calc_sub_region . ' AND'; | |
} | |
$calc_query_min = $db->select("SELECT * FROM products WHERE " . $select_category . " " . $select_subregion . " " . $select_rooms . " show_etalon = 'y' AND display = 'y' AND moderated = 'y' AND deleted = 'n' ORDER BY price_etalon ASC LIMIT 1;"); | |
$calc_query_avg = $db->select(" | |
SELECT | |
*, price_etalon, ABS(price_etalon - (SELECT AVG(price_etalon) FROM products)) AS deviation | |
FROM products | |
WHERE | |
" . $select_category . " " . $select_subregion . " " . $select_rooms . " show_usd = 'y' AND display = 'y' AND moderated = 'y' AND deleted = 'n' | |
ORDER BY deviation ASC | |
LIMIT 1; | |
"); | |
$calc_query_max = $db->select("SELECT * FROM products WHERE " . $select_category . " " . $select_subregion . " " . $select_rooms . " show_etalon = 'y' AND display = 'y' AND moderated = 'y' AND deleted = 'n' ORDER BY price_etalon DESC LIMIT 1;"); | |
class ProductViewModel | |
{ | |
public $id; | |
public $href; | |
public $price; | |
public $categoryId; | |
public $type; | |
public $regionId; | |
public $regionName; | |
public $regionLink; | |
public $square; | |
public $squarePlot; | |
public $rooms; | |
public $numberFloor; | |
public $allFloor; | |
public $image; | |
public $squareString; | |
} | |
class Fetching | |
{ | |
const IMAGE_QUERY = "SELECT * FROM products_photo WHERE product_id = %d AND display = 'y';"; | |
const REGION_QUERY = "SELECT title FROM regions_ext WHERE region_id = %d AND lang = 1 LIMIT 1;"; | |
const CATEGORY_TYPE_QUERY = "SELECT name2 FROM category_ext WHERE category_id = %d AND lang = 1 LIMIT 1;"; | |
private $db; | |
private $domain; | |
public function __construct($db, $domain) | |
{ | |
$this->db = $db; | |
$this->domain = $domain; | |
} | |
public function createProductView($data) | |
{ | |
$productView = new ProductViewModel(); | |
$productView->id = $data['id']; | |
$productView->href = sprintf('%s/object/%d', $this->domain, $productView->id); | |
$productView->price = $data['price_etalon']; | |
$productView->categoryId = $data['category_id']; | |
$productView->type = $this->getCategoryType($productView->categoryId); | |
$productView->regionId = $data['region_id']; | |
$productView->regionName = $this->getRegionTitle($productView->regionId); | |
$productView->regionLink = sprintf('%s/board/?region_id=%d', $this->domain, $productView->categoryId); | |
$productView->square = $data['all_square']; | |
$productView->squarePlot = $data['square_plot']; | |
$productView->rooms = $data['count_rooms']; | |
$productView->numberFloor = $data['number_floor']; | |
$productView->allFloor = $data['all_floor']; | |
$productView->image = sprintf( | |
'%s/upload/products/small/%s', | |
$this->domain, | |
$this->getImageUrl($productView->id) | |
); | |
$productView->squareString = ''; | |
return $productView; | |
} | |
private function getImageUrl($productId) | |
{ | |
$imgData = $this->db->select(sprintf(self::IMAGE_QUERY, $productId)); | |
return isset($imgData[0]['url']) ? $imgData[0]['url'] : 'noimage.jpg'; | |
} | |
private function getCategoryType($categoryId) | |
{ | |
$categories = $this->db->select(sprintf(self::CATEGORY_TYPE_QUERY, $categoryId)); | |
return isset($categories[0]['name2']) ? $categories[0]['name2'] : ''; | |
} | |
private function getRegionTitle($regionId) | |
{ | |
$regions = $this->db->select(sprintf(self::REGION_QUERY, $regionId)); | |
return isset($regions[0]['title']) ? $regions[0]['title'] : ''; | |
} | |
} | |
$fetch = new Fetching($db, $domain); | |
echo json_encode( | |
array( | |
$fetch->createProductView($calc_query_min[0]), | |
$fetch->createProductView($calc_query_avg[0]), | |
$fetch->createProductView($calc_query_max[0]) | |
) | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment