diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index a585fdc30..7d3643e1b 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -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; }); } diff --git a/modules/system/controllers/MailTemplates.php b/modules/system/controllers/MailTemplates.php index 1a2ac28a8..58cf602e7 100644 --- a/modules/system/controllers/MailTemplates.php +++ b/modules/system/controllers/MailTemplates.php @@ -49,7 +49,7 @@ class MailTemplates extends Controller public function formBeforeSave($model) { - $model->is_custom = true; + $model->is_custom = 1; } public function onTest($recordId) diff --git a/modules/system/models/MailLayout.php b/modules/system/models/MailLayout.php index abcac3e8d..602f5708e 100644 --- a/modules/system/models/MailLayout.php +++ b/modules/system/models/MailLayout.php @@ -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); + } + } diff --git a/modules/system/models/MailTemplate.php b/modules/system/models/MailTemplate.php index b4a006e98..ba8fd484d 100644 --- a/modules/system/models/MailTemplate.php +++ b/modules/system/models/MailTemplate.php @@ -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; } //