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\Generic\NullPointerException;
7 use CoreFramework\Manager\ManageableApplication;
8 use CoreFramework\Template\CompileableTemplate;
9
10 /**
11  * A mailer class for debugging purposes only. This class will print the
12  * prepared mail out and will not send it to the recipient.
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class DebugMailer extends BaseMailer implements DeliverableMail {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Creates an instance of this mailer class
46          *
47          * @param       $templateInstance               A template instance
48          * @param       $applicationInstance    An application helper class
49          * @param       $templateName                   Name of email template to set
50          * @return      $mailerInstance                 An instance of this mailer class
51          */
52         public static final function createDebugMailer (CompileableTemplate $templateInstance, ManageableApplication $applicationInstance, $templateName) {
53                 // Get a new instance
54                 $mailerInstance = new DebugMailer();
55
56                 // Set template instance
57                 $mailerInstance->setTemplateInstance($templateInstance);
58
59                 // Set application instance
60                 $mailerInstance->setApplicationInstance($applicationInstance);
61
62                 // Set template name
63                 $mailerInstance->setTemplateName($templateName);
64
65                 // Return the instance
66                 return $mailerInstance;
67         }
68
69         /**
70          * Deliver email to the recipient(s)
71          *
72          * @return      void
73          * @throws      InvalidInterfaceException       If the recipient instance does not implement ManageableMember
74          */
75         public function deliverEmail () {
76                 // Get template instance
77                 $templateInstance = $this->getTemplateInstance();
78
79                 // "Deliver" all emails
80                 foreach ($this->getRecipientList() as $templateName => $recipientList) {
81                         // Walk through all recipients and "sent", or better print, it out
82                         foreach ($recipientList['recipients'] as $recipientInstance) {
83                                 // The recipient should be a user instance, right?
84                                 if (!$recipientInstance instanceof ManageableMember) {
85                                         // Invalid entry found!
86                                         throw new InvalidInterfaceException(array($this, 'ManageableMember'), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
87                                 } // END - if
88
89                                 // User class found, so entry is valid, first load the template
90                                 $this->loadTemplate($templateName);
91
92                                 // Set subject line
93                                 $templateInstance->assignVariable('subject', $this->getSubjectLine());
94
95                                 // Walk through all variables, first config to assign them
96                                 foreach ($recipientList['config_vars'] as $variable => $dummy) {
97                                         // Load the config value and set it
98                                         $templateInstance->assignConfigVariable($variable);
99                                 } // END - if
100
101                                 // Now do the same with the values but ask the "value instance" instead!
102                                 foreach ($recipientList['value_vars'] as $variable => $dummy) {
103                                         // Is the value instance there?
104                                         if (!isset($recipientList['values'][$variable])) {
105                                                 // Throw exception
106                                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
107                                         } // END - if
108
109                                         // Get the field from the value instance
110                                         $fieldValue = $recipientList['values'][$variable]->getField($variable);
111
112                                         // Set it in template engine
113                                         $templateInstance->assignVariable($variable, $fieldValue);
114                                 }
115
116                                 // Render the content
117                                 $templateInstance->renderXmlContent();
118
119                                 // Get responce instance
120                                 $responseInstance = $this->getApplicationInstance()->getResponseInstance();
121
122                                 // Transfer the data to the response
123                                 $templateInstance->transferToResponse($responseInstance);
124                         } // END - foreach
125                 } // END - foreach
126         }
127
128         /**
129          * Send notification to the admin
130          *
131          * @return      void
132          * @todo        0% done
133          */
134         public function sendAdminNotification () {
135                 // Unfinished work
136         }
137
138         /**
139          * Invokes the mail delivery process which will prepare the output of the message in a code template
140          *
141          * @return      void
142          */
143         public function invokeMailDelivery () {
144                 // Get template instance
145                 $templateInstance = $this->getTemplateInstance();
146
147                 // Get the compiled message and set it as new template variable
148                 $message = $templateInstance->getCompiledData();
149                 $templateInstance->assignVariable('message', $message);
150
151                 // Load the code template
152                 $templateInstance->loadCodeTemplate('mail_debug');
153
154                 // Compile the template
155                 $templateInstance->compileTemplate();
156
157                 // Assign this template with variable
158                 $templateInstance->assignTemplateWithVariable('mail_debug', 'main_content');
159
160                 // Load header template
161                 $templateInstance->loadCodeTemplate('header');
162
163                 // Compile and assign it with a variable
164                 $templateInstance->compileTemplate();
165                 $templateInstance->assignTemplateWithVariable('header', 'header');
166
167                 // Load footer template
168                 $templateInstance->loadCodeTemplate('footer');
169
170                 // Compile and assign it with a variable
171                 $templateInstance->compileTemplate();
172                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
173
174                 // Load the master template
175                 $templateInstance->loadCodeTemplate($this->getApplicationInstance()->buildMasterTemplateName());
176
177                 // Then compile it again
178                 $templateInstance->compileVariables();
179         }
180
181 }