Files
thetool/application/Termination/Termination.php
2022-02-15 20:41:59 +01:00

294 lines
6.9 KiB
PHP

<?php
class Termination extends mfBaseModel {
protected $forcestr = ['phone','email','note'];
private $building;
private $status;
private $lineworker;
private $workflowitems;
private $cpeprovisioning;
private $files;
private $patching;
private $linework_enabler;
private $creator;
private $editor;
public function getAddress($singelLine = false) {
if(!$this->id) {
return false;
}
$b = $this->getProperty("building");
$address = $b->street;
if($this->name) {
$address .= " ".$this->name;
}
$address .= "\n".$b->zip." ".$b->city;
if($singelLine) {
$address = str_replace("\n", " ", $address);
}
return $address;
}
public function getWorkflowvalue($itemname, $type = false) {
$item = WorkflowitemModel::getFirst(['name' => $itemname]);
if(!$item->id) {
return null;
}
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 isWorkflowPatchingDone() {
$item_names = [
'pop_id',
'schrank',
'baugruppe',
'modul',
'ports',
'abschlusstyp',
'bb_kabel',
'bb_kabel_steps',
'bb_fasern',
'kundenkabel_typ',
'kundenkabel_fasern'
];
$done = true;
foreach($item_names as $name) {
$this->log->debug(__FILE__.": (Termination ".$this->id.") $name => $type == '".$this->getWorkflowvalue($name)."' (ist: '".$this->getWorkflowvalue("ist_".$name)."')");
if(empty($this->getWorkflowvalue($name)) && empty($this->getWorkflowvalue("ist_".$name))) {
$done = false;
break;
}
}
return $done;
}
public function getPop() {
$pop_id = $this->getWorkflowvalue('pop_id');
if(!$pop_id) {
return null;
}
return new Pop($pop_id);
}
public function getNewObjectCode() {
if(!$this->building_id) {
return false;
}
$bcode = $this->getProperty("building")->code;
var_dump($bcode);
if(!$bcode) {
return false;
}
$codes = [];
// get existing codes
$res = $this->db->select("Termination", "code", "code like '$bcode.%'");
if($this->db->num_rows($res)) {
while($data = $this->db->fetch_object($res)) {
$codes[] = $data->code;
}
} else {
return $bcode.".001";
}
if(count($codes)) {
sort($codes);
}
$last_code = end($codes);
$m = [];
if(preg_match('/\.(\d+)$/', $last_code, $m)) {
if($m[1]) {
$last_num = $m[1];
}
} else {
return false;
}
$new_num = ++$last_num;
$code = $bcode.".".sprintf("%03d", $new_num);
return $code;
}
public function getLineworkportPairs() {
$ports = $this->getProperty("workflowitems")["ports"]->value->value_string;
$ist_port = $this->getProperty("workflowitems")["ist_ports"]->value->value_string;
if(strlen($ist_port)) {
$ports = $ist_port;
}
if(!$ports) {
return [];
}
$return = [];
$return["range"] = [];
$return["pairs"] = [];
$ports = preg_replace('/[^0-9-]+/', "", $ports);
if(strpos($ports, "-") !== false) {
// port range
$this->log->debug("is range");
$port_parts = explode("-", $ports);
if(is_array($port_parts) && count($port_parts) == 2) {
$from = $port_parts[0];
$to = $port_parts[1];
if($port_parts[0] > $port_parts[1]) {
$from = $port_parts[1];
$to = $port_parts[0];
}
$range = [];
$pairs = [];
for($i = $from; $i <= $to; $i++) {
$range[] = intval($i);
if($i + 1 <= $to) {
$pairs[] = $i."-".($i+1);
}
}
$return["range"] = $range;
$return["pairs"] = $pairs;
}
} else {
// single port
$this->log->debug("not a range");
$return["range"][] = $ports;
}
//var_dump($return);exit;
return $return;
}
public function resetProperties() {
$this->building = null;
$this->status = null;
$this->lineworker = null;
$this->workflowitems = null;
$this->files = null;
$this->creator = null;
$this->editor = null;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "status") {
$this->status = TerminationstatusModel::getOne($this->status_id);
return $this->status;
}
if($name == "workflowitems") {
foreach(WorkflowitemModel::search(["object_type" => "Termination", "active" => 1]) as $item) {
$item->setObjectId($this->id);
$this->workflowitems[$item->name] = $item;
}
//var_dump($this->workflowitems);exit;
return $this->workflowitems;
}
if($name == "lineworker") {
$this->lineworker = new Address($this->getProperty("building")->lineworker_id);
return $this->lineworker;
}
if($name == "files") {
$this->files = TerminationFileModel::search(["termination_id" => $this->id]);
return $this->files;
}
if($name == "patching") {
$this->patching = PatchingModel::getFirst(["termination_id" => $this->id]);
return $this->patching;
}
if($name == "Cpeprovisioning") {
$this->cpeprovisioning = CpeprovisioningModel::getFirst(["termination_id" => $this->id]);
return $this->cpeprovisioning;
}
if($name == "linework_enabler") {
$this->linework_enabler = new User($this->linework_enabled_by);
return $this->linework_enabler;
}
if($name == "creator") {
$this->creator = new User($this->create_by);
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$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;
}
}