144 lines
4.3 KiB
PHP
144 lines
4.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This is the model class for table "regions".
|
||
|
|
*
|
||
|
|
* @property integer $id
|
||
|
|
* @property string $name
|
||
|
|
* @property integer $is_city_region
|
||
|
|
* @property string $created_at
|
||
|
|
* @property string $updated_at
|
||
|
|
* @property boolean $newRecord
|
||
|
|
*/
|
||
|
|
class Region implements ImportRecord
|
||
|
|
{
|
||
|
|
public $id;
|
||
|
|
public $name;
|
||
|
|
public $is_city_region;
|
||
|
|
public $created_at;
|
||
|
|
public $updated_at;
|
||
|
|
public $newRecord;
|
||
|
|
public $parent_id;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Region constructor.
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->newRecord = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function save()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
if ($this->newRecord) {
|
||
|
|
if (!isset($this->id)) {
|
||
|
|
$sql = "INSERT INTO regions (name, created_at, is_city_region) VALUES ('"
|
||
|
|
. $this->name . "', NOW(), '" . $this->is_city_region . "')";
|
||
|
|
} else {
|
||
|
|
$sql = "INSERT INTO regions (id, name, created_at, is_city_region) VALUES ('"
|
||
|
|
. $this->id . "', '" . $this->name . "', NOW(), '" . $this->is_city_region . "')";
|
||
|
|
}
|
||
|
|
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 regions SET name = '" . $this->name .
|
||
|
|
"', updated_at = NOW(), is_city_region = '" . $this->is_city_region . "' 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 * FROM regions WHERE id='$id'";
|
||
|
|
$rez = mysql_query($sql);
|
||
|
|
if (mysql_num_rows($rez)) {
|
||
|
|
$region = new Region();
|
||
|
|
$region->newRecord = false;
|
||
|
|
$dbObject = mysql_fetch_array($rez);
|
||
|
|
$region->id = $dbObject['id'];
|
||
|
|
$region->name = $dbObject['name'];
|
||
|
|
$region->is_city_region = $dbObject['is_city_region'];
|
||
|
|
$region->created_at = $dbObject['created_at'];
|
||
|
|
$region->updated_at = $dbObject['updated_at'];
|
||
|
|
|
||
|
|
return $region;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
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 * FROM regions where id in (27, 26, 29, 32, 33, 24, 34, 38, 22, 23, 31, 35, 37, 21, 30, 20, 36, 28, 5, 3, 7434) ORDER BY name";
|
||
|
|
$sql = "SELECT DISTINCT regions.* FROM regions
|
||
|
|
INNER JOIN blocks b ON b.region_id = regions.id OR regions.parent_id is null
|
||
|
|
INNER JOIN apartments a ON a.block_id = b.id
|
||
|
|
WHERE regions.rf_region_id = $region_sql
|
||
|
|
AND (regions.domclick_id IS NOT NULL )"; // ORDER BY name // AND regions.parent_id IS NOT NULL
|
||
|
|
$rez = mysql_query($sql);
|
||
|
|
|
||
|
|
$regions = array();
|
||
|
|
if (mysql_num_rows($rez) > 0) {
|
||
|
|
while ($dbObject = mysql_fetch_assoc($rez)) {
|
||
|
|
$region = new Region();
|
||
|
|
$region->newRecord = false;
|
||
|
|
$region->id = $dbObject['id'];
|
||
|
|
$region->name = $dbObject['name'];
|
||
|
|
$region->is_city_region = $dbObject['is_city_region'];
|
||
|
|
$region->created_at = $dbObject['created_at'];
|
||
|
|
$region->updated_at = $dbObject['updated_at'];
|
||
|
|
$region->parent_id = $dbObject['parent_id'];
|
||
|
|
$regions[] = $region;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $regions;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|