8746c878160788c9ea65f8bf7d30e1409c45bcef
[core.git] / inc / classes / main / template / mail / class_MailTemplateEngine.php
1 <?php
2 /**
3  * The own template engine for loading caching and sending out images
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 MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree
27          */
28         private $mainNodes = array("mail-data");
29
30         /**
31          * Sub nodes in the XML tree
32          */
33         private $subNodes = array("subject-line", "sender-address", "recipient-address", "message");
34
35         /**
36          * Mailer instance
37          */
38         private $mailerInstance = null;
39
40         /**
41          * Current main node
42          */
43         private $currMainNode = "";
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Creates an instance of the class TemplateEngine and prepares it for usage
57          *
58          * @param       $basePath               The local base path for all templates
59          * @param       $langInstance   An instance of LanguageSystem (default)
60          * @param       $ioInstance             An instance of FileIoHandler (default, middleware!)
61          * @return      $tplInstance    An instance of TemplateEngine
62          * @throws      BasePathIsEmptyException                If the provided $basePath is empty
63          * @throws      InvalidBasePathStringException  If $basePath is no string
64          * @throws      BasePathIsNoDirectoryException  If $basePath is no
65          *                                                                                      directory or not found
66          * @throws      BasePathReadProtectedException  If $basePath is
67          *                                                                                      read-protected
68          */
69         public final static function createMailTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
70                 // Get a new instance
71                 $tplInstance = new MailTemplateEngine();
72
73                 // Is the base path valid?
74                 if (empty($basePath)) {
75                         // Base path is empty
76                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77                 } elseif (!is_string($basePath)) {
78                         // Is not a string
79                         throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
80                 } elseif (!is_dir($basePath)) {
81                         // Is not a path
82                         throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
83                 } elseif (!is_readable($basePath)) {
84                         // Is not readable
85                         throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
86                 }
87
88                 // Get configuration instance
89                 $cfgInstance = FrameworkConfiguration::getInstance();
90
91                 // Set the base path
92                 $tplInstance->setBasePath($basePath);
93
94                 // Set the language and IO instances
95                 $tplInstance->setLanguageInstance($langInstance);
96                 $tplInstance->setFileIoInstance($ioInstance);
97
98                 // Set template extensions
99                 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
100                 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
101
102                 // Absolute output path for compiled templates
103                 $tplInstance->setCompileOutputPath($cfgInstance->readConfig('base_path') . $cfgInstance->readConfig('compile_output_path'));
104
105                 // Return the prepared instance
106                 return $tplInstance;
107         }
108
109         /**
110          * Getter for current main node
111          *
112          * @return      $currMainNode   Current main node
113          */
114         public final function getCurrMainNode () {
115                 return $this->currMainNode;
116         }
117
118         /**
119          * Getter for main node array
120          *
121          * @return      $mainNodes      Array with valid main node names
122          */
123         public final function getMainNodes () {
124                 return $this->mainNodes;
125         }
126
127         /**
128          * Getter for sub node array
129          *
130          * @return      $subNodes       Array with valid sub node names
131          */
132         public final function getSubNodes () {
133                 return $this->subNodes;
134         }
135
136         /**
137          * Handles the start element of an XML resource
138          *
139          * @param       $resource               XML parser resource (currently ignored)
140          * @param       $element                The element we shall handle
141          * @param       $attributes             All attributes
142          * @return      void
143          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
144          */
145         protected function startElement ($resource, $element, array $attributes) {
146                 // Initial method name which will never be called...
147                 $methodName = 'initEmail';
148
149                 // Make the element name lower-case
150                 $element = strtolower($element);
151
152                 // Is the element a main node?
153                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
154                 if (in_array($element, $this->getMainNodes())) {
155                         // Okay, main node found!
156                         $methodName = 'setEmail' . $this->convertToClassName($element);
157                 } elseif (in_array($element, $this->getSubNodes())) {
158                         // Sub node found
159                         $methodName = 'setEmailProperty' . $this->convertToClassName($element);
160                 } elseif ($element != 'text-mail') {
161                         // Invalid node name found
162                         throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
163                 }
164
165                 // Call method
166                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
167                 call_user_func_array(array($this, $methodName), $attributes);
168         }
169
170         /**
171          * Ends the main or sub node by sending out the gathered data
172          *
173          * @param       $resource       An XML resource pointer (currently ignored)
174          * @param       $nodeName       Name of the node we want to finish
175          * @return      void
176          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
177          */
178         protected function endElement ($resource, $nodeName) {
179                 // Make all lower-case
180                 $nodeName = strtolower($nodeName);
181
182                 // Does this match with current main node?
183                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
184                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
185                         // Did not match!
186                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
187                 } elseif (in_array($nodeName, $this->getSubNodes())) {
188                         // Silently ignore sub nodes
189                         return;
190                 }
191
192                 // Construct method name
193                 $methodName = 'finish' . $this->convertToClassName($nodeName);
194
195                 // Call the corresponding method
196                 call_user_func_array(array($this, $methodName), array());
197         }
198
199         /**
200          * Adds the message text to the template engine
201          *
202          * @param       $resource               XML parser resource (currently ignored)
203          * @param       $characters             Characters to handle
204          * @return      void
205          */
206         protected function characterHandler ($resource, $characters) {
207                 // Trim all spaces away
208                 $characters = trim($characters);
209
210                 // Is this string empty?
211                 if (empty($characters)) {
212                         // Then skip it silently
213                         return false;
214                 } // END - if
215
216                 // Add the message now
217                 $this->assignVariable('message', $characters);
218         }
219
220         /**
221          * Intializes the mail
222          *
223          * @return      void
224          * @todo        Add cache creation here
225          */
226         private function initEmail () {
227                 // Unfinished work!
228         }
229
230         /**
231          * Setter for mail data node
232          *
233          * @return      void
234          * @todo        Should we call back the mailer class here?
235          */
236         private function setEmailMailData () {
237                 // Set current main node
238                 $this->currMainNode = 'mail-data';
239         }
240
241         /**
242          * Setter for sender address property
243          *
244          * @param       $senderAddress  Sender address to set in email
245          * @return      void
246          */
247         private function setEmailPropertySenderAddress ($senderAddress) {
248                 // Set the template variable
249                 $this->assignVariable('sender', $senderAddress);
250         }
251
252         /**
253          * Setter for recipient address property
254          *
255          * @param       $recipientAddress       Recipient address to set in email
256          * @return      void
257          */
258         private function setEmailPropertyRecipientAddress ($recipientAddress) {
259                 // Set the template variable
260                 $this->assignVariable('recipient', $recipientAddress);
261         }
262
263         /**
264          * Setter for subject line property
265          *
266          * @param       $subjectLine    Subject line to set in email
267          * @return      void
268          */
269         private function setEmailPropertySubjectLine ($subjectLine) {
270                 // Set the template variable
271                 $this->assignVariable('subject', $subjectLine);
272         }
273
274         /**
275          * Method stub to avoid output
276          *
277          * @return      void
278          */
279         private function setEmailPropertyMessage () {
280                 // Empty for now
281         }
282
283         /**
284          * Gets the template variable "message", stores it back as raw template data
285          * and compiles all variables so the mail message got prepared for output
286          *
287          * @return      void
288          */
289         private function finishMailData () {
290                 // Get the message and set it as new raw template data back
291                 $message = $this->readVariable('message');
292                 $this->setRawTemplateData($message);
293
294                 // Get some variables to compile
295                 //$sender = $this->compileRawCode($this->readVariable('sender'));
296                 //$this->assignVariable('sender', $sender);
297
298                 // Then compile all variables
299                 $this->compileVariables();
300         }
301
302         /**
303          * Invokes the final mail process
304          *
305          * @return      void
306          */
307         private function finishTextMail () {
308                 $this->getMailerInstance()->invokeMailDelivery();
309         }
310
311         /**
312          * Getter for image cache file (FQFN)
313          *
314          * @return      $fqfn   Full-qualified file name of the image cache
315          * @todo        0% done
316          */
317         public function getMailCacheFqfn () {
318                 // Initialize FQFN
319                 $fqfn = "";
320                 $this->debugBackTrace();
321
322                 // Return it
323                 return $fqfn;
324         }
325
326         /**
327          * Setter for mailer instance
328          *
329          * @param       $mailerInstance         A mailer instance
330          * @return      void
331          */
332         public final function setMailerInstance (DeliverableMail $mailerInstance) {
333                 $this->mailerInstance = $mailerInstance;
334         }
335
336         /**
337          * Getter for mailer instance
338          *
339          * @return      $mailerInstance         A mailer instance
340          */
341         protected final function getMailerInstance () {
342                 return $this->mailerInstance;
343         }
344
345         /**
346          * Outputs the mail to the world. This should only the mailer debug class do!
347          *
348          * @param       $responseInstance       An instance of a Responseable class
349          * @return      void
350          */
351         public function transferToResponse (Responseable $responseInstance) {
352                 $responseInstance->writeToBody($this->getCompiledData());
353         }
354 }
355
356 // [EOF]
357 ?>