Added Buildings

This commit is contained in:
Frank Schubert
2021-07-13 23:00:16 +02:00
parent 1ebe069cea
commit da952bfe3a
20 changed files with 816 additions and 18 deletions

View File

@@ -0,0 +1,42 @@
<?php
class Building extends mfBaseModel {
private $network;
private $pop;
private $type;
private $status;
private $pipeworker;
public function getProperty($name) {
if($this->$name == null) {
if($name == "type") {
$this->type = new Buildingtype($this->type_id);
return $this->type;
}
if($name == "status") {
$this->status = new Buildingstatus($this->status_id);
return $this->status;
}
if($name == "pipeworker") {
$this->pipeworker = new Address($this->pipeworker_id);
return $this->pipeworker;
}
$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;
}
}

View File

@@ -0,0 +1,112 @@
<?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());
}
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['code'] = $r->code;
$data['oan_id'] = $r->oan_id;
$data['street'] = $r->street;
$data['zip'] = $r->zip;
$data['city'] = $r->city;
$data['gps_lat'] = $r->gps_lat;
$data['gps_long'] = $r->gps_long;
$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($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();
}
$this->layout()->setFlash("Objekt erfolgreich gespeichert.", "success");
$this->redirect("Building", "Edit", ['id' => $new_id]);
}
}

View File

@@ -0,0 +1,150 @@
<?php
class BuildingModel {
public $network_id = null;
public $pop_id = null;
public $type_id = null;
public $status_id = null;
public $pipeworker_id = null;
public $code = null;
public $oan_id = null;
public $street = null;
public $zip = null;
public $city = null;
public $gps_lat = null;
public $gps_long = null;
public $contact = null;
public $phone = null;
public $email = null;
public $units = null;
public $description = null;
public $note = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Building();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model ->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("Building", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Building($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Building", "*", "1=1 ORDER BY network_id,pop_id,street,zip,city");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Building($data);
}
}
return $items;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Building.* FROM Building
LEFT JOIN Buildingtype ON (Buildingtype.id = Building.type_id)
LEFT JOIN Buildingstatus ON (Buildingstatus.id = Building.status_id)
WHERE $where
ORDER BY network_id,pop_id,street,zip,city";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Address($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
/*
* Address Type
*/
if(is_array($filter['type']) && count($filter['type'])) {
$ot = $filter['type'];
$in = [];
foreach($ot as $type) {
$type = FronkDB::singleton()->escape($type);
$in[] = "Buildingtype.name = '$type'";
}
$or = "";
if(count($in)) {
$or = implode(" OR ", $in);
$where .= " AND ( $or )";
}
}
if(array_key_exists("status", $filter)) {
if(in_array(substr($filter['status'], 1, 2), ["<=", ">="])) {
$op = substr($filter['status'], 1, 2);
$status = substr($filter['status'], 3);
} elseif(in_array(substr($filter['status'], 1, 1), ["<", ">"])) {
$op = substr($filter['status'], 1, 1);
$status = substr($filter['status'], 2);
} else {
$op = "=";
$status = $filter['status'];
}
if(is_numeric($status)) {
$where .= " AND Buildingstatus.code $op $status";
} else {
// get code of statusname
$code = Buildingstatus::getOne(["name" => $status]);
if($code) {
$status = FronkDB::singleton()->escape($status);
$where .= " AND Buildingstatus.code $op '$status'";
}
}
}
//var_dump($filter, $where);exit;
return $where;
}
}