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