summaryrefslogtreecommitdiff
path: root/app/frontend/mail/MailModule.php
blob: e10c55606c684df6557f24b37f9f408a2f34bcc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

Prado::using('System.I18N.Globalization');
Prado::using('Application.mail.Mailer');
Prado::using('Application.mail.MailTemplate');
Prado::using('Application.mail.MailTemplateTranslator');

class MailModule extends TModule {

    private $_configPath;
    private $_config;

    private $_templatePath;
    private static $_templateExtension = 'html';
    private $_templateCacheSubpath = 'mail';
    private static $_templateTranslationCatalogue = 'messages';

    public function setConfigPath($configPath) {
        $this->_configPath = TPropertyValue::ensureString($configPath);
        $this->_config = json_decode(
            file_get_contents(
                Prado::getPathOfNamespace($this->_configPath, '.json')
            )
        );
    }

    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;
    }

    public function getMailer() {
        if (!$this->_config) {
            throw new TConfigurationException(
                Prado::localize(
                    'Mailer not configured'
                )
            );
        }
        $mailer = new Mailer();
        $mailer->configure($this->_config);
        return $mailer;
    }

}

?>