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