204 lines
6.1 KiB
PHP
204 lines
6.1 KiB
PHP
<?php
|
|
|
|
class BuildingController extends mfBaseController {
|
|
|
|
protected function init() {
|
|
$this->needlogin=true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me",$me);
|
|
|
|
if(!$me->isAdmin()) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$this->layout()->setTemplate("Building/Index");
|
|
$this->layout()->set("buildings", BuildingModel::getAll());
|
|
}
|
|
|
|
protected function addAction() {
|
|
$this->layout()->setTemplate("Building/Form");
|
|
$this->layout()->set("networks", NetworkModel::getAll());
|
|
$this->layout()->set("types", BuildingtypeModel::getAll());
|
|
$this->layout()->set("statuses", BuildingstatusModel::getAll());
|
|
$this->layout()->set("pipeworkers", AddressModel::search(["addresstype" => ["pipeworker"]]));
|
|
$this->layout()->set("lineworkers", AddressModel::search(["addresstype" => ["lineworker"]]));
|
|
$this->layout()->set("networksections", NetworksectionModel::getAll());
|
|
}
|
|
|
|
protected function editAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Objekt nicht gefunden", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
$building = new Building($id);
|
|
if($building->id != $id) {
|
|
$this->layout()->setFlash("Objekt nicht gefunden", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
$this->layout()->set("building", $building);
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
$r = $this->request;
|
|
$id = $r->id;
|
|
//var_dump($r);exit;
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$building = new Building($id);
|
|
if(!$building->id) {
|
|
$this->layout()->setFlash("Objekt nicht gefunden", "error");
|
|
$this->redirect("Objekt");
|
|
}
|
|
} else {
|
|
$mode = "add";
|
|
}
|
|
|
|
if(!$r->network_id || !$r->type_id) {
|
|
$this->layout()->setFlash("Bitte Netzgebiet und Typ auswählen", "error");
|
|
$this->layout()->set("building", $building);
|
|
return $this->add();
|
|
}
|
|
|
|
$data = [];
|
|
$data['network_id'] = $r->network_id;
|
|
$data['pop_id'] = ($r->pop_id) ? $r->pop_id : null;
|
|
$data['type_id'] = $r->type_id;
|
|
$data['status_id'] = ($r->status_id) ? $r->status_id : null;
|
|
$data['pipeworker_id'] = ($r->pipeworker_id) ? $r->pipeworker_id : null;
|
|
$data['lineworker_id'] = ($r->lineworker_id) ? $r->lineworker_id : null;
|
|
$data['networksection_id'] = ($r->networksection_id) ? $r->networksection_id : null;
|
|
|
|
$data['oan_id'] = $r->oan_id;
|
|
$data['street'] = $r->street;
|
|
$data['zip'] = $r->zip;
|
|
$data['city'] = $r->city;
|
|
|
|
$data['contact'] = $r->contact;
|
|
$data['phone'] = $r->phone;
|
|
$data['email'] = $r->email;
|
|
$data['units'] = $r->units;
|
|
$data['description'] = $r->description;
|
|
$data['note'] = $r->note;
|
|
|
|
$data['edit_by'] = 1;
|
|
|
|
if($this->me->is("Admin")) {
|
|
if($r->gps_lat) $data['gps_lat'] = $r->gps_lat;
|
|
if($r->gps_long) $data['gps_long'] = $r->gps_long;
|
|
if($r->code) $data['code'] = $r->code;
|
|
if($r->laea) $data['laea'] = $r->laea;
|
|
}
|
|
|
|
if($mode == "add") {
|
|
$data['status_id'] = 1;
|
|
$data['create_by'] = 1;
|
|
$building = BuildingModel::create($data);
|
|
} else {
|
|
$building->update($data);
|
|
}
|
|
|
|
//var_dump($address);exit;
|
|
$new_id = $building->save();
|
|
if(!$new_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
|
$this->layout()->set("building", $building);
|
|
return $this->add();
|
|
}
|
|
|
|
// get GPS location
|
|
if((!$building->gps_lat && !$building->gps_long) || $r->gps_again ) {
|
|
$search = [
|
|
'country' => "AT",
|
|
'city' => $building->city,
|
|
'zip' => $building->zip,
|
|
'street' => $building->street
|
|
];
|
|
$coords = Gmaps_Geocoding::getCoords($search);
|
|
|
|
if(is_array($coords) && count($coords) == 2) {
|
|
$building->gps_lat = str_replace(",",".",$coords[0]);
|
|
$building->gps_long = str_replace(",",".",$coords[1]);
|
|
$building->save();
|
|
}
|
|
}
|
|
|
|
// generate object code and LAEA coords
|
|
if(!$building->code) {
|
|
$building->code = $building->getNewObjectCode();
|
|
$building->save();
|
|
}
|
|
if(!$building->laea || $r->gps_again) {
|
|
$building->laea = $building->getLaeaCoordinates();
|
|
$building->save();
|
|
}
|
|
|
|
// Anschlüsse anlegen
|
|
if($building->units > 0) {
|
|
for($i = 1; $i <= $building->units; $i++) {
|
|
$data = [];
|
|
$data['building_id'] = $building->id;
|
|
$data['code'] = $building->code . "." . sprintf("%03d", $i);
|
|
|
|
if($building->units == 1) {
|
|
$data['contact'] = $building->contact;
|
|
$data['phone'] = $building->phone;
|
|
$data['email'] = $building->email;
|
|
}
|
|
|
|
if($building->lineworker_id) {
|
|
$data['lineworker_id'] = $building->lineworker_id;
|
|
}
|
|
if($building->oan_id) {
|
|
$data['oan_id'] = $building->oan_id. "." . sprintf("%03d", $i);
|
|
}
|
|
|
|
$term = TerminationModel::create($data);
|
|
$term->save();
|
|
}
|
|
}
|
|
|
|
|
|
$this->layout()->setFlash("Objekt erfolgreich gespeichert.", "success");
|
|
$this->redirect("Building", "Edit", ['id' => $new_id]);
|
|
}
|
|
|
|
protected function deleteAction() {
|
|
if(!$this->me->is("Admin") && !$this->me->is("pipeplanner")) {
|
|
$this->layout()->setFlash("Keine Berechtigung", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
$id = $this->request->id;
|
|
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Objekt nicht gefunden", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
$building = new Building($id);
|
|
if(!$building->id) {
|
|
$this->layout()->setFlash("Objekt nicht gefunden", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
if(TerminationModel::search(["building_id" => $id])) {
|
|
$this->layout()->setFlash("Das Objekt kann nicht gelöscht werden, da noch Anschlüsse zugeordnet sind.", "error");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
$building->delete();
|
|
|
|
$this->layout()->setFlash("Objekt gelöscht", "success");
|
|
$this->redirect("Building");
|
|
}
|
|
|
|
|
|
} |