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