Added Terminations to buildings

This commit is contained in:
Frank Schubert
2021-07-22 22:56:36 +02:00
parent 7f6ed3e1eb
commit 4ff09b319b
25 changed files with 655 additions and 137 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
class Termination extends mfBaseModel {
protected $forcestr = ['phone','email','note'];
private $building;
private $status;
private $lineworker;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
$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;
}
}
@@ -0,0 +1,69 @@
<?php
class TerminationController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->isAdmin() && !$me->is("netowner") && !$me->is("lineplanner") && !$me->is("lineworker")) {
$this->redirect("Dashboard");
}
}
protected function apiAction() {
$do = $this->request->do;
$data = [];
switch($do) {
case "setValue":
$return = $this->setValueApi();
break;
default:
$return = false;
}
if(!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
private function setValueApi() {
$term_id = $this->request->id;
$field = $this->request->type;
$value = $this->db->escape($this->request->value);
if(!is_numeric($term_id) || $term_id < 1) {
$this->log->debug(__FILE__.": id not a number");
return false;
}
if(!in_array($field, ["name","contact","phone","email"])) {
$this->log->debug(__FILE__.": invalid field");
return false;
}
$term = new Termination($term_id);
if(!$term->id) {
$this->log->debug(__FILE__.": no termination");
return false;
}
$term->$field = $value;
if(!$term->save()) {
$this->log->debug(__FILE__.": error saving");
return false;
}
return ["msg" => "Saved successfully"];
}
}
@@ -0,0 +1,139 @@
<?php
class TerminationModel {
public $building_id = null;
public $status_id = null;
public $lineworker_id = null;
public $name = null;
public $code = null;
public $oan_id = null;
public $rimo_id = null;
public $contact = null;
public $phone = null;
public $email = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Termination();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
if(!$model->status_id) {
$model->status_id = 1;
}
$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("Termination", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Termination($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Termination", "*", "1=1 ORDER BY code");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Termination($data);
}
}
return $items;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Termination.* FROM Termination
LEFT JOIN Building ON (Building.id = Termination.building_id)
LEFT JOIN Terminationstatus ON (Terminationstatus.id = Termination.status_id)
WHERE $where
ORDER BY code
";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Termination($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("building_id", $filter)) {
$building_id = $filter['building_id'];
if(is_numeric($building_id) && $building_id > 0) {
$where .= " AND building_id = $building_id";
}
}
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 Terminationstatus.code $op $status";
} else {
// get code of statusname
$code = Terminationstatus::getOne(["name" => $status]);
if($code) {
$status = FronkDB::singleton()->escape($status);
$where .= " AND Terminationstatus.code $op '$status'";
}
}
}
return $where;
}
}