d4472f7bcc7781b3a61c4a7d697053026759ab47
[core.git] / inc / main / classes / mailer / class_BaseMailer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Mailer;
4
5 // Load framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
7
8 /**
9  * A general mailer class for all other mailers
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class BaseMailer extends BaseFrameworkSystem {
31         /**
32          * Template name
33          */
34         private $templateName = '';
35
36         /**
37          * Protected constructor
38          *
39          * @param       $className      Name of the class
40          * @return      void
41          */
42         protected function __construct ($className) {
43                 // Call parent constructor
44                 parent::__construct($className);
45         }
46
47         /**
48          * Loads a text or HTML template depending on configuration into the template engine
49          *
50          * @param       $templateName   Name of the template we shall load
51          * @return      void
52          */
53         protected final function loadTemplate ($templateName) {
54                 // Set template name
55                 $this->setTemplateName($templateName);
56
57                 // Get configuration entry
58                 $templatePrefix = $this->getConfigInstance()->getConfigEntry('email_tpl_' . $templateName);
59
60                 // Load this email template
61                 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
62         }
63
64         /**
65          * Adds a user class to the recipient list for current template
66          *
67          * @param       $userInstance   An instance of a user class
68          * @return      void
69          */
70         public function addRecipientByUserInstance (ManageableMember $userInstance) {
71                 // Get template name
72                 $templateName = $this->getTemplateName();
73
74                 // Is the list initialized?
75                 $this->pushValueToGenericArrayKey('recipients', $templateName, 'recipients', $userInstance);
76         }
77
78         /**
79          * Adds a template variable (just the name) to the recipient list in given section of current template
80          *
81          * @param       $section                Section can be 'config' or "value" currently
82          * @param       $variableName   Template variable name to add
83          * @return      void
84          */
85         private final function addTemplateVariable ($section, $variableName) {
86                 // Get template name
87                 $templateName = $this->getTemplateName();
88
89                 // Generate full section name
90                 $sectionName = $section . '_vars';
91
92                 // Is the list initialized?
93                 $this->setGenericArrayElement('recipients', $templateName, $sectionName, $variableName, 'OK');
94         }
95
96         /**
97          * Adds a config template variable to the recipient list of current template
98          *
99          * @param       $variableName   Template variable name to add
100          * @return      void
101          */
102         public final function addConfigTemplateVariable ($variableName) {
103                 $this->addTemplateVariable('config', $variableName);
104         }
105
106         /**
107          * Adds a 'value' template variable to the recipient list of current template
108          *
109          * @param       $variableName   Template variable name to add
110          * @return      void
111          */
112         public final function addValueTemplateVariable ($variableName) {
113                 $this->addTemplateVariable('value', $variableName);
114         }
115
116         /**
117          * Adds a value instance for a given variable name. It should be set!
118          *
119          * @param       $variableName   Template variable we want to assign a value instance
120          * @param       $valueInstance  An object instance which can provide "field values"
121          * @return      void
122          */
123         public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
124                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'values', $variableName, $valueInstance);
125         }
126
127         /**
128          * Protected setter for template name
129          *
130          * @param       $templateName   Name of email template
131          * @return      void
132          */
133         public final function setTemplateName ($templateName) {
134                 $this->templateName = (string) $templateName;
135         }
136
137         /**
138          * Protected getter for template name
139          *
140          * @return      $templateName   Name of email template
141          */
142         protected final function getTemplateName () {
143                 return $this->templateName;
144         }
145
146         /**
147          * Setter for subject line
148          *
149          * @param       $subjectLine    Subject line to set
150          * @return      void
151          */
152         public final function setSubjectLine ($subjectLine) {
153                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'generic', 'subject', $subjectLine);
154         }
155
156         /**
157          * Getter for subject line or null if not found
158          *
159          * @return      $subjectLine    Subject line to set
160          */
161         public final function getSubjectLine () {
162                 // Default subject is null
163                 $subjectLine = NULL;
164
165                 // Get template name
166                 $templateName = $this->getTemplateName();
167
168                 // Does the subject line exist?
169                 if ((!empty($templateName)) && ($this->isGenericArrayElementSet('recipients', $templateName, 'generic', 'subject'))) {
170                         // Then use it
171                         $subjectLine = $this->getGenericArrayElement('recipients', $templateName, 'generic', 'subject');
172                 } // END - if
173
174                 // Return it
175                 return $subjectLine;
176         }
177
178         /**
179          * Use subject line provided by the (XML) template otherwise a subject line must be set
180          *
181          * @return      void
182          */
183         public function useSubjectFromTemplate () {
184                 // Set the subject line
185                 $this->setSubjectLine('{?subject?}');
186         }
187
188         /**
189          * Getter for recipient list array
190          *
191          * @return      $recipientList  Array with reciepients
192          */
193         public final function getRecipientList () {
194                 return $this->getGenericArray('recipients');
195         }
196
197 }