147 lines
3.7 KiB
PHP
147 lines
3.7 KiB
PHP
<?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("network_id", $filter)) {
|
|
$network_id = $filter['network_id'];
|
|
if(is_numeric($network_id)) {
|
|
$where .= " AND Building.network_id=$network_id";
|
|
} elseif(is_array($network_id) && count($network_id)) {
|
|
$where .= " AND Building.network_id IN (". implode(",", $network_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;
|
|
|
|
}
|
|
|
|
|
|
} |