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