]> git.mxchange.org Git - core.git/blob - framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php
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\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                 }
101
102                 // Get the application instance
103                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
104
105                 // Assign base URL
106                 $this->getTemplateInstance()->assignConfigVariable('base_url');
107
108                 // Assign all the application's data with template variables
109                 $this->getTemplateInstance()->assignApplicationData();
110
111                 // Load the master template
112                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
113
114                 // Load header template
115                 $this->getTemplateInstance()->loadCodeTemplate('header');
116
117                 // Compile and assign it with a variable
118                 $this->getTemplateInstance()->compileTemplate();
119                 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
120
121                 // Load footer template
122                 $this->getTemplateInstance()->loadCodeTemplate('footer');
123
124                 // Compile and assign it with a variable
125                 $this->getTemplateInstance()->compileTemplate();
126                 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
127
128                 // Load the matching template
129                 $this->getTemplateInstance()->loadCodeTemplate('action_' . $this->actionName);
130
131                 // Assign the template with the master template as a content ... ;)
132                 $this->getTemplateInstance()->compileTemplate();
133                 $this->getTemplateInstance()->assignTemplateWithVariable('action_' . $this->actionName, 'login_content');
134
135                 // Load main template
136                 $this->getTemplateInstance()->loadCodeTemplate('login_main');
137
138                 // Assign the main template with the master template as a content ... ;)
139                 $this->getTemplateInstance()->compileTemplate();
140                 $this->getTemplateInstance()->assignTemplateWithVariable('login_main', 'main_content');
141
142                 // Load the master template
143                 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
144
145                 // Set title
146                 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage($this->actionName . '_title'));
147
148                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
149                 // *every* command has a navigation system and that is want we don't want.
150                 $menuInstance = ObjectFactory::createObjectByConfiguredName('login_area_menu_class', array($applicationInstance));
151
152                 // Render the menu
153                 $menuInstance->renderMenu();
154
155                 // Transfer it to the template engine instance
156                 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
157
158                 /*
159                  * ... and all variables. This should be merged together in a pattern
160                  * to make things easier. A cache mechanism should be added between
161                  * these two calls to cache compiled templates.
162                  */
163                 $this->getTemplateInstance()->compileVariables();
164
165                 // Get the content back from the template engine and put it in response class
166                 $this->getTemplateInstance()->transferToResponse($responseInstance);
167         }
168
169         /**
170          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
171          *
172          * @param       $controllerInstance             A controller instance
173          * @param       $requestInstance                An instance of a class with an Requestable interface
174          * @return      void
175          */
176         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
177                 // Get registry
178                 $registryInstance = GenericRegistry::getRegistry();
179
180                 // Get our application instance from the registry
181                 $applicationInstance = $registryInstance->getInstance('application');
182
183                 // Default action is the one from configuration
184                 $this->actionName = StringUtils::convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('login_default_action');
185
186                 // Get "action" from request
187                 $actReq = $requestInstance->getRequestElement('action');
188
189                 // Do we have a "action" parameter set?
190                 if ((is_string($actReq)) && (!empty($actReq))) {
191                         // Then use it with prefix
192                         $this->actionName = StringUtils::convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $actReq;
193                 } // END - if
194
195                 // Get a resolver
196                 $actionResolver = HtmlActionResolver::createHtmlActionResolver($this->actionName);
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                 GenericRegistry::getRegistry()->addInstance('action', $actionInstance);
206         }
207
208 }