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