Added Mailtemplate::renderBody() function

This commit is contained in:
Frank Schubert
2024-09-17 18:18:38 +02:00
parent 0168c951e9
commit ade1e0897f
4 changed files with 57 additions and 6 deletions

View File

@@ -41,6 +41,15 @@
<input type="text" class="form-control" name="filter[subject]" id="filter_betreff" value="<?=$filter['subject']?>" />
</div>
<div class="col-1">
<label class="form-label" for="filter_is_include">Betreff</label>
<select class="form-control" name="filter[is_include]" id="filter_is_include">
<option></option>
<option value="0" <?=(array_key_exists("is_include", $filter) && $filter["is_include"] == 0) ? "selected='selected'" : ""?>>Dokumente</option>
<option value="1" <?=(array_key_exists("is_include", $filter) && $filter["is_include"] == 1) ? "selected='selected'" : ""?>>Templates</option>
</select>
</div>
</div>
<div class="row mt-2">
@@ -66,6 +75,7 @@
<table class="table table-striped table-hover">
<tr>
<th>Typ</th>
<th>Code</th>
<th>Name</th>
<th>Beschreibung</th>
@@ -77,13 +87,14 @@
</tr>
<?php foreach($templates as $template): ?>
<tr>
<td title="<?=($template->is_include) ? "Template" : "Dokument"?>"><i class="far fa-fw <?=($template->is_include) ? "fa-grip-lines text-info" : "fa-file-lines text-primary"?>"></i></td>
<td class="text-monospace text-monospace-valign-fix text-pink " style="max-width: 15vh"><div><?=$template->code?></div></td>
<td><?=$template->name?></td>
<td><?=$template->description?></td>
<td><?=$template->subject?></td>
<td><?=count($template->files)?></td>
<td><?=date("d.m.Y H:i",$template->edit)?> (<?=$template->editor->name?>)</td>
<td><?=date("d.m.Y H:i",$template->create)?> (<?=$template->creator->name?>)</td>
<td><?=date("d.m.Y H:i",$template->create)?> (<?=$template->editor->name?>)</td>
<td><?=date("d.m.Y H:i",$template->edit)?> (<?=$template->creator->name?>)</td>
<td style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
<a href="<?=self::getUrl("Mailtemplate", "edit", ["id" => $template->id])?>"><i class="far fa-edit" title="Emailtemplate bearbeiten"></i></a>
<a href="<?=self::getUrl("Mailtemplate", "delete", ["id" => $template->id])?>" class="text-danger" onclick="if(!confirm('Emailtemplate wirklich löschen?')) return false;" title="Emailtemplate Löschen"><i class="fas fa-trash"></i></a>

View File

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

View File

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

View File

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