From ade1e0897f907426e851f3632e95fb1cac31f334 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 17 Sep 2024 18:18:38 +0200 Subject: [PATCH] Added Mailtemplate::renderBody() function --- Layout/default/Mailtemplate/Index.php | 15 ++++++- application/Mailtemplate/Mailtemplate.php | 42 +++++++++++++++++-- .../Mailtemplate/MailtemplateController.php | 4 ++ .../20240822153505_create_mailtemplate.php | 2 +- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Layout/default/Mailtemplate/Index.php b/Layout/default/Mailtemplate/Index.php index 6e9b2cb24..01d51ff3e 100644 --- a/Layout/default/Mailtemplate/Index.php +++ b/Layout/default/Mailtemplate/Index.php @@ -41,6 +41,15 @@ +
+ + +
+
@@ -66,6 +75,7 @@ + @@ -77,13 +87,14 @@ + - - + +
Typ Code Name Beschreibung
">">
code?>
name?> description?> subject?> files)?>edit)?> (editor->name?>)create)?> (creator->name?>)create)?> (editor->name?>)edit)?> (creator->name?>) $template->id])?>"> $template->id])?>" class="text-danger" onclick="if(!confirm('Emailtemplate wirklich löschen?')) return false;" title="Emailtemplate Löschen"> diff --git a/application/Mailtemplate/Mailtemplate.php b/application/Mailtemplate/Mailtemplate.php index 4031b3ef8..670b5080c 100644 --- a/application/Mailtemplate/Mailtemplate.php +++ b/application/Mailtemplate/Mailtemplate.php @@ -11,19 +11,55 @@ class Mailtemplate extends mfBaseModel { } public function getVariableReplacedBody($replaceVars) { - return $this->replaceVariables($this->body, $replaceVars); + $body = $this->body_html ? $this->body_html : $this->body_text; + return $this->replaceVariables($body, $replaceVars); } private function replaceVariables($text, $replaceVars) { - if(!is_array($replaceVars)) return false; + if(!is_array($replaceVars)) return $text; foreach($replaceVars as $key => $replacement) { - $body = str_replace("{{".$key."}}", $replacement, $body); + $text = str_replace("{{".$key."}}", $replacement, $text); + } + + return $text; + } + + public function renderBody($body_replacers = null) { + if(is_array($body_replacers)) { + $body = $this->getVariableReplacedBody($body_replacers); + } else { + $body = $this->body_html ? $this->body_html : $this->body_text; + } + + // handle EMBED Tokens + $m = []; + if(preg_match_all('/{{EMBED:([^}]+)}}/i', $body, $m)) { + if(array_key_exists(1, $m)) { + foreach($m[1] as $match) { + $tpl_name = $match; + $tpl = MailtemplateModel::getFirst(["code" => $tpl_name]); + if(!$tpl) continue; + $tpl_body = $tpl->body_html ? $tpl->body_html : $tpl->body_text; + $tpl_replace = $this->replaceVariables($tpl_body, $body_replacers); + $body = $this->replaceVariables($body, ["EMBED:$tpl_name" => $tpl_replace]); + } + } } return $body; } + public function renderSubject($subject_replacers = null) { + if(is_array($subject_replacers)) { + $subject = $this->getVariableReplacedSubject($subject_replacers); + } else { + $subject = $this->subject; + } + + return $subject; + } + public function getProperty($name) { if($this->$name == null) { diff --git a/application/Mailtemplate/MailtemplateController.php b/application/Mailtemplate/MailtemplateController.php index 7b8bd3f6f..a4e571295 100644 --- a/application/Mailtemplate/MailtemplateController.php +++ b/application/Mailtemplate/MailtemplateController.php @@ -53,6 +53,10 @@ class MailtemplateController extends mfBaseController { private function getPreparedFilter($filter) { $new_filter = []; + if(array_key_exists("is_include", $filter) && !is_numeric($filter["is_include"])) { + unset($filter["is_include"]); + } + foreach($filter as $name => $value) { $new_filter[$name] = $value; } diff --git a/db/migrations/20240822153505_create_mailtemplate.php b/db/migrations/20240822153505_create_mailtemplate.php index 23cd78fbd..09da7a1f4 100644 --- a/db/migrations/20240822153505_create_mailtemplate.php +++ b/db/migrations/20240822153505_create_mailtemplate.php @@ -13,7 +13,7 @@ final class CreateMailtemplate extends AbstractMigration $table->addColumn("name", "string", ["null" => false, "limit" => 255]); $table->addColumn("code", "string", ["null" => false, "limit" => 64]); $table->addColumn("description", "string", ["null" => true, "default" => null, "limit" => 1024]); - $table->addColumn("subject", "string", ["null" => false, "limit" => 255]); + $table->addColumn("subject", "string", ["null" => true, "default" => null, "limit" => 255]); $table->addColumn("body_text", "text", ["null" => true, "default" => null]); $table->addColumn("body_html", "text", ["null" => true, "default" => null]); $table->addColumn("note", "text", ["null" => true, "default" => null]);