From f2cfefc7a23875b7c785d342cc0cf9701e0a08d5 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Mon, 13 Jan 2025 19:06:50 +0100 Subject: [PATCH] Added image upload to ConstructionConsent Form --- .../ConstructionConsent/Consentform.pdf.php | 4 ++- Layout/default/ConstructionConsent/Form.php | 13 +++++++- Layout/default/ConstructionConsent/View.php | 10 +++++++ .../ConstructionConsent.php | 10 +++++++ .../ConstructionConsentController.php | 30 +++++++++++++++++-- application/File/File.php | 21 ++++++++++++- 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/Layout/default/ConstructionConsent/Consentform.pdf.php b/Layout/default/ConstructionConsent/Consentform.pdf.php index c8dcf4121..dd0804b4a 100644 --- a/Layout/default/ConstructionConsent/Consentform.pdf.php +++ b/Layout/default/ConstructionConsent/Consentform.pdf.php @@ -328,7 +328,9 @@

Übersichtsplan / Planskizze

- + file): ?> + +
diff --git a/Layout/default/ConstructionConsent/Form.php b/Layout/default/ConstructionConsent/Form.php index 24c5ab857..5a9d3d560 100644 --- a/Layout/default/ConstructionConsent/Form.php +++ b/Layout/default/ConstructionConsent/Form.php @@ -25,7 +25,7 @@ Zustimmungserklärung -
"> + " enctype="multipart/form-data"> "/>
@@ -151,6 +151,17 @@
+
+ +
+ +
+ +
+
+ +
+
diff --git a/Layout/default/ConstructionConsent/View.php b/Layout/default/ConstructionConsent/View.php index c484e5a68..276add1bb 100644 --- a/Layout/default/ConstructionConsent/View.php +++ b/Layout/default/ConstructionConsent/View.php @@ -102,6 +102,14 @@ $pagination_entity_name = "Adressen"; GSTNR gstnr?> + + Plan/Skizze + + file): ?> + + + + @@ -113,6 +121,8 @@ $pagination_entity_name = "Adressen"; edit)?> (editor->name?>) + +
diff --git a/application/ConstructionConsent/ConstructionConsent.php b/application/ConstructionConsent/ConstructionConsent.php index 7c67b821e..e85a8dd77 100644 --- a/application/ConstructionConsent/ConstructionConsent.php +++ b/application/ConstructionConsent/ConstructionConsent.php @@ -7,6 +7,7 @@ class ConstructionConsent extends mfBaseModel { private $adb_hausnummer; private $adb_strasse; private $owners; + private $file; private $creator; private $editor; @@ -87,6 +88,15 @@ class ConstructionConsent extends mfBaseModel { return $this->owners; } + if($name == "file") { + if(!$this->id) return null; + $file = ConstructionConsentFile::getFirst(["constructionconsent_id" => $this->id]); + if($file) { + $this->file = $file; + } + return $this->file; + } + if($name == "creator") { $this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by); diff --git a/application/ConstructionConsent/ConstructionConsentController.php b/application/ConstructionConsent/ConstructionConsentController.php index 4b9f80db3..65e3965c0 100644 --- a/application/ConstructionConsent/ConstructionConsentController.php +++ b/application/ConstructionConsent/ConstructionConsentController.php @@ -106,7 +106,6 @@ class ConstructionConsentController extends mfBaseController { } $this->layout()->set("item", $item); - } protected function addAction() : void { @@ -227,8 +226,35 @@ class ConstructionConsentController extends mfBaseController { return $this->addAction(); } + if(is_array($_FILES) && array_key_exists("consent_plan_image", $_FILES) && !$_FILES['consent_plan_image']['error']) { + try { + // returns File object or throws Exception on error + $file = mfUpload::handleFormUpload("consent_plan_image", false, TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER); + } catch (Exception $ex) { + $this->layout()->setFlash("Fehler beim Hochladen: " . $ex->getMessage(), "warning"); + return $this->editAction(); + } + + $ccf = ConstructionConsentFile::create([ + 'constructionconsent_id' => $id, + 'file_id' => $file->id, + 'filename' => "zustimmungserklärung-".$item->id."-plan.png", + ]); + + // delete previous image + $img = ConstructionConsentFile::getFirst(["constructionconsent_id" => $id]); + if($img) { + $img->file->delete(); + $img->delete(); + } + + if(!$ccf->save()) { + $this->layout()->setFlash("Fehler beim Hochladen", "warning"); + } + } + $this->layout()->setFlash("Zustimmungserklärung erfolgreich gespeichert", "success"); - $this->redirect("ConstructionConsent"); + $this->redirect("ConstructionConsent", "View", ["id" => $id]); } diff --git a/application/File/File.php b/application/File/File.php index da8241163..af11895c5 100644 --- a/application/File/File.php +++ b/application/File/File.php @@ -58,7 +58,26 @@ class File extends mfBaseModel { } return false; } - + + public function asBase64String() { + $path = $this->getFullPath(); + if(!file_exists($path) || (!is_file($path) && !is_link($path))) { + return TT_IMAGE_PLACEHOLDER_B64; + } + return base64_encode(file_get_contents($path)); + } + + public function asDataUrl() { + $path = $this->getFullPath(); + if(!file_exists($path) || (!is_file($path) && !is_link($path))) { + $mime = "image/webp"; + $base64 = TT_IMAGE_PLACEHOLDER_B64; + } else { + $base64 = $this->asBase64String(); + $mime = $this->getMimetype(); + } + return "data:$mime;base64,$base64"; + } public function getFullPath() { /*if(!is_numeric($this->id) || $this->id < 1) { throw new Exception("File not found", 4040);