895ae2b5f9d1a4bfb7d1a9b44a9a0b08f76230b3
[core.git] / inc / classes / main / mailer / class_BaseMailer.php
1 <?php
2 /**
3  * A general mailer class for all other mailers
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007 - 2009 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseMailer extends BaseFrameworkSystem {
25         /**
26          * Iterateable list of recipients
27          */
28         private $recipientList = array();
29
30         /**
31          * Template name
32          */
33         private $templateName = "";
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
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 = $this->getConfigInstance()->readConfig('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                 if (!isset($this->recipientList[$templateName]['recipients'])) {
79                         // Then initialize it here
80                         $this->recipientList[$templateName]['recipients'] = array();
81                 } // END - if
82
83                 // Add it as a recipient
84                 $this->recipientList[$templateName]['recipients'][] = $userInstance;
85         }
86
87         /**
88          * Adds a template variable (just the name) to the recipient list in given section of current template
89          *
90          * @param       $section                Section can be "config" or "value" currently
91          * @param       $variableName   Template variable name to add
92          * @return      void
93          */
94         private final function addTemplateVariable ($section, $variableName) {
95                 // Get template name
96                 $templateName = $this->getTemplateName();
97
98                 // Generate full section name
99                 $sectionName = $section . '_vars';
100
101                 // Is the list initialized?
102                 if (!isset($this->recipientList[$templateName][$sectionName])) {
103                         // Then initialize it here
104                         $this->recipientList[$templateName][$sectionName] = array();
105                 } // END - if
106
107                 // Add the variable to the list
108                 $this->recipientList[$templateName][$sectionName][$variableName] = 'OK';
109         }
110
111         /**
112          * Adds a config template variable to the recipient list of current template
113          *
114          * @param       $variableName   Template variable name to add
115          * @return      void
116          */
117         public final function addConfigTemplateVariable ($variableName) {
118                 $this->addTemplateVariable("config", $variableName);
119         }
120
121         /**
122          * Adds a "value" template variable to the recipient list of current template
123          *
124          * @param       $variableName   Template variable name to add
125          * @return      void
126          */
127         public final function addValueTemplateVariable ($variableName) {
128                 $this->addTemplateVariable("value", $variableName);
129         }
130
131         /**
132          * Adds a value instance for a given variable name. It should be set!
133          *
134          * @param       $variableName   Template variable we want to assign a value instance
135          * @param       $valueInstance  An object instance which can provide "field values"
136          * @return      void
137          */
138         public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
139                 $this->recipientList[$this->getTemplateName()]['values'][$variableName] = $valueInstance;
140         }
141
142         /**
143          * Protected setter for template name
144          *
145          * @param       $templateName   Name of email template
146          * @return      void
147          */
148         public final function setTemplateName ($templateName) {
149                 $this->templateName = (string) $templateName;
150         }
151
152         /**
153          * Protected getter for template name
154          *
155          * @return      $templateName   Name of email template
156          */
157         protected final function getTemplateName () {
158                 return $this->templateName;
159         }
160
161         /**
162          * Setter for subject line
163          *
164          * @param       $subjectLine    Subject line to set
165          * @return      void
166          */
167         public final function setSubjectLine ($subjectLine) {
168                 $this->recipientList[$this->getTemplateName()]['subject'] = (string) $subjectLine;
169         }
170
171         /**
172          * Getter for subject line or null if not found
173          *
174          * @return      $subjectLine    Subject line to set
175          */
176         public final function getSubjectLine () {
177                 // Default subject is null
178                 $subjectLine = null;
179
180                 // Get template name
181                 $templateName = $this->getTemplateName();
182
183                 // Does the subject line exist?
184                 if ((!empty($templateName)) && (isset($this->recipientList[$templateName]['subject']))) {
185                         // Then use it
186                         $subjectLine = $this->recipientList[$templateName]['subject'];
187                 } // END - if
188
189                 // Return it
190                 return $subjectLine;
191         }
192
193         /**
194          * Use subject line provided by the (XML) template otherwise a subject line must be set
195          *
196          * @return      void
197          */
198         public function useSubjectFromTemplate () {
199                 // Set the subject line
200                 $this->setSubjectLine("{?subject?}");
201         }
202
203         /**
204          * Getter for recipient list array
205          *
206          * @return      $recipientList  Array with reciepients
207          */
208         public final function getRecipientList () {
209                 return $this->recipientList;
210         }
211 }
212
213 // [EOF]
214 ?>