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