]> git.mxchange.org Git - mailer.git/blobdiff - inc/classes/main/mailer/class_BaseMailer.php
Code syncronized with shipsimu code base
[mailer.git] / inc / classes / main / mailer / class_BaseMailer.php
diff --git a/inc/classes/main/mailer/class_BaseMailer.php b/inc/classes/main/mailer/class_BaseMailer.php
new file mode 100644 (file)
index 0000000..4230fbc
--- /dev/null
@@ -0,0 +1,214 @@
+<?php
+/**
+ * A general mailer class for all other mailers
+ *
+ * @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 BaseMailer extends BaseFrameworkSystem {
+       /**
+        * Iterateable list of recipients
+        */
+       private $recipientList = array();
+
+       /**
+        * Template name
+        */
+       private $templateName = "";
+
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the class
+        * @return      void
+        */
+       protected function __construct ($className) {
+               // Call parent constructor
+               parent::__construct($className);
+
+               // Clean up a little
+               $this->removeNumberFormaters();
+               $this->removeSystemArray();
+       }
+
+       /**
+        * Loads a text or HTML template depending on configuration into the template engine
+        *
+        * @param       $templateName   Name of the template we shall load
+        * @return      void
+        */
+       protected final function loadTemplate ($templateName) {
+               // Set template name
+               $this->setTemplateName($templateName);
+
+               // Get configuration entry
+               $templatePrefix = $this->getConfigInstance()->readConfig('email_tpl_' . $templateName);
+
+               // Load this email template
+               $this->getTemplateInstance()->loadEmailTemplate($templatePrefix . '_' . $templateName);
+       }
+
+       /**
+        * Adds a user class to the recipient list for current template
+        *
+        * @param       $userInstance   An instance of a user class
+        * @return      void
+        */
+       public function addRecipientByUserInstance (ManageableMember $userInstance) {
+               // Get template name
+               $templateName = $this->getTemplateName();
+
+               // Is the list initialized?
+               if (!isset($this->recipientList[$templateName]['recipients'])) {
+                       // Then initialize it here
+                       $this->recipientList[$templateName]['recipients'] = array();
+               } // END - if
+
+               // Add it as a recipient
+               $this->recipientList[$templateName]['recipients'][] = $userInstance;
+       }
+
+       /**
+        * Adds a template variable (just the name) to the recipient list in given section of current template
+        *
+        * @param       $section                Section can be "config" or "value" currently
+        * @param       $variableName   Template variable name to add
+        * @return      void
+        */
+       private final function addTemplateVariable ($section, $variableName) {
+               // Get template name
+               $templateName = $this->getTemplateName();
+
+               // Generate full section name
+               $sectionName = $section . '_vars';
+
+               // Is the list initialized?
+               if (!isset($this->recipientList[$templateName][$sectionName])) {
+                       // Then initialize it here
+                       $this->recipientList[$templateName][$sectionName] = array();
+               } // END - if
+
+               // Add the variable to the list
+               $this->recipientList[$templateName][$sectionName][$variableName] = 'OK';
+       }
+
+       /**
+        * Adds a config template variable to the recipient list of current template
+        *
+        * @param       $variableName   Template variable name to add
+        * @return      void
+        */
+       public final function addConfigTemplateVariable ($variableName) {
+               $this->addTemplateVariable("config", $variableName);
+       }
+
+       /**
+        * Adds a "value" template variable to the recipient list of current template
+        *
+        * @param       $variableName   Template variable name to add
+        * @return      void
+        */
+       public final function addValueTemplateVariable ($variableName) {
+               $this->addTemplateVariable("value", $variableName);
+       }
+
+       /**
+        * Adds a value instance for a given variable name. It should be set!
+        *
+        * @param       $variableName   Template variable we want to assign a value instance
+        * @param       $valueInstance  An object instance which can provide "field values"
+        * @return      void
+        */
+       public final function addValueInstance ($variableName, FrameworkInterface $valueInstance) {
+               $this->recipientList[$this->getTemplateName()]['values'][$variableName] = $valueInstance;
+       }
+
+       /**
+        * Protected setter for template name
+        *
+        * @param       $templateName   Name of email template
+        * @return      void
+        */
+       public final function setTemplateName ($templateName) {
+               $this->templateName = (string) $templateName;
+       }
+
+       /**
+        * Protected getter for template name
+        *
+        * @return      $templateName   Name of email template
+        */
+       protected final function getTemplateName () {
+               return $this->templateName;
+       }
+
+       /**
+        * Setter for subject line
+        *
+        * @param       $subjectLine    Subject line to set
+        * @return      void
+        */
+       public final function setSubjectLine ($subjectLine) {
+               $this->recipientList[$this->getTemplateName()]['subject'] = (string) $subjectLine;
+       }
+
+       /**
+        * Getter for subject line or null if not found
+        *
+        * @return      $subjectLine    Subject line to set
+        */
+       public final function getSubjectLine () {
+               // Default subject is null
+               $subjectLine = null;
+
+               // Get template name
+               $templateName = $this->getTemplateName();
+
+               // Does the subject line exist?
+               if ((!empty($templateName)) && (isset($this->recipientList[$templateName]['subject']))) {
+                       // Then use it
+                       $subjectLine = $this->recipientList[$templateName]['subject'];
+               } // END - if
+
+               // Return it
+               return $subjectLine;
+       }
+
+       /**
+        * Use subject line provided by the (XML) template otherwise a subject line must be set
+        *
+        * @return      void
+        */
+       public function useSubjectFromTemplate () {
+               // Set the subject line
+               $this->setSubjectLine("{?subject?}");
+       }
+
+       /**
+        * Getter for recipient list array
+        *
+        * @return      $recipientList  Array with reciepients
+        */
+       public final function getRecipientList () {
+               return $this->recipientList;
+       }
+}
+
+// [EOF]
+?>