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