Added image upload to ConstructionConsent Form

This commit is contained in:
Frank Schubert
2025-01-13 19:06:50 +01:00
parent 15bfe7c734
commit f2cfefc7a2
6 changed files with 83 additions and 5 deletions

View File

@@ -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);

View File

@@ -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]);
}

View File

@@ -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);