Merge branch 'fronkdev' into 'master'

Added Mailtemplate::renderBody() function

See merge request fronk/thetool!631
This commit is contained in:
Frank Schubert
2024-09-17 16:19:37 +00:00
4 changed files with 57 additions and 6 deletions

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