283 lines
8.8 KiB
PHP
283 lines
8.8 KiB
PHP
<?php
|
|
|
|
class ADBWohneinheitModel {
|
|
public $status_id;
|
|
public $oaid;
|
|
public $extref;
|
|
public $hausnummer_id;
|
|
public $num;
|
|
public $block;
|
|
public $stiege;
|
|
public $stock;
|
|
public $tuer;
|
|
public $zusatz;
|
|
public $bezeichner;
|
|
public $nutzung;
|
|
public $patch_cluster;
|
|
public $patch_shelf;
|
|
public $patch_module;
|
|
public $patch_port;
|
|
public $external_data;
|
|
|
|
public $note;
|
|
public $create_by = null;
|
|
public $edit_by = null;
|
|
public $create = null;
|
|
public $edit = null;
|
|
|
|
public static function create(Array $data) {
|
|
$model = new ADBWohneinheit();
|
|
|
|
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(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT Wohneinheit.* FROM Wohneinheit
|
|
LEFT JOIN Hausnummer ON (Hausnummer.id = Wohneinheit.hausnummer_id)
|
|
WHERE $where
|
|
GROUP BY Wohneinheit.id
|
|
ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer,num
|
|
LIMIT 1";
|
|
|
|
//mfLoghandler::singleton()->debug($sql);
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new ADBWohneinheit($data);
|
|
if($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function getAll() {
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$res = $db->select("Wohneinheit", "*", "1=1 ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer,num");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new ADBWohneinheit($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
public static function count($filter) {
|
|
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT COUNT(*) as cnt FROM
|
|
(SELECT Wohneinheit.* FROM Wohneinheit
|
|
LEFT JOIN Hausnummer ON (Hausnummer.id = Wohneinheit.hausnummer_id)
|
|
WHERE $where
|
|
GROUP BY Wohneinheit.id
|
|
) as tbl
|
|
";
|
|
|
|
//mfLoghandler::singleton()->debug($sql);
|
|
$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(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT Wohneinheit.* FROM Wohneinheit
|
|
LEFT JOIN Hausnummer ON (Hausnummer.id = Wohneinheit.hausnummer_id)
|
|
WHERE $where
|
|
GROUP BY Wohneinheit.id
|
|
ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer,num";
|
|
|
|
//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($limit['count'])) {
|
|
$sql .= " LIMIT ".$limit['count'];
|
|
}
|
|
}
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new ADBWohneinheit($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
if(array_key_exists("extref", $filter)) {
|
|
if($filter['extref'] === null || $filter['extref'] === false) {
|
|
$where .= " AND (Wohneinheit.`extref` IS NULL OR Wohneinheit.`extref` = '')";
|
|
} else {
|
|
$extref = FronkDB::singleton()->escape($filter['extref']);
|
|
if($extref) {
|
|
$where .= " AND Wohneinheit.`extref` = '$extref'";
|
|
}
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("netzgebiet_id", $filter)) {
|
|
$netzgebiet_id = $filter['netzgebiet_id'];
|
|
if(is_numeric($netzgebiet_id)) {
|
|
$where .= " AND Hausnummer.netzgebiet_id=$netzgebiet_id";
|
|
} elseif(is_array($netzgebiet_id) && count($netzgebiet_id)) {
|
|
$where .= " AND Hausnummer.netzgebiet_id IN (". implode(",", $netzgebiet_id).")";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("hausnummer_id", $filter)) {
|
|
$hausnummer_id = $filter['hausnummer_id'];
|
|
if(is_numeric($hausnummer_id)) {
|
|
$where .= " AND Wohneinheit.hausnummer_id=$hausnummer_id";
|
|
} elseif(is_array($hausnummer_id) && count($hausnummer_id)) {
|
|
$where .= " AND Wohneinheit.hausnummer_id IN (". implode(",", $hausnummer_id).")";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("oaid", $filter)) {
|
|
$oaid = FronkDB::singleton()->escape($filter['oaid']);
|
|
if(strlen($oaid)) {
|
|
$where .= " AND Wohneinheit.`oaid` = '$oaid'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`oaid` IS NULL OR Wohneinheit.`oaid` = '')";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("num", $filter)) {
|
|
$num = $filter['num'];
|
|
if($num === false || $num === null) {
|
|
$where .= " AND (Wohneinheit.num IS NULL OR Wohneinheit.num = 0)";
|
|
} elseif(is_numeric($num)) {
|
|
$where .= " AND Wohneinheit.num=$num";
|
|
} elseif(is_array($num) && count($num)) {
|
|
$where .= " AND Wohneinheit.num IN (". implode(",", $num).")";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("block", $filter)) {
|
|
$block = FronkDB::singleton()->escape($filter['block']);
|
|
if(strlen($block)) {
|
|
$where .= " AND Wohneinheit.`block` = '$block'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`block` IS NULL OR Wohneinheit.`block` = '')";
|
|
}
|
|
}
|
|
if(array_key_exists("stock", $filter)) {
|
|
$stock = FronkDB::singleton()->escape($filter['stock']);
|
|
if(strlen($stock)) {
|
|
$where .= " AND Wohneinheit.`stock` = '$stock'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`stock` IS NULL OR Wohneinheit.`stock` = '')";
|
|
}
|
|
}
|
|
if(array_key_exists("stiege", $filter)) {
|
|
$stiege = FronkDB::singleton()->escape($filter['stiege']);
|
|
if(strlen($stiege)) {
|
|
$where .= " AND Wohneinheit.`stiege` = '$stiege'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`stiege` IS NULL OR Wohneinheit.`stiege` = '')";
|
|
}
|
|
}
|
|
if(array_key_exists("tuer", $filter)) {
|
|
$tuer = FronkDB::singleton()->escape($filter['tuer']);
|
|
if(strlen($tuer)) {
|
|
$where .= " AND Wohneinheit.`tuer` = '$tuer'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`tuer` IS NULL OR Wohneinheit.`tuer` = '')";
|
|
}
|
|
}
|
|
if(array_key_exists("zusatz", $filter)) {
|
|
$zusatz = FronkDB::singleton()->escape($filter['zusatz']);
|
|
if(strlen($zusatz)) {
|
|
$where .= " AND Wohneinheit.`zusatz` LIKE '$zusatz'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`zusatz` IS NULL OR Wohneinheit.`zusatz` = '')";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("nutzung", $filter)) {
|
|
$nutzung = FronkDB::singleton()->escape($filter['nutzung']);
|
|
if(strlen($nutzung)) {
|
|
$where .= " AND Wohneinheit.`nutzung` = '$nutzung'";
|
|
} else {
|
|
$where .= " AND (Wohneinheit.`nutzung` IS NULL OR Wohneinheit.`nutzung` = '')";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("block%", $filter)) {
|
|
$block = FronkDB::singleton()->escape($filter['block']);
|
|
if($block) {
|
|
$where .= " AND Wohneinheit.`block` like '%$block%'";
|
|
}
|
|
}
|
|
if(array_key_exists("stock%", $filter)) {
|
|
$stock = FronkDB::singleton()->escape($filter['stock']);
|
|
if($stock) {
|
|
$where .= " AND Wohneinheit.`stock` like '%$stock%'";
|
|
}
|
|
}
|
|
if(array_key_exists("stiege%", $filter)) {
|
|
$stiege = FronkDB::singleton()->escape($filter['stiege']);
|
|
if($stiege) {
|
|
$where .= " AND Wohneinheit.`stiege` like '%$stiege%'";
|
|
}
|
|
}
|
|
if(array_key_exists("tuer%", $filter)) {
|
|
$tuer = FronkDB::singleton()->escape($filter['tuer']);
|
|
if($tuer) {
|
|
$where .= " AND Wohneinheit.`tuer` like '%$tuer%'";
|
|
}
|
|
}
|
|
if(array_key_exists("zusatz%", $filter)) {
|
|
$zusatz = FronkDB::singleton()->escape($filter['zusatz']);
|
|
if($zusatz) {
|
|
$where .= " AND Wohneinheit.`zusatz` like '%$zusatz%'";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|