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\Traits\Template\CompileableTemplateTrait;
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 - 2023 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 {
36 use CompileableTemplateTrait;
41 private $templateName = '';
44 * Protected constructor
46 * @param $className Name of the class
49 protected function __construct (string $className) {
50 // Call parent constructor
51 parent::__construct($className);
55 * Loads a text or HTML template depending on configuration into the template engine
57 * @param $templateName Name of the template we shall load
60 protected final function loadTemplate (string $templateName) {
62 $this->setTemplateName($templateName);
64 // Get configuration entry
65 $templatePrefix = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('email_tpl_' . $templateName);
67 // Load this email template
68 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
72 * Adds a user class to the recipient list for current template
74 * @param $userInstance An instance of a user class
77 public function addRecipientByUserInstance (ManageableMember $userInstance) {
79 $templateName = $this->getTemplateName();
81 // Is the list initialized?
82 $this->pushValueToGenericArrayKey('recipients', $templateName, 'recipients', $userInstance);
86 * Adds a template variable (just the name) to the recipient list in given section of current template
88 * @param $section Section can be 'config' or "value" currently
89 * @param $variableName Template variable name to add
92 private final function addTemplateVariable ($section, $variableName) {
94 $templateName = $this->getTemplateName();
96 // Generate full section name
97 $sectionName = $section . '_vars';
99 // Is the list initialized?
100 $this->setGenericArrayElement('recipients', $templateName, $sectionName, $variableName, 'OK');
104 * Adds a config template variable to the recipient list of current template
106 * @param $variableName Template variable name to add
109 public final function addConfigTemplateVariable ($variableName) {
110 $this->addTemplateVariable('config', $variableName);
114 * Adds a 'value' template variable to the recipient list of current template
116 * @param $variableName Template variable name to add
119 public final function addValueTemplateVariable ($variableName) {
120 $this->addTemplateVariable('value', $variableName);
124 * Adds a value instance for a given variable name. It should be set!
126 * @param $variableName Template variable we want to assign a value instance
127 * @param $valueInstance An object instance which can provide "field values"
130 public final function addValueInstance (string $variableName, FrameworkInterface $valueInstance) {
131 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'values', $variableName, $valueInstance);
135 * Protected setter for template name
137 * @param $templateName Name of email template
140 public final function setTemplateName (string $templateName) {
141 $this->templateName = $templateName;
145 * Protected getter for template name
147 * @return $templateName Name of email template
149 protected final function getTemplateName () {
150 return $this->templateName;
154 * Setter for subject line
156 * @param $subjectLine Subject line to set
159 public final function setSubjectLine (string $subjectLine) {
160 $this->setGenericArrayElement('recipients', $this->getTemplateName(), 'generic', 'subject', $subjectLine);
164 * Getter for subject line or null if not found
166 * @return $subjectLine Subject line to set
168 public final function getSubjectLine () {
169 // Default subject is null
173 $templateName = $this->getTemplateName();
175 // Does the subject line exist?
176 if ((!empty($templateName)) && ($this->isGenericArrayElementSet('recipients', $templateName, 'generic', 'subject'))) {
178 $subjectLine = $this->getGenericArrayElement('recipients', $templateName, 'generic', 'subject');
186 * Use subject line provided by the (XML) template otherwise a subject line must be set
190 public function useSubjectFromTemplate () {
191 // Set the subject line
192 $this->setSubjectLine('{?subject?}');
196 * Getter for recipient list array
198 * @return $recipientList Array with reciepients
200 public final function getRecipientList () {
201 return $this->getGenericArray('recipients');