3 * The own template engine for loading caching and sending out images
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree
28 private $mainNodes = array("mail-data");
31 * Sub nodes in the XML tree
33 private $subNodes = array("subject-line", "sender-address", "recipient-address", "message");
38 private $mailerInstance = null;
43 private $currMainNode = "";
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of the class TemplateEngine and prepares it for usage
58 * @param $basePath The local base path for all templates
59 * @param $langInstance An instance of LanguageSystem (default)
60 * @param $ioInstance An instance of FileIoHandler (default, middleware!)
61 * @return $tplInstance An instance of TemplateEngine
62 * @throws BasePathIsEmptyException If the provided $basePath is empty
63 * @throws InvalidBasePathStringException If $basePath is no string
64 * @throws BasePathIsNoDirectoryException If $basePath is no
65 * directory or not found
66 * @throws BasePathReadProtectedException If $basePath is
69 public final static function createMailTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
71 $tplInstance = new MailTemplateEngine();
73 // Is the base path valid?
74 if (empty($basePath)) {
76 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77 } elseif (!is_string($basePath)) {
79 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
80 } elseif (!is_dir($basePath)) {
82 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
83 } elseif (!is_readable($basePath)) {
85 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
88 // Get configuration instance
89 $cfgInstance = FrameworkConfiguration::getInstance();
92 $tplInstance->setBasePath($basePath);
94 // Set the language and IO instances
95 $tplInstance->setLanguageInstance($langInstance);
96 $tplInstance->setFileIoInstance($ioInstance);
98 // Set template extensions
99 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
100 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
102 // Absolute output path for compiled templates
103 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
105 // Return the prepared instance
110 * Getter for current main node
112 * @return $currMainNode Current main node
114 public final function getCurrMainNode () {
115 return $this->currMainNode;
119 * Getter for main node array
121 * @return $mainNodes Array with valid main node names
123 public final function getMainNodes () {
124 return $this->mainNodes;
128 * Getter for sub node array
130 * @return $subNodes Array with valid sub node names
132 public final function getSubNodes () {
133 return $this->subNodes;
137 * Handles the start element of an XML resource
139 * @param $resource XML parser resource (currently ignored)
140 * @param $element The element we shall handle
141 * @param $attributes All attributes
143 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
145 protected function startElement ($resource, $element, array $attributes) {
146 // Initial method name which will never be called...
147 $methodName = 'initEmail';
149 // Make the element name lower-case
150 $element = strtolower($element);
152 // Is the element a main node?
153 //* DEBUG: */ echo "START: >".$element."<<br />\n";
154 if (in_array($element, $this->getMainNodes())) {
155 // Okay, main node found!
156 $methodName = 'setEmail' . $this->convertToClassName($element);
157 } elseif (in_array($element, $this->getSubNodes())) {
159 $methodName = 'setEmailProperty' . $this->convertToClassName($element);
160 } elseif ($element != 'text-mail') {
161 // Invalid node name found
162 throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
166 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
167 call_user_func_array(array($this, $methodName), $attributes);
171 * Ends the main or sub node by sending out the gathered data
173 * @param $resource An XML resource pointer (currently ignored)
174 * @param $nodeName Name of the node we want to finish
176 * @throws XmlNodeMismatchException If current main node mismatches the closing one
178 protected function endElement ($resource, $nodeName) {
179 // Make all lower-case
180 $nodeName = strtolower($nodeName);
182 // Does this match with current main node?
183 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
184 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
186 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
187 } elseif (in_array($nodeName, $this->getSubNodes())) {
188 // Silently ignore sub nodes
192 // Construct method name
193 $methodName = 'finish' . $this->convertToClassName($nodeName);
195 // Call the corresponding method
196 call_user_func_array(array($this, $methodName), array());
200 * Adds the message text to the template engine
202 * @param $resource XML parser resource (currently ignored)
203 * @param $characters Characters to handle
206 protected function characterHandler ($resource, $characters) {
207 // Trim all spaces away
208 $characters = trim($characters);
210 // Is this string empty?
211 if (empty($characters)) {
212 // Then skip it silently
216 // Add the message now
217 $this->assignVariable('message', $characters);
221 * Intializes the mail
224 * @todo Add cache creation here
226 private function initEmail () {
231 * Setter for mail data node
234 * @todo Should we call back the mailer class here?
236 private function setEmailMailData () {
237 // Set current main node
238 $this->currMainNode = 'mail-data';
242 * Setter for sender address property
244 * @param $senderAddress Sender address to set in email
247 private function setEmailPropertySenderAddress ($senderAddress) {
248 // Set the template variable
249 $this->assignVariable('sender', $senderAddress);
253 * Setter for recipient address property
255 * @param $recipientAddress Recipient address to set in email
258 private function setEmailPropertyRecipientAddress ($recipientAddress) {
259 // Set the template variable
260 $this->assignVariable('recipient', $recipientAddress);
264 * Setter for subject line property
266 * @param $subjectLine Subject line to set in email
269 private function setEmailPropertySubjectLine ($subjectLine) {
270 // Set the template variable
271 $this->assignVariable('subject', $subjectLine);
275 * Method stub to avoid output
279 private function setEmailPropertyMessage () {
284 * Gets the template variable "message", stores it back as raw template data
285 * and compiles all variables so the mail message got prepared for output
289 private function finishMailData () {
290 // Get the message and set it as new raw template data back
291 $message = $this->readVariable('message');
292 $this->setRawTemplateData($message);
294 // Get some variables to compile
295 //$sender = $this->compileRawCode($this->readVariable('sender'));
296 //$this->assignVariable('sender', $sender);
298 // Then compile all variables
299 $this->compileVariables();
303 * Invokes the final mail process
307 private function finishTextMail () {
308 $this->getMailerInstance()->invokeMailDelivery();
312 * Getter for image cache file (FQFN)
314 * @return $fqfn Full-qualified file name of the image cache
317 public function getMailCacheFqfn () {
320 $this->debugBackTrace();
327 * Setter for mailer instance
329 * @param $mailerInstance A mailer instance
332 public final function setMailerInstance (DeliverableMail $mailerInstance) {
333 $this->mailerInstance = $mailerInstance;
337 * Getter for mailer instance
339 * @return $mailerInstance A mailer instance
341 protected final function getMailerInstance () {
342 return $this->mailerInstance;
346 * Outputs the mail to the world. This should only the mailer debug class do!
348 * @param $responseInstance An instance of a Responseable class
351 public function transferToResponse (Responseable $responseInstance) {
352 $responseInstance->writeToBody($this->getCompiledData());