3 namespace CoreFramework\Mailer\Debug;
5 // Import framework stuff
6 use CoreFramework\Manager\ManageableApplication;
7 use CoreFramework\Template\CompileableTemplate;
10 * A mailer class for debugging purposes only. This class will print the
11 * prepared mail out and will not send it to the recipient.
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class DebugMailer extends BaseMailer implements DeliverableMail {
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this mailer class
46 * @param $templateInstance A template instance
47 * @param $applicationInstance An application helper class
48 * @param $templateName Name of email template to set
49 * @return $mailerInstance An instance of this mailer class
51 public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
53 $mailerInstance = new DebugMailer();
55 // Set template instance
56 $mailerInstance->setTemplateInstance($templateInstance);
58 // Set application instance
59 $mailerInstance->setApplicationInstance($applicationInstance);
62 $mailerInstance->setTemplateName($templateName);
64 // Return the instance
65 return $mailerInstance;
69 * Deliver email to the recipient(s)
72 * @throws InvalidInterfaceException If the recipient instance does not implement ManageableMember
74 public function deliverEmail () {
75 // Get template instance
76 $templateInstance = $this->getTemplateInstance();
78 // "Deliver" all emails
79 foreach ($this->getRecipientList() as $templateName => $recipientList) {
80 // Walk through all recipients and "sent", or better print, it out
81 foreach ($recipientList['recipients'] as $recipientInstance) {
82 // The recipient should be a user instance, right?
83 if (!$recipientInstance instanceof ManageableMember) {
84 // Invalid entry found!
85 throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
88 // User class found, so entry is valid, first load the template
89 $this->loadTemplate($templateName);
92 $templateInstance->assignVariable('subject', $this->getSubjectLine());
94 // Walk through all variables, first config to assign them
95 foreach ($recipientList['config_vars'] as $variable => $dummy) {
96 // Load the config value and set it
97 $templateInstance->assignConfigVariable($variable);
100 // Now do the same with the values but ask the "value instance" instead!
101 foreach ($recipientList['value_vars'] as $variable => $dummy) {
102 // Is the value instance there?
103 if (!isset($recipientList['values'][$variable])) {
105 throw new NullPointerException ($this, self::EXCEPTION_IS_NULL_POINTER);
108 // Get the field from the value instance
109 $fieldValue = $recipientList['values'][$variable]->getField($variable);
111 // Set it in template engine
112 $templateInstance->assignVariable($variable, $fieldValue);
115 // Render the content
116 $templateInstance->renderXmlContent();
118 // Get responce instance
119 $responseInstance = $this->getApplicationInstance()->getResponseInstance();
121 // Transfer the data to the response
122 $templateInstance->transferToResponse($responseInstance);
128 * Send notification to the admin
133 public function sendAdminNotification () {
138 * Invokes the mail delivery process which will prepare the output of the message in a code template
142 public function invokeMailDelivery () {
143 // Get template instance
144 $templateInstance = $this->getTemplateInstance();
146 // Get the compiled message and set it as new template variable
147 $message = $templateInstance->getCompiledData();
148 $templateInstance->assignVariable('message', $message);
150 // Load the code template
151 $templateInstance->loadCodeTemplate('mail_debug');
153 // Compile the template
154 $templateInstance->compileTemplate();
156 // Assign this template with variable
157 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
159 // Load header template
160 $templateInstance->loadCodeTemplate('header');
162 // Compile and assign it with a variable
163 $templateInstance->compileTemplate();
164 $templateInstance->assignTemplateWithVariable('header', 'header');
166 // Load footer template
167 $templateInstance->loadCodeTemplate('footer');
169 // Compile and assign it with a variable
170 $templateInstance->compileTemplate();
171 $templateInstance->assignTemplateWithVariable('footer', 'footer');
173 // Load the master template
174 $templateInstance->loadCodeTemplate($this->getApplicationInstance()->buildMasterTemplateName());
176 // Then compile it again
177 $templateInstance->compileVariables();