Continued:
[core.git] / inc / main / classes / template / mail / class_MailTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
7
8 /**
9  * The own template engine for loading caching and sending out images
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  * @todo                This template engine does not make use of setTemplateType()
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
32         /**
33          * Main nodes in the XML tree
34          */
35         private $mainNodes = array(
36                 'mail-data'
37         );
38
39         /**
40          * Sub nodes in the XML tree
41          */
42         private $subNodes = array(
43                 'subject-line',
44                 'sender-address',
45                 'recipient-address',
46                 'message'
47         );
48
49         /**
50          * Mailer instance
51          */
52         private $mailerInstance = NULL;
53
54         /**
55          * Current main node
56          */
57         private $currMainNode = '';
58
59         /**
60          * Protected constructor
61          *
62          * @return      void
63          */
64         protected function __construct () {
65                 // Call parent constructor
66                 parent::__construct(__CLASS__);
67         }
68
69         /**
70          * Creates an instance of the class TemplateEngine and prepares it for usage
71          *
72          * @return      $templateInstance               An instance of TemplateEngine
73          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
74          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
75          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
76          *                                                                                      directory or not found
77          * @throws      BasePathReadProtectedException  If $templateBasePath is
78          *                                                                                      read-protected
79          */
80         public static final function createMailTemplateEngine () {
81                 // Get a new instance
82                 $templateInstance = new MailTemplateEngine();
83
84                 // Get the application instance from registry
85                 $applicationInstance = Registry::getRegistry()->getInstance('app');
86
87                 // Determine base path
88                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
89
90                 // Is the base path valid?
91                 if (empty($templateBasePath)) {
92                         // Base path is empty
93                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
94                 } elseif (!is_string($templateBasePath)) {
95                         // Is not a string
96                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
97                 } elseif (!is_dir($templateBasePath)) {
98                         // Is not a path
99                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
100                 } elseif (!is_readable($templateBasePath)) {
101                         // Is not readable
102                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
103                 }
104
105                 // Set the base path
106                 $templateInstance->setTemplateBasePath($templateBasePath);
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         public 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' . self::convertToClassName($element);
167                 } elseif (in_array($element, $this->getSubNodes())) {
168                         // Sub node found
169                         $methodName = 'setEmailProperty' . self::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         public function finishElement ($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' . self::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         public 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;
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          * @return      void
277          */
278         private function setEmailPropertySubjectLine () {
279                 // Empty for now
280         }
281
282         /**
283          * Method stub to avoid output
284          *
285          * @return      void
286          */
287         private function setEmailPropertyMessage () {
288                 // Empty for now
289         }
290
291         /**
292          * Gets the template variable "message", stores it back as raw template data
293          * and compiles all variables so the mail message got prepared for output
294          *
295          * @return      void
296          */
297         private function finishMailData () {
298                 // Get the message and set it as new raw template data back
299                 $message = $this->readVariable('message');
300                 $this->setRawTemplateData($message);
301
302                 // Get some variables to compile
303                 //$sender = $this->compileRawCode($this->readVariable('sender'));
304                 //$this->assignVariable('sender', $sender);
305
306                 // Then compile all variables
307                 $this->compileVariables();
308         }
309
310         /**
311          * Invokes the final mail process
312          *
313          * @return      void
314          */
315         private function finishTextMail () {
316                 $this->getMailerInstance()->invokeMailDelivery();
317         }
318
319         /**
320          * Getter for image cache file (FQFN)
321          *
322          * @return      $fqfn   Full-qualified file name of the image cache
323          * @todo        0% done
324          */
325         public function getMailCacheFqfn () {
326                 // Initialize FQFN
327                 $fqfn = '';
328                 $this->debugBackTrace('Unfinished area!');
329
330                 // Return it
331                 return $fqfn;
332         }
333
334         /**
335          * Setter for mailer instance
336          *
337          * @param       $mailerInstance         A mailer instance
338          * @return      void
339          */
340         public final function setMailerInstance (DeliverableMail $mailerInstance) {
341                 $this->mailerInstance = $mailerInstance;
342         }
343
344         /**
345          * Getter for mailer instance
346          *
347          * @return      $mailerInstance         A mailer instance
348          */
349         protected final function getMailerInstance () {
350                 return $this->mailerInstance;
351         }
352
353         /**
354          * Outputs the mail to the world. This should only the mailer debug class do!
355          *
356          * @param       $responseInstance       An instance of a Responseable class
357          * @return      void
358          */
359         public function transferToResponse (Responseable $responseInstance) {
360                 $responseInstance->writeToBody($this->getCompiledData());
361         }
362 }
363
364 // [EOF]
365 ?>