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