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