Code syncronized with shipsimu code base
[mailer.git] / inc / classes / main / mailer / debug / class_DebugMailer.php
diff --git a/inc/classes/main/mailer/debug/class_DebugMailer.php b/inc/classes/main/mailer/debug/class_DebugMailer.php
new file mode 100644 (file)
index 0000000..ffc3fc6
--- /dev/null
@@ -0,0 +1,175 @@
+<?php
+/**
+ * A mailer class for debugging purposes only. This class will print the
+ * prepared mail out and will not send it to the recipient.
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class DebugMailer extends BaseMailer implements DeliverableMail {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this mailer class
+        *
+        * @param       $templateInstance       A template instance
+        * @param       $appInstance            An application helper class
+        * @param       $templateName           Name of email template to set
+        * @return      $mailerInstance         An instance of this mailer class
+        */
+       public final static function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $appInstance, $templateName) {
+               // Get a new instance
+               $mailerInstance = new DebugMailer();
+
+               // Set template instance
+               $mailerInstance->setTemplateInstance($templateInstance);
+
+               // Set application instance
+               $mailerInstance->setApplicationInstance($appInstance);
+
+               // Set template name
+               $mailerInstance->setTemplateName('resend_link');
+
+               // Return the instance
+               return $mailerInstance;
+       }
+
+       /**
+        * Deliver email to the recipient(s)
+        *
+        * @return      void
+        * @throws      InvalidInterfaceException       If the recipient instance does not implement ManageableMember
+        */
+       public function deliverEmail () {
+               // Get template instance
+               $templateInstance = $this->getTemplateInstance();
+
+               // "Deliver" all emails
+               foreach ($this->getRecipientList() as $templateName => $recipientList) {
+                       // Walk through all recipients and "sent", or better print, it out
+                       foreach ($recipientList['recipients'] as $recipientInstance) {
+                               // The recipient should be a user instance, right?
+                               if (!$recipientInstance instanceof ManageableMember) {
+                                       // Invalid entry found!
+                                       throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
+                               }
+
+                               // User class found, so entry is valid, first load the template
+                               $this->loadTemplate($templateName);
+
+                               // Set subject line
+                               $templateInstance->assignVariable('subject', $this->getSubjectLine());
+
+                               // Walk through all variables, first config to assign them
+                               foreach ($recipientList['config_vars'] as $variable=>$dummy) {
+                                       // Load the config value and set it
+                                       $templateInstance->assignConfigVariable($variable);
+                               } // END - if
+
+                               // Now do the same with the values but ask the "value instance" instead!
+                               foreach ($recipientList['value_vars'] as $variable=>$dummy) {
+                                       // Is the value instance there?
+                                       if (!isset($recipientList['values'][$variable])) {
+                                               // Throw exception
+                                               throw new NullPointerException ($this, self::EXCEPTION_IS_NULL_POINTER);
+                                       } // END - if
+
+                                       // Get the field from the value instance
+                                       $fieldValue = $recipientList['values'][$variable]->getField($variable);
+
+                                       // Set it in the template engine
+                                       $templateInstance->assignVariable($variable, $fieldValue);
+                               }
+
+                               // Render the content
+                               $templateInstance->renderXmlContent();
+
+                               // Get responce instance
+                               $responseInstance = $this->getApplicationInstance()->getResponseInstance();
+
+                               // Transfer the data to the response
+                               $this->getTemplateInstance()->transferToResponse($responseInstance);
+                       } // END - foreach
+               } // END - foreach
+       }
+
+       /**
+        * Send notification to the admin
+        *
+        * @return      void
+        * @todo        0% done
+        */
+       public function sendAdminNotification () {
+               // Unfinished work
+       }
+
+       /**
+        * Invokes the mail delivery process which will prepare the output of the message in a code template
+        *
+        * @return      void
+        */
+       public function invokeMailDelivery () {
+               // Get template instance
+               $templateInstance = $this->getTemplateInstance();
+
+               // Get the compiled message and set it as new template variable
+               $message = $templateInstance->getCompiledData();
+               $templateInstance->assignVariable('message', $message);
+
+               // Load the code template
+               $templateInstance->loadCodeTemplate('mail_debug');
+
+               // Compile the template
+               $templateInstance->compileTemplate();
+
+               // Assign this template with variable
+               $templateInstance->assignTemplateWithVariable('mail_debug', 'content');
+
+               // Load header template
+               $templateInstance->loadCodeTemplate('header');
+
+               // Compile and assign it with a variable
+               $templateInstance->compileTemplate();
+               $templateInstance->assignTemplateWithVariable('header', 'header');
+
+               // Load footer template
+               $templateInstance->loadCodeTemplate('footer');
+
+               // Compile and assign it with a variable
+               $templateInstance->compileTemplate();
+               $templateInstance->assignTemplateWithVariable('footer', 'footer');
+
+               // Load the master template
+               $templateInstance->loadCodeTemplate($this->getApplicationInstance()->getMasterTemplate());
+
+               // Then compile it again
+               $templateInstance->compileVariables();
+       }
+}
+
+// [EOF]
+?>