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