192 lines
6.5 KiB
PHP
192 lines
6.5 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 ($this->me->is("Admin")) $this->constructionConsentProjects = array_column(ConstructionConsentProject::getAll(), 'id');
|
|
else {
|
|
$constructionConsentProjects = json_decode((new WorkerFlag($this->me->id, "constructionConsent_projects"))->value() ?? '[]');
|
|
empty($constructionConsentProjects) ? $this->redirect("Dashboard") : $this->constructionConsentProjects = $constructionConsentProjects;
|
|
}
|
|
}
|
|
|
|
protected function indexAction() : void {
|
|
$sessionKey = MFAPPNAME . '-ConstructionConsentProject-filter';
|
|
|
|
if ($this->request->resetFilter) {
|
|
unset($_SESSION[$sessionKey]);
|
|
}
|
|
|
|
$filter = is_array($this->request->filter)
|
|
? $this->request->filter
|
|
: $_SESSION[$sessionKey] ?? [];
|
|
|
|
if (isset($this->request->filter)) {
|
|
$_SESSION[$sessionKey] = $filter;
|
|
}
|
|
|
|
$prepared = $this->getPreparedFilter($filter);
|
|
$prepared["id"] = $this->constructionConsentProjects;
|
|
|
|
$this->layout()->setTemplate("ConstructionConsentProject/Index");
|
|
$this->layout()->set("filter", $filter);
|
|
$this->layout()->set("projects", ConstructionConsentProject::search($prepared));
|
|
$this->layout()->set("pagination", [
|
|
'start' => (int)($this->request->s ?? 0),
|
|
'count' => 25,
|
|
'maxItems' => ConstructionConsentProject::count($prepared)
|
|
]);
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
|
|
} |