update copyright as changes will happen this year
[core.git] / inc / main / classes / 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 - 2017 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                 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
56         }
57
58         /**
59          * Adds a user class to the recipient list for current template
60          *
61          * @param       $userInstance   An instance of a user class
62          * @return      void
63          */
64         public function addRecipientByUserInstance (ManageableMember $userInstance) {
65                 // Get template name
66                 $templateName = $this->getTemplateName();
67
68                 // Is the list initialized?
69                 $this->pushValueToGenericArrayKey('recipients', $templateName, 'recipients', $userInstance);
70         }
71
72         /**
73          * Adds a template variable (just the name) to the recipient list in given section of current template
74          *
75          * @param       $section                Section can be 'config' or "value" currently
76          * @param       $variableName   Template variable name to add
77          * @return      void
78          */
79         private final function addTemplateVariable ($section, $variableName) {
80                 // Get template name
81                 $templateName = $this->getTemplateName();
82
83                 // Generate full section name
84                 $sectionName = $section . '_vars';
85
86                 // Is the list initialized?
87                 $this->setGenericArrayElement('recipients', $templateName, $sectionName, $variableName, 'OK');
88         }
89
90         /**
91          * Adds a config template variable to the recipient list of current template
92          *
93          * @param       $variableName   Template variable name to add
94          * @return      void
95          */
96         public final function addConfigTemplateVariable ($variableName) {
97                 $this->addTemplateVariable('config', $variableName);
98         }
99
100         /**
101          * Adds a 'value' template variable to the recipient list of current template
102          *
103          * @param       $variableName   Template variable name to add
104          * @return      void
105          */
106         public final function addValueTemplateVariable ($variableName) {
107                 $this->addTemplateVariable('value', $variableName);
108         }
109
110         /**
111          * Adds a value instance for a given variable name. It should be set!
112          *
113          * @param       $variableName   Template variable we want to assign a value instance
114          * @param       $valueInstance  An object instance which can provide "field values"
115          * @return      void
116          */
117         public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
118                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'values', $variableName, $valueInstance);
119         }
120
121         /**
122          * Protected setter for template name
123          *
124          * @param       $templateName   Name of email template
125          * @return      void
126          */
127         public final function setTemplateName ($templateName) {
128                 $this->templateName = (string) $templateName;
129         }
130
131         /**
132          * Protected getter for template name
133          *
134          * @return      $templateName   Name of email template
135          */
136         protected final function getTemplateName () {
137                 return $this->templateName;
138         }
139
140         /**
141          * Setter for subject line
142          *
143          * @param       $subjectLine    Subject line to set
144          * @return      void
145          */
146         public final function setSubjectLine ($subjectLine) {
147                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'generic', 'subject', $subjectLine);
148         }
149
150         /**
151          * Getter for subject line or null if not found
152          *
153          * @return      $subjectLine    Subject line to set
154          */
155         public final function getSubjectLine () {
156                 // Default subject is null
157                 $subjectLine = NULL;
158
159                 // Get template name
160                 $templateName = $this->getTemplateName();
161
162                 // Does the subject line exist?
163                 if ((!empty($templateName)) && ($this->isGenericArrayElementSet('recipients', $templateName, 'generic', 'subject'))) {
164                         // Then use it
165                         $subjectLine = $this->getGenericArrayElement('recipients', $templateName, 'generic', 'subject');
166                 } // END - if
167
168                 // Return it
169                 return $subjectLine;
170         }
171
172         /**
173          * Use subject line provided by the (XML) template otherwise a subject line must be set
174          *
175          * @return      void
176          */
177         public function useSubjectFromTemplate () {
178                 // Set the subject line
179                 $this->setSubjectLine('{?subject?}');
180         }
181
182         /**
183          * Getter for recipient list array
184          *
185          * @return      $recipientList  Array with reciepients
186          */
187         public final function getRecipientList () {
188                 return $this->getGenericArray('recipients');
189         }
190 }
191
192 // [EOF]
193 ?>