newRecord = true; } public function save() { try { if ($this->newRecord) { if (!isset($this->id)) { $sql = "INSERT INTO buildings (corp, floors, number, line, type, mortgage, block_id, deadline, created_at, military_mortgage) VALUES ('" . $this->corp . "', '" . $this->floors . "', '" . $this->number . "', '" . $this->line . "', '" . $this->type . "', '" . $this->mortgage . "', '" . $this->block_id . "', '" . $this->deadline . "', NOW(), '" . $this->military_mortgage . "')"; } else { $sql = "INSERT INTO buildings (id, corp, floors, number, line, type, mortgage, block_id, deadline, created_at, military_mortgage) VALUES ('" . $this->id . "','" . $this->corp . "', '" . $this->floors . "', '" . $this->number . "', '" . $this->line . "', '" . $this->type . "', '" . $this->mortgage . "', '" . $this->block_id . "', '" . $this->deadline . "', NOW(), '" . $this->military_mortgage . "')"; } 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 buildings SET corp='" . $this->corp . "', floors = '" . $this->floors . "', number = '" . $this->number . "', line = '" . $this->line . "', type = '" . $this->type . "', mortgage = '" . $this->mortgage . "', block_id = '" . $this->block_id . "', deadline = '" . $this->deadline . "', updated_at = NOW(), military_mortgage = '" . $this->military_mortgage . "' 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() { $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 buildings WHERE id='$id'"; $rez = mysql_query($sql); if (mysql_num_rows($rez)) { $building = new Building(); $building->newRecord = false; $dbObject = mysql_fetch_array($rez); $building->id = $dbObject['id']; $building->corp = $dbObject['corp']; $building->floors = $dbObject['floors']; $building->number = $dbObject['number']; $building->line = $dbObject['line']; $building->type = $dbObject['type']; $building->mortgage = $dbObject['mortgage']; $building->block_id = $dbObject['block_id']; $building->deadline = $dbObject['deadline']; $building->military_mortgage = $dbObject['military_mortgage']; $building->created_at = $dbObject['created_at']; $building->updated_at = $dbObject['updated_at']; return $building; } return null; } public static function getUniquePeriods() { $result = []; $sql = "SELECT DISTINCT deadline FROM buildings GROUP BY deadline LIMIT 1000"; $rez = mysql_query($sql); $date = Date::getLastPeriodDate(time()); $currentTs = strtotime($date); if (mysql_num_rows($rez) > 0) { while ($dbObject = mysql_fetch_assoc($rez)) { $date = Date::getLastPeriodDate(strtotime($dbObject['deadline'])); $ts = strtotime($date); $name = Date::getFormatedEndingPeriod($date); if ($currentTs < $ts) { $result[$ts] = $name; } } } $result[$currentTs] = Date::DEADLINE_DONE; ksort($result); return $result; } public static function getPeriodsByBlock($blockId) { $result = []; $sql = "SELECT DISTINCT deadline FROM buildings WHERE block_id = '$blockId' GROUP BY deadline"; $rez = mysql_query($sql); $date = Date::getLastPeriodDate(time()); $currentTs = strtotime($date); if (mysql_num_rows($rez) > 0) { while ($dbObject = mysql_fetch_assoc($rez)) { $date = Date::getLastPeriodDate(strtotime($dbObject['deadline'])); $ts = strtotime($date); $name = Date::getFormatedEndingPeriod($date); if ($currentTs < $ts) { $result[$ts] = $name; } } } $result[$currentTs] = Date::DEADLINE_DONE; ksort($result); return $result; } }