Files
thetool/application/ConstructionConsentProject/ConstructionConsentProject.php
2025-03-25 10:38:20 +01:00

240 lines
7.3 KiB
PHP

<?php
class ConstructionConsentProject extends mfBaseModel {
private $consents;
private $networks;
private $adb_networks;
private $addresses;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "addresses") {
$addresses = ConstructionConsentProjectAddress::search(["constructionconsentproject_id" => $this->id]);
if(!count($addresses)) {
return [];
}
foreach($addresses as $address) {
$this->addresses[$address->address->id] = $address;
}
return $this->addresses;
}
if($name == "consents") {
$consents = ConstructionConsent::search(["constructionconsentproject_id" => $this->id]);
if(!$consents) {
return [];
}
$this->consents = $consents;
return $this->consents;
}
if($name == "networks") {
$networks = ConstructionConsentNetwork::search(["constructionconsentproject_id" => $this->id]);
if(!$networks) {
return [];
}
foreach($networks as $network) {
$this->networks[$network->id] = $network;
}
return $this->networks;
}
if($name == "adb_networks") {
$networks = ConstructionConsentNetwork::search(["constructionconsentproject_id" => $this->id]);
if(!$networks) {
return [];
}
foreach($networks as $network) {
$this->networks[$network->adb_netzgebiet->id] = $network->adb_netzgebiet;
}
return $this->networks;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new ConstructionConsentProject();
$table_fields = [
"name", "sender_name", "sender_email", "sender_reply_to", "email", "phone", "note",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$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 getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ConstructionConsentProject", "*", "1 = 1 ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentProject($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentProject.* FROM ConstructionConsentProject
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
WHERE $where
GROUP BY ConstructionConsentProject.id
ORDER BY name
LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentProject($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) AS cnt FROM ConstructionConsentProject WHERE $where";
$result = $db->query($sql);
if ($result && $db->num_rows($result) > 0) {
$data = $db->fetch_object($result);
return (int)$data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "name ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentProject.* FROM ConstructionConsentProject
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
WHERE $where
GROUP BY ConstructionConsentProject.id
ORDER BY $order";
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'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ConstructionConsentProject($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter["name"]);
if($name) {
$where .= " AND name='$name'";
}
}
if(array_key_exists("id", $filter)) {
if(is_numeric($filter["id"])) {
$where .= " AND ConstructionConsentProject.id = ".$filter["id"];
} elseif(is_array($filter["id"])) {
$ids = [];
foreach($filter["id"] as $id) {
if(is_numeric($id)) {
$ids[] = $id;
}
}
if(count($ids)) {
$where .= " AND ConstructionConsentProject.id IN (".implode(",", $ids).")";
}
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}