515 lines
20 KiB
PHP
515 lines
20 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This is the model class for table "blocks".
|
|
*
|
|
* @property integer $id
|
|
* @property string $address
|
|
* @property integer $region_id
|
|
* @property string $name
|
|
* @property string $note
|
|
* @property string $video_url
|
|
* @property double $latitude
|
|
* @property double $longitude
|
|
* @property integer $builder_id
|
|
* @property integer $type
|
|
* @property float $fee
|
|
* @property float $builderFee
|
|
* @property float $builderRetransferFee
|
|
* @property string $image
|
|
* @property string $additional;
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
* @property boolean $newRecord
|
|
* @property boolean $yandex_building_id
|
|
* @property integer $apartmentsCount
|
|
* @property string $author_name
|
|
* @property string $author_photo
|
|
* @property string $author_desc
|
|
* @property integer $class_id
|
|
*/
|
|
class Block implements ImportRecord, DocTarget
|
|
{
|
|
|
|
public static $types = [
|
|
0 => '-',
|
|
1 => 'ДДУ',
|
|
2 => 'ЖСК',
|
|
3 => 'КП',
|
|
4 => 'ПДКП',
|
|
];
|
|
|
|
public $id;
|
|
public $address;
|
|
public $region_id;
|
|
public $name;
|
|
public $note;
|
|
public $phone;
|
|
public $video_url;
|
|
public $latitude;
|
|
public $longitude;
|
|
public $builder_id;
|
|
public $type;
|
|
public $fee;
|
|
public $builderFee;
|
|
public $builderRetransferFee;
|
|
public $image;
|
|
public $additional;
|
|
public $additog;
|
|
public $created_at;
|
|
public $updated_at;
|
|
public $newRecord;
|
|
public $yandex_building_id;
|
|
public $apartmentsCount;
|
|
public $author_name;
|
|
public $author_photo;
|
|
public $author_desc;
|
|
public $class_id;
|
|
public $recommended;
|
|
|
|
/**
|
|
* Block constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->newRecord = true;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
try {
|
|
if ($this->newRecord) {
|
|
if (!isset($this->id)) {
|
|
$sql = "INSERT INTO blocks (region_id, address, name, phone, note, latitude, longitude,
|
|
builder_id, image, created_at, fee, additional, additog, video_url, type, yandex_building_id, author_name, author_photo, author_desc, class_id) VALUES
|
|
('" . $this->region_id . "', '" . $this->address . "', '" . $this->name .
|
|
"', '" .$this->phone. "', '" . $this->note . "', '" . $this->latitude . "', '" . $this->longitude . "', '" . $this->builder_id .
|
|
"', '" . $this->image . "', NOW(), '" . $this->fee . "', '" . $this->additional . "', '" . $this->additog .
|
|
"', '" . $this->video_url . "', '" . $this->type . "', '" . $this->yandex_building_id . "', '" . $this->author_name . "', '" . $this->author_photo . "', '" . $this->author_desc . "', '" . $this->class_id . "')";
|
|
} else {
|
|
$sql = "INSERT INTO blocks (id, region_id, address, name, phone, note, latitude, longitude,
|
|
builder_id, image, created_at, fee, additional, additog, video_url, type, yandex_building_id, author_name, author_photo, author_desc, class_id) VALUES
|
|
('" . $this->id . "', '" . $this->region_id . "', '" . $this->address . "', '" . $this->name .
|
|
"', '" .$this->phone. "', '" . $this->note . "', '" . $this->latitude . "', '" . $this->longitude . "', '" . $this->builder_id .
|
|
"', '" . $this->image . "', NOW(), '" . $this->fee . "', '" . $this->additional . "', '" . $this->additog .
|
|
"', '" . $this->video_url . "', '" . $this->type . "', '" . $this->yandex_building_id . "', '" . $this->author_name . "', '" . $this->author_photo . "', '" . $this->author_desc . "', '" . $this->class_id . "')";
|
|
}
|
|
mysql_query($sql);
|
|
$err = mysql_errno();
|
|
if ($err > 0) {
|
|
echo mysql_error();
|
|
return false;
|
|
}
|
|
if (!isset($this->id)) {
|
|
$this->id = mysql_insert_id();
|
|
}
|
|
} else {
|
|
$sql = "UPDATE blocks SET region_id='" . $this->region_id . "',address='" . $this->address . "',name='" . $this->name .
|
|
"',note='" . $this->note . "',phone='" . $this->phone . "',latitude='" . $this->latitude . "',longitude='" . $this->longitude .
|
|
"',builder_id='" . $this->builder_id . "',image='" . $this->image . "',updated_at=NOW(),fee='" . $this->fee .
|
|
"',additional='" . $this->additional . "', additog='".$this->additog."', video_url='" . $this->video_url . "',type='" . $this->type .
|
|
"',yandex_building_id='" . $this->yandex_building_id . "',author_name='" . $this->author_name . "',
|
|
author_photo='" . $this->author_photo . "', author_desc='" . $this->author_desc . "', class_id='" . $this->class_id .
|
|
"', recommended='". $this->recommended ."' WHERE id = '" . $this->id . "'";
|
|
mysql_query($sql);
|
|
$err = mysql_errno();
|
|
if ($err > 0) {
|
|
echo mysql_error();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function updateParameters()
|
|
{
|
|
try {
|
|
$sql = "UPDATE blocks SET updated_at=NOW(), fee='" . $this->fee . "',additional='" . $this->additional . "', additog='" . $this->additog .
|
|
"',video_url='" . $this->video_url . "',type='" . $this->type . "', phone='" . $this->phone . "', recommended='" . $this->recommended . "' WHERE id = '" . $this->id . "'";
|
|
mysql_query($sql);
|
|
$err = mysql_errno();
|
|
if ($err > 0) {
|
|
echo mysql_error();
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function updateAuthorDesc()
|
|
{
|
|
try {
|
|
$sql = "UPDATE blocks SET updated_at=NOW(), author_name='" . $this->author_name . "', author_photo='" . $this->author_photo . "', author_desc='" . $this->author_desc . "' WHERE id = '" . $this->id . "'";
|
|
mysql_query($sql);
|
|
$err = mysql_errno();
|
|
if ($err > 0) {
|
|
echo mysql_error();
|
|
return false;
|
|
}
|
|
return true;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getIsNewRecord()
|
|
{
|
|
return $this->newRecord;
|
|
}
|
|
|
|
public static function deleteAll($condition = '')
|
|
{
|
|
throw new Exception("deleteALL not implemented");
|
|
}
|
|
|
|
public static function className()
|
|
{
|
|
return get_called_class();
|
|
}
|
|
|
|
public static function findOne($id)
|
|
{
|
|
$sql = "SELECT b.*, (select count(id) from apartments where block_id = b.id) as apartmentsCount,
|
|
(select bl.fee from builders bl where bl.id = b.builder_id) as builderFee,
|
|
(select bl.discount_fee from builders bl where bl.id = b.builder_id) as builderRetransferFee FROM blocks b WHERE id='$id'";
|
|
$rez = mysql_query($sql);
|
|
if (mysql_num_rows($rez)) {
|
|
$block = new Block();
|
|
$block->newRecord = false;
|
|
$dbObject = mysql_fetch_array($rez);
|
|
$block->id = $dbObject['id'];
|
|
$block->name = $dbObject['name'];
|
|
$block->region_id = $dbObject['region_id'];
|
|
$block->address = $dbObject['address'];
|
|
$block->phone = $dbObject['phone'];
|
|
$block->note = $dbObject['note'];
|
|
$block->latitude = $dbObject['latitude'];
|
|
$block->longitude = $dbObject['longitude'];
|
|
$block->builder_id = $dbObject['builder_id'];
|
|
$block->image = $dbObject['image'];
|
|
$block->fee = $dbObject['fee'];
|
|
$block->additional = $dbObject['additional'];
|
|
$block->additog = $dbObject['additog'];
|
|
$block->video_url = $dbObject['video_url'];
|
|
$block->type = $dbObject['type'];
|
|
$block->yandex_building_id = $dbObject['yandex_building_id'];
|
|
$block->created_at = $dbObject['created_at'];
|
|
$block->updated_at = $dbObject['updated_at'];
|
|
$block->apartmentsCount = $dbObject['apartmentsCount'];
|
|
$block->builderFee = $dbObject['builderFee'];
|
|
$block->builderRetransferFee = $dbObject['builderRetransferFee'];
|
|
$block->author_name = $dbObject['author_name'];
|
|
$block->author_photo = $dbObject['author_photo'];
|
|
$block->author_desc = $dbObject['author_desc'];
|
|
$block->class_id = $dbObject['class_id'];
|
|
$block->recommended = $dbObject['recommended'];
|
|
return $block;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function count($searchName)
|
|
{
|
|
if ($searchName != null) {
|
|
$sql = "SELECT count(id) FROM blocks WHERE LOWER(name) like LOWER('%$searchName%')";
|
|
} else {
|
|
$sql = "SELECT count(id) FROM blocks";
|
|
}
|
|
$rez = mysql_query($sql);
|
|
|
|
return mysql_result($rez, 0);
|
|
}
|
|
|
|
public static function search($searchName, $from, $to)
|
|
{
|
|
if ($searchName != null) {
|
|
$sql = "SELECT b.*, (select count(id) from apartments where block_id = b.id) as apartmentsCount,
|
|
(select bl.fee from builders bl where bl.id = b.builder_id) as builderFee,
|
|
(select bl.discount_fee from builders bl where bl.id = b.builder_id) as builderRetransferFee
|
|
FROM blocks b WHERE LOWER(name) like LOWER('%$searchName%') LIMIT $from, $to";
|
|
} else {
|
|
$sql = "SELECT b.*, (select count(id) from apartments where block_id = b.id) as apartmentsCount,
|
|
(select bl.fee from builders bl where bl.id = b.builder_id) as builderFee,
|
|
(select bl.discount_fee from builders bl where bl.id = b.builder_id) as builderRetransferFee
|
|
FROM blocks b LIMIT $from, $to";
|
|
}
|
|
$rez = mysql_query($sql);
|
|
$blocks = array();
|
|
if (mysql_num_rows($rez) > 0) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$block = new Block();
|
|
$block->newRecord = false;
|
|
$block->id = $dbObject['id'];
|
|
$block->name = $dbObject['name'];
|
|
$block->region_id = $dbObject['region_id'];
|
|
$block->address = $dbObject['address'];
|
|
$block->phone = $dbObject['phone'];
|
|
$block->note = $dbObject['note'];
|
|
$block->latitude = $dbObject['latitude'];
|
|
$block->longitude = $dbObject['longitude'];
|
|
$block->builder_id = $dbObject['builder_id'];
|
|
$block->image = $dbObject['image'];
|
|
$block->fee = $dbObject['fee'];
|
|
$block->additional = $dbObject['additional'];
|
|
$block->additog = $dbObject['additog'];
|
|
$block->video_url = $dbObject['video_url'];
|
|
$block->type = $dbObject['type'];
|
|
$block->yandex_building_id = $dbObject['yandex_building_id'];
|
|
$block->created_at = $dbObject['created_at'];
|
|
$block->updated_at = $dbObject['updated_at'];
|
|
$block->apartmentsCount = $dbObject['apartmentsCount'];
|
|
$block->builderFee = $dbObject['builderFee'];
|
|
$block->builderRetransferFee = $dbObject['builderRetransferFee'];
|
|
$block->author_name = $dbObject['author_name'];
|
|
$block->author_photo = $dbObject['author_photo'];
|
|
$block->author_desc = $dbObject['author_desc'];
|
|
$block->class_id = $dbObject['class_id'];
|
|
$block->recommended = $dbObject['recommended'];
|
|
$blocks[] = $block;
|
|
}
|
|
}
|
|
|
|
return $blocks;
|
|
}
|
|
|
|
public function getCalculatedFee()
|
|
{
|
|
if ($this->fee && (float)$this->fee) {
|
|
return (float)$this->fee;
|
|
}
|
|
|
|
if ($this->builderFee && (float)$this->builderFee) {
|
|
return (float)$this->builderFee;
|
|
}
|
|
|
|
return (float)(new ImporterService())->getDefaultFee();
|
|
}
|
|
|
|
public function getCalculatedRetransferFee()
|
|
{
|
|
if ($this->fee && (float)$this->fee) {
|
|
return (float)$this->fee;
|
|
}
|
|
|
|
if ($this->builderRetransferFee && (float)$this->builderRetransferFee) {
|
|
return (float)$this->builderRetransferFee;
|
|
}
|
|
|
|
return (float)(new ImporterService())->getDefaultFee();
|
|
}
|
|
|
|
public function getTypeName()
|
|
{
|
|
return isset(self::$types[$this->type]) ? self::$types[$this->type] : '';
|
|
}
|
|
|
|
/**
|
|
* @return integer
|
|
*/
|
|
public function getDocTargetType()
|
|
{
|
|
return Document::TYPE_BLOCK;
|
|
}
|
|
|
|
/**
|
|
* @return integer
|
|
*/
|
|
public function getDocTargetId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @return BlockImage[]
|
|
*/
|
|
public function getImages($type = Null)
|
|
{
|
|
if ((int)$type > 0) {
|
|
$sql = "SELECT * FROM block_images WHERE model_id='" . $this->id . "' AND block_images.type=" . $type;
|
|
} else {
|
|
$sql = "SELECT * FROM block_images WHERE model_id='" . $this->id . "' AND type is NULL";
|
|
}
|
|
|
|
$rez = mysql_query($sql);
|
|
|
|
$images = array();
|
|
if (mysql_num_rows($rez) > 0) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$blockImage = new BlockImage();
|
|
$blockImage->newRecord = false;
|
|
$blockImage->id = $dbObject['id'];
|
|
$blockImage->model_id = $dbObject['model_id'];
|
|
$blockImage->image = $dbObject['image'];
|
|
$blockImage->img_date = $dbObject['img_date'];
|
|
$blockImage->video_id = $dbObject['video_id'];
|
|
$images[] = $blockImage;
|
|
}
|
|
}
|
|
|
|
return $images;
|
|
}
|
|
|
|
public static function getElementsList($temp_region)
|
|
{
|
|
$user = new User;
|
|
$user->get($_SESSION['id']);
|
|
|
|
if(isset($temp_region)) {
|
|
$region_sql = $temp_region;
|
|
} else {
|
|
$region_sql = $user->region_rf_id;
|
|
}
|
|
|
|
|
|
$sql = "SELECT DISTINCT b.* FROM blocks as b
|
|
INNER JOIN regions r ON b.region_id = r.id
|
|
INNER JOIN rf_regions rr ON r.rf_region_id = rr.id
|
|
INNER JOIN apartments a ON b.id = a.block_id
|
|
WHERE rr.id = $region_sql
|
|
ORDER BY b.name";
|
|
// INNER JOIN apartments a ON b.id = a.block_id
|
|
|
|
|
|
// $sql_count = "SELECT COUNT(b.id)
|
|
// FROM blocks b
|
|
// INNER JOIN regions r ON b.region_id = r.id
|
|
// INNER JOIN rf_regions rr ON r.rf_region_id = rr.id
|
|
// INNER JOIN apartments a ON b.id = a.block_id
|
|
|
|
// WHERE b.id = a.block_id AND rr.id = $user->region_rf_id
|
|
// GROUP BY b.id";
|
|
|
|
// $sql = "SELECT b.* FROM blocks as b
|
|
// INNER JOIN regions r ON b.region_id = r.id
|
|
// INNER JOIN rf_regions rr ON r.rf_region_id = rr.id
|
|
// INNER JOIN apartments a ON a.block_id = b.id
|
|
// WHERE rr.id = $user->region_rf_id AND b.id = a.block_id
|
|
// ORDER BY b.name";
|
|
|
|
$rez = mysql_query($sql);
|
|
|
|
$blocks = array();
|
|
if (mysql_num_rows($rez) > 0) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$block = new Block();
|
|
$block->newRecord = false;
|
|
$block->id = $dbObject['id'];
|
|
$block->name = $dbObject['name'];
|
|
$block->region_id = $dbObject['region_id'];
|
|
$block->address = $dbObject['address'];
|
|
$block->phone = $dbObject['phone'];
|
|
$block->note = $dbObject['note'];
|
|
$block->latitude = $dbObject['latitude'];
|
|
$block->longitude = $dbObject['longitude'];
|
|
$block->builder_id = $dbObject['builder_id'];
|
|
$block->image = $dbObject['image'];
|
|
$block->fee = $dbObject['fee'];
|
|
$block->additional = $dbObject['additional'];
|
|
$block->additog = $dbObject['additog'];
|
|
$block->video_url = $dbObject['video_url'];
|
|
$block->type = $dbObject['type'];
|
|
$block->yandex_building_id = $dbObject['yandex_building_id'];
|
|
$block->created_at = $dbObject['created_at'];
|
|
$block->updated_at = $dbObject['updated_at'];
|
|
$block->apartmentsCount = $dbObject['apartmentsCount'];
|
|
$block->builderFee = $dbObject['builderFee'];
|
|
$block->recommended = $dbObject['recommended'];
|
|
$blocks[] = $block;
|
|
}
|
|
}
|
|
|
|
return $blocks;
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
public static function getAllImagesUrl($blockId)
|
|
{
|
|
$sql = "SELECT image FROM block_images WHERE `model_id`='{$blockId}' and `type` is null or `type` != 10";
|
|
$rez = mysql_query($sql);
|
|
|
|
$images = array();
|
|
if (mysql_num_rows($rez) > 0) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$images[] = $dbObject['image'];
|
|
}
|
|
}
|
|
|
|
return $images;
|
|
}
|
|
|
|
public static function getAllPayments($blockId) {
|
|
$strings = array();
|
|
|
|
$sql = "SELECT a.subsidy FROM apartments a WHERE a.subsidy > 0 AND a.block_id='$blockId'";
|
|
$rez = mysql_query($sql);
|
|
|
|
if (mysql_num_rows($rez) > 0) {
|
|
$strings[] = 'Субсидии';
|
|
}
|
|
|
|
$sql = "SELECT max(b.mortgage) as mortgage, max(b.military_mortgage) as military_mortgage FROM buildings b WHERE b.block_id='$blockId'";
|
|
$rez = mysql_query($sql);
|
|
|
|
if (mysql_num_rows($rez)) {
|
|
$dbObject = mysql_fetch_array($rez);
|
|
if($dbObject['mortgage'] == 1) {
|
|
$strings[] = 'Ипотека';
|
|
}
|
|
if($dbObject['military_mortgage'] == 1) {
|
|
$strings[] = 'Военная ипотека';
|
|
}
|
|
}
|
|
|
|
return $strings;
|
|
}
|
|
|
|
public static function getRoomTypesWithPrices($blockId, $period, $str_usl_bd = '1') {
|
|
// $sqlAdditional = "1";
|
|
|
|
// if (isset($period) && $period && $period > 0) {
|
|
// $sqlAdditional = "a.building_id in (select bd.id from buildings bd where UNIX_TIMESTAMP(bd.deadline) <=" . $period . ")";
|
|
// }
|
|
|
|
$sql = "SELECT DISTINCT a.room_type_id, room.name, MIN(a.price) as price, buildings.deadline
|
|
FROM apartments a, room_types room, buildings
|
|
INNER JOIN blocks b ON b.id = '$blockId'
|
|
INNER JOIN regions r ON b.region_id = r.id
|
|
INNER JOIN rf_regions rr ON r.rf_region_id = rr.id
|
|
INNER JOIN builders bld ON b.builder_id = bld.id
|
|
where
|
|
a.room_type_id = room.id
|
|
and a.building_id = buildings.id
|
|
and a.block_id = '$blockId'
|
|
and $str_usl_bd
|
|
GROUP BY a.room_type_id ORDER BY a.price";
|
|
|
|
|
|
|
|
$rez = mysql_query($sql);
|
|
|
|
$types = array();
|
|
|
|
if (mysql_num_rows($rez)) {
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
|
$types[$dbObject['room_type_id']]['name'] = $dbObject['name'];
|
|
$types[$dbObject['room_type_id']]['price'] = $dbObject['price'];
|
|
$types[$dbObject['room_type_id']]['deadline'] = $dbObject['deadline'];
|
|
}
|
|
}
|
|
|
|
return $types;
|
|
}
|
|
}
|