328 lines
9.3 KiB
PHP
328 lines
9.3 KiB
PHP
<?php
|
|
|
|
class BuildingModel {
|
|
public $network_id = null;
|
|
public $pop_id = null;
|
|
public $type_id = null;
|
|
public $status_id = null;
|
|
public $pipeworker_id = null;
|
|
public $lineworker_id = null;
|
|
public $networksection_id = null;
|
|
public $code = null;
|
|
public $oaid = null;
|
|
public $street = null;
|
|
public $zip = null;
|
|
public $city = null;
|
|
public $gps_lat = null;
|
|
public $gps_long = null;
|
|
public $laea = 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 = mfValuecache::singleton()->get("me");
|
|
if(!$me) {
|
|
$me = new User();
|
|
$me->loadMe();
|
|
mfValuecache::singleton()->set("me", $me);
|
|
}
|
|
|
|
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 getFirst($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$res = $db->select("Building", "*", "$where ORDER BY network_id,pop_id,street,zip,city LIMIT 1");
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new Building($data);
|
|
if($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
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 count($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT COUNT(*) as cnt FROM Building
|
|
LEFT JOIN Buildingtype ON (Buildingtype.id = Building.type_id)
|
|
LEFT JOIN Buildingstatus ON (Buildingstatus.id = Building.status_id)
|
|
WHERE $where
|
|
";
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
return $data->cnt;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static function search($filter=[], $limit = false) {
|
|
$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";
|
|
|
|
mfLoghandler::singleton()->debug($sql);
|
|
if(is_array($limit) && count($limit)) {
|
|
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
|
|
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
|
|
} elseif(is_numeric($count)) {
|
|
$sql .= " LIMIT ".$limit['count'];
|
|
}
|
|
}
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new Building($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
|
|
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("networksection_id", $filter)) {
|
|
$networksection_id = $filter['networksection_id'];
|
|
if(is_numeric($networksection_id)) {
|
|
$where .= " AND Building.networksection_id=$networksection_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("status_id", $filter)) {
|
|
$status_id = $filter['status_id'];
|
|
if(is_numeric($status_id)) {
|
|
$where .= " AND Building.status_id=$status_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("pipeworker_id", $filter)) {
|
|
$pipeworker_id = $filter['pipeworker_id'];
|
|
if(is_numeric($pipeworker_id)) {
|
|
$where .= " AND Building.pipeworker_id=$pipeworker_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("pipework_enabled", $filter)) {
|
|
$pipework_enabled = $filter['pipework_enabled'];
|
|
if(!empty($pipework_enabled) || $pipework_enabled === "0") {
|
|
$where .= " AND Building.pipework_enabled=$pipework_enabled";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("type", $filter) && 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'";
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("code", $filter)) {
|
|
$code = FronkDB::singleton()->escape($filter['code']);
|
|
if($code) {
|
|
$where .= " AND Building.`code` like '%$code%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("oaid", $filter)) {
|
|
$oaid = FronkDB::singleton()->escape($filter['oaid']);
|
|
if($oaid) {
|
|
$where .= " AND Building.`oaid` like '%$oaid%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("street", $filter)) {
|
|
$street = FronkDB::singleton()->escape($filter["street"]);
|
|
if($street) {
|
|
$where .= " AND street like '%$street%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("=street", $filter)) {
|
|
$street = FronkDB::singleton()->escape($filter["=street"]);
|
|
if($street) {
|
|
$where .= " AND street like '$street'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("zip", $filter)) {
|
|
$zip = FronkDB::singleton()->escape($filter["zip"]);
|
|
if($zip) {
|
|
$where .= " AND zip like '%$zip%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("=zip", $filter)) {
|
|
$zip = FronkDB::singleton()->escape($filter["=zip"]);
|
|
if($zip) {
|
|
$where .= " AND zip like '$zip'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("city", $filter)) {
|
|
$city = FronkDB::singleton()->escape($filter["city"]);
|
|
if($city) {
|
|
$where .= " AND city like '%$city%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("=city", $filter)) {
|
|
$city = FronkDB::singleton()->escape($filter["=city"]);
|
|
if($city) {
|
|
$where .= " AND city like '$city'";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
public static function getHistory($from, $to, $network_id, $street_filter): array {
|
|
$sql = "SELECT b.id AS building_id,
|
|
b.street AS building_street,
|
|
b.zip AS building_zip,
|
|
b.city AS building_city,
|
|
wi.id AS item_id,
|
|
wi.name AS item_name,
|
|
wi.label AS item_label,
|
|
wi.type AS item_type,
|
|
wv.id AS value_id,
|
|
wv.value_string,
|
|
wv.value_int,
|
|
wv.value_text,
|
|
(wv.create) AS created_at,
|
|
wv.create_by AS created_by_user_id,
|
|
(wv.edit) AS last_edited_at,
|
|
wv.edit_by AS last_edited_by_user_id
|
|
FROM Workflowvalue wv
|
|
JOIN Workflowitem wi ON wv.item_id = wi.id
|
|
JOIN Building b ON wv.object_id = b.id";
|
|
$where = ["wi.object_type = 'Building'", "wi.num < 150"];
|
|
|
|
if ($from !== null && $to !== null) {
|
|
$where[] = "((wv.create >= " . intval($from) . " AND wv.create <= " . intval($to) . ") OR (wv.edit >= " . intval($from) . " AND wv.edit <= " . intval($to) . "))";
|
|
}
|
|
if ($network_id !== null) {
|
|
$where[] = "b.network_id = " . intval($network_id);
|
|
}
|
|
if (!empty($street_filter)) {
|
|
$escaped_street = addslashes($street_filter);
|
|
$where[] = "b.street LIKE '%" . $escaped_street . "%'";
|
|
}
|
|
|
|
$sql .= " WHERE " . implode(" AND ", $where);
|
|
$sql .= " ORDER BY wv.edit DESC, wv.create DESC;";
|
|
|
|
$db = FronkDB::singleton();
|
|
$res = $db->query($sql);
|
|
|
|
if ($db->num_rows($res)) {
|
|
$items = [];
|
|
while ($data = $db->fetch_object($res)) {
|
|
$items[] = $data;
|
|
}
|
|
return $items;
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
}
|