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