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