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