Added AdressDB + AddressDB API

This commit is contained in:
Frank Schubert
2022-08-29 17:14:55 +02:00
parent 36b41b4e5a
commit 952f25797d
21 changed files with 1494 additions and 173 deletions

View File

@@ -4,6 +4,7 @@ class ADBPlz extends mfBaseModel {
protected function init() {
$this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->table = "Plz";
}
}

View File

@@ -0,0 +1,156 @@
<?php
class ADBPlzModel {
public $gemeinde_id;
public $plz;
public $plzstring;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new ADBPlz();
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);
$res = $db->select("Plz", "*", "$where ORDER BY gemeinde_id,plz LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ADBPlz($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("Plz", "*", "1=1 ORDER BY gemeinde_id,plz");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ADBPlz($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 Plz
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(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT Plz.* FROM Plz
WHERE $where
ORDER BY gemeinde_id,plz";
//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 ADBPlz($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("gemeinde_id", $filter)) {
$gemeinde_id = $filter['gemeinde_id'];
if(is_numeric($gemeinde_id)) {
$where .= " AND Plz.gemeinde_id=$gemeinde_id";
} elseif(is_array($gemeinde_id) && count($gemeinde_id)) {
$where .= " AND Plz.gemeinde_id IN (". implode(",", $gemeinde_id).")";
}
}
if(array_key_exists("plz", $filter)) {
$plz = $filter['plz'];
if(is_numeric($plz)) {
$where .= " AND Plz.plz=$plz";
} elseif(is_array($plz) && count($plz)) {
$where .= " AND Plz.plz IN (". implode(",", $plz).")";
}
}
if(array_key_exists("plzstring", $filter)) {
$plzstring = FronkDB::singleton()->escape($filter['plzstring']);
if($plzstring) {
$where .= " AND Plz.`plzstring` = '$plzstring'";
}
}
if(array_key_exists("plzstring%", $filter)) {
$plzstring = FronkDB::singleton()->escape($filter['plzstring%']);
if($plzstring) {
$where .= " AND Plz.`plzstring` like '%$plzstring%'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}