Files
thetool/application/ConstructionConsentOwner/ConstructionConsentOwnerController.php
2025-03-25 13:49:16 +01:00

246 lines
8.9 KiB
PHP

<?php
class ConstructionConsentOwnerController 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 uploadDocumentAction() {
$owner_id = $this->request->owner_id;
$owner = new ConstructionConsentOwner($owner_id);
if(!$owner->id) {
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
$this->redirect("ConstructionConsent");
}
$constructionConsent = new ConstructionConsent($owner->constructionconsent_id);
$filename = "ZU_KG" . $constructionConsent->kg . "_EZ" . $constructionConsent->ez . "_GST" . $constructionConsent->gst . "_" . $constructionConsent->name . "_" . $owner->lastname . ".pdf";
$_FILES['consentOwnerUpload']['name'] = $filename;
if(is_array($_FILES) && array_key_exists("consentOwnerUpload", $_FILES) && !$_FILES['consentOwnerUpload']['error']) {
try {
// returns File object or throws Exception on error
$file = mfUpload::handleFormUpload("consentOwnerUpload", false, TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER);
} catch (Exception $ex) {
$this->layout()->setFlash("Fehler beim Hochladen: " . $ex->getMessage(), "warning");
return $this->editAction();
}
$ccof = ConstructionConsentOwnerFile::create([
'constructionconsentowner_id' => $owner->id,
'file_id' => $file->id,
'filename' => $filename,
]);
if(!$ccof->save()) {
$this->layout()->setFlash("Fehler beim Hochladen", "error");
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
}
$this->layout()->setFlash("Datei erfolgreich hochgeladen", "success");
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
}
$this->layout()->setFlash("Keine Datei ausgewählt", "info");
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
}
protected function saveAction()
{
$r = $this->request;
//var_dump($r->get());exit;
$id = $r->id;
if (is_numeric($id) && $id > 0) {
$mode = "edit";
$item = new ConstructionConsentOwner($id);
if (!$item->id) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
} else {
$id = false;
$mode = "add";
}
$cc_id = $r->constructionconsent_id;
$cc = new ConstructionConsent($cc_id);
if(!$cc_id || !$cc->id) {
$this->layout()->setFlash("Beim Speichern ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
$this->redirect("ConstructionConsent");
}
if (!in_array($cc->constructionconsentproject_id, $this->constructionConsentProjects)) {
$this->layout()->setFlash("Sie sind nicht berechtigt, diese Zustimmungserklärung zu bearbeiten", "error");
$this->redirect("ConstructionConsent");
}
$data = [];
$data["constructionconsent_id"] = $cc_id;
$data["title"] = $r->title;
$data["firstname"] = $r->firstname;
$data["lastname"] = $r->lastname;
$data["street"] = $r->street;
$data["zip"] = $r->zip;
$data["city"] = $r->city;
$data["country"] = $r->country;
$data["phone"] = $r->phone;
$data["phone2"] = $r->phone2;
$data["fax"] = $r->fax;
$data["email"] = $r->email;
$data["birthdate"] = null;
if($r->birthdate) {
try {
$birthdate = DateTime::createFromFormat("d.m.Y", $r->birthdate, new DateTimeZone("Europe/Vienna"));
$data["birthdate"] = $birthdate->format("Y-m-d");
} catch(Exception $e) {
$this->layout()->setFlash("Ungültiges Geburtsdateum", "warning");
}
}
if($mode == "add") {
$data["status"] = "new";
$item = ConstructionConsentOwner::create($data);
} else {
$item->update($data);
}
if(!$item->save()) {
$this->layout()->setFlash("Beim Speichern ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
} else {
$this->layout()->setFlash("Besitzer wurde erfolgreich gespeichert.", "success");
}
$this->redirect("ConstructionConsent", "View", ["id" => $cc_id]);
}
protected function deleteAction() {
$id = $this->request->id;
$owner = new ConstructionConsentOwner($id);
if(!$owner->id) {
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
$this->redirect("ConstructionConsent");
}
$consent = $owner->consent;
if (!in_array($consent->constructionconsentproject_id, $this->constructionConsentProjects)) {
$this->layout()->setFlash("Sie sind nicht berechtigt, diese Zustimmungserklärung zu bearbeiten", "error");
$this->redirect("ConstructionConsent");
}
foreach($owner->files as $file) {
$file->file->delete();
$file->delete();
}
$owner->delete();
$this->layout()->setFlash("Besitzer gelöscht!", "success");
$this->redirect("ConstructionConsent", "View", ["id" => $consent->id]);
}
protected function apiAction() {
$do = $this->request->do;
$data = [];
switch($do) {
case "updateStatus":
$return = $this->updateStatusApi();
break;
case "updateResult":
$return = $this->updateResultApi();
break;
default:
$this->log->warn(__METHOD__ . ": Called API function '$do' does not exist");
$return = false;
}
if(!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
private function updateStatusApi() {
$owner_id = trim($this->request->id);
$new_status = trim($this->request->status);
$owner = new ConstructionConsentOwner($owner_id);
if(!$owner->id) {
return false;
}
if(!in_array($new_status, ['new', 'sent', 'returned', 'outstanding'])) {
return false;
}
$owner->status = $new_status;
if(!$owner->save()) {
return false;
}
return ["message" => "Status saved successfully", "update" => ["id" => $owner->id, "status" => $owner->status, "status_text" => __($owner->status, "consent")]];
}
private function updateResultApi() {
$owner_id = trim($this->request->id);
$new_result = trim($this->request->result);
$owner = new ConstructionConsentOwner($owner_id);
if(!$owner->id) {
return false;
}
if(!in_array($new_result, ['open', 'accepted', 'denied', 'unresolvable', 'moved'])) {
return false;
}
$owner->result = $new_result;
if(!$owner->save()) {
return false;
}
return ["message" => "Result saved successfully", "update" => ["id" => $owner->id, "result" => $owner->result, "result_text" => __($owner->result, "consent")]];
}
protected function searchOwnerAction() {
$search = $this->request->search;
$results = PreorderModel::search(['add-where' => " AND firstname LIKE '%$search%' OR lastname LIKE '%$search%'"]);
self::returnJson(array_map(function($result) {
return [
'id' => $result->id,
'firstname' => $result->firstname,
'lastname' => $result->lastname,
'street' => $result->street,
'zip' => $result->zip,
'city' => $result->city,
'phone' => $result->phone,
'email' => $result->email,
'text' => $result->firstname . " " . $result->lastname . " (" . $result->street . ", " . $result->zip . " " . $result->city . ") [" . $result->phone . " | " . $result->email . "]"
];
}, $results));
}
}