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