5a6602d24f11a627138622025a6b165d05c00401
[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, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
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
46         /**
47          * Loads a text or HTML template depending on configuration into the template engine
48          *
49          * @param       $templateName   Name of the template we shall load
50          * @return      void
51          */
52         protected final function loadTemplate ($templateName) {
53                 // Set template name
54                 $this->setTemplateName($templateName);
55
56                 // Get configuration entry
57                 $templatePrefix = $this->getConfigInstance()->getConfigEntry('email_tpl_' . $templateName);
58
59                 // Load this email template
60                 // @TODO This needs testing/fixes because the deprecated method
61                 // loadEmailTemplate() has been removed from interface CompileableTemplate.
62                 $this->getTemplateInstance()->loadCodeTemplate($templatePrefix . '_' . $templateName);
63         }
64
65         /**
66          * Adds a user class to the recipient list for current template
67          *
68          * @param       $userInstance   An instance of a user class
69          * @return      void
70          */
71         public function addRecipientByUserInstance (ManageableMember $userInstance) {
72                 // Get template name
73                 $templateName = $this->getTemplateName();
74
75                 // Is the list initialized?
76                 if (!isset($this->recipientList[$templateName]['recipients'])) {
77                         // Then initialize it here
78                         $this->recipientList[$templateName]['recipients'] = array();
79                 } // END - if
80
81                 // Add it as a recipient
82                 $this->recipientList[$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                 if (!isset($this->recipientList[$templateName][$sectionName])) {
101                         // Then initialize it here
102                         $this->recipientList[$templateName][$sectionName] = array();
103                 } // END - if
104
105                 // Add the variable to the list
106                 $this->recipientList[$templateName][$sectionName][$variableName] = 'OK';
107         }
108
109         /**
110          * Adds a config template variable to the recipient list of current template
111          *
112          * @param       $variableName   Template variable name to add
113          * @return      void
114          */
115         public final function addConfigTemplateVariable ($variableName) {
116                 $this->addTemplateVariable("config", $variableName);
117         }
118
119         /**
120          * Adds a "value" template variable to the recipient list of current template
121          *
122          * @param       $variableName   Template variable name to add
123          * @return      void
124          */
125         public final function addValueTemplateVariable ($variableName) {
126                 $this->addTemplateVariable("value", $variableName);
127         }
128
129         /**
130          * Adds a value instance for a given variable name. It should be set!
131          *
132          * @param       $variableName   Template variable we want to assign a value instance
133          * @param       $valueInstance  An object instance which can provide "field values"
134          * @return      void
135          */
136         public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
137                 $this->recipientList[$this->getTemplateName()]['values'][$variableName] = $valueInstance;
138         }
139
140         /**
141          * Protected setter for template name
142          *
143          * @param       $templateName   Name of email template
144          * @return      void
145          */
146         public final function setTemplateName ($templateName) {
147                 $this->templateName = (string) $templateName;
148         }
149
150         /**
151          * Protected getter for template name
152          *
153          * @return      $templateName   Name of email template
154          */
155         protected final function getTemplateName () {
156                 return $this->templateName;
157         }
158
159         /**
160          * Setter for subject line
161          *
162          * @param       $subjectLine    Subject line to set
163          * @return      void
164          */
165         public final function setSubjectLine ($subjectLine) {
166                 $this->recipientList[$this->getTemplateName()]['subject'] = (string) $subjectLine;
167         }
168
169         /**
170          * Getter for subject line or null if not found
171          *
172          * @return      $subjectLine    Subject line to set
173          */
174         public final function getSubjectLine () {
175                 // Default subject is null
176                 $subjectLine = null;
177
178                 // Get template name
179                 $templateName = $this->getTemplateName();
180
181                 // Does the subject line exist?
182                 if ((!empty($templateName)) && (isset($this->recipientList[$templateName]['subject']))) {
183                         // Then use it
184                         $subjectLine = $this->recipientList[$templateName]['subject'];
185                 } // END - if
186
187                 // Return it
188                 return $subjectLine;
189         }
190
191         /**
192          * Use subject line provided by the (XML) template otherwise a subject line must be set
193          *
194          * @return      void
195          */
196         public function useSubjectFromTemplate () {
197                 // Set the subject line
198                 $this->setSubjectLine("{?subject?}");
199         }
200
201         /**
202          * Getter for recipient list array
203          *
204          * @return      $recipientList  Array with reciepients
205          */
206         public final function getRecipientList () {
207                 return $this->recipientList;
208         }
209 }
210
211 // [EOF]
212 ?>