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