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