Files
thetool/application/ConstructionConsentProject/ConstructionConsentProjectController.php
2025-02-13 12:09:35 +01:00

198 lines
6.6 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","netowner","salespartner"]) && in_array($me->address_id, [1,209,5908,2187]))) $this->redirect("Dashboard");
}
protected function indexAction() : void {
$this->layout()->setTemplate("ConstructionConsentProject/Index");
if ($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter']);
}
$filter = [];
if (is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'] = $filter;
} else {
if (array_key_exists(MFAPPNAME . '-ConstructionConsentProject-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'])) {
$filter = $_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'];
}
}
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 25;
$pagination['maxItems'] = 0;
if (is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
//var_dump($filter);exit;
$pagination['maxItems'] = ConstructionConsentProject::count($filter);
$projects = ConstructionConsentProject::getAll();
$this->layout()->set("projects", $projects);
$this->layout()->set("pagination", $pagination);
}
private function getPreparedFilter($filter) {
$new_filter = [];
if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
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;
$data["sender_name"] = $r->sender_name;
$data["sender_email"] = $r->sender_email;
$data["sender_reply_to"] = $r->sender_reply_to;
$data["email"] = $r->email;
$data["phone"] = $r->phone;
$data["note"] = $r->note;
if($mode == "add") {
$project = ConstructionConsentProject::create($data);
} else {
$project->update($data);
}
$this->layout()->set("project", $project);
if(!$r->name || !$r->sender_name || !$r->sender_email || !$r->email || !$r->phone) {
$this->layout()->setFlash("Bitte alle erforderlichen Felder ausfüllen", "error");
return $this->addAction();
}
if(!is_array($r->adb_netzgebiet_id) || !count($r->adb_netzgebiet_id)) {
$this->layout()->setFlash("Bitte mindestens ein Netzgebiet auswählen", "error");
return $this->addAction();
}
if(!$project->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
return $this->addAction();
}
// save networks
$netzgebiete = [];
foreach($r->adb_netzgebiet_id as $netzgebiet_id) {
$netzgebiet = new ADBNetzgebiet($netzgebiet_id);
if(!$netzgebiet->id) continue;
$netzgebiete[] = $netzgebiet_id;
}
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();
}
}
//var_dump($r->get());exit;
// save addresses
$addresses = [];
foreach($r->address_id as $address_id) {
$address = new Address($address_id);
if(!$address->id) continue;
$addresses[] = $address_id;
}
foreach($addresses as $address_id) {
$cca = ConstructionConsentProjectAddress::getFirst(["constructionconsentproject_id" => $project->id, "address_id" => $address_id]);
if(!$cca) {
$cca = ConstructionConsentProjectAddress::create([
"constructionconsentproject_id" => $project->id,
"address_id" => $address_id
]);
$cca->save();
}
}
foreach(ConstructionConsentProjectAddress::search(["constructionconsentproject_id" => $project->id]) as $cca) {
if(!in_array($cca->address_id, $addresses)) {
$cca->delete();
}
}
$this->layout()->setFlash("Zustimmungserklärungsprojekt erfolgreich gespeichert", "success");
$this->redirect("ConstructionConsentProject");
}
}