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