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