3 namespace CoreFramework\Mailer\Debug;
5 // Import framework stuff
6 use CoreFramework\Generic\NullPointerException;
7 use CoreFramework\Manager\Login\ManageableMember;
8 use CoreFramework\Manager\ManageableApplication;
9 use CoreFramework\Template\CompileableTemplate;
12 * A mailer class for debugging purposes only. This class will print the
13 * prepared mail out and will not send it to the recipient.
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class DebugMailer extends BaseMailer implements DeliverableMail {
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this mailer class
48 * @param $templateInstance A template instance
49 * @param $applicationInstance An application helper class
50 * @param $templateName Name of email template to set
51 * @return $mailerInstance An instance of this mailer class
53 public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
55 $mailerInstance = new DebugMailer();
57 // Set template instance
58 $mailerInstance->setTemplateInstance($templateInstance);
60 // Set application instance
61 $mailerInstance->setApplicationInstance($applicationInstance);
64 $mailerInstance->setTemplateName($templateName);
66 // Return the instance
67 return $mailerInstance;
71 * Deliver email to the recipient(s)
74 * @throws InvalidInterfaceException If the recipient instance does not implement ManageableMember
76 public function deliverEmail () {
77 // Get template instance
78 $templateInstance = $this->getTemplateInstance();
80 // "Deliver" all emails
81 foreach ($this->getRecipientList() as $templateName => $recipientList) {
82 // Walk through all recipients and "sent", or better print, it out
83 foreach ($recipientList['recipients'] as $recipientInstance) {
84 // The recipient should be a user instance, right?
85 if (!$recipientInstance instanceof ManageableMember) {
86 // Invalid entry found!
87 throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
90 // User class found, so entry is valid, first load the template
91 $this->loadTemplate($templateName);
94 $templateInstance->assignVariable('subject', $this->getSubjectLine());
96 // Walk through all variables, first config to assign them
97 foreach ($recipientList['config_vars'] as $variable => $dummy) {
98 // Load the config value and set it
99 $templateInstance->assignConfigVariable($variable);
102 // Now do the same with the values but ask the "value instance" instead!
103 foreach ($recipientList['value_vars'] as $variable => $dummy) {
104 // Is the value instance there?
105 if (!isset($recipientList['values'][$variable])) {
107 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
110 // Get the field from the value instance
111 $fieldValue = $recipientList['values'][$variable]->getField($variable);
113 // Set it in template engine
114 $templateInstance->assignVariable($variable, $fieldValue);
117 // Render the content
118 $templateInstance->renderXmlContent();
120 // Get responce instance
121 $responseInstance = $this->getApplicationInstance()->getResponseInstance();
123 // Transfer the data to the response
124 $templateInstance->transferToResponse($responseInstance);
130 * Send notification to the admin
135 public function sendAdminNotification () {
140 * Invokes the mail delivery process which will prepare the output of the message in a code template
144 public function invokeMailDelivery () {
145 // Get template instance
146 $templateInstance = $this->getTemplateInstance();
148 // Get the compiled message and set it as new template variable
149 $message = $templateInstance->getCompiledData();
150 $templateInstance->assignVariable('message', $message);
152 // Load the code template
153 $templateInstance->loadCodeTemplate('mail_debug');
155 // Compile the template
156 $templateInstance->compileTemplate();
158 // Assign this template with variable
159 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
161 // Load header template
162 $templateInstance->loadCodeTemplate('header');
164 // Compile and assign it with a variable
165 $templateInstance->compileTemplate();
166 $templateInstance->assignTemplateWithVariable('header', 'header');
168 // Load footer template
169 $templateInstance->loadCodeTemplate('footer');
171 // Compile and assign it with a variable
172 $templateInstance->compileTemplate();
173 $templateInstance->assignTemplateWithVariable('footer', 'footer');
175 // Load the master template
176 $templateInstance->loadCodeTemplate($this->getApplicationInstance()->buildMasterTemplateName());
178 // Then compile it again
179 $templateInstance->compileVariables();