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