Merge branch 'ConstructionConsent/add-owner-number' into 'master'

Added new ConstructionConsent Features

See merge request fronk/thetool!1101
This commit is contained in:
Luca Haid
2025-03-11 13:37:55 +00:00
4 changed files with 101 additions and 67 deletions
@@ -84,42 +84,34 @@ class ConstructionConsent extends mfBaseModel {
} catch(Exception $e) {
$this->log->debug($e->getTraceAsString());
}
return true;
}
public function createConsentFormPdf(ConstructionConsentOwner $owner = null) : ?string {
if($owner) {
$owners = [$owner];
} else {
if(!is_array($this->getProperty("owners")) || !count($this->getProperty("owners"))) {
return null;
}
$owners = $this->owners;
}
$pdf_vars = [
"consent" => $this,
"owners" => $owners
];
$footer_text = $this->footer_text;
$footer_font = $this->footer_font;
$footer_size = $this->footer_size;
$pdf = new PdfForm("ConstructionConsent/Consentform.pdf", $pdf_vars);
//$wkOpts = "--footer-center '$footer_text' --footer-font-name '$footer_font' --footer-font-size '$footer_size'";
$wkOpts = "--footer-html '".$_SERVER["HTTP_HOST"].MFFANCYBASEURL."/assets/pdf/ConstructionConsent/Consentform.footer.html' --margin-bottom 15mm --margin-top 10mm";
$filename = $pdf->render($wkOpts);
if(!file_exists($filename) || !is_file($filename)) {
public function createConsentFormPdf(array $owners): ?string
{
if (empty($owners)) {
return null;
}
return $filename;
$pdf = new PdfForm("ConstructionConsent/Consentform.pdf", [
'consent' => $this,
'owners' => $owners
]);
$footerFile = BASEDIR . "/public/assets/pdf/ConstructionConsent/Consentform.footer.html";
$wkOpts = sprintf(
"--footer-html '%s' --margin-bottom 15mm --margin-top 10mm --margin-bottom 15mm --margin-top 10mm",
$footerFile
);
$filename = $pdf->render($wkOpts);
return @is_file($filename) ? $filename : null;
}
public function getProperty($name) {
if($this->$name == null) {
@@ -140,45 +140,69 @@ class ConstructionConsentController extends mfBaseController {
$this->addAction();
}
protected function download() {
//var_dump($r->get());exit;
protected function generatePdf(array $owners, ConstructionConsent $consent): ?string
{
if (empty($owners)) return null;
$this->layout()->setTemplate("ConstructionConsent/Consentform.pdf");
$this->layout()->set("ressourcePathPrefix", MFFANCYBASEURL);
$this->layout()->set("owners", $owners);
$this->layout()->set("consent", $consent);
return $consent->createConsentFormPdf($owners); // Might need adjustment for multiple
}
protected function downloadAction()
{
$owner_id = $this->request->owner_id;
if (!is_numeric($owner_id) || $owner_id < 1) {
if (!is_numeric($owner_id) || $owner_id < 1 || !($owner = new ConstructionConsentOwner($owner_id))->id) {
$this->layout()->setFlash("Besitzer nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$owner = new ConstructionConsentOwner($owner_id);
if (!$owner->id) {
$this->layout()->setFlash("Besitzer nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$cc = $owner->consent;
if(!$cc) {
if (!($cc = $owner->consent)) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden.", "error");
$this->redirect("ConstructionConsent");
}
$this->layout()->setTemplate("ConstructionConsent/Consentform.pdf");
$this->layout()->set("ressourcePathPrefix", MFFANCYBASEURL);
$this->layout()->set("owners", [$owner]);
$this->layout()->set("consent", $cc);
//return true;
$filename = $cc->createConsentFormPdf($owner);
if(!$filename) {
$this->layout()->setFlash("Beim Erstellen des PDFs ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
if (!($filename = $this->generatePdf([$owner], $cc))) {
$this->layout()->setFlash("PDF-Erstellung fehlgeschlagen", "error");
$this->redirect("ConstructionConsent", "View", ["id" => $cc->id]);
}
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="Zustimmungserklärung-'.$cc->id.'-'.$owner_id.'.pdf"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: ' . mime_content_type($filename));
header("Content-Length: " . filesize($filename));
$this->sendPdfResponse($filename, "Zustimmungserklärung-{$cc->id}-{$owner_id}.pdf");
}
protected function downloadMultipleAction()
{
// Reuse index filter logic
$filter = $this->getPreparedFilter($this->request->filter);
$owners = [];
foreach (ConstructionConsent::search($filter, []) as $consent) {
$owners = array_merge($owners, $consent->owners ?: []);
}
if (empty($owners)) {
$this->layout()->setFlash("Keine Besitzer gefunden", "error");
$this->redirect("ConstructionConsent");
}
// Use first consent as base - might need adjustment for your use case
if (!($filename = $this->generatePdf($owners, $owners[0]->consent))) {
$this->layout()->setFlash("PDF-Erstellung fehlgeschlagen", "error");
$this->redirect("ConstructionConsent");
}
$this->sendPdfResponse($filename, "Zustimmungserklärungen-".date('Y-m-d').".pdf");
}
private function sendPdfResponse(string $filename, string $downloadName): void
{
header('Content-Type: ' . mime_content_type($filename));
header('Content-disposition: attachment; filename="' . $downloadName . '"');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
}