Files
thetool/application/ConstructionConsentProject/ConstructionConsentProjectController.php
2024-12-13 12:02:06 +01:00

130 lines
4.0 KiB
PHP

<?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");
}
}