diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/frontend/application.xml | 2 | ||||
-rw-r--r-- | app/frontend/mail/MailModule.php | 95 | ||||
-rw-r--r-- | app/frontend/mail/MailTemplate.php | 9 | ||||
-rw-r--r-- | app/frontend/mail/MailTemplateTranslator.php | 80 | ||||
-rw-r--r-- | app/frontend/mail/config.xml | 9 | ||||
-rw-r--r-- | app/frontend/mail/templates/.empty | 0 |
6 files changed, 195 insertions, 0 deletions
diff --git a/app/frontend/application.xml b/app/frontend/application.xml index 6937e53..612e5a1 100644 --- a/app/frontend/application.xml +++ b/app/frontend/application.xml @@ -17,6 +17,8 @@ <include file="Application.pages.config" /> <include file="Application.controls.config" /> + <include file="Application.mail.config" /> + <include file="Application.events.config" /> <modules> diff --git a/app/frontend/mail/MailModule.php b/app/frontend/mail/MailModule.php new file mode 100644 index 0000000..5f7749b --- /dev/null +++ b/app/frontend/mail/MailModule.php @@ -0,0 +1,95 @@ +<?php + +Prado::using('System.I18N.Globalization'); +Prado::using('Application.mail.MailTemplate'); +Prado::using('Application.mail.MailTemplateTranslator'); + +class MailModule extends TModule { + + private $_templatePath; + private static $_templateExtension = 'html'; + private $_templateCacheSubpath = 'mail'; + private static $_templateTranslationCatalogue = 'messages'; + + public function setTemplatePath($templatePath) { + $this->_templatePath = Prado::getPathOfNamespace( + TPropertyValue::ensureString($templatePath) + ); + } + + public function getTemplatePath() { + if (!file_exists($this->_templatePath) + || !is_dir($this->_templatePath)) { + throw new TConfigurationException( + Prado::localize( + 'Invalid mail template path' + ) + ); + } + return $this->_templatePath; + } + + public function setCachePath($cachePath) { + $this->templateCacheSubpath = TPropertyValue::ensureString($cachePath); + } + + public function getCachePath() { + $cachePath = $this->getApplication()->getRuntimePath() + . DIRECTORY_SEPARATOR + . $this->_templateCacheSubpath; + if (!file_exists($cachePath)) { + mkdir($cachePath); + } else { + if (!is_dir($cachePath) + || !is_writable($cachePath)) { + throw new TConfigurationException( + Prado::localize( + 'Template cache path must be a writable directory' + ) + ); + } + } + return $cachePath; + } + + public function setTemplateExtension($ext) { + self::$_templateExtension = TPropertyValue::ensureString($ext); + } + + public function getTemplateExtension() { + return self::$_templateExtension; + } + + public function setTranslationCatalogue($ext) { + self::$_templateTranslationCatalogue = TPropertyValue::ensureString($ext); + } + + public function getTranslationCatalogue() { + return self::$_templateTranslationCatalogue; + } + + private static $_translator; + + private static function _getTranslator() { + if (!isset(self::$_translator)) { + self::$_translator = new MailTemplateTranslator( + self::$_templateTranslationCatalogue + ); + } + return self::$_translator; + } + + public function getTemplate($template, $language=NULL) { + $template = new MailTemplate($template . '.' . $this->TemplateExtension); + $template->setOutputMode(PHPTAL::HTML5); + $template->setTemplateRepository($this->_templatePath); + $template->setPhpCodeDestination($this->CachePath); + $translator = self::_getTranslator(); + $translator->setLanguage($language); + $template->setTranslator($translator); + return $template; + } + +} + +?> diff --git a/app/frontend/mail/MailTemplate.php b/app/frontend/mail/MailTemplate.php new file mode 100644 index 0000000..ab5c9a5 --- /dev/null +++ b/app/frontend/mail/MailTemplate.php @@ -0,0 +1,9 @@ +<?php + +Prado::using('Lib.phptal.PHPTAL'); + +class MailTemplate extends PHPTAL { + +} + +?> diff --git a/app/frontend/mail/MailTemplateTranslator.php b/app/frontend/mail/MailTemplateTranslator.php new file mode 100644 index 0000000..0e9d7c9 --- /dev/null +++ b/app/frontend/mail/MailTemplateTranslator.php @@ -0,0 +1,80 @@ +<?php + +Prado::using('Lib.phptal.PHPTAL.TranslationService'); + +class MailTemplateTranslator implements PHPTAL_TranslationService { + + private $_module; + private $_catalogue = 'messages'; + private $_charset; + private $_formatter; + private $_source; + + public function __construct($catalogue=NULL) { + $this->_module = Prado::getApplication()->getGlobalization(FALSE); + $this->_catalogue = $catalogue; + } + + private $_language = NULL; + + function setLanguage(/* ... */) { + $this->_source = NULL; + $langs = array_filter(func_get_args()); + if ($langs) { + $availableLangs = $this->_module->AllowedCultures; + foreach ($langs as $lang) { + if (in_array($lang, $availableLangs)) { + $this->_language = $lang; + return; + } + } + throw new TInvalidDataValueException( + Prado::localize( + 'Languages [{lang}] not available', + ['lang' => implode(',', $langs)] + ) + ); + } else { + $this->_language = NULL; + } + } + + function setEncoding($encoding) { + $this->_charset = $encoding; + } + + function useDomain($domain) { + $this->_catalogue = $domain; + } + + private $_variables = []; + + function setVar($key, $value) { + $this->_variables[$key] = $value; + } + + function translate($key, $htmlescape=TRUE) { + $key = preg_replace('/\$(\{.*\})/', '\1', $key); + if ($this->_language) { + if (!$this->_source) { + $appConfig = Prado::getApplication()->getGlobalization()->getTranslationConfiguration(); + $this->_source = MessageSource::factory($appConfig['type'], $appConfig['source'], $appConfig['filename']); + $this->_source->setCulture($this->_language); + } + $this->_formatter = new MessageFormat( + $this->_source, + $this->_charset ?: Prado::getApplication()->getGlobalization()->getCharset() + ); + $variables = []; + foreach ($this->_variables as $varKey => $value) { + $variables['{' . $varKey . '}'] = $value; + } + return $this->_formatter->format($key, $variables, $this->_catalogue); + } else { + return Prado::localize($key, $this->_variables); + } + } + +} + +?> diff --git a/app/frontend/mail/config.xml b/app/frontend/mail/config.xml new file mode 100644 index 0000000..d7d0e64 --- /dev/null +++ b/app/frontend/mail/config.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<configuration> + <modules> + <module id="mail" + class="Application.mail.MailModule" + TemplatePath="Application.mail.templates" + TranslationCatalogue="messages" /> + </modules> +</configuration> diff --git a/app/frontend/mail/templates/.empty b/app/frontend/mail/templates/.empty new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/frontend/mail/templates/.empty |