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