From 5a4e2b6b117bd9ceb324002121a5ad92d508020e Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 14 Jul 2017 16:20:53 +1000 Subject: [PATCH] Email templates now use Markdown Move mail template management to MailManager --- modules/backend/ServiceProvider.php | 4 +- modules/system/ServiceProvider.php | 3 +- modules/system/classes/MailManager.php | 159 ++++++++++++++++++ modules/system/classes/MarkupManager.php | 2 +- modules/system/models/MailTemplate.php | 138 +-------------- .../system/models/mailtemplate/fields.yaml | 2 +- 6 files changed, 172 insertions(+), 136 deletions(-) create mode 100644 modules/system/classes/MailManager.php diff --git a/modules/backend/ServiceProvider.php b/modules/backend/ServiceProvider.php index 908e120c2..50874b5a0 100644 --- a/modules/backend/ServiceProvider.php +++ b/modules/backend/ServiceProvider.php @@ -5,7 +5,7 @@ use Backend; use BackendMenu; use BackendAuth; use Backend\Classes\WidgetManager; -use System\Models\MailTemplate; +use System\Classes\MailManager; use System\Classes\CombineAssets; use System\Classes\SettingsManager; use October\Rain\Support\ModuleServiceProvider; @@ -51,7 +51,7 @@ class ServiceProvider extends ModuleServiceProvider */ protected function registerMailer() { - MailTemplate::registerCallback(function ($template) { + MailManager::instance()->registerCallback(function ($template) { $template->registerMailTemplates([ 'backend::mail.invite' => 'Invitation for newly created administrators.', 'backend::mail.restore' => 'Password reset instructions for backend-end administrators.', diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 32f220ed8..336beba4e 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -11,6 +11,7 @@ use BackendMenu; use BackendAuth; use Twig_Environment; use Twig_Loader_String; +use System\Classes\MailManager; use System\Classes\ErrorHandler; use System\Classes\MarkupManager; use System\Classes\PluginManager; @@ -299,7 +300,7 @@ class ServiceProvider extends ModuleServiceProvider * Override standard Mailer content with template */ Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) { - MailTemplate::addContentToMailer($message, $view, $data); + MailManager::instance()->addContentToMailer($message, $view, $data); return false; }); } diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php new file mode 100644 index 000000000..7e5667417 --- /dev/null +++ b/modules/system/classes/MailManager.php @@ -0,0 +1,159 @@ +templateCache[$code])) { + $template = $this->templateCache[$code]; + } + else { + $this->templateCache[$code] = $template = MailTemplate::findOrMakeTemplate($code); + } + + /* + * Inject global view variables + */ + $globalVars = ViewHelper::getGlobalVars(); + if (!empty($globalVars)) { + $data = (array) $data + $globalVars; + } + + /* + * Subject + */ + $customSubject = $message->getSwiftMessage()->getSubject(); + if (empty($customSubject)) { + $message->subject(Twig::parse($template->subject, $data)); + } + + /* + * HTML contents + */ + $templateHtml = Markdown::parse($template->content_html); + + $html = Twig::parse($templateHtml, $data); + if ($template->layout) { + $html = Twig::parse($template->layout->content_html, [ + 'content' => $html, + 'css' => $template->layout->content_css + ] + (array) $data); + } + + $message->setBody($html, 'text/html'); + + /* + * Text contents + */ + if (strlen($template->content_text)) { + $text = Twig::parse($template->content_text, $data); + if ($template->layout) { + $text = Twig::parse($template->layout->content_text, [ + 'content' => $text + ] + (array) $data); + } + + $message->addPart($text, 'text/plain'); + } + } + + // + // Registration + // + + /** + * Loads registered mail templates from modules and plugins + * @return void + */ + public function loadRegisteredTemplates() + { + foreach ($this->callbacks as $callback) { + $callback($this); + } + + $plugins = PluginManager::instance()->getPlugins(); + foreach ($plugins as $pluginId => $pluginObj) { + $templates = $pluginObj->registerMailTemplates(); + if (!is_array($templates)) { + continue; + } + + $this->registerMailTemplates($templates); + } + } + + /** + * Returns a list of the registered templates. + * @return array + */ + public function listRegisteredTemplates() + { + if ($this->registeredTemplates === null) { + $this->loadRegisteredTemplates(); + } + + return $this->registeredTemplates; + } + + /** + * Registers a callback function that defines mail templates. + * The callback function should register templates by calling the manager's + * registerMailTemplates() function. Thi instance is passed to the + * callback function as an argument. Usage: + * + * MailManager::registerCallback(function($manager) { + * $manager->registerMailTemplates([...]); + * }); + * + * @param callable $callback A callable function. + */ + public function registerCallback(callable $callback) + { + $this->callbacks[] = $callback; + } + + /** + * Registers mail views and manageable templates. + */ + public function registerMailTemplates(array $definitions) + { + if (!$this->registeredTemplates) { + $this->registeredTemplates = []; + } + + $this->registeredTemplates = array_merge($this->registeredTemplates, $definitions); + } +} diff --git a/modules/system/classes/MarkupManager.php b/modules/system/classes/MarkupManager.php index 6d63463f9..331cfc94e 100644 --- a/modules/system/classes/MarkupManager.php +++ b/modules/system/classes/MarkupManager.php @@ -10,7 +10,7 @@ use System\Classes\PluginManager; /** * This class manages Twig functions, token parsers and filters. * - * @package october\cms + * @package october\system * @author Alexey Bobkov, Samuel Georges */ class MarkupManager diff --git a/modules/system/models/MailTemplate.php b/modules/system/models/MailTemplate.php index 8363a3284..6fbdfc038 100644 --- a/modules/system/models/MailTemplate.php +++ b/modules/system/models/MailTemplate.php @@ -2,12 +2,10 @@ use App; use File; -use Twig; use View; use Model; +use System\Classes\MailManager; use October\Rain\Mail\MailParser; -use System\Classes\PluginManager; -use System\Helpers\View as ViewHelper; /** * Mail template @@ -35,25 +33,13 @@ class MailTemplate extends Model 'layout' => MailLayout::class ]; - /** - * @var array A cache of customised mail templates. - */ - protected static $cache = []; - - /** - * @var array Cache of registration callbacks. - */ - private static $callbacks = []; - - protected static $registeredTemplates; - /** * Returns an array of template codes and descriptions. * @return array */ public static function listAllTemplates() { - $fileTemplates = (array) self::make()->listRegisteredTemplates(); + $fileTemplates = (array) MailManager::instance()->listRegisteredTemplates(); $dbTemplates = (array) self::lists('description', 'code'); $templates = $fileTemplates + $dbTemplates; ksort($templates); @@ -66,7 +52,7 @@ class MailTemplate extends Model */ public static function syncAll() { - $templates = self::make()->listRegisteredTemplates(); + $templates = MailManager::instance()->listRegisteredTemplates(); $dbTemplates = self::lists('is_custom', 'code'); $newTemplates = array_diff_key($templates, $dbTemplates); @@ -133,123 +119,13 @@ class MailTemplate extends Model return $template; } - public static function addContentToMailer($message, $code, $data) - { - if (isset(self::$cache[$code])) { - $template = self::$cache[$code]; - } - else { - self::$cache[$code] = $template = self::findOrMakeTemplate($code); - } - - /* - * Inject global view variables - */ - $globalVars = ViewHelper::getGlobalVars(); - if (!empty($globalVars)) { - $data = (array) $data + $globalVars; - } - - /* - * Subject - */ - $customSubject = $message->getSwiftMessage()->getSubject(); - if (empty($customSubject)) { - $message->subject(Twig::parse($template->subject, $data)); - } - - /* - * HTML contents - */ - $html = Twig::parse($template->content_html, $data); - if ($template->layout) { - $html = Twig::parse($template->layout->content_html, [ - 'content' => $html, - 'css' => $template->layout->content_css - ] + (array) $data); - } - - $message->setBody($html, 'text/html'); - - /* - * Text contents - */ - if (strlen($template->content_text)) { - $text = Twig::parse($template->content_text, $data); - if ($template->layout) { - $text = Twig::parse($template->layout->content_text, [ - 'content' => $text - ] + (array) $data); - } - - $message->addPart($text, 'text/plain'); - } - } - - // - // Registration - // - /** - * Loads registered mail templates from modules and plugins - * @return void - */ - public function loadRegisteredTemplates() - { - foreach (static::$callbacks as $callback) { - $callback($this); - } - - $plugins = PluginManager::instance()->getPlugins(); - foreach ($plugins as $pluginId => $pluginObj) { - $templates = $pluginObj->registerMailTemplates(); - if (!is_array($templates)) { - continue; - } - - $this->registerMailTemplates($templates); - } - } - - /** - * Returns a list of the registered templates. - * @return array - */ - public function listRegisteredTemplates() - { - if (self::$registeredTemplates === null) { - $this->loadRegisteredTemplates(); - } - - return self::$registeredTemplates; - } - - /** - * Registers a callback function that defines mail templates. - * The callback function should register templates by calling the manager's - * registerMailTemplates() function. Thi instance is passed to the - * callback function as an argument. Usage: - * - * MailTemplate::registerCallback(function($template){ - * $template->registerMailTemplates([...]); - * }); - * - * @param callable $callback A callable function. + * @deprecated see System\Classes\MailManager::registerCallback + * Remove if year >= 2019 */ public static function registerCallback(callable $callback) { - self::$callbacks[] = $callback; - } - - /** - * Registers mail views and manageable templates. - */ - public function registerMailTemplates(array $definitions) - { - if (!static::$registeredTemplates) { - static::$registeredTemplates = []; - } - - static::$registeredTemplates = array_merge(static::$registeredTemplates, $definitions); + traceLog('MailTemplate::registerCallback is deprecated, use System\Classes\MailManager::registerCallback instead'); + MailManager::instance()->registerCallback($callback); } } diff --git a/modules/system/models/mailtemplate/fields.yaml b/modules/system/models/mailtemplate/fields.yaml index 1e4235310..6551900fd 100644 --- a/modules/system/models/mailtemplate/fields.yaml +++ b/modules/system/models/mailtemplate/fields.yaml @@ -32,7 +32,7 @@ secondaryTabs: fields: content_html: - type: codeeditor + type: markdown size: giant language: html tab: system::lang.mail_templates.content_html