]> 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\Traits\Template\CompileableTemplateTrait;
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 - 2022 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         // Load traits
36         use CompileableTemplateTrait;
37
38         /**
39          * Template name
40          */
41         private $templateName = '';
42
43         /**
44          * Protected constructor
45          *
46          * @param       $className      Name of the class
47          * @return      void
48          */
49         protected function __construct (string $className) {
50                 // Call parent constructor
51                 parent::__construct($className);
52         }
53
54         /**
55          * Loads a text or HTML template depending on configuration into the template engine
56          *
57          * @param       $templateName   Name of the template we shall load
58          * @return      void
59          */
60         protected final function loadTemplate (string $templateName) {
61                 // Set template name
62                 $this->setTemplateName($templateName);
63
64                 // Get configuration entry
65                 $templatePrefix = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('email_tpl_' . $templateName);
66
67                 // Load this email template
68                 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
69         }
70
71         /**
72          * Adds a user class to the recipient list for current template
73          *
74          * @param       $userInstance   An instance of a user class
75          * @return      void
76          */
77         public function addRecipientByUserInstance (ManageableMember $userInstance) {
78                 // Get template name
79                 $templateName = $this->getTemplateName();
80
81                 // Is the list initialized?
82                 $this->pushValueToGenericArrayKey('recipients', $templateName, 'recipients', $userInstance);
83         }
84
85         /**
86          * Adds a template variable (just the name) to the recipient list in given section of current template
87          *
88          * @param       $section                Section can be 'config' or "value" currently
89          * @param       $variableName   Template variable name to add
90          * @return      void
91          */
92         private final function addTemplateVariable ($section, $variableName) {
93                 // Get template name
94                 $templateName = $this->getTemplateName();
95
96                 // Generate full section name
97                 $sectionName = $section . '_vars';
98
99                 // Is the list initialized?
100                 $this->setGenericArrayElement('recipients', $templateName, $sectionName, $variableName, 'OK');
101         }
102
103         /**
104          * Adds a config template variable to the recipient list of current template
105          *
106          * @param       $variableName   Template variable name to add
107          * @return      void
108          */
109         public final function addConfigTemplateVariable ($variableName) {
110                 $this->addTemplateVariable('config', $variableName);
111         }
112
113         /**
114          * Adds a 'value' template variable to the recipient list of current template
115          *
116          * @param       $variableName   Template variable name to add
117          * @return      void
118          */
119         public final function addValueTemplateVariable ($variableName) {
120                 $this->addTemplateVariable('value', $variableName);
121         }
122
123         /**
124          * Adds a value instance for a given variable name. It should be set!
125          *
126          * @param       $variableName   Template variable we want to assign a value instance
127          * @param       $valueInstance  An object instance which can provide "field values"
128          * @return      void
129          */
130         public final function addValueInstance (string $variableName, FrameworkInterface $valueInstance) {
131                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'values', $variableName, $valueInstance);
132         }
133
134         /**
135          * Protected setter for template name
136          *
137          * @param       $templateName   Name of email template
138          * @return      void
139          */
140         public final function setTemplateName (string $templateName) {
141                 $this->templateName = $templateName;
142         }
143
144         /**
145          * Protected getter for template name
146          *
147          * @return      $templateName   Name of email template
148          */
149         protected final function getTemplateName () {
150                 return $this->templateName;
151         }
152
153         /**
154          * Setter for subject line
155          *
156          * @param       $subjectLine    Subject line to set
157          * @return      void
158          */
159         public final function setSubjectLine (string $subjectLine) {
160                 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'generic', 'subject', $subjectLine);
161         }
162
163         /**
164          * Getter for subject line or null if not found
165          *
166          * @return      $subjectLine    Subject line to set
167          */
168         public final function getSubjectLine () {
169                 // Default subject is null
170                 $subjectLine = NULL;
171
172                 // Get template name
173                 $templateName = $this->getTemplateName();
174
175                 // Does the subject line exist?
176                 if ((!empty($templateName)) && ($this->isGenericArrayElementSet('recipients', $templateName, 'generic', 'subject'))) {
177                         // Then use it
178                         $subjectLine = $this->getGenericArrayElement('recipients', $templateName, 'generic', 'subject');
179                 }
180
181                 // Return it
182                 return $subjectLine;
183         }
184
185         /**
186          * Use subject line provided by the (XML) template otherwise a subject line must be set
187          *
188          * @return      void
189          */
190         public function useSubjectFromTemplate () {
191                 // Set the subject line
192                 $this->setSubjectLine('{?subject?}');
193         }
194
195         /**
196          * Getter for recipient list array
197          *
198          * @return      $recipientList  Array with reciepients
199          */
200         public final function getRecipientList () {
201                 return $this->getGenericArray('recipients');
202         }
203
204 }