Added Document upload to ConstructionConsent Owners

This commit is contained in:
Frank Schubert
2025-01-15 17:14:49 +01:00
parent 2366dc0300
commit 905a2d0ede
8 changed files with 557 additions and 44 deletions

View File

@@ -329,7 +329,7 @@
<h3>Übersichtsplan / Planskizze</h3>
<?php if($consent->file): ?>
<img src="<?=$consent->file->file->asDataUrl()?>" style="max-width: 600px; border: 1px solid #000" />
<img src="<?=$consent->file->file->asDataUrl()?>" style="max-width: 640px; max-height: 640px; border: 1px solid #000" />
<?php endif; ?>
</div>

View File

@@ -38,7 +38,7 @@ $pagination_entity_name = "Adressen";
</div>
<div class="row">
<div class="col-6">
<div class="col-5">
<div class="card border-top-primary">
<div class="card-body">
@@ -127,7 +127,7 @@ $pagination_entity_name = "Adressen";
</div>
</div>
<div class="col-6">
<div class="col-7">
<div class="card border-top-success">
<div class="card-body">
@@ -140,41 +140,71 @@ $pagination_entity_name = "Adressen";
<?php if(!is_array($item->owners) || !count($item->owners)): ?>
<i>Noch keine Besitzer bekannt</i>
<?php else: ?>
<?php foreach($item->owners as $owner): ?>
<div class="card">
<div class="card-body">
<table class="table table-sm table-hover table-striped">
<table class="table table-sm table-striped table-hover">
<tr>
<th>Name:</th>
<td><?=$owner->name?></td>
</tr><tr>
<th>Anschrift:</th>
<th></th>
<th>Name/Anschrift</th>
<th>Telefon<br />Fax<br />Email</th>
<th>Anfragestatus</th>
<th>Ergebnis</th>
</tr>
<?php foreach($item->owners as $owner): ?>
<tr>
<td style="font-size: 1.4em;">
<a href="<?=self::getUrl("ConstructionConsent", "Download", ["owner_id" => $owner->id])?>" title="Zustimmungserklärungsformular herunterladen"><i class="far fa-fw fa-file-download"></i></a>
</td>
<td>
<strong><?=$owner->name?></strong><br />
<?=$owner->street?><br />
<?=$owner->zip?> <?=$owner->city?><br />
<?=$owner->country?>
</td>
</tr><tr>
<th>Telefon:</th>
<td><?=$owner->phone?></td>
</tr><tr>
<th>Fax:</th>
<td><?=$owner->fax?></td>
</tr><tr>
<th>Email:</th>
<td><?=$owner->email?></td>
</tr><tr>
<th>Anfragestatus</th>
<td class="text-monospace"><?=__($item->status, "consent")?></td>
</tr><tr>
<th>Anfrage Resultat</th>
<td class="text-monospace"><?=($item->result) ? __($item->result, "consent") : ""?></td>
</tr>
</table>
<a href="<?=self::getUrl("ConstructionConsent", "Download", ["owner_id" => $owner->id])?>"><i class="fas fa-fw fa-file-pdf"></i> Zustimmungserklärungsformular herunterladen</a>
<td>
<?=$owner->phone?><br />
<?=$owner->fax?><br />
<?=$owner->email?>
</td>
<td class="text-monospace">
<span id="status-<?=$owner->id?>-text" data-status-id="<?=$owner->status?>"><span id="status-<?=$owner->id?>-statustext"><?=__($owner->status, "consent")?></span> <a href="#" onclick="return toggleStatusControl(<?=$owner->id?>)"><i class="fas fa-fw fa-edit"></i></a></span>
<div class="input-group" id="status-<?=$owner->id?>-input" style="display:none">
<select class="form-control">
<option value="new" <?=($owner->status == "new") ? "selected='selected'" : ""?>><?=__("new", "consent")?></option>
<option value="requested" <?=($owner->status == "requested") ? "selected='selected'" : ""?>><?=__("requested", "consent")?></option>
<option value="answered" <?=($owner->status == "answered") ? "selected='selected'" : ""?>><?=__("answered", "consent")?></option>
</select>
<div class="input-group-append">
<button type="button" class="btn btn-primary" title="Speichern" onclick="saveStatusControl(<?=$owner->id?>)"><i class="fas fa-check"></i></button>
<button type="button" class="btn btn-secondary" title="Abbrechen" onclick="toggleStatusControl(<?=$owner->id?>)"><i class="fas fa-times"></i></button>
</div>
</div>
</td>
<td class="text-monospace">
<span id="result-<?=$owner->id?>-text" data-result-id="<?=$owner->result?>"><span id="result-<?=$owner->id?>-resulttext"><?=($owner->result) ? __($owner->result, "consent") : ""?></span> <a href="#" onclick="return toggleResultControl(<?=$owner->id?>)"><i class="fas fa-fw fa-edit"></i></a></span>
<div class="input-group" id="result-<?=$owner->id?>-input" style="display:none">
<select class="form-control">
<option></option>
<option value="success" <?=($owner->result == "success") ? "selected='selected'" : ""?>><?=__("success", "consent")?></option>
<option value="failure" <?=($owner->result == "failure") ? "selected='selected'" : ""?>><?=__("failure", "consent")?></option>
</select>
<div class="input-group-append">
<button type="button" class="btn btn-primary" title="Speichern" onclick="saveResultControl(<?=$owner->id?>)"><i class="fas fa-check"></i></button>
<button type="button" class="btn btn-secondary" title="Abbrechen" onclick="toggleResultControl(<?=$owner->id?>)"><i class="fas fa-times"></i></button>
</div>
</div>
<div>
<?php foreach($owner->files as $file): ?>
<a href="<?=self::getUrl("File", "download", ["id" => $file->file_id])?>"><i class="far fa-fw fa-file"></i> <?=$file->filename?></a><br />
<?php endforeach; ?>
</div>
<hr />
<div class="mt-2"><a href="#" onclick="openOwnerUploadModal(<?=$owner->id?>); return false;"><i class="far fa-cloud-upload" style="font-size: 1.4em;"></i> Dokument hochladen</a></div>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
</div>
@@ -255,13 +285,146 @@ $pagination_entity_name = "Adressen";
</div>
</div>
<div class="modal fade" id="ownerUpload" aria-hidden="true" aria-labelledby="#ownerUploadLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form method="post" action="<?=self::getUrl("ConstructionConsentOwner", "uploadDocument")?>" enctype="multipart/form-data">
<div class="modal-header">
<h3 class="modal-title" id="ownerUploadLabel">Dokument hochladen</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<input type="hidden" name="owner_id" id="owner-upload-owner_id" value="" />
<div class="form-group">
<label for="owner-upload-name" class="col-form-label">Name *</label>
<input type="text" class="form-control" name="name" id="owner-upload-name" />
<small>Beschreibender Kurzname, z.B: Zustimmung oder Ablehnung</small>
</div>
<div class="form-group">
<label for="consentOwnerUpload" class="col-form-label">Datei auswählen *</label>
<input type="file" class="form-control" name="consentOwnerUpload" id="consentOwnerUpload" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Abbrechen</button>
<button type="submit" class="btn btn-primary">Hochladen</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
function addOwner() {
function openOwnerUploadModal(oid) {
$('#owner-upload-owner_id').val(oid);
$("#consentOwnerUpload").val("");
$("#owner-upload-name").val("");
$('#ownerUpload').modal();
}
function toggleStatusControl(oid) {
// set select to current status id
$("#status-" + oid + "-input select").val($("#status-" + oid + "-text").data("status-id"));
// toggle controls
$("#status-" + oid + "-text").toggle();
$("#status-" + oid + "-input").toggle();
return false;
}
function saveStatusControl(oid) {
if(!Number.isInteger(oid) || oid < 1) {
return false;
}
var value = $("#status-" + oid + "-input select").val();
$.post("<?=self::getUrl("ConstructionConsentOwner","Api")?>",
{
'do': "updateStatus",
id: oid,
status: value
},
function(success) {
if(success.status == "OK") {
var update = success.result.update;
$("#status-" + update.id + "-statustext").text(update.status_text);
$("#status-" + update.id + "-text")
.addClass("text-success")
.data("status-id", update.status);
setTimeout(() => {
$("#status-" + update.id + "-text").removeClass("text-success")
}, 1500);
toggleStatusControl(oid);
}
},
'json');
}
function toggleResultControl(oid) {
// set select to current result id
$("#result-" + oid + "-input select").val($("#result-" + oid + "-text").data("result-id"));
// toggle controls
$("#result-" + oid + "-text").toggle();
$("#result-" + oid + "-input").toggle();
return false;
}
function saveResultControl(oid) {
if(!Number.isInteger(oid) || oid < 1) {
return false;
}
var value = $("#result-" + oid + "-input select").val();
$.post("<?=self::getUrl("ConstructionConsentOwner","Api")?>",
{
'do': "updateResult",
id: oid,
result: value
},
function(success) {
if(success.status == "OK") {
var update = success.result.update;
$("#result-" + update.id + "-resulttext").text(update.result_text);
$("#result-" + update.id + "-text")
.addClass("text-success")
.data("result-id", update.result);
setTimeout(() => {
$("#result-" + update.id + "-text").removeClass("text-success")
}, 1500);
toggleResultControl(update.id);
}
},
'json');
}
</script>

View File

@@ -167,7 +167,7 @@ class ConstructionConsentController extends mfBaseController {
$this->layout()->set("consent", $cc);
//return true;
$filename = $cc->createConsentFormPdf();
$filename = $cc->createConsentFormPdf($owner);
if(!$filename) {
$this->layout()->setFlash("Beim Erstellen des PDFs ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
$this->redirect("ConstructionConsent", "View", ["id" => $cc->id]);

View File

@@ -160,10 +160,10 @@ class ConstructionConsentFile extends mfBaseModel {
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("mailtemplate_id", $filter)) {
$mailtemplate_id = $filter['mailtemplate_id'];
if(is_numeric($mailtemplate_id)) {
$where .= " AND ConstructionConsentFile.`mailtemplate_id` = $mailtemplate_id";
if(array_key_exists("constructionconsent_id", $filter)) {
$constructionconsent_id = $filter['constructionconsent_id'];
if(is_numeric($constructionconsent_id)) {
$where .= " AND ConstructionConsentFile.`constructionconsent_id` = $constructionconsent_id";
}
}

View File

@@ -2,10 +2,24 @@
class ConstructionConsentOwner extends mfBaseModel {
private $consent;
private $files;
private $result_file;
public function getProperty($name) {
if($this->$name == null) {
if($name == "files") {
if(!$this->id) return null;
$files = ConstructionConsentOwnerFile::search(["constructionconsentowner_id" => $this->id]);
if(count($files)) {
foreach($files as $file) {
$this->files[$file->filename] = $file;
}
}
return $this->files;
}
if($name == "consent") {
$consent = new ConstructionConsent($this->constructionconsent_id);
if($consent->id) {

View File

@@ -16,6 +16,44 @@ class ConstructionConsentOwnerController extends mfBaseController
}
}
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;
@@ -67,4 +105,85 @@ class ConstructionConsentOwnerController extends mfBaseController
$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" => ""]];
}
}
}

View File

@@ -0,0 +1,180 @@
<?php
class ConstructionConsentOwnerFile extends mfBaseModel {
private $file;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-" . $this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-" . $this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name . "_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-" . $this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-" . $this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(array $data) {
$model = new ConstructionConsentOwnerFile();
$table_fields = [
"constructionconsentowner_id", "file_id", "filename",
"create_by", "edit_by", "create", "edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ConstructionConsentOwnerFile", "*", "1 = 1 ORDER BY file_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentOwnerFile($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ConstructionConsentOwnerFile", "*", "$where ORDER BY file_id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentOwnerFile($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM `ConstructionConsentOwnerFile`
WHERE $where
";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentOwnerFile.* FROM `ConstructionConsentOwnerFile`
WHERE $where
ORDER BY file_id
";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT " . $limit['start'] . ", " . $limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT " . $limit['count'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentOwnerFile($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("constructionconsentowner_id", $filter)) {
$constructionconsentowner_id = $filter['constructionconsentowner_id'];
if(is_numeric($constructionconsentowner_id)) {
$where .= " AND ConstructionConsentOwnerFile.`constructionconsentowner_id` = $constructionconsentowner_id";
}
}
if(array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if(is_numeric($file_id)) {
$where .= " AND ConstructionConsentOwnerFile.`file_id` = $file_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class CreateContructionConsentOwnerFile extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("ConstructionConsentOwnerFile");
$table->addColumn("constructionconsentowner_id", "integer", ["null" => false]);
$table->addColumn("file_id", "integer", ["null" => false]);
$table->addColumn("filename", "string", ["null" => false]);
$table->addColumn("create_by", "integer", ["null" => false]);
$table->addColumn("edit_by", "integer", ["null" => false]);
$table->addColumn("create", "integer", ["null" => false]);
$table->addColumn("edit", "integer", ["null" => false]);
$table->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("ConstructionConsentOwnerFile")->drop()->save();
}
if($this->getEnvironment() == "addressdb") {
}
}
}