189 lines
6.1 KiB
PHP
189 lines
6.1 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 (!$me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function uploadDocumentAction() {
|
|
$owner_id = $this->request->owner_id;
|
|
$filename = trim($this->request->name);
|
|
|
|
$owner = new ConstructionConsentOwner($owner_id);
|
|
if(!$owner->id) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
$data = [];
|
|
$data["constructionconsent_id"] = $cc_id;
|
|
$data["name"] = $r->name;
|
|
$data["street"] = $r->street;
|
|
$data["zip"] = $r->zip;
|
|
$data["city"] = $r->city;
|
|
$data["country"] = $r->country;
|
|
$data["phone"] = $r->phone;
|
|
$data["fax"] = $r->fax;
|
|
$data["email"] = $r->email;
|
|
|
|
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 apiAction() {
|
|
if(!$this->me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
$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", "requested", "answered"])) {
|
|
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;
|
|
}
|
|
|
|
// allow empty result
|
|
if($new_result) {
|
|
if(!in_array($new_result, ["success", "failure"])) {
|
|
return false;
|
|
}
|
|
|
|
$owner->result = $new_result;
|
|
} else {
|
|
$owner->result = null;
|
|
}
|
|
|
|
if(!$owner->save()) {
|
|
return false;
|
|
}
|
|
|
|
if($owner->result) {
|
|
return ["message" => "Result saved successfully", "update" => ["id" => $owner->id, "result" => $owner->result, "result_text" => __($owner->result, "consent")]];
|
|
} else {
|
|
return ["message" => "Result saved successfully", "update" => ["id" => $owner->id, "result" => null, "result_text" => ""]];
|
|
}
|
|
|
|
}
|
|
} |