3685bf7cf353af51b68f1865b355d2f1b5e743ce
[shipsimu.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, 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         public 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 (ManageableUser $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'] = new FrameworkArrayObject("FakedRecipientList");
81                 } // END - if
82
83                 // Add it as a recipient
84                 $this->recipientList[$templateName]['recipients']->append($userInstance);
85         }
86
87         /**
88          * Protected setter for template name
89          *
90          * @param       $templateName   Name of email template
91          * @return      void
92          */
93         protected final function setTemplateName ($templateName) {
94                 $this->templateName = (string) $templateName;
95         }
96
97         /**
98          * Protected getter for template name
99          *
100          * @return      $templateName   Name of email template
101          */
102         protected final function getTemplateName () {
103                 return $this->templateName;
104         }
105
106         /**
107          * Setter for subject line
108          *
109          * @param       $subjectLine    Subject line to set
110          * @return      void
111          */
112         public final function setSubjectLine ($subjectLine) {
113                 $this->recipientList[$this->getTemplateName()]['subject'] = (string) $subjectLine;
114         }
115
116         /**
117          * Getter for subject line or null if not found
118          *
119          * @return      $subjectLine    Subject line to set
120          */
121         public final function getSubjectLine () {
122                 // Default subject is null
123                 $subjectLine = null;
124
125                 // Get template name
126                 $templateName = $this->getTemplateName();
127
128                 // Does the subject line exist?
129                 if ((!empty($templateName)) && (isset($this->recipientList[$templateName]['subject']))) {
130                         // Then use it
131                         $subjectLine = $this->recipientList[$templateName]['subject'];
132                 } // END - if
133
134                 // Return it
135                 return $subjectLine;
136         }
137
138         /**
139          * Use subject line provided by the (XML) template otherwise a subject line must be set
140          *
141          * @return      void
142          */
143         public function useSubjectFromTemplate () {
144                 // Set the subject line
145                 $this->setSubjectLine("{?subject?}");
146         }
147 }
148
149 // [EOF]
150 ?>