Continued:
[core.git] / framework / main / classes / mailer / debug / class_DebugMailer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Mailer\Debug;
4
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;
12
13 /**
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.
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class DebugMailer extends BaseMailer implements DeliverableMail {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of this mailer class
49          *
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
54          */
55         public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
56                 // Get a new instance
57                 $mailerInstance = new DebugMailer();
58
59                 // Set template instance
60                 $mailerInstance->setTemplateInstance($templateInstance);
61
62                 // Set application instance
63                 $mailerInstance->setApplicationInstance($applicationInstance);
64
65                 // Set template name
66                 $mailerInstance->setTemplateName($templateName);
67
68                 // Return the instance
69                 return $mailerInstance;
70         }
71
72         /**
73          * Deliver email to the recipient(s)
74          *
75          * @return      void
76          * @throws      InvalidInterfaceException       If the recipient instance does not implement ManageableMember
77          */
78         public function deliverEmail () {
79                 // Get template instance
80                 $templateInstance = $this->getTemplateInstance();
81
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);
90                                 } // END - if
91
92                                 // User class found, so entry is valid, first load the template
93                                 $this->loadTemplate($templateName);
94
95                                 // Set subject line
96                                 $templateInstance->assignVariable('subject', $this->getSubjectLine());
97
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);
102                                 } // END - if
103
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])) {
108                                                 // Throw exception
109                                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
110                                         } // END - if
111
112                                         // Get the field from the value instance
113                                         $fieldValue = $recipientList['values'][$variable]->getField($variable);
114
115                                         // Set it in template engine
116                                         $templateInstance->assignVariable($variable, $fieldValue);
117                                 }
118
119                                 // Render the content
120                                 $templateInstance->renderXmlContent();
121
122                                 // Get responce instance
123                                 $responseInstance = $this->getApplicationInstance()->getResponseInstance();
124
125                                 // Transfer the data to the response
126                                 $templateInstance->transferToResponse($responseInstance);
127                         } // END - foreach
128                 } // END - foreach
129         }
130
131         /**
132          * Send notification to the admin
133          *
134          * @return      void
135          * @todo        0% done
136          */
137         public function sendAdminNotification () {
138                 // Unfinished work
139         }
140
141         /**
142          * Invokes the mail delivery process which will prepare the output of the message in a code template
143          *
144          * @return      void
145          */
146         public function invokeMailDelivery () {
147                 // Get template instance
148                 $templateInstance = $this->getTemplateInstance();
149
150                 // Get the compiled message and set it as new template variable
151                 $message = $templateInstance->getCompiledData();
152                 $templateInstance->assignVariable('message', $message);
153
154                 // Load the code template
155                 $templateInstance->loadCodeTemplate('mail_debug');
156
157                 // Compile the template
158                 $templateInstance->compileTemplate();
159
160                 // Assign this template with variable
161                 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
162
163                 // Load header template
164                 $templateInstance->loadCodeTemplate('header');
165
166                 // Compile and assign it with a variable
167                 $templateInstance->compileTemplate();
168                 $templateInstance->assignTemplateWithVariable('header', 'header');
169
170                 // Load footer template
171                 $templateInstance->loadCodeTemplate('footer');
172
173                 // Compile and assign it with a variable
174                 $templateInstance->compileTemplate();
175                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
176
177                 // Load the master template
178                 $templateInstance->loadCodeTemplate($this->getApplicationInstance()->buildMasterTemplateName());
179
180                 // Then compile it again
181                 $templateInstance->compileVariables();
182         }
183
184 }