Files
thetool/application/ADBHausnummer/ADBHausnummer.php
2024-05-16 15:47:41 +02:00

153 lines
4.5 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 $freigaben = [];
private $wohneinheiten = [];
protected function init() {
$this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->table = "Hausnummer";
}
public function afterSave() {
if($this->netzgebiet_id && !$this->gps_long && !$this->gps_lat) {
$this->getGpsCoords();
}
}
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(__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(__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;
}
$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;
}
}