mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Apply layout to unregistered mail templates
This commit is contained in:
parent
11cf46008f
commit
7affc4bec8
@ -291,9 +291,8 @@ class ServiceProvider extends ModuleServiceProvider
|
||||
* Override standard Mailer content with template
|
||||
*/
|
||||
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) {
|
||||
if (MailTemplate::addContentToMailer($message, $view, $data)) {
|
||||
return false;
|
||||
}
|
||||
MailTemplate::addContentToMailer($message, $view, $data);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ class MailTemplates extends Controller
|
||||
|
||||
public function formBeforeSave($model)
|
||||
{
|
||||
$model->is_custom = true;
|
||||
$model->is_custom = 1;
|
||||
}
|
||||
|
||||
public function onTest($recordId)
|
||||
|
@ -24,10 +24,27 @@ class MailLayout extends Model
|
||||
'content_html' => 'required',
|
||||
];
|
||||
|
||||
public static $codeCache;
|
||||
|
||||
public function beforeDelete()
|
||||
{
|
||||
if ($this->is_locked) {
|
||||
throw new ApplicationException('Cannot delete this template because it is locked');
|
||||
}
|
||||
}
|
||||
|
||||
public static function listCodes()
|
||||
{
|
||||
if (self::$codeCache !== null) {
|
||||
return self::$codeCache;
|
||||
}
|
||||
|
||||
return self::$codeCache = self::lists('id', 'code');
|
||||
}
|
||||
|
||||
public static function getIdFromCode($code)
|
||||
{
|
||||
return array_get(self::listCodes(), $code);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -85,10 +85,6 @@ class MailTemplate extends Model
|
||||
/*
|
||||
* Create new templates
|
||||
*/
|
||||
if (count($newTemplates)) {
|
||||
$categories = MailLayout::lists('id', 'code');
|
||||
}
|
||||
|
||||
foreach ($newTemplates as $code => $description) {
|
||||
$sections = self::getTemplateSections($code);
|
||||
$layoutCode = array_get($sections, 'settings.layout', 'default');
|
||||
@ -97,7 +93,7 @@ class MailTemplate extends Model
|
||||
$template->code = $code;
|
||||
$template->description = $description;
|
||||
$template->is_custom = 0;
|
||||
$template->layout_id = isset($categories[$layoutCode]) ? $categories[$layoutCode] : null;
|
||||
$template->layout_id = MailLayout::getIdFromCode($layoutCode);
|
||||
$template->forceSave();
|
||||
}
|
||||
}
|
||||
@ -105,29 +101,44 @@ class MailTemplate extends Model
|
||||
public function afterFetch()
|
||||
{
|
||||
if (!$this->is_custom) {
|
||||
$sections = self::getTemplateSections($this->code);
|
||||
$this->content_html = $sections['html'];
|
||||
$this->content_text = $sections['text'];
|
||||
$this->subject = array_get($sections, 'settings.subject', 'No subject');
|
||||
$this->fillFromView();
|
||||
}
|
||||
}
|
||||
|
||||
public function fillFromView()
|
||||
{
|
||||
$sections = self::getTemplateSections($this->code);
|
||||
$this->content_html = $sections['html'];
|
||||
$this->content_text = $sections['text'];
|
||||
$this->subject = array_get($sections, 'settings.subject', 'No subject');
|
||||
|
||||
$layoutCode = array_get($sections, 'settings.layout', 'default');
|
||||
$this->layout_id = MailLayout::getIdFromCode($layoutCode);
|
||||
}
|
||||
|
||||
protected static function getTemplateSections($code)
|
||||
{
|
||||
return MailParser::parse(File::get(View::make($code)->getPath()));
|
||||
}
|
||||
|
||||
public static function findOrMakeTemplate($code)
|
||||
{
|
||||
if (!$template = self::whereCode($code)->first()) {
|
||||
$template = new self;
|
||||
$template->code = $code;
|
||||
$template->fillFromView();
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
public static function addContentToMailer($message, $code, $data)
|
||||
{
|
||||
if (!isset(self::$cache[$code])) {
|
||||
if (!$template = self::whereCode($code)->first()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self::$cache[$code] = $template;
|
||||
if (isset(self::$cache[$code])) {
|
||||
$template = self::$cache[$code];
|
||||
}
|
||||
else {
|
||||
$template = self::$cache[$code];
|
||||
self::$cache[$code] = $template = self::findOrMakeTemplate($code);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -164,8 +175,6 @@ class MailTemplate extends Model
|
||||
|
||||
$message->addPart($text, 'text/plain');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user