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