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