Copyright year updated, converted double->single quotes
[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@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  * @todo                This template engine does not make use of setTemplateType()
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Main nodes in the XML tree
28          */
29         private $mainNodes = array(
30                 'mail-data'
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37                 'subject-line',
38                 'sender-address',
39                 'recipient-address',
40                 'message'
41         );
42
43         /**
44          * Mailer instance
45          */
46         private $mailerInstance = NULL;
47
48         /**
49          * Current main node
50          */
51         private $currMainNode = '';
52
53         /**
54          * Protected constructor
55          *
56          * @return      void
57          */
58         protected function __construct () {
59                 // Call parent constructor
60                 parent::__construct(__CLASS__);
61         }
62
63         /**
64          * Creates an instance of the class TemplateEngine and prepares it for usage
65          *
66          * @return      $templateInstance               An instance of TemplateEngine
67          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
68          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
69          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
70          *                                                                                      directory or not found
71          * @throws      BasePathReadProtectedException  If $templateBasePath is
72          *                                                                                      read-protected
73          */
74         public static final function createMailTemplateEngine () {
75                 // Get a new instance
76                 $templateInstance = new MailTemplateEngine();
77
78                 // Get the application instance from registry
79                 $applicationInstance = Registry::getRegistry()->getInstance('app');
80
81                 // Determine base path
82                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
83
84                 // Is the base path valid?
85                 if (empty($templateBasePath)) {
86                         // Base path is empty
87                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
88                 } elseif (!is_string($templateBasePath)) {
89                         // Is not a string
90                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
91                 } elseif (!is_dir($templateBasePath)) {
92                         // Is not a path
93                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
94                 } elseif (!is_readable($templateBasePath)) {
95                         // Is not readable
96                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
97                 }
98
99                 // Set the base path
100                 $templateInstance->setTemplateBasePath($templateBasePath);
101
102                 // Set template extensions
103                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
104                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
105
106                 // Absolute output path for compiled templates
107                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
108
109                 // Return the prepared instance
110                 return $templateInstance;
111         }
112
113         /**
114          * Getter for current main node
115          *
116          * @return      $currMainNode   Current main node
117          */
118         public final function getCurrMainNode () {
119                 return $this->currMainNode;
120         }
121
122         /**
123          * Getter for main node array
124          *
125          * @return      $mainNodes      Array with valid main node names
126          */
127         public final function getMainNodes () {
128                 return $this->mainNodes;
129         }
130
131         /**
132          * Getter for sub node array
133          *
134          * @return      $subNodes       Array with valid sub node names
135          */
136         public final function getSubNodes () {
137                 return $this->subNodes;
138         }
139
140         /**
141          * Handles the start element of an XML resource
142          *
143          * @param       $resource               XML parser resource (currently ignored)
144          * @param       $element                The element we shall handle
145          * @param       $attributes             All attributes
146          * @return      void
147          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
148          */
149         protected function startElement ($resource, $element, array $attributes) {
150                 // Initial method name which will never be called...
151                 $methodName = 'initEmail';
152
153                 // Make the element name lower-case
154                 $element = strtolower($element);
155
156                 // Is the element a main node?
157                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
158                 if (in_array($element, $this->getMainNodes())) {
159                         // Okay, main node found!
160                         $methodName = 'setEmail' . $this->convertToClassName($element);
161                 } elseif (in_array($element, $this->getSubNodes())) {
162                         // Sub node found
163                         $methodName = 'setEmailProperty' . $this->convertToClassName($element);
164                 } elseif ($element != 'text-mail') {
165                         // Invalid node name found
166                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
167                 }
168
169                 // Call method
170                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
171                 call_user_func_array(array($this, $methodName), $attributes);
172         }
173
174         /**
175          * Ends the main or sub node by sending out the gathered data
176          *
177          * @param       $resource       An XML resource pointer (currently ignored)
178          * @param       $nodeName       Name of the node we want to finish
179          * @return      void
180          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
181          */
182         protected function finishElement ($resource, $nodeName) {
183                 // Make all lower-case
184                 $nodeName = strtolower($nodeName);
185
186                 // Does this match with current main node?
187                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
188                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
189                         // Did not match!
190                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
191                 } elseif (in_array($nodeName, $this->getSubNodes())) {
192                         // Silently ignore sub nodes
193                         return;
194                 }
195
196                 // Construct method name
197                 $methodName = 'finish' . $this->convertToClassName($nodeName);
198
199                 // Call the corresponding method
200                 call_user_func_array(array($this, $methodName), array());
201         }
202
203         /**
204          * Adds the message text to the template engine
205          *
206          * @param       $resource               XML parser resource (currently ignored)
207          * @param       $characters             Characters to handle
208          * @return      void
209          */
210         protected function characterHandler ($resource, $characters) {
211                 // Trim all spaces away
212                 $characters = trim($characters);
213
214                 // Is this string empty?
215                 if (empty($characters)) {
216                         // Then skip it silently
217                         return;
218                 } // END - if
219
220                 // Add the message now
221                 $this->assignVariable('message', $characters);
222         }
223
224         /**
225          * Intializes the mail
226          *
227          * @return      void
228          * @todo        Add cache creation here
229          */
230         private function initEmail () {
231                 // Unfinished work!
232         }
233
234         /**
235          * Setter for mail data node
236          *
237          * @return      void
238          * @todo        Should we call back the mailer class here?
239          */
240         private function setEmailMailData () {
241                 // Set current main node
242                 $this->currMainNode = 'mail-data';
243         }
244
245         /**
246          * Setter for sender address property
247          *
248          * @param       $senderAddress  Sender address to set in email
249          * @return      void
250          */
251         private function setEmailPropertySenderAddress ($senderAddress) {
252                 // Set the template variable
253                 $this->assignVariable('sender', $senderAddress);
254         }
255
256         /**
257          * Setter for recipient address property
258          *
259          * @param       $recipientAddress       Recipient address to set in email
260          * @return      void
261          */
262         private function setEmailPropertyRecipientAddress ($recipientAddress) {
263                 // Set the template variable
264                 $this->assignVariable('recipient', $recipientAddress);
265         }
266
267         /**
268          * Setter for subject line property
269          *
270          * @param       $subjectLine    Subject line to set in email
271          * @return      void
272          */
273         private function setEmailPropertySubjectLine ($subjectLine) {
274                 // Set the template variable
275                 $this->assignVariable('subject', $subjectLine);
276         }
277
278         /**
279          * Method stub to avoid output
280          *
281          * @return      void
282          */
283         private function setEmailPropertyMessage () {
284                 // Empty for now
285         }
286
287         /**
288          * Gets the template variable "message", stores it back as raw template data
289          * and compiles all variables so the mail message got prepared for output
290          *
291          * @return      void
292          */
293         private function finishMailData () {
294                 // Get the message and set it as new raw template data back
295                 $message = $this->readVariable('message');
296                 $this->setRawTemplateData($message);
297
298                 // Get some variables to compile
299                 //$sender = $this->compileRawCode($this->readVariable('sender'));
300                 //$this->assignVariable('sender', $sender);
301
302                 // Then compile all variables
303                 $this->compileVariables();
304         }
305
306         /**
307          * Invokes the final mail process
308          *
309          * @return      void
310          */
311         private function finishTextMail () {
312                 $this->getMailerInstance()->invokeMailDelivery();
313         }
314
315         /**
316          * Getter for image cache file (FQFN)
317          *
318          * @return      $fqfn   Full-qualified file name of the image cache
319          * @todo        0% done
320          */
321         public function getMailCacheFqfn () {
322                 // Initialize FQFN
323                 $fqfn = '';
324                 $this->debugBackTrace('Unfinished area!');
325
326                 // Return it
327                 return $fqfn;
328         }
329
330         /**
331          * Setter for mailer instance
332          *
333          * @param       $mailerInstance         A mailer instance
334          * @return      void
335          */
336         public final function setMailerInstance (DeliverableMail $mailerInstance) {
337                 $this->mailerInstance = $mailerInstance;
338         }
339
340         /**
341          * Getter for mailer instance
342          *
343          * @return      $mailerInstance         A mailer instance
344          */
345         protected final function getMailerInstance () {
346                 return $this->mailerInstance;
347         }
348
349         /**
350          * Outputs the mail to the world. This should only the mailer debug class do!
351          *
352          * @param       $responseInstance       An instance of a Responseable class
353          * @return      void
354          */
355         public function transferToResponse (Responseable $responseInstance) {
356                 $responseInstance->writeToBody($this->getCompiledData());
357         }
358 }
359
360 // [EOF]
361 ?>