Files
thetool/application/Building/Building.php
2025-04-22 17:11:20 +02:00

385 lines
11 KiB
PHP

<?php
class Building extends mfBaseModel {
protected $forcestr = ['street','zip','phone','email','note'];
private $in_after_save;
private $network;
private $networksection;
private $pop;
private $type;
private $status;
private $pipeworker;
private $lineworker;
private $terminations;
private $termination_workflow_comments;
private $workflowitems;
private $files;
private $pipework_enabler;
private $creator;
private $editor;
public function getAddress($singelLine = false) {
if(!$this->id) {
return false;
}
$address = $this->street."\n".$this->zip." ".$this->city;
if($singelLine) {
$address = str_replace("\n", " ", $address);
}
return $address;
}
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
protected function afterSave() {
if($this->in_after_save) return true;
$this->in_after_save++;
$this->resetProperties();
$this->in_after_save--;
}
public function resetProperties() {
$this->network = null;
$this->pop = null;
$this->type = null;
$this->status = null;
$this->pipeworker = null;
$this->lineworker = null;
$this->terminations = null;
$this->workflowitems = null;
$this->files = null;
}
public function updateUnitCount() {
if(!$this->id) return true;
$unit_count = TerminationModel::count(["building_id" => $this->id]);
if($this->units != $unit_count) {
$this->units = $unit_count;
$this->save();
}
return true;
}
public function getNewObjectCode() {
if(!$this->zip) {
return false;
}
$cc = "AT";
$zip = $this->zip;
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(BuildingModel::search(['code' => $code])) {
$this->log->warn(__FILE__."::getNewObjectCode: New Code already in use. Trying again for a maximum of $try times.");
} else {
// code is unique
break;
}
}
//var_dump($code);
//var_dump($try);exit;
if($try == 0) {
return null;
}
return $code;
}
public function getUsedTerminationCount($includePreorders = false) {
if(!$this->id) {
return false;
}
$count = 0;
$orders = OrderModel::search(['building_id' => $this->id]);
$count += count($orders);
if($includePreorders) {
$preorders = PreorderModel::search(['building_id' => $this->id]);
$count += count($preorders);
}
return $count;
}
public function loadWorkflowItems() {
$item_ids = [];
$building_wfitems = mfValuecache::singleton()->get("wfitems-building-active");
if(!$building_wfitems) {
$building_wfitems = WorkflowitemModel::search(["object_type" => "building", "active" => 1]);
mfValuecache::singleton()->set("wfitems-building-active", $building_wfitems);
}
foreach($building_wfitems as $item) {
mfValuecache::singleton()->set("wfBuilding-name-".$item->name, $item);
mfValuecache::singleton()->set("wfBuilding-id-".$item->id, $item);
$item->setObjectId($this->id);
$new_value = new Workflowvalue();
$new_value->item_id = $item->id;
$new_value->object_id = $this->id;
mfValuecache::singleton()->set("wfItemvalue-item-".$item->id."-object-".$this->id, $new_value);
$item->setValue($new_value);
$this->workflowitems[$item->name] = $item;
}
// get values
$values = WorkflowvalueModel::search(["object_id" => $this->id, "object_type" => "building"]);
foreach($values as $value) {
//$this->log->debug(print_r($value->item, true));
$this->workflowitems[$value->item->name]->setValue($value);
mfValuecache::singleton()->set("wfItemvalue-item-".$value->item_id."-object-".$this->id, $value);
}
}
public function getWorkflowvalue($itemname, $type = false) {
$item = mfValuecache::singleton()->get("wfBuilding-name-".$itemname);
if(!$item) {
$item = WorkflowitemModel::getFirst(['name' => $itemname]);
if(!$item || !$item->id) {
return null;
}
mfValuecache::singleton()->set("wfBuilding-name-".$itemname, $item);
}
switch($item->type) {
case "string":
$value_type = "string";
break;
case "enum":
$value_type = "string";
break;
case "gps":
$value_type = "string";
break;
case "color":
$value_type = "string";
break;
case "date":
$value_type = "string";
break;
case "int":
$value_type = "int";
break;
case "bool":
$value_type = "int";
break;
case "file":
$value_type = "int";
break;
case "text":
$value_type = "text";
break;
default:
$value_type = "string";
}
if(array_key_exists($itemname, $this->getProperty("workflowitems"))) {
return $this->getProperty("workflowitems")[$itemname]->value->{"value_$value_type"};
}
return null;
}
public function getLaeaCoordinates($gps_lat = 0, $gps_long = 0) {
if(!$gps_lat || !$gps_long) {
$gps_lat = $this->gps_lat;
$gps_long = $this->gps_long;
}
if(!$gps_lat || !$gps_long) {
return false;
}
// Elipsenparameter der Erdoberfläche
$a = 6378137;
$e = 0.081819191;
// Lage des Ausgangspunktes in natürlichen Koordinaten und Korrekturwerte in Meter
$latO = 0.907571211;
$lonO = 0.174532925;
$FE = 4321000;
$FN = 3210000;
// Mathematische Konstanten
$qP = 1.99553108748562;
$qO = 1.56982570415136;
$PI = M_PI;
// Umrechnung der Eingabekoordinaten in rad
$Lat = $gps_lat * $PI / 180;
$Lon = $gps_long * $PI / 180;
// Berechnungen
$q = (1 - $e ** 2) * ((sin($Lat) / (1 - $e ** 2 * sin($Lat) ** 2)) - ((1 / (2 * $e)) * log((1 - $e * sin($Lat)) / (1 + $e * sin($Lat)))));
$beta = asin($q / $qP);
$betaO = asin($qO / $qP);
$Rq = $a * ($qP / 2) ** 0.5;
$b = $Rq * (2 / (1 + Sin($betaO) * Sin($beta) + Cos($betaO) * Cos($beta) * Cos($Lon - $lonO))) ** 0.5;
$D = $a * (Cos($latO) / (1 - $e ** 2 * Sin($latO) ** 2) ** 0.5) / ($Rq * Cos($betaO));
$East = floor(($FE + (($b * $D) * (Cos($beta) * Sin($Lon - $lonO))))/100);
$North = floor(($FN + ($b / $D) * ((Cos($betaO) * Sin($beta)) - (Sin($betaO) * Cos($beta) * Cos($Lon - $lonO))))/100);
$Gridcell = "100mN{$North}E{$East}";
return $Gridcell;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "type") {
$type = mfValuecache::singleton()->get("mfObjectmodel-Buildingtype-".$this->type_id);
if(!$type) {
$type = new Buildingtype($this->type_id);
mfValuecache::singleton()->set("mfObjectmodel-Buildingtype-".$this->type_id, $type);
}
$this->type = $type;
return $this->type;
}
if($name == "status") {
$status = mfValuecache::singleton()->get("mfObjectmodel-Buildingstatus-".$this->status_id);
if(!$status) {
$status = new Buildingstatus($this->status_id);
mfValuecache::singleton()->set("mfObjectmodel-Buildingstatus-".$this->status_id, $status);
}
$this->status = $status;
return $this->status;
/*$this->status = new Buildingstatus($this->status_id);
return $this->status;*/
}
if($name == "pipeworker") {
$pipeworker = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->pipeworker_id);
if(!$pipeworker) {
$pipeworker = new Address($this->pipeworker_id);
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->pipeworker_id, $pipeworker);
}
$this->pipeworker = $pipeworker;
//$this->pipeworker = new Address($this->pipeworker_id);
return $this->pipeworker;
}
if($name == "lineworker") {
$lineworker = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->lineworker_id);
if(!$lineworker) {
$lineworker = new Address($this->lineworker_id);
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->lineworker_id, $lineworker);
}
$this->lineworker = $lineworker;
//$this->lineworker = new Address($this->lineworker_id);
return $this->lineworker;
}
if($name == "terminations") {
$this->terminations = TerminationModel::search(['building_id' => $this->id]);
return $this->terminations;
}
if($name == "termination_workflow_comments") {
$comments = "";
foreach($this->getProperty("terminations") as $term) {
if($term->workflow_comment) {
$comment .= $term->code.": ".trim($term->workflow_comment)."\n\n";
}
}
$this->termination_workflow_comments = $comment;
return $this->termination_workflow_comments;
}
if($name == "workflowitems") {
$this->loadWorkflowItems();
foreach(WorkflowitemModel::search(["object_type" => "building", "active" => 1]) as $item) {
$item->setObjectId($this->id);
$this->workflowitems[$item->name] = $item;
}
//var_dump($this->workflowitems);exit;
return $this->workflowitems;
}
if($name == "files") {
$this->files = BuildingFileModel::search(["building_id" => $this->id]);
return $this->files;
}
if($name == "pipework_enabler") {
$this->pipework_enabler = new User($this->pipework_enabled_by);
return $this->pipework_enabler;
}
if($name == "creator") {
$this->creator = mfValuecache::singleton()->get("Worker-id-" . $this->create_by);
if($this->creator === null) {
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-" . $this->create_by, $this->creator);
}
}
return $this->creator;
}
if($name == "editor") {
$this->editor = mfValuecache::singleton()->get("Worker-id-" . $this->edit_by);
if($this->editor === null) {
$this->editor = new User($this->edit_by);
if($this->editor->id) {
mfValuecache::singleton()->set("Worker-id-" . $this->edit_by, $this->editor);
}
}
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}