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