Added Buildings
This commit is contained in:
150
application/Building/BuildingModel.php
Normal file
150
application/Building/BuildingModel.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user