Continued:
[core.git] / inc / main / classes / commands / html / class_HtmlLoginAreaCommand.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Command\Login;
4
5 /**
6  * A command for the login area (member/gamer area)
7  *
8  * @author              Roland Haeder <webmaster@shipsimu.org>
9  * @version             0.0.0
10  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.shipsimu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class HtmlLoginAreaCommand extends BaseCommand implements Commandable {
28         /**
29          * Name of the action
30          */
31         private $actionName = '';
32
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 class
45          *
46          * @param       $resolverInstance       An instance of a command resolver class
47          * @return      $commandInstance        An instance a prepared command class
48          */
49         public static final function createHtmlLoginAreaCommand (CommandResolver $resolverInstance) {
50                 // Get new instance
51                 $commandInstance = new HtmlLoginAreaCommand();
52
53                 // Set the application instance
54                 $commandInstance->setResolverInstance($resolverInstance);
55
56                 // Load general data like user status and such
57                 $commandInstance->prepareCommand();
58
59                 // Return the prepared instance
60                 return $commandInstance;
61         }
62
63         /**
64          * Prepares some general data which shall be displayed on every page
65          *
66          * @return      void
67          * @todo        Add some stuff here: Some personal data, app/game related data
68          */
69         protected function prepareCommand () {
70         }
71
72         /**
73          * Executes the given command with given request and response objects
74          *
75          * @param       $requestInstance        An instance of a class with an Requestable interface
76          * @param       $responseInstance       An instance of a class with an Responseable interface
77          * @return      void
78          */
79         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
80                 // Get the action instance from registry
81                 $actionInstance = Registry::getRegistry()->getInstance('action');
82
83                 // Do we have an action here?
84                 if ($actionInstance instanceof PerformableAction) {
85                         // Execute the action (shall not output anything, see below why)
86                         $actionInstance->execute($requestInstance, $responseInstance);
87                 } // END - if
88
89                 // Get the application instance
90                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
91
92                 // Prepare a template instance
93                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
94
95                 // Assign base URL
96                 $templateInstance->assignConfigVariable('base_url');
97
98                 // Assign all the application's data with template variables
99                 $templateInstance->assignApplicationData($applicationInstance);
100
101                 // Load the master template
102                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
103
104                 // Load header template
105                 $templateInstance->loadCodeTemplate('header');
106
107                 // Compile and assign it with a variable
108                 $templateInstance->compileTemplate();
109                 $templateInstance->assignTemplateWithVariable('header', 'header');
110
111                 // Load footer template
112                 $templateInstance->loadCodeTemplate('footer');
113
114                 // Compile and assign it with a variable
115                 $templateInstance->compileTemplate();
116                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
117
118                 // Load the matching template
119                 $templateInstance->loadCodeTemplate('action_' . $this->actionName);
120
121                 // Assign the template with the master template as a content ... ;)
122                 $templateInstance->compileTemplate();
123                 $templateInstance->assignTemplateWithVariable('action_' . $this->actionName, 'login_content');
124
125                 // Load main template
126                 $templateInstance->loadCodeTemplate('login_main');
127
128                 // Assign the main template with the master template as a content ... ;)
129                 $templateInstance->compileTemplate();
130                 $templateInstance->assignTemplateWithVariable('login_main', 'main_content');
131
132                 // Load the master template
133                 $templateInstance->loadCodeTemplate($masterTemplate);
134
135                 // Set title
136                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage($this->actionName . '_title'));
137
138                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
139                 // *every* command has a navigation system and that is want we don't want.
140                 $menuInstance = ObjectFactory::createObjectByConfiguredName('login_area_menu_class', array($applicationInstance));
141
142                 // Render the menu
143                 $menuInstance->renderMenu();
144
145                 // Transfer it to the template engine instance
146                 $menuInstance->transferContentToTemplateEngine($templateInstance);
147
148                 /*
149                  * ... and all variables. This should be merged together in a pattern
150                  * to make things easier. A cache mechanism should be added between
151                  * these two calls to cache compiled templates.
152                  */
153                 $templateInstance->compileVariables();
154
155                 // Get the content back from the template engine and put it in response class
156                 $templateInstance->transferToResponse($responseInstance);
157         }
158
159         /**
160          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
161          *
162          * @param       $controllerInstance             A controller instance
163          * @param       $requestInstance                An instance of a class with an Requestable interface
164          * @return      void
165          */
166         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
167                 // Default is no action
168                 $actionInstance = NULL;
169
170                 // Get registry
171                 $registryInstance = Registry::getRegistry();
172
173                 // Get our application instance from the registry
174                 $applicationInstance = $registryInstance->getInstance('application');
175
176                 // Default action is the one from configuration
177                 $this->actionName = self::convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $this->getConfigInstance()->getConfigEntry('login_default_action');
178
179                 // Get "action" from request
180                 $actReq = $requestInstance->getRequestElement('action');
181
182                 // Do we have a "action" parameter set?
183                 if ((is_string($actReq)) && (!empty($actReq))) {
184                         // Then use it with prefix
185                         $this->actionName = self::convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $actReq;
186                 } // END - if
187
188                 // Get application instance
189                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
190
191                 // Get a resolver
192                 $actionResolver = HtmlActionResolver::createHtmlActionResolver($this->actionName, $applicationInstance);
193
194                 // Resolve the action
195                 $actionInstance = $actionResolver->resolveAction();
196
197                 // Add more action-specific filters
198                 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
199
200                 // Remember this action in registry
201                 Registry::getRegistry()->addInstance('action', $actionInstance);
202         }
203
204 }