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