3 * A general mailer class for all other mailers
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseMailer extends BaseFrameworkSystem {
26 * Iterateable list of recipients
28 private $recipientList = array();
33 private $templateName = '';
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Loads a text or HTML template depending on configuration into the template engine
49 * @param $templateName Name of the template we shall load
52 protected final function loadTemplate ($templateName) {
54 $this->setTemplateName($templateName);
56 // Get configuration entry
57 $templatePrefix = $this->getConfigInstance()->getConfigEntry('email_tpl_' . $templateName);
59 // Load this email template
60 $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
64 * Adds a user class to the recipient list for current template
66 * @param $userInstance An instance of a user class
69 public function addRecipientByUserInstance (ManageableMember $userInstance) {
71 $templateName = $this->getTemplateName();
73 // Is the list initialized?
74 if (!isset($this->recipientList[$templateName]['recipients'])) {
75 // Then initialize it here
76 $this->recipientList[$templateName]['recipients'] = array();
79 // Add it as a recipient
80 $this->recipientList[$templateName]['recipients'][] = $userInstance;
84 * Adds a template variable (just the name) to the recipient list in given section of current template
86 * @param $section Section can be "config" or "value" currently
87 * @param $variableName Template variable name to add
90 private final function addTemplateVariable ($section, $variableName) {
92 $templateName = $this->getTemplateName();
94 // Generate full section name
95 $sectionName = $section . '_vars';
97 // Is the list initialized?
98 if (!isset($this->recipientList[$templateName][$sectionName])) {
99 // Then initialize it here
100 $this->recipientList[$templateName][$sectionName] = array();
103 // Add the variable to the list
104 $this->recipientList[$templateName][$sectionName][$variableName] = 'OK';
108 * Adds a config template variable to the recipient list of current template
110 * @param $variableName Template variable name to add
113 public final function addConfigTemplateVariable ($variableName) {
114 $this->addTemplateVariable("config", $variableName);
118 * Adds a "value" template variable to the recipient list of current template
120 * @param $variableName Template variable name to add
123 public final function addValueTemplateVariable ($variableName) {
124 $this->addTemplateVariable("value", $variableName);
128 * Adds a value instance for a given variable name. It should be set!
130 * @param $variableName Template variable we want to assign a value instance
131 * @param $valueInstance An object instance which can provide "field values"
134 public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
135 $this->recipientList[$this->getTemplateName()]['values'][$variableName] = $valueInstance;
139 * Protected setter for template name
141 * @param $templateName Name of email template
144 public final function setTemplateName ($templateName) {
145 $this->templateName = (string) $templateName;
149 * Protected getter for template name
151 * @return $templateName Name of email template
153 protected final function getTemplateName () {
154 return $this->templateName;
158 * Setter for subject line
160 * @param $subjectLine Subject line to set
163 public final function setSubjectLine ($subjectLine) {
164 $this->recipientList[$this->getTemplateName()]['subject'] = (string) $subjectLine;
168 * Getter for subject line or null if not found
170 * @return $subjectLine Subject line to set
172 public final function getSubjectLine () {
173 // Default subject is null
177 $templateName = $this->getTemplateName();
179 // Does the subject line exist?
180 if ((!empty($templateName)) && (isset($this->recipientList[$templateName]['subject']))) {
182 $subjectLine = $this->recipientList[$templateName]['subject'];
190 * Use subject line provided by the (XML) template otherwise a subject line must be set
194 public function useSubjectFromTemplate () {
195 // Set the subject line
196 $this->setSubjectLine("{?subject?}");
200 * Getter for recipient list array
202 * @return $recipientList Array with reciepients
204 public final function getRecipientList () {
205 return $this->recipientList;