WIP 2024-12-12 ConstructionConsent
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
class ConstructionConsentProject extends mfBaseModel {
|
||||
private $consents;
|
||||
private $networks;
|
||||
private $adb_networks;
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
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_id;
|
||||
}
|
||||
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", "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
|
||||
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
|
||||
WHERE $where
|
||||
GROUP BY ConstructionConsentProject.id
|
||||
";
|
||||
|
||||
//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, $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("add-where", $filter)) {
|
||||
$where .= " ".$filter['add-where'];
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
class ConstructionConsentProjectController extends mfBaseController {
|
||||
|
||||
protected function init() : void
|
||||
{
|
||||
$this->needlogin = true;
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->me = $me;
|
||||
$this->layout()->set("me", $me);
|
||||
|
||||
if (!$me->is(["Admin"])) {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
}
|
||||
|
||||
protected function indexAction() : void {
|
||||
$this->layout()->setTemplate("ConstructionConsentProject/Index");
|
||||
|
||||
$projects = ConstructionConsentProject::getAll();
|
||||
$this->layout()->set("projects", $projects);
|
||||
|
||||
}
|
||||
|
||||
protected function addAction() {
|
||||
$this->layout()->setTemplate("ConstructionConsentProject/Form");
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function editAction() {
|
||||
$id = $this->request->id;
|
||||
if(!$id || $id < 1) {
|
||||
$this->layout()->setFlash("Projekt nicht gefunden", "error");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
}
|
||||
|
||||
$project = new ConstructionConsentProject($id);
|
||||
if(!$project->id) {
|
||||
$this->layout()->setFlash("Projekt nicht gefunden", "error");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
}
|
||||
|
||||
$this->layout()->set("project", $project);
|
||||
return $this->addAction();
|
||||
}
|
||||
|
||||
protected function saveAction() {
|
||||
if(!$this->me->is("Admin")) {
|
||||
$this->redirect("ConstructionConsent");
|
||||
}
|
||||
|
||||
$r = $this->request;
|
||||
//var_dump($r->get());exit;
|
||||
$id = $r->id;
|
||||
if(is_numeric($id) && $id > 0) {
|
||||
$mode = "edit";
|
||||
$project = new ConstructionConsentProject($id);
|
||||
if(!$project->id) {
|
||||
$this->layout()->setFlash("Zustimmungserklärungsprojekt nicht gefunden", "error");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
}
|
||||
} else {
|
||||
$id = false;
|
||||
$mode = "add";
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data["name"] = $r->name;
|
||||
|
||||
if(!$r->name) {
|
||||
$this->layout()->setFlash("Bitte alle erforderlichen Felder ausfüllen", "error");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
}
|
||||
if(!is_array($r->adb_netzgebiet_id) || !count($r->adb_netzgebiet_id)) {
|
||||
$this->layout()->setFlash("Bitte alle erforderlichen Felder ausfüllen", "error");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
}
|
||||
|
||||
$netzgebiete = [];
|
||||
|
||||
foreach($r->adb_netzgebiet_id as $netzgebiet_id) {
|
||||
$netzgebiet = new ADBNetzgebiet($netzgebiet_id);
|
||||
if(!$netzgebiet->id) continue;
|
||||
|
||||
$netzgebiete[] = $netzgebiet_id;
|
||||
}
|
||||
|
||||
if($mode == "add") {
|
||||
$project = ConstructionConsentProject::create($data);
|
||||
} else {
|
||||
$project->update($data);
|
||||
}
|
||||
|
||||
if(!$project->save()) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
||||
if($mode == "add") {
|
||||
$this->redirect("ConstructionConsentProject", "add");
|
||||
} else {
|
||||
$this->redirect("ConstructionConsentProject", "edit", ["id" => $project->id]);
|
||||
}
|
||||
}
|
||||
|
||||
//save networks
|
||||
foreach($netzgebiete as $netzgebiet_id) {
|
||||
$ccn = ConstructionConsentNetwork::getFirst(["constructionconsentproject_id" => $project->id, "adb_netzgebiet_id" => $netzgebiet_id]);
|
||||
if(!$ccn) {
|
||||
$ccn = ConstructionConsentNetwork::create([
|
||||
"constructionconsentproject_id" => $project->id,
|
||||
"adb_netzgebiet_id" => $netzgebiet_id
|
||||
]);
|
||||
$ccn->save();
|
||||
}
|
||||
}
|
||||
|
||||
foreach(ConstructionConsentNetwork::search(["constructionconsentproject_id" => $project->id]) as $ccn) {
|
||||
if(!in_array($ccn->adb_netzgebiet_id, $netzgebiete)) {
|
||||
$ccn->delete();
|
||||
}
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Zustimmungserklärungsprojekt erfolgreich gespeichert", "success");
|
||||
$this->redirect("ConstructionConsentProject");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user