111 lines
3.6 KiB
PHP
111 lines
3.6 KiB
PHP
<?php
|
|
|
|
class Mailtemplate extends mfBaseModel {
|
|
private $files;
|
|
private $creator;
|
|
private $editor;
|
|
|
|
|
|
public function getVariableReplacedSubject($replaceVars) {
|
|
return $this->replaceVariables($this->subject, $replaceVars);
|
|
}
|
|
|
|
public function getVariableReplacedBody($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 $text;
|
|
|
|
foreach($replaceVars as $key => $replacement) {
|
|
$key = strtoupper($key);
|
|
$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) {
|
|
|
|
if($name == "files") {
|
|
$files = MailtemplateFileModel::search(["mailtemplate_id" => $this->id]);
|
|
if(!count($files)) {
|
|
return [];
|
|
}
|
|
$this->files = $files;
|
|
return $this->files;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |