Files
thetool/application/ADBHausnummer/ADBHausnummer.php

290 lines
9.1 KiB
PHP

<?php
class ADBHausnummer extends mfBaseModel {
protected $forcestr = ["oaid","adrcd","extref","hausnummer","zusatz","grund_nr","gdaeigenschaft","meridian","rollout_info","rimo_fcp_name"];
private $netzgebiet;
private $ortschaft;
private $strasse;
private $plz;
private $status;
private $statusflags;
private $freigaben;
private $wohneinheiten;
protected function init() {
$this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->table = "Hausnummer";
mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$this->id, 0);
}
public function afterSave($_params = []) {
if(array_key_exists("no_aftersave", $_params) && $_params["no_aftersave"]) return true;
$this->log->debug("[".$this->_ruid."] "."-----------------------------------------");
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": hausnummer_id: ".$this->id);
// prevent potential infinite loop
$nesting_level = mfValuecache::singleton()->get("adbhausnummer-save-nesting-level-".$this->id);
if(!$nesting_level) {
$nesting_level = 1;
} else {
$nesting_level++;
}
mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$this->id, $nesting_level);
if($nesting_level > 1) {
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Preventing potential infinite loop");
return true;
}
if($this->netzgebiet_id && !$this->gps_long && !$this->gps_lat) {
$this->getGpsCoords();
}
$this->logChanges();
// Statuschange from Rimo statuschange for all units
foreach(ADBWohneinheitModel::search(["hausnummer_id" => $this->id]) as $wohneinheit) {
AddressDB::handleRimoStatusUpdate($wohneinheit->id);
}
// reload self from database
$this->fetch($this->id);
return true;
}
public function refreshUnitCount() {
if(!$this->id) return true;
$unit = ADBWohneinheitModel::getFirst(["hausnummer_id" => $this->id]);
if($unit) {
$unit->refreshUnitCount();
}
return true;
}
public function resetSaveNesting() {
mfValuecache::singleton()->delete("adbhausnummer-save-nesting-level-" . $this->id);
}
private function logChanges() {
$changes = $this->getChangedFields();
if(!count($changes)) return true;
$logstr = "";
foreach($changes as $key) {
if($key == "status_id") {
$old_status = new ADBStatus($this->_old_data->status_id);
$new_status = new ADBStatus($this->data->status_id);
$logstr .= "| '$key' => FROM '".$this->_old_data->$key."' TO '".$this->data->$key."' [".$old_status->code." => ".$new_status->code."]";
} else {
$logstr .= "| '$key' => FROM '".$this->_old_data->$key."' TO '".$this->data->$key."'";
}
}
$me = new User();
$me->loadMe();
$this->log->info("[".$this->_ruid."] ".__CLASS__." Changes: User ".$me->username." $logstr");
return true;
}
public function getBuildingType() {
$rimo_type = $this->rimo_type === NULL ? '' : strtolower($this->rimo_type);
if(array_key_exists($this->rimo_type, TT_ADB_GDA_TYPES)) {
return TT_ADB_GDA_TYPES[$this->rimo_type];
}
$gdaeigenschaft = $this->gdaeigenschaft === NULL ? '' : strtolower($this->gdaeigenschaft);
if(array_key_exists($gdaeigenschaft, TT_ADB_GDA_TYPES)) {
return TT_ADB_GDA_TYPES[$gdaeigenschaft];
}
return "sd";
}
public function setNewStatusCode($new_status_code) {
if(!$new_status_code) return false;
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Want new Hausnummer (".$this->id.") Status ".$new_status_code);
$new_status = ADBStatusModel::getFirst(["code" => $new_status_code]);
if(!$new_status) {
// try flag
$flag = ADBStatusflagModel::getFirst(["code" => $new_status_code]);
if(!$flag) return false;
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Statuscode $new_status_code is Flag");
$flag_value = ADBHausnummerStatusflagValueModel::getFirst(["flag_id" => $flag->id, "hausnummer_id" => $this->id]);
if(!$flag_value) {
$flag_value = ADBHausnummerStatusflagValueModel::create([
"hausnummer_id" => $this->id,
"flag_id" => $flag->id
]);
}
$flag_value->value = 1;
$flag_value->save();
return true;
}
$old_status = $this->getProperty("status");
if($old_status->code < $new_status->code) {
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Setting Hausnummer (".$this->id.") Status from ".$old_status->code." to ".$new_status->code);
$this->status_id = $new_status->id;
}
return true;
}
private function getGpsCoords() {
$search = [
'country' => "AT",
'city' => $this->getProperty("strasse")->gemeinde->name,
'zip' => $this->getProperty("plz")->plz,
'street' => $this->getProperty("strasse")->name
];
if(!$search['country'] || !$search['city'] || !$search['zip'] || !$search['street']) {
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": Unable to retrieve GPS Coordinates. Search key missing (hausnummer_id: ".$this->id.")");
return false;
}
$coords = Gmaps_Geocoding::getCoords($search);
if(is_array($coords) && count($coords) == 2 && $coords[0] && $coords[1]) {
$this->gps_lat = str_replace(",",".",$coords[0]);
$this->gps_long = str_replace(",",".",$coords[1]);
$this->save();
}
return true;
}
public function getAddress() {
$address = "[".$this->getProperty("strasse")->gemeinde->name."]";
$address .= " ".$this->getProperty("plz")->plz;
$address .= " ".$this->getProperty("ortschaft")->name.",";
$address .= " ".$this->getProperty("strasse")->name;
$address .= " ".$this->hausnummer;
if($this->stiege) {
$address .= "/".$this->stiege;
}
return $address;
}
public function getNewOAID() {
if(!$this->plz_id) {
return false;
}
$cc = "AT";
$zip = $this->getProperty("plz")->plz;
for($try = 16; $try > 0; $try--) {
$rnd[0] = random_int(0, 255);
$rnd[1] = random_int(0, 255);
$rnd[2] = random_int(0, 255);
$rnd[3] = random_int(0, 255);
$code = "$cc-$zip-";
foreach($rnd as $r) {
$code .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
}
if(ADBHausnummerModel::search(['oaid' => $code])) {
$this->log->warn("[".$this->_ruid."] ".__FILE__."::getNewObjectCode: New Code already in use. Trying again for a maximum of $try times.");
} else {
// code is unique
break;
}
}
if($try == 0) {
return null;
}
return $code;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "freigaben") {
$this->freigaben = json_decode($this->freigabe);
if(!is_array($this->freigaben)) {
$this->freigaben = [];
}
return $this->freigaben;
}
if($name == "netzgebiet") {
$this->netzgebiet = mfValuecache::singleton()->getMfObject("ADBNetzgebiet", $this->netzgebiet_id);
return $this->netzgebiet;
}
if($name == "ortschaft") {
$this->ortschaft = mfValuecache::singleton()->getMfObject("ADBOrtschaft", $this->ortschaft_id);
return $this->ortschaft;
}
if($name == "strasse") {
$this->strasse = mfValuecache::singleton()->getMfObject("ADBStrasse", $this->strasse_id);
return $this->strasse;
}
if($name == "plz") {
$this->plz = mfValuecache::singleton()->getMfObject("ADBPlz", $this->plz_id);
return $this->plz;
}
if($name == "wohneinheiten") {
$this->wohneinheiten = mfValuecache::singleton()->get("adbWohneinheiten-hausnummer-".$this->id);
if($this->wohneinheiten === null) {
$this->wohneinheiten = ADBWohneinheitModel::search(['hausnummer_id' => $this->id]);
if($this->wohneinheiten) {
mfValuecache::singleton()->set("adbWohneinheiten-hausnummer-".$this->id, $this->wohneinheiten);
}
}
return $this->wohneinheiten;
}
if($name == "status") {
$this->status = new ADBStatus($this->status_id);
return $this->status;
}
if($name == "statusflags") {
$flags = [];
foreach(ADBStatusflagModel::getAll() as $flag) {
$flag->hausnummer_id = $this->id;
$flags[$flag->id] = $flag;
}
if(count($flags)) {
$this->statusflags = $flags;
}
return $this->statusflags;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}