3 namespace Org\Mxchange\CoreFramework\Mailer;
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;
13 * A general mailer class for all other mailers
15 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
34 abstract class BaseMailer extends BaseFrameworkSystem {
38 private $templateName = '';
41 * Template engine instance
43 private $templateInstance = NULL;
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct (string $className) {
52 // Call parent constructor
53 parent::__construct($className);
57 * Loads a text or HTML template depending on configuration into the template engine
59 * @param $templateName Name of the template we shall load
62 protected final function loadTemplate ($templateName) {
64 $this->setTemplateName($templateName);
66 // Get configuration entry
67 $templatePrefix = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('email_tpl_' . $templateName);
69 // Load this email template
70 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
74 * Adds a user class to the recipient list for current template
76 * @param $userInstance An instance of a user class
79 public function addRecipientByUserInstance (ManageableMember $userInstance) {
81 $templateName = $this->getTemplateName();
83 // Is the list initialized?
84 $this->pushValueToGenericArrayKey('recipients', $templateName, 'recipients', $userInstance);
88 * Adds a template variable (just the name) to the recipient list in given section of current template
90 * @param $section Section can be 'config' or "value" currently
91 * @param $variableName Template variable name to add
94 private final function addTemplateVariable ($section, $variableName) {
96 $templateName = $this->getTemplateName();
98 // Generate full section name
99 $sectionName = $section . '_vars';
101 // Is the list initialized?
102 $this->setGenericArrayElement('recipients', $templateName, $sectionName, $variableName, 'OK');
106 * Adds a config template variable to the recipient list of current template
108 * @param $variableName Template variable name to add
111 public final function addConfigTemplateVariable ($variableName) {
112 $this->addTemplateVariable('config', $variableName);
116 * Adds a 'value' template variable to the recipient list of current template
118 * @param $variableName Template variable name to add
121 public final function addValueTemplateVariable ($variableName) {
122 $this->addTemplateVariable('value', $variableName);
126 * Adds a value instance for a given variable name. It should be set!
128 * @param $variableName Template variable we want to assign a value instance
129 * @param $valueInstance An object instance which can provide "field values"
132 public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
133 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'values', $variableName, $valueInstance);
137 * Protected setter for template name
139 * @param $templateName Name of email template
142 public final function setTemplateName ($templateName) {
143 $this->templateName = (string) $templateName;
147 * Protected getter for template name
149 * @return $templateName Name of email template
151 protected final function getTemplateName () {
152 return $this->templateName;
156 * Setter for subject line
158 * @param $subjectLine Subject line to set
161 public final function setSubjectLine ($subjectLine) {
162 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'generic', 'subject', $subjectLine);
166 * Getter for subject line or null if not found
168 * @return $subjectLine Subject line to set
170 public final function getSubjectLine () {
171 // Default subject is null
175 $templateName = $this->getTemplateName();
177 // Does the subject line exist?
178 if ((!empty($templateName)) && ($this->isGenericArrayElementSet('recipients', $templateName, 'generic', 'subject'))) {
180 $subjectLine = $this->getGenericArrayElement('recipients', $templateName, 'generic', 'subject');
188 * Use subject line provided by the (XML) template otherwise a subject line must be set
192 public function useSubjectFromTemplate () {
193 // Set the subject line
194 $this->setSubjectLine('{?subject?}');
198 * Getter for recipient list array
200 * @return $recipientList Array with reciepients
202 public final function getRecipientList () {
203 return $this->getGenericArray('recipients');
207 * Setter for template engine instances
209 * @param $templateInstance An instance of a CompileableTemplate class
212 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
213 $this->templateInstance = $templateInstance;
217 * Getter for template engine instances
219 * @return $templateInstance An instance of a CompileableTemplate class
221 public final function getTemplateInstance () {
222 return $this->templateInstance;